简体   繁体   English

关于Keras LSTM的输出

[英]About Output of the Keras LSTM

I have a built a LSTM architecture using Keras. 我已经使用Keras构建了一个LSTM体系结构。 My goal is to map length 29 time series input sequences of floats to length 29 output sequences of floats. 我的目标是将长度为29的浮点数的时间序列输入序列映射到长度为29的浮点数的输出序列。 I am trying to implement a "many-to-many" approach. 我正在尝试实施“多对多”方法。 I followed this post for implementing such a model. 我按照这篇文章来实现这样的模型。

I start by reshaping each data point into an np.array of shape `(1, 29, 1). 首先,将每个数据点重塑为形状为(( np.array ))的np.array I have multiple data points and train the model on each one separately. 我有多个数据点,并分别在每个模型上训练模型。 The following code is how I build my model: 以下代码是我建立模型的方式:

def build_model():
    # define model
    model = tf.keras.Sequential()
    model.add(tf.keras.layers.LSTM(29, return_sequences=True, input_shape=(29, 1)))
    model.add(tf.keras.layers.LeakyReLU(alpha=0.3))

    model.compile(optimizer='sgd', loss='mse', metrics = ['mae'])

    #cast data
    for point in train_dict:
        train_data = train_dict[point]

        train_dataset = tf.data.Dataset.from_tensor_slices((
            tf.cast(train_data[0], features_type),
            tf.cast(train_data[1], target_type))
        ).repeat() #cast into X, Y

        # fit model


        model.fit(train_dataset, epochs=100,steps_per_epoch = 1,verbose=0)


        print(model.summary())   
    return model 

I am confused because when I call model.predict(test_point, steps = 1, verbose = 1) the model returns 29 length 29 sequences! 我很困惑,因为当我调用model.predict(test_point, steps = 1, verbose = 1) ,模型返回29个长度为29的序列! I don't understand why this is happening, based on my understanding from the linked post. 根据我对链接帖子的了解,我不知道为什么会这样。 When I try return_state=True instead of return_sequences=True then my code raises this error: ValueError: All layers in a Sequential model should have a single output tensor. For multi-output layers, use the functional API. 当我尝试return_state=True而不是return_sequences=True我的代码将引发以下错误: ValueError: All layers in a Sequential model should have a single output tensor. For multi-output layers, use the functional API. ValueError: All layers in a Sequential model should have a single output tensor. For multi-output layers, use the functional API.

How do I solve the problem? 我该如何解决这个问题?

Your model has few flaws. 您的模型几乎没有缺陷。

  1. The last layer of your model is an LSTM. 模型的最后一层是LSTM。 Assuming you're doing either classification / regression. 假设您要进行分类/回归。 This should be followed by a Dense layer (SoftMax/sigmoid - classification, linear - regression). 这之后应该是密集层(SoftMax / Sigmoid-分类,线性-回归)。 But since this is a time-series problem, dense layer should be wrapped in a TimeDistributed wrapper. 但是,由于这是一个时序问题,因此应将密集层包装在TimeDistributed包装器中。

  2. It's odd to apply a LeakyReLU on top of the LSTM. 在LSTM之上应用LeakyReLU很奇怪。

I've fixed the code with fixes for above issues. 我已经修复了上述问题的代码。 See if that helps. 看看是否有帮助。

from tensorflow.keras.layers import Embedding, Input, Bidirectional, LSTM, Dense, Concatenate, LeakyReLU, TimeDistributed
from tensorflow.keras.initializers import Constant
from tensorflow.keras.models import Model
from tensorflow.keras.models import Sequential
def build_model():
    # define model
    model = Sequential()
    model.add(LSTM(29, return_sequences=True, input_shape=(29, 1)))
    model.add(TimeDistributed(Dense(1)))
    model.compile(optimizer='sgd', loss='mse', metrics = ['mae'])


    print(model.summary())   
    return model 

model = build_model()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM