1. ホーム

Tensorflow】エラー報告の落とし穴--入力配列を形状(100,784)から形状(100)にブロードキャストできなかった。

2022-02-11 13:10:12

tensorflowで自己コーディングマシンを実装すると、バッチの選択について以下のようなエラーが報告され続ける。

ValueError: 入力配列を形状 (100,784) から形状 (100) にブロードキャストすることができませんでした。

位置決めコードです。

batch_test_x = mnist.train.next_batch(batch_size)
You can see that there was a problem with selecting the dataset





When it comes to training, you need to select the right dataset for the batch, and there are two correct ways to do this.





Method 1: Use mnist.train.next_batch(batch_size)








When using the mnist.train.next_batch(batch_size) method to select a batch, be sure to note that the correct code is

batch_test_x, batch_test_y= mnist.train.next_batch(batch_size) The dataset and label must be selected at the same time, and it is possible to throw only batch_test_x into the self-coding machine for training:.
cost,_ = sess.run([cost,optimizer],feed_dict={x:batch_test_x}) Method 2: Use a custom function to randomly select a range of datasets The code is :
def get_random_block_from_data(data, batch_size): start_index = np.random.randint(0, len(data) - batch_size ) return data[start_index:(start_index + batch_size)] batch_test_x = get_random_block_from_data(x_train, batch_size) c,_ = sess.run([cost,optimizer],feed_dict={x:batch_test_x}) At this point, it's time to throw batch_test_x into the self-coding machine for training
Method three.

batch = mnist.train.next_batch(50)
avg_acc = sess.run(accuracy, feed_dict={x: batch[0], y: batch[1], keep_prob: 1.0})


























The dataset and label must be selected at the same time, and it is possible to throw only batch_test_x into the self-coding machine for training:.
cost,_ = sess.run([cost,optimizer],feed_dict={x:batch_test_x})


Method 2: Use a custom function to randomly select a range of datasets
The code is :
def get_random_block_from_data(data, batch_size):
    start_index = np.random.randint(0, len(data) - batch_size )
    return data[start_index:(start_index + batch_size)]

batch_test_x = get_random_block_from_data(x_train, batch_size)
c,_ = sess.run([cost,optimizer],feed_dict={x:batch_test_x})


At this point, it's time to throw batch_test_x into the self-coding machine for training
Method three.



batch = mnist.train.next_batch(50)
avg_acc = sess.run(accuracy, feed_dict={x: batch[0], y: batch[1], keep_prob: 1.0})