我制作了Keras LSTM模型。但是我的问题是,使用我的input_shape [800,200,48]可以预测形状为[800,200,48]的输出。
我只需要预测800x48标签即可,无需任何序列。
输入:800个样本,200个时间步长,每个时间步长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
为此,return_sequences
最后LSTM
一层的参数应为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
_________________________________________________________________
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句