【问题标题】:How to send multiple vectors to a SimpleRNN?如何将多个向量发送到 SimpleRNN?
【发布时间】:2019-05-23 13:41:32
【问题描述】:

我没有。每个“m”大小的向量。我需要将它们发送到 keras 的 SimpleRNN。应该发送向量,使得 RNN 的每个神经元都采用一个向量(例如:向量 1 到神经元 1,向量 2 t 神经元 2 等)以及先前输入向量的隐藏状态。

我尝试将它们连接起来,但这会扭曲输入的性质。

input1 = Dense(20, activation = "relu")(input1)
input2 = Dense(20, activation = "relu")(input2)

我需要将这些向量(输入 1 和输入 2)发送到 RNN。

【问题讨论】:

    标签: keras recurrent-neural-network rnn keras-layer


    【解决方案1】:

    您可以在 Tensorflow 中使用 tf.stack 或在 Keras 中使用 keras.backend.stack。该运算符:

    将 rank-R 张量列表堆叠成一个 rank-(R+1) 张量

    根据您的代码,Dense layers 可以按以下方式堆叠:

    import tensorflow as tf
    
    inps1 = tf.keras.layers.Input(shape=(30,))
    inps2 = tf.keras.layers.Input(shape=(30,))
    dense1 = tf.keras.layers.Dense(20, activation='relu')(inps1)
    dense2 = tf.keras.layers.Dense(20, activation='relu')(inps2)
    
    dense = tf.keras.layers.Lambda(lambda x: tf.stack([x[0], x[1]], axis=1), output_shape=(None, 2, 20))([dense1, dense2])
    
    rnn = tf.keras.layers.SimpleRNN(100)(dense)
    

    【讨论】:

    • 将向量堆叠在一起以确保第一个向量到达第一个神经元,第二个到第二个神经元,依此类推直到 n?
    • 当我尝试 out = stack([input1, input2], axis=1) out = SimpleRNN(50, activation="relu")(out) 它给了我错误 TypeError: 'NoneType' object is not subscriptable
    • Dense1 在 t=0 时馈入网络,dense2 在时间=1 时馈入网络,依此类推。
    • @yaminigoel 需要 Lambda 层。
    • 我收到ValueError: Input 0 is incompatible with layer simple_rnn_1: expected shape=(None, None, 20), found shape=(None, None, 2, 20) ,当我将输出形状更改为output_shape=(None, None, 20) 时,我收到ValueError: Error when checking target: expected simple_rnn_1 to have shape (50,) but got array with shape (1,)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-11
    • 2017-04-26
    • 2021-11-24
    • 2011-06-15
    • 1970-01-01
    • 2013-07-10
    • 1970-01-01
    相关资源
    最近更新 更多