python - Keras 模型 LSTM 预测 2 个特征
问题描述
我正在尝试预测 2 个功能。这就是我的模型的样子:
定义模型
def my_model():
input_x = Input(batch_shape=(batch_size, look_back, x_train.shape[2]), name='input')
drop = Dropout(0.5)
lstm_1 = LSTM(100, return_sequences=True, batch_input_shape=(batch_size, look_back, x_train.shape[2]), name='3dLSTM', stateful=True)(input_x)
lstm_1_drop = drop(lstm_1)
lstm_2 = LSTM(100, batch_input_shape=(batch_size, look_back, x_train.shape[2]), name='2dLSTM', stateful=True)(lstm_1_drop)
lstm_2_drop = drop(lstm_2)
y1 = Dense(1, activation='relu', name='op1')(lstm_2_drop)
y2 = Dense(1, activation='relu', name='op2')(lstm_2_drop)
model = Model(inputs=input_x, outputs=[y1,y2])
optimizer = Adam(lr=0.001, decay=0.00001)
model.compile(loss='mse', optimizer=optimizer,metrics=['mse'])
model.summary()
return model
model = my_model()
for j in range(50):
start = time.time()
history = model.fit(x_train, [y_11_train,y_22_train], epochs=1, batch_size=batch_size, verbose=0, shuffle=False)
model.reset_states()
print("Epoch",j, time.time()-start,"s")
p = model.predict(x_test, batch_size=batch_size)
我的数据集有 9 个特征:
x_train (31251, 6, 9)
y_11_train (31251,)
y_22_train (31251,)
x_test (13399, 6, 9)
y_11_test (13399,)
y_22_test (13399,)
我正在尝试预测我的数据集的第一个(y_11
)和第二个(y_22
)特征。但我得到的只是第一个特征而不是第二个特征的预测。
关于如何获得两个预测而不是一个预测的任何帮助?
解决方案
首先,您应该删除同一事物的多个输入:
(batch_size,look_back,x_train.shape[2])
另外,尝试像这样在模型中连接您的输出:
def my_model():
from keras.layers import concatenate
lstm_1 = LSTM(100, return_sequences=True, batch_input_shape=(batch_size, look_back, x_train.shape[2]), name='3dLSTM', stateful=True)
lstm_1_drop = drop(lstm_1)
lstm_2 = LSTM(100, name='2dLSTM', stateful=True)(lstm_1_drop)
lstm_2_drop = drop(lstm_2)
y1 = Dense(1, activation='linear', name='op1')(lstm_2_drop)
y2 = Dense(1, activation='linear', name='op2')(lstm_2_drop)
y= concatenate([y1,y2])
model = Model(inputs=input_x, outputs=y)
optimizer = Adam(lr=0.001, decay=0.00001)
model.compile(loss='mse', optimizer=optimizer,metrics=['mse'])
model.summary()
return model
编辑我认为你应该像这样:
y_11_train = y_11_train.reshape(y_11_train.shape[0],1)
y_22_train = y_22_train.reshape(y_22_train.shape[0],1)
model = my_model()
model.fit(x_train,np.concatenate((y_11_train,y_22_train),axis=1),...)
推荐阅读
- mysql - 错误:SET FOREIGN_KEY_CHECKS = ON 和 #2014 - 命令不同步;你现在不能运行这个命令
- java - Butterknife @BindView 中的 AnnotationTypeMismatchException,布局在另一个文件夹结构中定义
- reactjs - 使用 Webpack StaticSiteGeneratorPlugin 和样式化组件反应静态站点渲染
- email - 指定多个电子邮件收件人时,SendGrid 如何通知(传递打开单击)事件
- c - 我是编程新手,面对 vs 代码的这个问题
- algorithm - 基于 Rust 中的第一次出现将第一个数组的元素替换为第二个数组的元素
- python - 将多个 post 请求合并为一个,转换此批并返回这些 post 请求的答案
- jira - 通过 webhook 将 Jira 与现有应用程序集成 - 如果代理回复,则不会向客户发送电子邮件
- c# - 解码 WebP 显示、播放/停止
- sql - 如何在 Postgres 中使用 xpath 获取元素的名称