Hi,
I am using this below code to build a CNN to train a cat-classifier( project):_
import tensorflow as tf
def reset_graph(seed=42):
tf.reset_default_graph()
tf.set_random_seed(seed)
np.random.seed(seed)
height = 64
width = 64
channels = 3
conv1_fmaps = 32
conv1_ksize = 3
conv1_stride = 1
conv1_pad = “SAME”
conv2_fmaps = 64
conv2_ksize = 3
conv2_stride = 2
conv2_pad = “SAME”
pool3_fmaps = conv2_fmaps
n_fc1 = 64
n_outputs = 2
reset_graph()
with tf.name_scope(“inputs”):
X = tf.placeholder(tf.float32, shape=[None, height, width, channels], name=“X”) # None, 64,64,3
y = tf.placeholder(tf.int32, shape=[None], name=“y”) # [None]
conv1 = tf.layers.conv2d(X, filters=conv1_fmaps, kernel_size=conv1_ksize,
strides=conv1_stride, padding=conv1_pad,
activation=tf.nn.relu, name=“conv1”)
conv2 = tf.layers.conv2d(conv1, filters=conv2_fmaps, kernel_size=conv2_ksize,
strides=conv2_stride, padding=conv2_pad,
activation=tf.nn.relu, name=“conv2”)
with tf.name_scope(“pool3”):
pool3 = tf.nn.max_pool(conv2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding=“VALID”)
pool3_flat = tf.reshape(pool3, shape=[-1, pool3_fmaps * 8 * 8])
with tf.name_scope(“fc1”):
fc1 = tf.layers.dense(pool3_flat, n_fc1, activation=tf.nn.relu, name=“fc1”)
with tf.name_scope(“output”):
logits = tf.layers.dense(fc1, n_outputs, name=“output”)
Y_proba = tf.nn.softmax(logits, name=“Y_proba”)
with tf.name_scope(“train”):
xentropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=y)
loss = tf.reduce_mean(xentropy)
optimizer = tf.train.AdamOptimizer()
training_op = optimizer.minimize(loss)
with tf.name_scope(“eval”):
correct = tf.nn.in_top_k(logits, y, 1)
accuracy = tf.reduce_mean(tf.cast(correct, tf.float32))
with tf.name_scope(“init_and_save”):
init = tf.global_variables_initializer()
saver = tf.train.Saver()
Lets Execute the network
n_epochs = 15
batch_size = 15
with tf.Session() as sess:
init.run()
for epoch in range(n_epochs):
X_batch, y_batch = train_set_x_orig,train_set_y_orig
sess.run(training_op, feed_dict={X: X_batch, y: y_batch})
acc_train = accuracy.eval(feed_dict={X: X_batch, y: y_batch})
acc_test = accuracy.eval(feed_dict={X: test_set_x_orig, y: test_set_y_orig})
print(epoch, “Train accuracy:”, acc_train, “Test accuracy:”, acc_test)
save_path = saver.save(sess, "model_ckps/my_mnist_model")
i am getting these below error :-
nvalidArgumentError: logits and labels must have the same first dimension, got logits shape [836,2] and labels shape [209]
[[node train/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits (defined at :62) ]]
Errors may have originated from an input operation.
Input Source operations connected to node train/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits:
inputs/y (defined at :32)
output/output/BiasAdd (defined at /tmp/tmp2fdf602y.py:74)
Please tell me how to resolve it and why is this error coming when my Network build is correct