TensorFlow/TFLearn: ValueError:不能为张量u'target/Y:0',它的形状(?)提供形状的值(64,)。,10)

时间:2022-05-14 13:47:36

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

我一直在尝试使用tflearn和我自己的数据集来执行回归。

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

使用tflearn,我尝试使用MNIST数据集实现一个基于示例的卷积网络。我没有使用MNIST数据集,而是尝试用自己的方法替换训练和测试数据。我的数据是从csv文件中读取的,与MNIST数据的形状不同。我有255个特征,代表15*15的网格和一个目标值。在示例中,我用(并包含import numpy as np)替换了24-30行:

#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:不能为张量u'target/Y:0',它有形状的形状(64,)的饲料值。,10)

I have tried various combinations and have seen a similar question in * 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 个解决方案

#1


23  

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行中,您还必须将网络= fully_connected(网络,10,激活='softmax')的输出大小更改为10,以网络= fully_connected(网络,1,激活='线性')。注意,您可以删除最后的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):

查看您的代码,您似乎有一个目标值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_dict argument Y.
  • 错误不能给值…对于张量'目标/Y',这意味着它来自于feed_dict参数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)
    • 它期望一个形状(batch_size, 10),因为最初它是一个MNIST(10个类)的网络
  • 同样,根据错误,您尝试输入形状(64)的Y值,而网络期望一个形状(?,10)。它期望一个形状(batch_size, 10),因为最初它是一个MNIST(10个类)的网络
  • 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
    • 在代码中,我们看到最后一层fully_connected(网络,10,激活='softmax')正在返回10号的输出。
    • We change that to an output of size 1 without softmax: fully_connected(network, 1, activation='linear')
    • 我们将其更改为不使用softmax的大小为1的输出:fully_connected(网络,1,激活='线性')
  • 我们现在想要改变网络的预期值,在代码中,我们看到最后一层fully_connected(网络,10,激活='softmax')返回一个大小为10的输出,我们将其改为1号大小的输出,没有软max: fully_connected(网络,1,激活='线性')

In the end, it was not a bug, but a wrong model architecture.

最后,它不是一个错误,而是一个错误的模型架构。

#1


23  

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行中,您还必须将网络= fully_connected(网络,10,激活='softmax')的输出大小更改为10,以网络= fully_connected(网络,1,激活='线性')。注意,您可以删除最后的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):

查看您的代码,您似乎有一个目标值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_dict argument Y.
  • 错误不能给值…对于张量'目标/Y',这意味着它来自于feed_dict参数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)
    • 它期望一个形状(batch_size, 10),因为最初它是一个MNIST(10个类)的网络
  • 同样,根据错误,您尝试输入形状(64)的Y值,而网络期望一个形状(?,10)。它期望一个形状(batch_size, 10),因为最初它是一个MNIST(10个类)的网络
  • 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
    • 在代码中,我们看到最后一层fully_connected(网络,10,激活='softmax')正在返回10号的输出。
    • We change that to an output of size 1 without softmax: fully_connected(network, 1, activation='linear')
    • 我们将其更改为不使用softmax的大小为1的输出:fully_connected(网络,1,激活='线性')
  • 我们现在想要改变网络的预期值,在代码中,我们看到最后一层fully_connected(网络,10,激活='softmax')返回一个大小为10的输出,我们将其改为1号大小的输出,没有软max: fully_connected(网络,1,激活='线性')

In the end, it was not a bug, but a wrong model architecture.

最后,它不是一个错误,而是一个错误的模型架构。