我是 Keras 的新手,我正在尝试在带有注意力层的 keras 中构建一个简单的自动编码器:
这是我尝试过的:
data = Input(shape=(w,), dtype=np.float32, name='input_da')
noisy_data = Dropout(rate=0.2, name='drop1')(data)
encoded = Dense(256, activation='relu',
name='encoded1', **kwargs)(noisy_data)
encoded = Lambda(mvn, name='mvn1')(encoded)
encoded = Dense(128, activation='relu',
name='encoded2', **kwargs)(encoded)
encoded = Lambda(mvn, name='mvn2')(encoded)
encoded = Dropout(rate=0.5, name='drop2')(encoded)
encoder = Model([data], encoded)
encoded1 = encoder.get_layer('encoded1')
encoded2 = encoder.get_layer('encoded2')
decoded = DenseTied(256, tie_to=encoded2, transpose=True,
activation='relu', name='decoded2')(encoded)
decoded = Lambda(mvn, name='new_mv')(decoded)
decoded = DenseTied(w, tie_to=encoded1, transpose=True,
activation='linear', name='decoded1')(decoded)
它看起来像这样:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
data (InputLayer) (None, 2693) 0
_________________________________________________________________
drop1 (Dropout) (None, 2693) 0
_________________________________________________________________
encoded1 (Dense) (None, 256) 689664
_________________________________________________________________
mvn1 (Lambda) (None, 256) 0
_________________________________________________________________
encoded2 (Dense) (None, 128) 32896
_________________________________________________________________
mvn2 (Lambda) (None, 128) 0
_________________________________________________________________
drop2 (Dropout) (None, 128) 0
_________________________________________________________________
decoded2 (DenseTied) (None, 256) 256
_________________________________________________________________
mvn3 (Lambda) (None, 256) 0
_________________________________________________________________
decoded1 (DenseTied) (None, 2693) 2693
=================================================================
在这个模型中我可以在哪里添加注意力层?我应该在第一个编码输出之后和第二个编码输入之前添加吗?
encoded = Lambda(mvn, name='mvn1')(encoded)
Here?
encoded = Dense(128, activation='relu',
name='encoded2', **kwargs)(encoded)
我也在通过这个美丽的库:
https://github.com/CyberZHG/keras-self-attention
他们已经实现了各种类型的注意力机制,但它是针对顺序模型的。我如何在我的模型中添加这些注意力?
我尝试了非常简单的注意:
encoded = Dense(256, activation='relu',
name='encoded1', **kwargs)(noisy_data)
encoded = Lambda(mvn, name='mvn1')(encoded)
attention_probs = Dense(256, activation='softmax', name='attention_vec')(encoded)
attention_mul = multiply([encoded, attention_probs], name='attention_mul')
attention_mul = Dense(256)(attention_mul)
print(attention_mul.shape)
encoded = Dense(128, activation='relu',
name='encoded2', **kwargs)(attention_mul)
它在正确的位置吗?我可以在这个模型中添加任何其他注意机制吗?