我正在实现一个逻辑回归函数。它的实现非常简单,并且在计算准确率之前都能正常工作。以下是我的逻辑回归代码…
mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)# tf Graph Inputx = tf.get_variable("input_image", shape=[100,784], dtype=tf.float32)x_placeholder = tf.placeholder(tf.float32, shape=[100, 784])assign_x_op = x.assign(x_placeholder).opy = tf.placeholder(shape=[100,10], name='input_label', dtype=tf.float32) # 0-9 digits recognition => 10 classes# set model weightsW = tf.get_variable("weights", shape=[784, 10], dtype=tf.float32, initializer=tf.random_normal_initializer())b = tf.get_variable("biases", shape=[1, 10], dtype=tf.float32, initializer=tf.zeros_initializer())# construct modellogits = tf.matmul(x, W) + bpred = tf.nn.softmax(logits) # Softmax# minimize error using cross entropycost = tf.reduce_mean(-tf.reduce_sum(y * tf.log(pred), reduction_indices=1))# Gradient Descentoptimizer = tf.train.GradientDescentOptimizer(FLAGS.learning_rate).minimize(cost)# initializing the variablesinit = tf.global_variables_initializer()saver = tf.train.Saver()# launch the graphwith tf.Session() as sess: sess.run(init) # training cycle for epoch in range(FLAGS.training_epochs): avg_cost = 0 total_batch = int(mnist.train.num_examples/FLAGS.batch_size) # loop over all batches for i in range(total_batch): batch_xs, batch_ys = mnist.train.next_batch(FLAGS.batch_size) # Assign the contents of `batch_xs` to variable `x`. sess.run(assign_x_op, feed_dict={x_placeholder: batch_xs}) _, c = sess.run([optimizer, cost], feed_dict={y: batch_ys}) # compute average loss avg_cost += c / total_batch # display logs per epoch step if (epoch + 1) % FLAGS.display_step == 0: print("Epoch:", '%04d' % (epoch + 1), "cost=", "{:.9f}".format(avg_cost)) save_path = saver.save(sess, "/tmp/model.ckpt") print("Model saved in file: %s" % save_path) print("Optimization Finished!")
如您所见,这是一个基本的逻辑回归函数,并且运行得很完美。
需要注意的是,batch_size
是100
。
现在,在上面的代码片段之后,我尝试了以下操作…
# list of booleans to determine the correct predictionscorrect_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))print(correct_prediction.eval({x_placeholder:mnist.test.images, y:mnist.test.labels}))# calculate total accuracyaccuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))print("Accuracy:", accuracy.eval({x: mnist.test.images, y: mnist.test.labels}))
然而,代码在correct_prediction
上失败了。我得到了以下错误…
% (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape()))) ValueError: Cannot feed value of shape (10000, 784) for Tensor ‘Placeholder:0’, which has shape ‘(100, 784)’
我认为我得到这个错误是因为我试图为x
分配的占位符的值。如何解决这个问题?我是否需要reshape
数组?
回答:
在
x_placeholder = tf.placeholder(tf.float32, shape=[100, 784])y = tf.placeholder(shape=[100,10], name='input_label', dtype=tf.float32) # 0-9
避免将第一个维度固定为100,因为这会阻止您使用任何其他批量大小(所以如果mnist.test.images
中的图像数量与100不同,您会得到错误)。相反,将它们指定为None
:
x_placeholder = tf.placeholder(tf.float32, shape=[None, 784])y = tf.placeholder(shape=[None,10], name='input_label', dtype=tf.float32) #
然后您可以使用任何批量大小