【问题标题】:Tensorflow - Cannot feed value of shape (256, 784) for Tensor which has shape '(?, 28, 28, 1)'Tensorflow - 无法为具有形状“(?,28,28,1)”的张量提供形状(256、784)的值
【发布时间】:2018-10-07 16:02:25
【问题描述】:

这是我的简单 covnet。 我的输入是 28 * 28 * 1(灰度)。 我已将输入占位符声明为 (None, 28, 28, 1) 以适应批次,并使用 conv 过滤器形状。 当我评估模型时,我认为它期望大小为 28*28*1 的图像,但它得到一个行向量。但即使我在输入 feed_dict 之前将图像重新整形为 28*28*1,它仍然给我错误。你能帮我改正吗?

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

【问题讨论】:

    标签: tensorflow conv-neural-network


    【解决方案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])
    ...
    

    【讨论】:

    • 重塑创造了奇迹,谢谢!一个问题,当我没有重塑时,它是如何运行我的模型的,因为 conv1 声明了 [None, 28, 28, 1] 形状?
    • Tbh,我不确定,因为它没有在我的计算机上运行。
    【解决方案2】:

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

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

    在将其放入sess.run() 或之前

    features = tf.reshape(features, (-1, 28, 28, 1))
    

    在模型定义中。

    【讨论】:

      猜你喜欢
      • 2023-03-19
      • 1970-01-01
      • 2018-08-04
      • 2016-09-11
      • 1970-01-01
      • 1970-01-01
      • 2018-08-04
      • 1970-01-01
      • 2018-03-01
      相关资源
      最近更新 更多