Keras LSTM 预测序列
Keras LSTM predict with sequence
我制作了一个 Keras LSTM 模型。但我的问题是,使用 input_shape [800, 200, 48] 我预测输出的形状为 [800, 200, 48].
我只需要预测没有任何序列的 800x48 标签。
enter image description here
输入:800 个样本,200 个 time_steps,每个 time_step
48 个特征
需要的输出是:800 个样本,每个 time_step
48 个特征
我希望有人对此有解决方案!
代码:
from keras.models import Sequential
from keras.layers import Dense, LSTM
from keras.layers import Dropout
model = Sequential()
def addInputLayer(units, shape, dropout):
model.add(LSTM(input_shape=shape, units=units, use_bias=True, unit_forget_bias=True, return_sequences=True))
model.add(Dropout(dropout))
def addHiddenLayer(anz, units, dropout):
for i in range(anz):
model.add(LSTM(units=units, use_bias=True, unit_forget_bias=True, return_sequences=True))
model.add(Dropout(dropout))
def addOutputLayer(units):
model.add(Dense(units=units))
def compLstm(optimizer, loss_function):
model.compile(optimizer=optimizer, loss=loss_function)
def konfigure(feature, label, epochs, validationFeature, validationLabel, batch_size):
history = model.fit(feature, label, epochs=epochs, validation_data=(validationFeature, validationLabel), batch_size=batch_size, verbose=2)
return history
def predict(test):
predictions = model.predict(test)
return predictions
为此,最后一个 LSTM
层的参数 return_sequences
应该是 False
。由于您使用的是循环,请尝试这样的事情。在这里,除了最后一个循环迭代之外,return_sequences
将是 True
。
import tensorflow as tf
model = tf.keras.Sequential()
anz = 8
for i in range(anz):
model.add(tf.keras.layers.LSTM(units=200, return_sequences=i != anz - 1))
model.add(tf.keras.layers.Dense(48, activation='softmax'))
model.build(input_shape=(None, 200, 48))
model.summary()
Model: "sequential_4"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
lstm_38 (LSTM) (None, 200, 200) 199200
_________________________________________________________________
lstm_39 (LSTM) (None, 200, 200) 320800
_________________________________________________________________
lstm_40 (LSTM) (None, 200, 200) 320800
_________________________________________________________________
lstm_41 (LSTM) (None, 200, 200) 320800
_________________________________________________________________
lstm_42 (LSTM) (None, 200, 200) 320800
_________________________________________________________________
lstm_43 (LSTM) (None, 200, 200) 320800
_________________________________________________________________
lstm_44 (LSTM) (None, 200, 200) 320800
_________________________________________________________________
lstm_45 (LSTM) (None, 200) 320800
_________________________________________________________________
dense_4 (Dense) (None, 48) 9648
=================================================================
Total params: 2,454,448
Trainable params: 2,454,448
Non-trainable params: 0
_________________________________________________________________
我制作了一个 Keras LSTM 模型。但我的问题是,使用 input_shape [800, 200, 48] 我预测输出的形状为 [800, 200, 48].
我只需要预测没有任何序列的 800x48 标签。
enter image description here
输入:800 个样本,200 个 time_steps,每个 time_step
48 个特征需要的输出是:800 个样本,每个 time_step
48 个特征我希望有人对此有解决方案!
代码:
from keras.models import Sequential
from keras.layers import Dense, LSTM
from keras.layers import Dropout
model = Sequential()
def addInputLayer(units, shape, dropout):
model.add(LSTM(input_shape=shape, units=units, use_bias=True, unit_forget_bias=True, return_sequences=True))
model.add(Dropout(dropout))
def addHiddenLayer(anz, units, dropout):
for i in range(anz):
model.add(LSTM(units=units, use_bias=True, unit_forget_bias=True, return_sequences=True))
model.add(Dropout(dropout))
def addOutputLayer(units):
model.add(Dense(units=units))
def compLstm(optimizer, loss_function):
model.compile(optimizer=optimizer, loss=loss_function)
def konfigure(feature, label, epochs, validationFeature, validationLabel, batch_size):
history = model.fit(feature, label, epochs=epochs, validation_data=(validationFeature, validationLabel), batch_size=batch_size, verbose=2)
return history
def predict(test):
predictions = model.predict(test)
return predictions
为此,最后一个 LSTM
层的参数 return_sequences
应该是 False
。由于您使用的是循环,请尝试这样的事情。在这里,除了最后一个循环迭代之外,return_sequences
将是 True
。
import tensorflow as tf
model = tf.keras.Sequential()
anz = 8
for i in range(anz):
model.add(tf.keras.layers.LSTM(units=200, return_sequences=i != anz - 1))
model.add(tf.keras.layers.Dense(48, activation='softmax'))
model.build(input_shape=(None, 200, 48))
model.summary()
Model: "sequential_4"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
lstm_38 (LSTM) (None, 200, 200) 199200
_________________________________________________________________
lstm_39 (LSTM) (None, 200, 200) 320800
_________________________________________________________________
lstm_40 (LSTM) (None, 200, 200) 320800
_________________________________________________________________
lstm_41 (LSTM) (None, 200, 200) 320800
_________________________________________________________________
lstm_42 (LSTM) (None, 200, 200) 320800
_________________________________________________________________
lstm_43 (LSTM) (None, 200, 200) 320800
_________________________________________________________________
lstm_44 (LSTM) (None, 200, 200) 320800
_________________________________________________________________
lstm_45 (LSTM) (None, 200) 320800
_________________________________________________________________
dense_4 (Dense) (None, 48) 9648
=================================================================
Total params: 2,454,448
Trainable params: 2,454,448
Non-trainable params: 0
_________________________________________________________________