我在Keras中有一个简单的LSTM网络:
def create_model(x):
lstm_model = Sequential()
lstm_model.add(LSTM(100, input_dim=x.shape[2], input_length=x.shape[1]))
lstm_model.add(Dense(1,activation='sigmoid'))
lstm_model.compile(loss='mean_squared_error', optimizer='adam')
return lstm_model
我正在尝试对具有以下形状的数据进行训练:
训练数据输入:(100,2784,6),训练数据输出:(100,2784,1)
验证数据输入:(50,27,6),验证数据输出:(50,27,1)
测试数据输入:(50,27,6),测试数据输出:(50,27,1)
model.fit(train_x, train_y, validation_data=(validation_x, validation_y), epochs=EPOCHS, batch_size=BATCH_SIZE, shuffle=False, callbacks=[...])
我继续在尺寸上失败,或者是因为a)验证数据与训练数据的形状不同,或者b)因为y形状不正确
我在做什么错了?
P.S。方便的独立代码
import numpy as np
import keras
from keras.models import Sequential
from keras.layers import LSTM, Dense
x_train=np.arange(1670400)
x_train=x_train.reshape((100, 2784, 6))
y_train=np.arange(278400)
y_train=y_train.reshape((100, 2784, 1))
x_val=np.arange(8100)
x_val=x_val.reshape((50, 27, 6))
y_val=np.arange(1350)
y_val=y_val.reshape((50, 27, 1))
x_test=np.arange(8100)
x_test=x_test.reshape((50, 27, 6))
y_test=np.arange(1350)
y_test=y_test.reshape((50, 27, 1))
def create_model(x):
lstm_model = Sequential()
lstm_model.add(LSTM(100, input_dim=x.shape[2], input_length=x.shape[1]))
lstm_model.add(Dense(1,activation='sigmoid'))
lstm_model.compile(loss='mean_squared_error', optimizer='adam')
return lstm_model
model=create_model(x_train)
model.fit(x_train, y_train, validation_data=(x_val, y_val), epochs=10, batch_size=32, shuffle=False)
答案 0 :(得分:0)
此修改有效:
更改为 batch_input_shape ,并在LSTM层中添加 return_sequences = True
import numpy as np
import keras
from keras.models import Sequential
from keras.layers import LSTM, Dense
x_train=np.arange(1670400)
x_train=x_train.reshape((100, 2784, 6))
y_train=np.arange(278400)
y_train=y_train.reshape((100, 2784, 1))
x_val=np.arange(8100)
x_val=x_val.reshape((50, 27, 6))
y_val=np.arange(1350)
y_val=y_val.reshape((50, 27, 1))
x_test=np.arange(8100)
x_test=x_test.reshape((50, 27, 6))
y_test=np.arange(1350)
y_test=y_test.reshape((50, 27, 1))
def create_model():
lstm_model = Sequential()
lstm_model.add(LSTM(100, batch_input_shape=(None,None,6), return_sequences=True))
lstm_model.add(Dense(1, activation='sigmoid'))
lstm_model.compile(loss='mean_squared_error', optimizer='adam')
return lstm_model
model=create_model()
model.fit(x_train, y_train, validation_data=(x_val, y_val), epochs=10, batch_size=32, shuffle=False)