python - Incompatible shapes on tensorflow -


i new in cnn,i trying make cnn classify image data set of handwritten english alphabet(a-z),(a-z) , numbers (0-9),which have 62 labels.each image size 30*30 pixels.i following steps in tutorial https://www.tensorflow.org/get_started/mnist/pros. when run model error

tensorflow.python.framework.errors.invalidargumenterror: incompatible shapes: [40] vs. [10]

my batch size 10,the error appears @ correct_prediction. solution of same problem found in tensorflow incompatable shapes error in tutorial,didn't fix problem.any appreciated. data set compressed in pickle first,here codes.

import tensorflow tf import pandas pd import numpy np sklearn.model_selection import train_test_split x = [] y = [] import pickle  #load data pickle pickle_file = 'let.pickle'  open(pickle_file, 'rb') f:     save = pickle.load(f)     x = save['dataset']     y = save['labels']      del save  # hint gc free memory  #normalise features x = (x - 255/2) / 255  # 1 hot encoding y = pd.get_dummies(y) y = y.values  # change ndarray y = np.float32(y) x = np.float32(x) xtr, xte, ytr, yte = train_test_split(x, y, train_size=0.7)  batch_size = 10  sess = tf.interactivesession()  x = tf.placeholder(tf.float32, shape=[none, 900]) y_ = tf.placeholder(tf.float32, shape=[none, 62])  def weight_variable(shape):   initial = tf.truncated_normal(shape, stddev=0.1)   return tf.variable(initial)  def bias_variable(shape):   initial = tf.constant(0.1, shape=shape)   return tf.variable(initial)  def conv2d(x, w):   return tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='same')  def max_pool_2x2(x):   return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],                         strides=[1, 2, 2, 1], padding='same')  w_conv1 = weight_variable([5, 5, 1, 32]) b_conv1 = bias_variable([32])  x_image = tf.reshape(x, [-1,30,30,1])  h_conv1 = tf.nn.relu(conv2d(x_image, w_conv1) + b_conv1) h_pool1 = max_pool_2x2(h_conv1)  w_conv2 = weight_variable([5, 5, 32, 64]) b_conv2 = bias_variable([64])  h_conv2 = tf.nn.relu(conv2d(h_pool1, w_conv2) + b_conv2) h_pool2 = max_pool_2x2(h_conv2)  w_fc1 = weight_variable([4 * 4 * 64, 1024]) b_fc1 = bias_variable([1024])  h_pool2_flat = tf.reshape(h_pool2, [-1, 4*4*64]) h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, w_fc1) + b_fc1)  keep_prob = tf.placeholder(tf.float32) h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)  w_fc2 = weight_variable([1024, 62]) b_fc2 = bias_variable([62])  y_conv = tf.matmul(h_fc1_drop, w_fc2) + b_fc2  cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv)) train_step = tf.train.adamoptimizer(1e-4).minimize(cross_entropy) correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))  accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) sess.run(tf.global_variables_initializer()) in range(20000):     offset = (i * batch_size) % (ytr.shape[0] - batch_size)     batch_x = xtr[offset:(offset + batch_size), :]     batch_y = ytr[offset:(offset + batch_size), :]     if i%100 == 0:         train_accuracy = accuracy.eval(feed_dict={x:batch_x , y_: batch_y, keep_prob: 1.0})         print("step %d, training accuracy %g"%(i, train_accuracy))     train_step.run(feed_dict={x: xtr[offset:(offset + batch_size), :], y_: ytr[offset:(offset + batch_size), :], keep_prob: 0.5})  print("test accuracy %g"%accuracy.eval(feed_dict={x: xte, y_: yte, keep_prob: 1.0})) 

take @ these lines of code:

cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv)) correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1)) 

if labels list index of correct prediction (for example: [1,32,13, ...]) softmax_cross_entropy_with_logits function correct. means error in these lines. put in comment do:

tf.argmax(y_conv,1) # takes max index of logits tf.argmax(y_,1) # takes max index of ???.  

although did not test it, replacing line should work:

correct_prediction = tf.equal(tf.argmax(y_conv,1), y_) 

let me know when fixed :d


Comments

Popular posts from this blog

How to understand 2 main() functions after using uftrace to profile the C++ program? -

c# - Update a combobox from a presenter (MVP) -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -