Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Tensorflow-无法为具有形状';(?,28,28,1)和#x27;_Tensorflow_Conv Neural Network - Fatal编程技术网

Tensorflow-无法为具有形状';(?,28,28,1)和#x27;

Tensorflow-无法为具有形状';(?,28,28,1)和#x27;,tensorflow,conv-neural-network,Tensorflow,Conv Neural Network,这是我的简单网络。 我的输入是28*28*1(灰度)。 我已将输入占位符声明为(None、28、28、1)以容纳批,并使用conv过滤器形状。 当我评估模型时,我认为它期望的是大小为28*28*1的图像,但它得到的是一个行向量。但是,即使在将图像输入到提要之前,我将图像重塑为28*28*1,它仍然会给我错误。你能帮我纠正一下吗 import tensorflow as tf import numpy as np from tensorflow.examples.tutorials.mnist i

这是我的简单网络。 我的输入是28*28*1(灰度)。 我已将输入占位符声明为(None、28、28、1)以容纳批,并使用conv过滤器形状。 当我评估模型时,我认为它期望的是大小为28*28*1的图像,但它得到的是一个行向量。但是,即使在将图像输入到提要之前,我将图像重塑为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)'
---------------------------------------------------------------------------
ValueError回溯(最近一次调用上次)
在里面
106 sess.run(初始化)
107对于批次图像,批次标签(批次大小、序列特征、序列标签):
-->108 sess.run(优化器,feed_dict={特性:批处理图像,标签:批处理标签})
109
110总精度=sess.run(精度,馈送精度={特性:测试特性,标签:测试标签})
F:\ProgramFiles\miniconda\envs\IntroToTensorFlow\lib\site packages\tensorflow\python\client\session.py正在运行(self、fetches、feed\u dict、options、run\u元数据)
875尝试:
876结果=自运行(无、取数、馈送、选项、,
-->877运行_元数据_ptr)
878如果运行元数据:
879 proto_data=tf_session.tf_GetBuffer(运行元数据ptr)
F:\ProgramFiles\miniconda\envs\IntroToTensorFlow\lib\site packages\tensorflow\python\client\session.py in\u run(self、handle、fetches、feed\u dict、options、run\u元数据)
1074'具有形状%r'%
1075(np值形状、子进纸名称、,
->1076 str(子进纸获取形状())
1077如果不是自进给,则图形可进给(副进给):
1078 raise VALUE ERROR('张量%s可能无法进给。'%subfeed\t)
ValueError:无法为具有形状“(?,28,28,1)”的张量“占位符_26:0”提供形状(256,784)的值

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

np.reshape(batch_images, (-1, 28, 28, 1))
将其放入
sess.run()

features = tf.reshape(features, (-1, 28, 28, 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,我不确定,因为它没有在我的计算机上运行。