首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >修改残差LSTM

修改残差LSTM
EN

Stack Overflow用户
提问于 2020-07-15 23:29:06
回答 1查看 233关注 0票数 1

我在这里找到了剩余LSTM的一些代码:https://gist.github.com/bzamecnik/8ed16e361a0a6e80e2a4a259222f101e

我一直在使用LSTM对具有3d输入(样本、时间步长、特征)和单一输出的时间序列进行分类。我有兴趣在我的数据上尝试残差模型,但我需要的是具有sigmoid激活的单个输出。有人知道怎么做吗?当前模型似乎返回10个输出(输入数据中的特征数量)。

代码语言:javascript
复制
def make_residual_lstm_layers(input, rnn_width, rnn_depth, rnn_dropout):
    """
    The intermediate LSTM layers return sequences, while the last returns a single element.
    The input is also a sequence. In order to match the shape of input and output of the LSTM
    to sum them we can do it only for all layers but the last.
    """
    x = input
    for i in range(rnn_depth):
        return_sequences = i < rnn_depth - 1
        x_rnn = LSTM(rnn_width, recurrent_dropout=rnn_dropout, dropout=rnn_dropout, return_sequences=return_sequences)(x)
        if return_sequences:
            # Intermediate layers return sequences, input is also a sequence.
            if i > 0 or input.shape[-1] == rnn_width:
                x = add([x, x_rnn])
            else:
                # Note that the input size and RNN output has to match, due to the sum operation.
                # If we want different rnn_width, we'd have to perform the sum from layer 2 on.
                x = x_rnn
        else:
            # Last layer does not return sequences, just the last element
            # so we select only the last element of the previous output.
            def slice_last(x):
                return x[..., -1, :]

            x = add([Lambda(slice_last)(x), x_rnn])
    return x

input = Input(shape=(32, 10))
output = make_residual_lstm_layers(input, rnn_width=10, rnn_depth=8, rnn_dropout=0.2)
model = Model(inputs=input, outputs=output)
model.summary()

这部分: model.compile(loss='binary_crossentropy',optimizer='adam')我可以这样添加:

代码语言:javascript
复制
model = Model(inputs=input, outputs=output)
model.compile(loss='binary_crossentropy', optimizer='adam')
model.summary()

但我需要的是这样的:

代码语言:javascript
复制
input = Input(shape=(32, 10))
output = make_residual_lstm_layers(input, rnn_width=10, rnn_depth=8, rnn_dropout=0.2)
newoutput = Dense(1, activation='sigmoid')(output)
model = Model(inputs=input, outputs=newoutput)
model.compile(loss='binary_crossentropy', optimizer='adam')
model.summary()

有谁知道如何修改模型来实现这一点吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-16 05:07:00

主要问题是特征尺寸不匹配(10 != 1),因此不可能在最后一部分中应用跳过连接。这是我的建议,我用一个简单的LSTM层替换最后一个块,该层有1个输出和一个sigmoid激活

代码语言:javascript
复制
def make_residual_lstm_layers(input, rnn_width, rnn_depth, rnn_dropout):

    x = input
    for i in range(rnn_depth):
        
        return_sequences = i < rnn_depth - 1
        x_rnn = LSTM(rnn_width, recurrent_dropout=rnn_dropout, dropout=rnn_dropout, 
                     return_sequences=return_sequences)(x)
        
        if return_sequences:
            
            if i > 0 or input.shape[-1] == rnn_width:
                x = add([x, x_rnn])
            else:
                x = x_rnn
        else:
            
            x = LSTM(1, activation='sigmoid', 
                     recurrent_dropout=rnn_dropout, dropout=rnn_dropout, 
                     return_sequences=return_sequences)(x)            
    return x

input = Input(shape=(32, 10))
output = make_residual_lstm_layers(input, rnn_width=10, rnn_depth=8, rnn_dropout=0.2)
model = Model(inputs=input, outputs=output)
model.summary()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62918396

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档