TensorFlow/TFLearn: ValueError: Cannot feed value of shape (64,) for Tensor u'target/Y:0', which has shape '(?, 10)'

匿名 (未验证) 提交于 2019-12-03 02:06:01

问题:

I have been trying to perform regression using tflearn and my own dataset.

Using tflearn I have been trying to implement a convolutional network based off an example using 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):

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

I have tried various combinations and have seen a similar question in 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.

How do I use my own data?

回答1:

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.

Looking at your code, it seems you have a target value Y, which means using the L2 loss with mean_square (you will find here all the losses available):

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


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_dict argument 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 end, it was not a bug, but a wrong model architecture.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!