【发布时间】:2019-06-11 09:19:12
【问题描述】:
我使用 Keras 和 TensorFlow 作为后端。如果我想对 LSTM 单元进行修改,例如“移除”输出门,我该怎么做?它是一个乘法门,所以我必须以某种方式将其设置为固定值,这样无论乘以它,都没有效果。
【问题讨论】:
标签: python tensorflow keras lstm
我使用 Keras 和 TensorFlow 作为后端。如果我想对 LSTM 单元进行修改,例如“移除”输出门,我该怎么做?它是一个乘法门,所以我必须以某种方式将其设置为固定值,这样无论乘以它,都没有效果。
【问题讨论】:
标签: python tensorflow keras lstm
首先,您应该定义您的own custom layer。如果您需要一些直觉如何实现自己的单元格,请参阅 Keras 存储库中的LSTMCell。例如。您的自定义单元格将是:
class MinimalRNNCell(keras.layers.Layer):
def __init__(self, units, **kwargs):
self.units = units
self.state_size = units
super(MinimalRNNCell, self).__init__(**kwargs)
def build(self, input_shape):
self.kernel = self.add_weight(shape=(input_shape[-1], self.units),
initializer='uniform',
name='kernel')
self.recurrent_kernel = self.add_weight(
shape=(self.units, self.units),
initializer='uniform',
name='recurrent_kernel')
self.built = True
def call(self, inputs, states):
prev_output = states[0]
h = K.dot(inputs, self.kernel)
output = h + K.dot(prev_output, self.recurrent_kernel)
return output, [output]
然后,使用tf.keras.layers.RNN 使用您的手机:
cell = MinimalRNNCell(32)
x = keras.Input((None, 5))
layer = RNN(cell)
y = layer(x)
# Here's how to use the cell to build a stacked RNN:
cells = [MinimalRNNCell(32), MinimalRNNCell(64)]
x = keras.Input((None, 5))
layer = RNN(cells)
y = layer(x)
【讨论】:
training 和 mask,以及返回的内容这些方法:build() 方法没有任何内容,call() 方法的输入和状态。