Reputation: 331
I am using CNN to classifies MNIST dataset into 10 classes. But the error shows the batch size of the pred is different.
InvalidArgumentError (see above for traceback): logits and labels must be same size: logits_size=[36,10] labels_size=[64,10]
[[Node: SoftmaxCrossEntropyWithLogits = SoftmaxCrossEntropyWithLogits[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](Reshape_2, Reshape_3)]]
I can't find the reason why the batch size became 36 instead of 64. Here is my code. The image size is 28*28*1.
import tensorflow as tf
# input data
from tensorflow.examples.tutorials.mnist import input_data
# mnist = input_data.read_data_sets('/tmp/data/', one_hot=True)
mnist = input_data.read_data_sets('./MNIST_data', one_hot=True) # runing on server
learning_rate = 0.001
training_iters = 200000
batch_size = 64
display_step = 10
n_input = 784
n_classes = 10
dropout = 0.75
x = tf.placeholder(tf.float32, [None, n_input])
y = tf.placeholder(tf.float32, [None, n_classes])
keep_prob = tf.placeholder(tf.float32) # dropout
def conv2d(name, x, W, b, s=1):
return tf.nn.relu(tf.nn.conv2d(x, W, strides=[1, s, s, 1], padding='SAME'))
def maxpool2d(name, x, k=2, s=2):
return tf.nn.max_pool(x, ksize=[1, k, k, 1], strides=[1, s, s, 1],
padding='VALID', name=name)
def norm(name, l_input, lsize=4):
return tf.nn.lrn(l_input, lsize, bias=1.0, alpha=0.001 / 9.0,
beta=0.75, name=name)
def alex_net(x, weights, biases, dropout):
# Reshape input picture
x = tf.reshape(x, shape=[-1, 28, 28, 1])
conv1 = conv2d('conv1', x, weights['wc1'], biases['bc1'], s=1)
pool1 = maxpool2d('pool1', conv1, k=2, s=2)
norm1 = norm('norm1', pool1)
conv2 = conv2d('conv2', norm1, weights['wc2'], biases['bc2'], s=1)
pool2 = maxpool2d('pool2', conv2, k=2, s=2)
norm2 = norm('norm2', pool2)
conv3 = conv2d('conv3', norm2, weights['wc3'], biases['bc3'], s=1)
pool3 = maxpool2d('pool3', conv3, k=2, s=2)
norm3 = norm('norm3', pool3)
fc1 = tf.reshape(norm3, [-1, weights['wd1'].get_shape().as_list()[0]])
fc1 = tf.add(tf.matmul(fc1, weights['wd1']), biases['bd1'])
fc1 = tf.nn.relu(fc1)
fc2 = tf.add(tf.matmul(fc1, weights['wd2']), biases['bd2'])
fc2 = tf.nn.relu(fc2)
out = tf.matmul(fc2, weights['out']) + biases['out']
return out
weights = {
'wc1': tf.Variable(tf.random_normal([3, 3, 1, 64])),
'wc2': tf.Variable(tf.random_normal([3, 3, 64, 128])),
'wc3': tf.Variable(tf.random_normal([3, 3, 128, 256])),
'wd1': tf.Variable(tf.random_normal([4*4*256, 1024])),
'wd2': tf.Variable(tf.random_normal([1024, 1024])),
'out': tf.Variable(tf.random_normal([1024, n_classes]))
}
biases = {
'bc1': tf.Variable(tf.random_normal([64])),
'bc2': tf.Variable(tf.random_normal([128])),
'bc3': tf.Variable(tf.random_normal([256])),
'bd1': tf.Variable(tf.random_normal([1024])),
'bd2': tf.Variable(tf.random_normal([1024])),
'out': tf.Variable(tf.random_normal([n_classes]))
}
pred = alex_net(x, weights, biases, keep_prob)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
correct_pred = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
step = 1
while step * batch_size < training_iters:
batch_x, batch_y = mnist.train.next_batch(batch_size)
sess.run(optimizer, feed_dict={x: batch_x, y: batch_y,
keep_prob: dropout})
if step % display_step == 0:
loss, acc = sess.run([cost, accuracy], feed_dict={x: batch_x,
y: batch_y,
keep_prob: 1.})
print("Iter " + str(step*batch_size) + ", Minibatch Loss= " + \
"{:.6f}".format(loss) + ", Training Accuracy= " + \
"{:.5f}".format(acc))
step += 1
print("Optimization Finished!")
print("Testing Accuracy:", \
sess.run(accuracy, feed_dict={x: mnist.test.images[:256],
y: mnist.test.labels[:256],
keep_prob: 1.}))
Upvotes: 0
Views: 837
Reputation: 345
It should be because the padding used for maxpool2d
is 'VALID' instead of 'SAME'. How it affected the batch layer was due to reshaping fc1 = tf.reshape(norm3, [-1, weights['wd1'].get_shape().as_list()[0]])
If the above answer didn't correct the error, you should check the output shape of each function by running function_name.eval(sess, feed_dict = {x: mnist.test.images[:256], y: mnist.test.labels[:256], keep_prob: 1.})).shape
in your terminal and see what is the output shape, and whether it is the desired shape for that layer.
Upvotes: 1