【问题标题】:Keras LSTM predict with sequenceKeras LSTM 用序列预测
【发布时间】:2021-04-21 11:22:37
【问题描述】:

我制作了一个 Keras LSTM 模型。但我的问题是,我的 input_shape [800, 200, 48] 我预测输出形状为 [800, 200, 48]。

我只需要预测没有任何序列的 800x48 标签。

enter image description here

输入: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

【问题讨论】:

    标签: python tensorflow keras lstm tensorflow2.0


    【解决方案1】:

    为此,您最后一个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
    _________________________________________________________________
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多