如何使用多维输入训练 Keras LSTM?
How to train a Keras LSTM with a multidimensional input?
这是我输入数据的形状:
>> print(X_train.shape)
(1125, 75, 2)
然后我尝试通过这种方式构建模型:
model = Sequential()
model.add(LSTM(
output_dim=50,
input_shape = (75, 2),
#input_shape = X_train.shape[1:],
return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(
100,
return_sequences=False))
model.add(Dropout(0.2))
model.add(Dense(
output_dim=1))
model.add(Activation("linear"))
model.compile(loss="mse", optimizer="rmsprop")
model.fit(
X_train,
y_train,
batch_size=512,
nb_epoch=5,
validation_split=0.1,
verbose=0,
shuffle=True)
但是returns拟合时出现如下错误:
ValueError: Error when checking model target: expected activation_32
to have 2 dimensions, but got array with shape (1125, 75, 2)
这是完整的错误输出:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-48-b209fe29a91d> in <module>()
152 verbose=0,
--> 153 shuffle=True)
154
/usr/local/lib/python3.5/dist-packages/keras/models.py in fit(self, x, y, batch_size, nb_epoch, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, **kwargs)
670 class_weight=class_weight,
671 sample_weight=sample_weight,
--> 672 initial_epoch=initial_epoch)
673
674 def evaluate(self, x, y, batch_size=32, verbose=1,
/usr/local/lib/python3.5/dist-packages/keras/engine/training.py in fit(self, x, y, batch_size, nb_epoch, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch)
1114 class_weight=class_weight,
1115 check_batch_axis=False,
-> 1116 batch_size=batch_size)
1117 # prepare validation data
1118 if validation_data:
/usr/local/lib/python3.5/dist-packages/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_batch_axis, batch_size)
1031 output_shapes,
1032 check_batch_axis=False,
-> 1033 exception_prefix='model target')
1034 sample_weights = standardize_sample_weights(sample_weight,
1035 self.output_names)
/usr/local/lib/python3.5/dist-packages/keras/engine/training.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
110 ' to have ' + str(len(shapes[i])) +
111 ' dimensions, but got array with shape ' +
--> 112 str(array.shape))
113 for j, (dim, ref_dim) in enumerate(zip(array.shape, shapes[i])):
114 if not j and not check_batch_axis:
ValueError: Error when checking model target: expected activation_32 to have 2 dimensions, but got array with shape (1125, 75, 2)
我做错了什么?我关注了 this tutorial of Keras documentation.
您的问题不在于输入形状,而在于输出形状。您需要重新检查 y_train
的形状是否合适。
这是我输入数据的形状:
>> print(X_train.shape)
(1125, 75, 2)
然后我尝试通过这种方式构建模型:
model = Sequential()
model.add(LSTM(
output_dim=50,
input_shape = (75, 2),
#input_shape = X_train.shape[1:],
return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(
100,
return_sequences=False))
model.add(Dropout(0.2))
model.add(Dense(
output_dim=1))
model.add(Activation("linear"))
model.compile(loss="mse", optimizer="rmsprop")
model.fit(
X_train,
y_train,
batch_size=512,
nb_epoch=5,
validation_split=0.1,
verbose=0,
shuffle=True)
但是returns拟合时出现如下错误:
ValueError: Error when checking model target: expected activation_32 to have 2 dimensions, but got array with shape (1125, 75, 2)
这是完整的错误输出:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-48-b209fe29a91d> in <module>()
152 verbose=0,
--> 153 shuffle=True)
154
/usr/local/lib/python3.5/dist-packages/keras/models.py in fit(self, x, y, batch_size, nb_epoch, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, **kwargs)
670 class_weight=class_weight,
671 sample_weight=sample_weight,
--> 672 initial_epoch=initial_epoch)
673
674 def evaluate(self, x, y, batch_size=32, verbose=1,
/usr/local/lib/python3.5/dist-packages/keras/engine/training.py in fit(self, x, y, batch_size, nb_epoch, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch)
1114 class_weight=class_weight,
1115 check_batch_axis=False,
-> 1116 batch_size=batch_size)
1117 # prepare validation data
1118 if validation_data:
/usr/local/lib/python3.5/dist-packages/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_batch_axis, batch_size)
1031 output_shapes,
1032 check_batch_axis=False,
-> 1033 exception_prefix='model target')
1034 sample_weights = standardize_sample_weights(sample_weight,
1035 self.output_names)
/usr/local/lib/python3.5/dist-packages/keras/engine/training.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
110 ' to have ' + str(len(shapes[i])) +
111 ' dimensions, but got array with shape ' +
--> 112 str(array.shape))
113 for j, (dim, ref_dim) in enumerate(zip(array.shape, shapes[i])):
114 if not j and not check_batch_axis:
ValueError: Error when checking model target: expected activation_32 to have 2 dimensions, but got array with shape (1125, 75, 2)
我做错了什么?我关注了 this tutorial of Keras documentation.
您的问题不在于输入形状,而在于输出形状。您需要重新检查 y_train
的形状是否合适。