python - (千层面)ValueError : Input dimension mis-match

标签 python machine-learning deep-learning theano lasagne

当我运行代码时,出现值错误并显示以下消息:

ValueError: Input dimension mis-match. (input[0].shape[1] = 1, input[2].shape[1] = 20)
Apply node that caused the error: Elemwise{Composite{((i0 + i1) - i2)}}[(0, 0)](Dot22.0, InplaceDimShuffle{x,0}.0, InplaceDimShuffle{x,0}.0)
Toposort index: 18
Inputs types: [TensorType(float64, matrix), TensorType(float64, row), TensorType(float64, row)]
Inputs shapes: [(20, 1), (1, 1), (1, 20)]
Inputs strides: [(8, 8), (8, 8), (160, 8)]
Inputs values: ['not shown', array([[ 0.]]), 'not shown']
Outputs clients: [[Elemwise{Composite{((i0 * i1) / i2)}}(TensorConstant{(1, 1) of 2.0}, Elemwise{Composite{((i0 + i1) - i2)}}[(0, 0)].0, Elemwise{mul,no_inplace}.0), Elemwise{Sqr}[(0, 0)](Elemwise{Composite{((i0 + i1) - i2)}}[(0, 0)].0)]]

我的训练数据是一个条目矩阵,例如..

[ 815.257786    320.447   310.841]

我输入到训练函数的批处理的形状为(BATCH_SIZE, 3),类型为TensorType(float64, matrix)

我的神经网络非常简单:

    self.inpt = T.dmatrix('inpt')
    self.out  = T.dvector('out')

    self.network_in = nnet.layers.InputLayer(shape=(BATCH_SIZE, 3), input_var=self.inpt)
    self.l0         = nnet.layers.DenseLayer(self.network_in, num_units=40,
                        nonlinearity=nnet.nonlinearities.rectify,
    )
    self.network    = nnet.layers.DenseLayer(self.l0, num_units=1,
                        nonlinearity=nnet.nonlinearities.linear
    )

我的损失函数是:

    pred = nnet.layers.get_output(self.network)
    loss = nnet.objectives.squared_error(pred, self.out)
    loss = loss.mean()

我对为什么尺寸不匹配感到有点困惑。我传递了正确的输入和标签类型(根据我的符号变量),并且输入数据的形状对应于我为输入层提供的预期“形状”参数。我相信这是我如何指定批量大小的问题,因为当我使用批量大小 1 时,我的网络可以毫无问题地进行训练,并且错误消息中的 input[2].shape[1] 值是我的批量大小。我对机器学习很陌生,任何帮助将不胜感激!

最佳答案

事实证明,问题是我的标签维度错误。

我的数据有形状:

x_train.shape == (batch_size, 3)
y_train.shape == (batch_size,)

我的网络的符号输入是:

self.inpt = T.dmatrix('inpt')
self.out  = T.dvector('out')

我能够通过 reshape y_train来解决我的问题。然后,我将符号输出变量更改为矩阵以解释这些更改。

y_train = np.reshape(y_train, y_train.shape + (1,))
# y_train.shape == (batch_size, 1)

self.out = T.dmatrix('out')

关于python - (千层面)ValueError : Input dimension mis-match,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42740871/

相关文章:

python - 获取由其中之一标记的多个数组的所有组件状态

machine-learning - 如何在新数据上重新训练模型而不丢失早期模型

python - 为什么我的神经网络预测应用于 MNIST 手绘图像时正确,但应用于我自己的手绘图像时却不正确?

matlab - 如何检查relu梯度

machine-learning - TensorFlow:更改丢失概率等超参数是否会增加训练所需的 GPU 内存?

Python Networkx graphviz : Plot right position of nodes

python - python 如何破坏对象名称和类定义?

php - 当用php执行python文件时,将其放置在哪里?

Python sklearn.mixture.GMM 不适合扩展吗?

Python;使用字典查找数字代码的因子。为什么这不起作用?