【问题标题】:How to prepare data for a many to one binary classification LSTM?如何为多对一二元分类 LSTM 准备数据?
【发布时间】:2019-07-07 06:40:27
【问题描述】:

我有一个包含 38,000 名不同患者的时间序列数据集,其中包括他们 48 小时的生理数据和 30 个特征,因此每个患者都有 48 行(每小时)和 48 日结束时的二进制结果(0/1)仅一小时,总训练集为 (38,000*48 = 1,824,000) rows 。

据我了解,这是一个Many-to-one LSTM binary classification,所以我的输入形状是否应该是(38,000,48,30) (sample_size, time_steps, features),并且是否应该将return_sequence 设置为False 以仅返回最后一个隐藏神经元的输出?

有人可以回顾一下我对此的理解吗?

谢谢。

【问题讨论】:

    标签: keras time-series classification lstm many-to-one


    【解决方案1】:

    是的,大多数情况下你都在正确的轨道上。请参阅下面的代码以更好地理解这一点。

    from keras.models import Sequential
    from keras.layers import Dense
    from keras.layers import LSTM
    from keras.layers import Bidirectional
    from keras.metrics import binary_crossentropy
    
    # vocab size
    total_features = 30
    no_of_pateints = 38,000
    time_steps = 48
    
    
    model = Sequential()
    
    # you can also use Bidirectional layer to speed up the learning and reduce 
    # training time and here you can keep return_sequence as true
    # model.add(
        Bidirectional(LSTM(
            units=100, 
            input_shape=(no_of_patients, time_steps, total_features), 
            return_sequences=True
        )))
    # return_sequence should be False if there is only one LSTM layer. Otherwise in case of multiple layers, 
    the last layers should have return_sequence as False
    model.add(LSTM(
        units=100, 
        input_shape=(no_of_patients, time_steps, total_features), 
        return_sequences=False 
        ))
    model.add(Dense(2, activation='softmax'))
    model.compile(
        loss=binary_crossentropy,
        optimizer='rmsprop',
        metrics=['accuracy']
    )
    

    如果您对上述代码有任何混淆或需要更多解释,请告诉我

    【讨论】:

    • 嘿,非常感谢!将应用这个。
    • @AnushaPrakash,如果它对你有用,请接受它作为答案。所以它也可以帮助其他人。
    • @Abinav Anand 是的,当然。此外,由于我想要二进制类的概率,我会将输出神经元的编号从 2 更改为 1,并将激活更改为 sigmoid。听起来合理吗?
    • @AnushaPrakash 抱歉,回复晚了,但您需要将输出神经元保持为 2,因为您希望看到两个输出类的概率而不仅仅是一个。在分类问题的情况下,我个人的偏好是保持 softmax 作为激活,但这完全取决于你。
    • 是的,我可以这样做并检查哪种方法最适合我的问题。谢谢。
    【解决方案2】:

    是的,你基本上是对的:

    • 输入形状 = (patients, 48, 30)
    • 目标形状 = (patients, 1)

    你应该在你的last LSTM 层中使用return_sequences=False。 (如果在最后一个 LSTM 之前有更多循环层,请在其中保留 return_sequences=True

    【讨论】:

    • 是的,我计划使用多个循环层。谢谢! :)
    猜你喜欢
    • 2021-12-20
    • 1970-01-01
    • 2018-01-26
    • 2019-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-05
    • 2019-05-24
    相关资源
    最近更新 更多