Keras中的输入尺寸不匹配

时间:2018-02-12 17:59:44

标签: machine-learning neural-network keras time-series lstm

嗨,任何人都可以帮我解决这个错误,我似乎在搜索文档但是无济于事。

目的是预测时间序列。我使用了伪数据df = df.reset_index() df.drop('index', axis = 1, inplace=True) index = df.index[df["Country"] == "Republic of Korea"] df.set_value(index, "Country", "South Korea") df = df.set_index("Country") df["Country"] = df.index 。我希望shape = (N, timesteps, features)predict x_2 x_1, x_3 x_2x_11 x_10 LSTM使用import numpy as np N = 13*12; T = 10; F = 3; X = np.random.rand(N, T, F); Y = np.random.rand(N, 1, F); Y = np.concatenate((X[:,1:T,:], Y), axis=1); import keras from keras.models import Model from keras.layers import Dense, Input, LSTM, Lambda, concatenate, Dropout from keras.optimizers import Adam, SGD from keras import regularizers from keras.metrics import categorical_accuracy from keras.models import load_model input_ = Input(shape = (T, F), name ='input'); x = Dense(15, activation='sigmoid', name='fc1')(input_); x = LSTM(25, return_sequences=True, activation='tanh', name='lstm')(x); x = Dense(F, activation='sigmoid', name='fc2')(x); model = Model(input_, x, name='dummy'); model.compile(optimizer='rmsprop', loss='mse', metrics=['accuracy']); print(model.input_shape); print(X.shape); print(model.output_shape); print(Y.shape); print(model.summary()); model.fit(X, Y, batch_size = 13, epochs=30, validation_split=0.20, shuffle=False); Using Theano backend. (None, 10, 3) (156, 10, 3) (None, 10, 3) (156, 10, 3) _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= input (InputLayer) (None, 10, 3) 0 _________________________________________________________________ fc1 (Dense) (None, 10, 15) 60 _________________________________________________________________ lstm (LSTM) (None, 10, 25) 4100 _________________________________________________________________ fc2 (Dense) (None, 10, 3) 78 ================================================================= Total params: 4,238 Trainable params: 4,238 Non-trainable params: 0 _________________________________________________________________ None Train on 124 samples, validate on 32 samples Epoch 1/30 Traceback (most recent call last): File "C:\Anaconda3\lib\site-packages\theano\compile\function_module.py", line 903, in __call__ self.fn() if output_subset is None else\ ValueError: Input dimension mis-match. (input[0].shape[1] = 10, input[1].shape[1] = 15) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "b.py", line 34, in <module> model.fit(X, Y, batch_size = 13, epochs=30, validation_split=0.20, shuffle=False); File "C:\Anaconda3\lib\site-packages\keras\engine\training.py", line 1498, in fit initial_epoch=initial_epoch) File "C:\Anaconda3\lib\site-packages\keras\engine\training.py", line 1152, in _fit_loop outs = f(ins_batch) File "C:\Anaconda3\lib\site-packages\keras\backend\theano_backend.py", line 1158, in __call__ return self.function(*inputs) File "C:\Anaconda3\lib\site-packages\theano\compile\function_module.py", line 917, in __call__ storage_map=getattr(self.fn, 'storage_map', None)) File "C:\Anaconda3\lib\site-packages\theano\gof\link.py", line 325, in raise_with_op reraise(exc_type, exc_value, exc_trace) File "C:\Anaconda3\lib\site-packages\six.py", line 692, in reraise raise value.with_traceback(tb) File "C:\Anaconda3\lib\site-packages\theano\compile\function_module.py", line 903, in __call__ self.fn() if output_subset is None else\ ValueError: Input dimension mis-match. (input[0].shape[1] = 10, input[1].shape[1] = 15) Apply node that caused the error: Elemwise{Add}[(0, 0)](Reshape{3}.0, InplaceDimShuffle{x,0,x}.0) Toposort index: 98 Inputs types: [TensorType(float32, 3D), TensorType(float32, (True, False, True))] Inputs shapes: [(13, 10, 15), (1, 15, 1)] Inputs strides: [(600, 60, 4), (60, 4, 4)] Inputs values: ['not shown', 'not shown'] Outputs clients: [[Reshape{2}(Elemwise{Add}[(0, 0)].0, TensorConstant{[-1 15]}), Elemwise{Composite{((i0 + i1 + i2 + i3) * scalar_sigmoid(i4) * (i5 - scalar_sigmoid(i4)))}}[(0, 0)](Reshape{3}.0, Reshape{3}.0, Reshape{3}.0, Reshape {3}.0, Elemwise{Add}[(0, 0)].0, TensorConstant{(1, 1, 1) of 1.0})]] 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 optimizati ons 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. (欢迎提出更好的建议)。输出(下图)显示了看似正确的预期输出形状。但是,该错误提到输入维度不匹配。根据文档,我似乎无法找到问题。

new

错误来自

original

我无法理解为什么输入形状在错误中是(1,15,1)的错误以及theano提到的2个输入是什么?

我使用的theano版本是0.9.0,keras版本是2.0.4。如果我不使用任何功能(F),代码将顺利运行。

编辑1 :批量大小为13,只是为了清除错误日志。删除它也会产生完全相同的错误。

0 个答案:

没有答案