Tensorflow返回太多预测

时间:2018-04-02 17:20:43

标签: python tensorflow mnist

我正在为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)。下图显示了调试信息和这些尺寸。

    Screen shot of debugging

代码

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}))

1 个答案:

答案 0 :(得分:0)

更新:我切换到tensorflow slim,几乎立即解决了我所有的问题。我从来没有想过上述代码的问题,因此从技术上讲这不是答案,只是想在其他人遇到类似问题时提供建议。