【发布时间】:2018-09-11 22:20:22
【问题描述】:
我正在为 mnist 开发 TensorFlow CNN 模型,修改此示例:https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/3_NeuralNetworks/convolutional_network_raw.py。
运行测试 256 个 mnist 图像时,它返回 784 个预测而不是 256。我猜测 784 来自 mnist 图像大小(28 像素 x 28 像素 = 784),但是我不清楚轴对齐的位置可能是出错了,如果它实际上是轴对齐问题。
具体来说,我从代码correct_pred = tf.equal(tf.argmax(prediction, 1), tf.argmax(Y, 1)的这一行得到以下错误:
InvalidArgumentError (see above for traceback): Incompatible shapes: [784] vs. [256]
[[Node: Equal = Equal[T=DT_INT64, _device="/job:localhost/replica:0/task:0/device:CPU:0"](ArgMax, ArgMax_1)]]
哪里出了问题:
- 我的权重和偏置矩阵维度可能不像我预期的那样对齐。我正在尝试使用手动定义的权重/偏差,虽然我认为尺寸对齐,但我显然遗漏了一些东西。
-
我可能没有正确使用密集层。我添加了密集层以从权重 (4x4x50x500) 到分类层 (500x10) 的正确大小。下图显示了调试信息和这些尺寸。
代码
import tensorflow as tf
import pickle
input_size = 28 # e.g. 28x28 input
model = pickle.load(open("model.p", "rb" ))
weights = {
'wc1': tf.Variable(model[0]['weights']), # 5x5x20
'wc2': tf.Variable(model[2]['weights']), # 5x5x20x50
'wd1': tf.Variable(model[4]['weights']), # 4x4x50x500
'out': tf.Variable(model[5]['weights']) # 500x10
}
biases = {
'bc1': tf.Variable(model[0]['bias']), # 20
'bc2': tf.Variable(model[2]['bias']), # 50
'bd1': tf.Variable(model[4]['bias']), # 500
'out': tf.Variable(model[5]['bias']), # 10
}
# Import MNIST data
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("./MNIST-data/", one_hot=True)
# tf Graph input
X = tf.placeholder(tf.float32, [None, 784])
Y = tf.placeholder(tf.float32, [None, 10])
keep_prob = tf.placeholder(tf.float32) # dropout (keep probability)
# Create some wrappers for simplicity
def conv2d(x, W, b, strides=1):
# Conv2D wrapper, with bias and relu activation
x = tf.nn.conv2d(x, W,strides=[1, strides, strides, 1], padding='SAME')
x = tf.nn.bias_add(x, b)
return tf.nn.relu(x)
def maxpool2d(x, k=2):
# MaxPool2D wrapper
return tf.nn.max_pool(x, ksize=[1, k, k, 1], strides=[1, k, k, 1],
padding='SAME')
# Create model
def conv_net(x, weights, biases):
x = tf.reshape(x, shape=[-1, input_size, input_size, 1])
conv1 = conv2d(x, tf.reshape(weights['wc1'], shape=[5, 5, 1, 20]), biases['bc1'])
pool1 = maxpool2d(conv1, k=2)
conv2 = conv2d(pool1, weights['wc2'], biases['bc2'])
pool2 = maxpool2d(conv2, k=2)
# Fully connected layer
# Reshape conv2 output to fit fully connected layer input
pool2_flat = tf.reshape(pool2, [-1, 4 * 4 * 50])
dense = tf.layers.dense(inputs=pool2_flat, units=500, activation=tf.nn.relu)
# Output, class prediction
out = tf.add(tf.matmul(dense, weights['out']), biases['out']) # shape = (?, 10)
return out
logits = conv_net(X, weights, biases)
prediction = tf.nn.softmax(logits)
# # Evaluate model
correct_pred = tf.equal(tf.argmax(prediction, 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)
# Calculate accuracy for 256 MNIST test images
print("Testing Accuracy:", \
sess.run(accuracy, {X: mnist.test.images[:256], Y: mnist.test.labels[:256], keep_prob: 1.0}))
【问题讨论】:
标签: python tensorflow mnist