Python/Keras/Theano-ValueError:维度不匹配;形状是(98,10),(98,1)

Python/Keras/Theano-ValueError:维度不匹配;形状是(98,10),(98,1),python,numpy,theano,keras,Python,Numpy,Theano,Keras,我一直在尝试用以下代码运行神经网络: model=Sequential() model.add(Dense(output_dim=40, input_dim=90, init="glorot_uniform")) model.add(Activation("tanh")) model.add(Dense(output_dim=10, init="glorot_uniform")) model.add(Activation("linear")) model.compile(loss="mean_a

我一直在尝试用以下代码运行神经网络:

model=Sequential()
model.add(Dense(output_dim=40, input_dim=90, init="glorot_uniform"))
model.add(Activation("tanh"))
model.add(Dense(output_dim=10, init="glorot_uniform"))
model.add(Activation("linear"))
model.compile(loss="mean_absolute_percentage_error", optimizer="rmsprop")
model.fit(X=predictor_train, y=target_train, nb_epoch=2, batch_size=90,show_accuracy=True)
我无法找出此错误的含义:

raise ValueError(base_exc_str)
ValueError: Dimension mismatch; shapes are (98, 10), (98, 1)
据我所知,形状应该相等,比如
(98,10)、(98,10)
(98,1)、(98,1)
,这就是问题的原因。是这样吗

如果是,是否有人知道我可以在代码或数据集中的何处修复此问题?10加1是什么意思

如果没有,谁能向我解释发生了什么事





额外资料:

可变预测器\u序列:

predictor_train.shape = (98, 90)
type(predictor_train) = numpy.ndarray
predictor_train.dtype = float64
len(predictor_train) = 98

predictor_train = [[ -9.28079499e+03  -5.44726790e+03   9.77551565e+03 ...,  -2.94089612e+01
1.05007607e+01   9.32395201e+00]
[ -9.32333218e+03  -4.06918099e+03   8.84849310e+03 ...,   3.02589395e+01
1.32480085e+01   7.35936371e+00]
[ -9.08950902e+03  -2.59672093e+03   6.78783637e+03 ...,  -7.22732280e+00
-8.72789507e+00  -3.38694330e+01]
..., 
[  6.00971088e+03   4.82090785e+02   2.06287833e+03 ...,   5.07504624e+00
-1.08715262e+01  -4.44630971e+00]
[  6.02593657e+03   1.04561016e+03   1.19684456e+03 ...,   2.10305449e+01
-1.00583976e+01  -5.45816394e-01]
[  6.11828134e+03   1.50004864e+03   3.00936969e+02 ...,  -1.66676535e+01
6.07002336e+00   3.00131153e+00]]
可变目标列车:

target_train.shape = (98,)
type(target_train) = pandas.core.series.Series
target_train.dtype = float64
len(target_train) = 98

target_train =
Date
2007-07-01    0.009137
2007-08-01    0.010607
2007-09-01    0.007146
...
2015-06-01   -0.008642
2015-07-01   -0.008642
2015-08-01   -0.008642
Freq: MS, Name: Actual, dtype: float64
完全回溯:

Traceback (most recent call last):
File "/Users/santanna_santanna/PycharmProjects/Predictive Models/teste2.py", line 1479, in Pred
model.fit(X=predictor_train, y=target_train, nb_epoch=2, batch_size=90,show_accuracy=True)
File "/Library/Python/2.7/site-packages/keras/models.py", line 581, in fit
shuffle=shuffle, metrics=metrics)
File "/Library/Python/2.7/site-packages/keras/models.py", line 239, in _fit
outs = f(ins_batch)
File "/Library/Python/2.7/site-packages/keras/backend/theano_backend.py", line 365, in __call__
return self.function(*inputs)
File "/Library/Python/2.7/site-packages/theano/compile/function_module.py", line 595, in __call__
outputs = self.fn()
File "/Library/Python/2.7/site-packages/theano/gof/vm.py", line 233, in __call__
link.raise_with_op(node, thunk)
File "/Library/Python/2.7/site-packages/theano/gof/vm.py", line 229, in __call__
thunk()
File "/Library/Python/2.7/site-packages/theano/gof/op.py", line 768, in rval
r = p(n, [x[0] for x in i], o)
File "/Library/Python/2.7/site-packages/theano/tensor/elemwise.py", line 808, in perform
raise ValueError(base_exc_str)
ValueError: Dimension mismatch; shapes are (98, 10), (98, 1)
Apply node that caused the error: Elemwise{Sub}[(0, 0)](Elemwise{Add}[(0, 0)].0, <TensorType(float32, matrix)>)
Inputs types: [TensorType(float32, matrix), TensorType(float32, matrix)]
Inputs shapes: [(98, 10), (98, 1)]
Inputs strides: [(40, 4), (4, 4)]
Inputs values: ['not shown', 'not shown']

