keras。初始化双向LSTM。传递单词嵌入

问题描述 投票:0回答:1

在我正在使用的实现中,lstm按以下方式初始化:

l_lstm = Bidirectional(LSTM(64, return_sequences=True))(embedded_sequences)

我真的不明白,可能是因为缺乏Python的经验:符号l_lstm= Bidirectional(LSTM(...))(embedded_sequences)。我不知道我通过embedded_sequences到什么?因为它不是LSTM()的参数,但似乎也不是Bidirectional()的论据,因为它是分开的。

这是Bidirectional的文档:

  def __init__(self, layer, merge_mode='concat', weights=None, **kwargs):
        if merge_mode not in ['sum', 'mul', 'ave', 'concat', None]:
            raise ValueError('Invalid merge mode. '
                             'Merge mode should be one of '
                             '{"sum", "mul", "ave", "concat", None}')
        self.forward_layer = copy.copy(layer)
        config = layer.get_config()
        config['go_backwards'] = not config['go_backwards']
        self.backward_layer = layer.__class__.from_config(config)
        self.forward_layer.name = 'forward_' + self.forward_layer.name
        self.backward_layer.name = 'backward_' + self.backward_layer.name
        self.merge_mode = merge_mode
        if weights:
            nw = len(weights)
            self.forward_layer.initial_weights = weights[:nw // 2]
            self.backward_layer.initial_weights = weights[nw // 2:]
        self.stateful = layer.stateful
        self.return_sequences = layer.return_sequences
        self.return_state = layer.return_state
        self.supports_masking = True
        self._trainable = True
        super(Bidirectional, self).__init__(layer, **kwargs)
        self.input_spec = layer.input_spec
        self._num_constants = None
python keras lstm embedding
1个回答
2
投票

让我们试着打破正在发生的事情:

  1. 你从LSTM(...)开始,它创建了一个LSTM层。现在layers in Keras are callable意味着你可以像功能一样使用它们。例如,lstm = LSTM(...)然后lstm(some_input)将在给定的输入张量上调用LSTM。
  2. Bidirectional(...)包裹任何RNN图层并返回另一个图层,当被调用时,在两个方向上应用包裹图层。所以l_lstm = Bidirectional(LSTM(...))是一个层,当被调用时,一些输入将在两个方向上应用LSTM。注意:双向创建传递的LSTM图层的副本,因此向后和向前是不同的LSTM。
  3. 最后,当你调用Bidirectional(LSTM(...))(embedded_seqences)时,双向层获取输入序列,将它传递给两个方向的包装LSTM,收集它们的输出并连接它。

要了解有关图层及其可调用性质的更多信息,您可以查看文档的functional API guide

© www.soinside.com 2019 - 2024. All rights reserved.