Categories
Mastering Development

Tensorflow with batch size and wrong dimesion

Here are my code:

# Model parameters: W and b
# tf.reset_default_graph()

W = tf.get_variable("weight",shape = [784, 10], dtype=tf.float32)
b = tf.get_variable("b", shape=(784,10), dtype= tf.float32)

input_X = tf.placeholder('float32', shape = (None,10)) 
input_y =  tf.placeholder('float32', [784, 10]) 

ogits = W*input_X + b 

probas = tf.nn.softmax(logits) 

classes = tf.argmax(probas) 

loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels = input_y, logits = logits))


step = tf.train.AdamOptimizer(loss)

s.run(tf.global_variables_initializer())

BATCH_SIZE = 512
EPOCHS = 40

# for logging the progress right here in Jupyter (for those who don't have TensorBoard)
simpleTrainingCurves = matplotlib_utils.SimpleTrainingCurves("cross-entropy", "accuracy")

for epoch in range(EPOCHS):  # we finish an epoch when we've looked at all training samples
    
    batch_losses = []
    for batch_start in range(0, X_train_flat.shape[0], BATCH_SIZE):  # data is already shuffled
        _, batch_loss = s.run([step, loss], {input_X: X_train_flat[batch_start:batch_start+BATCH_SIZE], 
                                             input_y: y_train_oh[batch_start:batch_start+BATCH_SIZE]})
        # collect batch losses, this is almost free as we need a forward pass for backprop anyway
        batch_losses.append(batch_loss)

    train_loss = np.mean(batch_losses)
    val_loss = s.run(loss, {input_X: X_val_flat, input_y: y_val_oh})  # this part is usually small
    train_accuracy = accuracy_score(y_train, s.run(classes, {input_X: X_train_flat}))  # this is slow and usually skipped
    valid_accuracy = accuracy_score(y_val, s.run(classes, {input_X: X_val_flat}))  
    simpleTrainingCurves.add(train_loss, val_loss, train_accuracy, valid_accuracy)

the error is:

/opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py
in _run(self, handle, fetches, feed_dict, options, run_metadata)
973 ‘Cannot feed value of shape %r for Tensor %r, ‘
974 ‘which has shape %r’
–> 975 % (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape())))
976 if not self.graph.is_feedable(subfeed_t):
977 raise ValueError(‘Tensor %s may not be fed.’ % subfeed_t)

ValueError: Cannot feed value of shape (512, 784) for Tensor
‘Placeholder_2:0’, which has shape ‘(?, 10)’

I am new at tensorflow and coursena is what I am learning. Please help me.

Leave a Reply

Your email address will not be published. Required fields are marked *