HINT: Re-running with most Theano optimization disabled could give you a back-trace of when this node was created. This can be done with by setting the Theano flag 'optimizer=fast_compile'. If that does not work, Theano optimizations can be disabled with 'optimizer=None'.
HINT: Use the Theano flag 'exception_verbosity=high' for a debugprint and storage map footprint of this apply node.
回溯(最近一次呼叫最后一次):
Pred中的文件“/Users/santanna_santanna/PycharmProjects/Predictive Models/teste2.py”,第1479行
模型拟合(X=预测序列,y=目标序列,nb=2,批次大小=90,显示精度=真)
文件“/Library/Python/2.7/site packages/keras/models.py”,第581行
洗牌=洗牌,指标=指标)
文件“/Library/Python/2.7/site packages/keras/models.py”,第239行,格式为
outs=f(ins\U批量)
文件“/Library/Python/2.7/site packages/keras/backend/theano_backend.py”,第365行,在调用中__
返回自我功能(*输入)
文件“/Library/Python/2.7/site packages/theano/compile/function_module.py”,第595行,在调用中__
输出=self.fn()
文件“/Library/Python/2.7/site packages/theano/gof/vm.py”,第233行,在__
链接。用_op(node,thunk)提升_
文件“/Library/Python/2.7/site packages/theano/gof/vm.py”,第229行,在调用中__
thunk()
rval中的文件“/Library/Python/2.7/site packages/theano/gof/op.py”,第768行
r=p(n[x[0]表示i中的x],o)
文件“/Library/Python/2.7/site packages/theano/tensor/elemwise.py”,第808行,在perform中
提升值错误(基本exc str)
ValueError:维度不匹配;形状是(98,10),(98,1)
导致错误的应用节点:Elemwise{Sub}[(0,0)](Elemwise{Add}[(0,0)].0,)
输入类型:[TensorType(浮点32,矩阵),TensorType(浮点32,矩阵)]
输入形状:[(98,10)、(98,1)]
输入跨步:[(40,4),(4,4)]
输入值:[“未显示”、“未显示”]
提示:在禁用大多数Theano优化的情况下重新运行可能会返回此节点创建的时间。这可以通过设置Theano标志“optimizer=fast\u compile”来完成。如果不起作用,可以使用“optimizer=None”禁用无优化。
提示:对此应用节点的调试打印和存储映射示意图使用Theano标志“exception\u verbosity=high”。

预期输出维度(98,10)与您正在使用的数据维度(98,1)之间不匹配

这是因为您正在使用一个示例代码,该代码应该在10类数据库上进行分类。如果要进行预测,请将最后一层更改为

model.add(Dense(output_dim=1, init="glorot_uniform"))

另外,我认为你的成本函数会有问题。如果有连续数据,则不应使用绝对百分比误差。改变这个

model.compile(loss="mean_absolute_percentage_error", optimizer="rmsprop")
也许是为了这个

model.compile(loss="mean_squared_error", optimizer="rmsprop")