当我运行代码时,出现值错误并显示以下消息:
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/