我正在尝试用Keras编写我的第一个LSTM,但我遇到了麻烦。那就是我的训练数据结构:x_data = [1265,12] y_data = [1265,3]
x_data示例:[102.7, 100.69, 103.39, 99.6, 319037.0, 365230.0, 1767412, 102.86, 13.98]
y_data示例:[0, 0, 1]
我的模型如下所示:
self._opt_cells = 12
self.model = Sequential()
self.model.add(LSTM(units = self._opt_cells, return_sequences = True, input_shape = (12, 1)))
self.model.add(Dropout(0.2))
self.model.add(LSTM(units = self._opt_cells, return_sequences = True))
self.model.add(Dropout(0.2))
self.model.add(LSTM(units = self._opt_cells, return_sequences = True))
self.model.add(Dropout(0.2))
self.model.add(LSTM(units = self._opt_cells))
self.model.add(Dropout(0.2))
self.model.add(Dense(3, activation = 'softmax'))
self.model.compile(loss='categorical_crossentropy', optimizer='Adam', metrics=['accuracy'])
使用此代码,我训练模型:
x_data = np.reshape(x_data, (x_data.shape[0], 12, 1))
y_data = np.reshape(y_data, (y_data.shape[0], 3))
for e in range(100):
cost = self.model.train_on_batch(x_data, y_data)
prediction = self.model.predict(x_data)
但是每个预测都是空的。请帮我!
修改
我已将培训代码更改为:
x_data = np.reshape(x_data, (x_data.shape[0], 1))
y_data = np.reshape(y_data, (y_data.shape[0]))
self.model.fit(x_data, y_data, epochs = 50, batch_size = 8)
但是那不起作用