Python TensorFlow/TFLearn:ValueError:无法为 Tensor u'target/Y:0' 提供形状 (64,) 的值,其形状为 '(?, 10)'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37433321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TensorFlow/TFLearn: ValueError: Cannot feed value of shape (64,) for Tensor u'target/Y:0', which has shape '(?, 10)'
提问by user728785
I have been trying to perform regression using tflearnand my own dataset.
我一直在尝试使用tflearn和我自己的数据集进行回归。
Using tflearn I have been trying to implement a convolutional network based off an exampleusing the MNIST dataset. Instead of using the MNIST dataset I have tried replacing the training and test data with my own. My data is read in from a csv file and is a different shape to the MNIST data. I have 255 features which represent a 15*15 grid and a target value. In the example I replaced the lines 24-30 with (and included import numpy as np):
使用 tflearn,我一直在尝试基于使用 MNIST 数据集的示例来实现卷积网络。我没有使用 MNIST 数据集,而是尝试用我自己的数据替换训练和测试数据。我的数据是从 csv 文件中读入的,与 MNIST 数据的形状不同。我有 255 个特征,它们代表一个 15*15 的网格和一个目标值。在示例中,我将第 24-30 行替换为(并包含 import numpy as np):
#read in train and test csv's where there are 255 features (15*15) and a target
csvTrain = np.genfromtxt('train.csv', delimiter=",")
X = np.array(csvTrain[:, :225]) #225, 15
Y = csvTrain[:,225]
csvTest = np.genfromtxt('test.csv', delimiter=",")
testX = np.array(csvTest[:, :225])
testY = csvTest[:,225]
#reshape features for each instance in to 15*15, targets are just a single number
X = X.reshape([-1,15,15,1])
testX = testX.reshape([-1,15,15,1])
## Building convolutional network
network = input_data(shape=[None, 15, 15, 1], name='input')
I get the following error:
我收到以下错误:
ValueError: Cannot feed value of shape (64,) for Tensor u'target/Y:0', which has shape '(?, 10)'
ValueError: 无法为 Tensor u'target/Y:0' 提供形状 (64,) 的值,其形状为 '(?, 10)'
I have tried various combinations and have seen a similar questionin stackoverflow but have not had success. The example in this page does not work for me and throws a similar error and I do not understand the answer provided or those provided by similar questions.
我尝试了各种组合,并在 stackoverflow 中看到了类似的问题,但没有成功。此页面中的示例对我不起作用并引发类似错误,我不明白所提供的答案或类似问题提供的答案。
How do I use my own data?
我如何使用我自己的数据?
回答by Olivier Moindrot
Short answer
简答
In the line 41 of the MNIST example, you also have to change the output size 10 to 1 in network = fully_connected(network, 10, activation='softmax')
to network = fully_connected(network, 1, activation='linear')
. Note that you can remove the final softmax.
在MNIST 示例的第 41 行中,您还必须将输出大小 10 更改为 1 in network = fully_connected(network, 10, activation='softmax')
to network = fully_connected(network, 1, activation='linear')
。请注意,您可以删除最终的 softmax。
Looking at your code, it seems you have a target value Y
, which means using the L2 losswith mean_square
(you will find hereall the losses available):
看你的代码,看来你有一个目标值Y
,其使用的手段L2损失与mean_square
(你会发现这里的一切损失提供):
regression(network, optimizer='adam', learning_rate=0.01,
loss='mean_square', name='target')
Also, reshape Y and Y_test to have shape (batch_size, 1).
此外,重塑 Y 和 Y_test 以具有形状 (batch_size, 1)。
Long answer: How to analyse the error and find the bug
长答案:如何分析错误并找到错误
Here is how to analyse the error:
以下是分析错误的方法:
- The error is
Cannot feed value ... for Tensor 'target/Y'
, which means it comes from the feed_dictargument Y. - Again, according to the error, you try to feed an Y value
of shape (64,)
whereas the network expect a shape(?, 10)
.- It expects a shape (batch_size, 10), because originally it's a network for MNIST (10 classes)
- We now want to change the expected value of the network for Y.
- in the code, we see that the last layer
fully_connected(network, 10, activation='softmax')
is returning an output of size 10 - We change that to an output of size 1 without softmax:
fully_connected(network, 1, activation='linear')
- in the code, we see that the last layer
- 错误是
Cannot feed value ... for Tensor 'target/Y'
,这意味着它来自feed_dict参数 Y。 - 同样,根据错误,您尝试提供 Y 值,
of shape (64,)
而网络期望提供 shape(?, 10)
。- 它需要一个形状 (batch_size, 10),因为它最初是一个用于 MNIST(10 个类)的网络
- 我们现在想要改变 Y 的网络期望值。
- 在代码中,我们看到最后一层
fully_connected(network, 10, activation='softmax')
正在返回大小为 10 的输出 - 我们将其更改为没有 softmax 的大小为 1 的输出:
fully_connected(network, 1, activation='linear')
- 在代码中,我们看到最后一层
In the end, it was not a bug, but a wrong model architecture.
最后,这不是错误,而是错误的模型架构。