【问题标题】:Skip connection in a neural network for one feature为一个特征跳过神经网络中的连接
【发布时间】:2020-11-17 14:35:21
【问题描述】:

我有 1000 个对象,每个对象有 100 个时间戳和 5 个特征,但是一个非常重要,所以我不想通过 LSTM,而是立即将其传递到最后一层,我该怎么做?神经网络中需要大量输入层?

【问题讨论】:

    标签: tensorflow keras neural-network lstm recurrent-neural-network


    【解决方案1】:

    我认为其中任何一个都应该为你做:

    import tensorflow as tf
    
    # dummy data
    inp1 = tf.random.uniform(shape=(1000, 100, 5))
    
    # ALTERNATIVE 1 (use lambda layer to split input)
    inputs = tf.keras.layers.Input((100, 5), name='inputs')
    
    # assuming that the important feature is at index -1
    input_lstm = tf.keras.layers.Lambda(lambda x: x[:, :, :4])(inputs)
    input_dense = tf.keras.layers.Lambda(lambda x: x[:, :, -1])(inputs)
    x = tf.keras.layers.LSTM(
        units=64, 
        recurrent_initializer='ones', 
        kernel_initializer='ones')(input_lstm)
    x = tf.keras.layers.Concatenate()([x, input_dense])
    out = tf.keras.layers.Dense(units=1, kernel_initializer='ones')(x)
    
    model = tf.keras.Model(inputs=inputs, outputs=out)
    
    # print(model.summary())
    out = model(inp1)
    print(out[:5])
    
    # ALTERNATIVE 2 (split data before neural net)
    # assuming that the important feature is at index -1
    inp2 = inp1[:, :, -1]
    inp1 = inp1[:, :, :4]
    
    input_lstm = tf.keras.layers.Input((100, 4), name='lstm_input')
    input_dense = tf.keras.layers.Input((100,), name='dense_input')
    x = tf.keras.layers.LSTM(
        units=64, 
        recurrent_initializer='ones', 
        kernel_initializer='ones')(input_lstm)
    x = tf.keras.layers.Concatenate()([x, input_dense])
    out = tf.keras.layers.Dense(units=1, kernel_initializer='ones')(x)
    
    model = tf.keras.Model(inputs=[input_lstm, input_dense], outputs=out)
    
    # print(model.summary())
    out = model([inp1, inp2])
    print(out[:5])
    
    # output:
    # tf.Tensor(
    # [[118.021736]
    #  [117.11683 ]
    #  [115.341644]
    #  [120.00911 ]
    #  [114.4716  ]], shape=(5, 1), dtype=float32)
    # tf.Tensor(
    # [[118.021736]
    #  [117.11683 ]
    #  [115.341644]
    #  [120.00911 ]
    #  [114.4716  ]], shape=(5, 1), dtype=float32)
    

    层的权重被初始化为 ones 只是为了说明它们给出相同的输出。

    【讨论】:

      猜你喜欢
      • 2017-10-24
      • 2018-01-22
      • 2011-07-30
      • 2012-07-10
      • 2015-12-28
      • 2018-10-22
      • 2016-03-10
      • 2018-08-16
      • 1970-01-01
      相关资源
      最近更新 更多