Tensorflow-无法为形状为((?,28,28,1)'的Tensor输入形状(256,784)的值

时间:2018-10-07 07:11:28

标签: tensorflow conv-neural-network

这是我简单的Covnet。 我的输入是28 * 28 * 1(灰度)。 我已声明输入占位符为(None,28,28,1)以容纳批次,并使用转换滤镜形状。 当我评估模型时,我认为它期望的图像大小为28 * 28 * 1,但它得到的行向量。但是,即使我在将图像重塑为28 * 28 * 1后再送入feed_dict,它仍然会给我错误。您能帮我改正吗?

import tensorflow as tf
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data


def batches(batch_size, features, labels):
    """
    Create batches of features and labels
    :param batch_size: The batch size
    :param features: List of features
    :param labels: List of labels
    :return: Batches of (Features, Labels)
    """
    assert len(features) == len(labels)
    outout_batches = []

    sample_size = len(features)
    for start_i in range(0, sample_size, batch_size):
        end_i = start_i + batch_size
        batch = [features[start_i:end_i], labels[start_i:end_i]]
        outout_batches.append(batch)

    return outout_batches


mnist = input_data.read_data_sets('/datasets/ud730/mnist', one_hot=True)
train_features = mnist.train.images
test_features = mnist.test.images

train_labels = mnist.train.labels.astype(np.float32)
test_labels = mnist.test.labels.astype(np.float32)

batch_size = 256
n_input = 28 * 28
n_labels = 10
learning_rate = 0.001
n_hidden_layer = 256
n_hidden_layer2 = 64
mu = 0.0
sigma = 0.1

########### CONVOLUTION LAYER NETWORK START ########################################################################
### INPUT --> LAYER 1 : CONV1(5*5 FILTER, DEPTH=6, S=1, P=YES/'SAME') --> RELU --> MAXPOOL -->
###           LAYER 2 :  --> CONV2(5*5 FILTER, DEPTH=16, S=1, P=YES/'SAME') --> RELU --> MAXPOOL -->
###           LAYER 3, 4, OUTPUT: --> DENSE --> DENSE --> OUTPUT --> ACTIVATION

# INPUT         : 28 * 28
# LAYER 1, d=6  : (28-5+1/1 = 24) = 24 * 24 * 6
# MAXPOOL1      : 12 * 12 * 6
# LAYER 2, d=16 : (12-5+1/1 = 8) = 8 * 8 * 16
# MAXPOOL2      : 4 * 4 * 16
# DENSE 1       : 256 --> 128
# DENSE 2       : 128 --> 64
# DENSE 3       : 64  --> 10

#LAYER1
#w1 is the convolution filter weight, not the complete weight 

features = tf.placeholder(tf.float32, [None, 28, 28, 1])
labels = tf.placeholder(tf.float32, [None])


w1 = tf.Variable(tf.truncated_normal(shape=[5, 5, 1, 6], mean=mu, stddev=sigma))
b1 = tf.Variable(tf.zeros(6))
conv1 = tf.nn.conv2d(features, w1, strides=[1,1,1,1], padding='VALID')
conv1 = tf.nn.relu(conv1)
conv1 = tf.nn.max_pool(conv1, strides=[1,2,2,1], ksize=[1,2,2,1], padding='SAME')

w2 = tf.Variable(tf.truncated_normal(shape=[5,5,6,16], mean=mu, stddev=sigma))
b2 = tf.Variable(tf.zeros(16))
conv2 = tf.nn.conv2d(conv1, w2, strides=[1,1,1,1], padding='VALID')
conv2 = tf.nn.relu(conv2)
conv2 = tf.nn.max_pool(conv2, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')

flattened_vector = tf.contrib.layers.flatten(conv2)

w3 = tf.Variable(tf.truncated_normal(shape=[256, 128], mean=mu, stddev=sigma))
b3 = tf.Variable(tf.zeros(128))
layer3 = tf.add(tf.matmul(flattened_vector, w3), b3)
layer3 = tf.nn.relu(layer3)

w4 = tf.Variable(tf.truncated_normal(shape=[128, 64], mean=mu, stddev=sigma))
b4 = tf.Variable(tf.zeros(64))
layer4 = tf.add(tf.matmul(layer3, w4), b4)
layer4 = tf.nn.relu(layer4)

w5 = tf.Variable(tf.truncated_normal(shape=[64, 10], mean=mu, stddev=sigma))
b5 = tf.Variable(tf.zeros(10))
layer5 = tf.add(tf.matmul(layer4, w5), b5)
logits = tf.nn.relu(layer5)

########### CONVOLUTION LAYER NETWORK END ########################################################################


cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=labels))
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(labels,1))
accuracy = tf.reduce_mean(tf.cast(prediction, tf.float32))


############ EVALUATING THE MODEL ############
init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    for batch_images, batch_labels in batches(batch_size, train_features, train_labels):
        sess.run(optimizer, feed_dict={features:batch_images, labels:batch_labels})

    total_accuracy = sess.run(accuracy, feed_dict={features:test_features, labels:test_labels})
    print(total_accuracy)

错误

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-14-3f341fec1373> in <module>
    106     sess.run(init)
    107     for batch_images, batch_labels in batches(batch_size, train_features, train_labels):
--> 108         sess.run(optimizer, feed_dict={features:batch_images, labels:batch_labels})
    109 
    110     total_accuracy = sess.run(accuracy, feed_dict={features:test_features, labels:test_labels})

F:\ProgramFiles\miniconda\envs\IntroToTensorFlow\lib\site-packages\tensorflow\python\client\session.py in run(self, fetches, feed_dict, options, run_metadata)
    875     try:
    876       result = self._run(None, fetches, feed_dict, options_ptr,
--> 877                          run_metadata_ptr)
    878       if run_metadata:
    879         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

F:\ProgramFiles\miniconda\envs\IntroToTensorFlow\lib\site-packages\tensorflow\python\client\session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
   1074                              'which has shape %r' %
   1075                              (np_val.shape, subfeed_t.name,
-> 1076                               str(subfeed_t.get_shape())))
   1077           if not self.graph.is_feedable(subfeed_t):
   1078             raise ValueError('Tensor %s may not be fed.' % subfeed_t)

ValueError: Cannot feed value of shape (256, 784) for Tensor 'Placeholder_26:0', which has shape '(?, 28, 28, 1)'

2 个答案:

答案 0 :(得分:1)

您的代码中有两个问题。

您应该重塑输入内容。例如,执行以下操作:

mnist = input_data.read_data_sets('/tmp/datasets/ud730/mnist', one_hot=True)
train_features = [d.reshape(28, 28, 1) for d in mnist.train.images]
test_features = [d.reshape(28, 28, 1) for d in mnist.test.images]

标签的尺寸也应为:

...
features = tf.placeholder(tf.float32, [None, 28, 28, 1])
labels = tf.placeholder(tf.float32, [None, 10])
...

答案 1 :(得分:0)

我看不到您在何处重塑输入数据。如错误消息所述,它期望输入的形状与占位符相同。所以您要么需要

RewriteRule ^api/(.*)$ /apiHandler.php [R=307]

放入np.reshape(batch_images, (-1, 28, 28, 1))

之前
sess.run()

在模型定义中。