【问题标题】:how to parallelize custom lstm (4d input)如何并行化自定义 lstm(4d 输入)
【发布时间】:2019-09-09 13:27:50
【问题描述】:

置换层后,维度变为 (None, None, 12, 16) 我想用带有 input_shape(12, 16) 的 LSTM(48 个单位) 总结最后两个维度 使整体维度变为 (None, None, 48)

目前我有一个使用自定义 lstm&lstmcell 的解决方法,但是它非常慢,因为我使用了另一个 LSTM within cell 等。

我想要的是这样的:

(None, None, 12, 16)
(None, None, 48)
(None, None, 60)

最后两个是在自定义 lstm 中完成的(目前),有没有办法将它们分开?

这样做的正确方法是什么? 我们可以为具有相同权重但不同细胞状态的细胞创建不同的(或多个)lstm 吗? 你能给我一些方向吗?

输入(InputLayer)(无,36,无,1)0


convlayer (Conv2D) (None, 36, None, 16) 160 个输入[0][0]


mp (MaxPooling2D) (None, 12, None, 16) 0 convlayer[0][0]


permute_1 (置换) (None, None, 12, 16) 0 mp[0][0]


reshape_1(重塑)(无,无,192)0 permute_1[0][0]


custom_lstm_extended_1 (CustomL (None, None, 60) 26160 reshape_1[0][0]

自定义 LSTM 的调用方式如下: CustomLSTMExtended(units=60, summarizeUnits=48, return_sequences=True, return_state=False, input_shape=(None, 192))(inner)

LSTM class:
self.summarizeUnits = summarizeUnits
self.summarizeLSTM = CuDNNLSTM(summarizeUnits, input_shape=(None, 16), return_sequences=False, return_state=True)

        cell = SummarizeLSTMCellExtended(self.summarizeLSTM, units,
                activation=activation,
                recurrent_activation=recurrent_activation,
                use_bias=use_bias,
                kernel_initializer=kernel_initializer,
                recurrent_initializer=recurrent_initializer,
                unit_forget_bias=unit_forget_bias,
                bias_initializer=bias_initializer,
                kernel_regularizer=kernel_regularizer,
                recurrent_regularizer=recurrent_regularizer,
                bias_regularizer=bias_regularizer,
                kernel_constraint=kernel_constraint,
                recurrent_constraint=recurrent_constraint,
                bias_constraint=bias_constraint,
                dropout=dropout,
                recurrent_dropout=recurrent_dropout,
                implementation=implementation)


        RNN.__init__(self, cell,
                                   return_sequences=return_sequences,
                                   return_state=return_state,
                                   go_backwards=go_backwards,
                                   stateful=stateful,
                                   unroll=unroll,
                                   **kwargs)
Cell class:
def call(self, inputs, states, training=None):
        #cell
        reshaped = Reshape([12, 16])(inputs)
        state_h = self.summarizeLayer(reshaped)
        inputsx = state_h[0]
        return super(SummarizeLSTMCellExtended, self).call(inputsx, states, training)

【问题讨论】:

    标签: tensorflow keras lstm summarize


    【解决方案1】:

    我使用 tf.reshape 而不是 keras Reshape 图层完成了这项工作。 Keras reshape layer 不希望你干扰“batch_size”维度

    shape = Lambda(lambda x: tf.shape(x), output_shape=(4,))(inner)
    ..
    ..
    inner = Lambda(lambda x : customreshape(x), output_shape=(None, 48))([inner, shape])
    ..
    def customreshape(inputs):
        inner = inputs[0]
        shape = inputs[1]
        import tensorflow as tf2 
        reshaped = tf2.reshape(inner, [shape[0], shape[1], 48] )
        return reshaped
    

    【讨论】:

      猜你喜欢
      • 2022-01-14
      • 2019-03-26
      • 1970-01-01
      • 2020-05-14
      • 1970-01-01
      • 2023-01-25
      • 2017-03-19
      • 2020-02-10
      • 1970-01-01
      相关资源
      最近更新 更多