我是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
=================================================================
在此模型中我可以在哪里添加关注层?我应该在第一个encode_output之后和第二个编码输入之前添加吗?
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)
它在正确的位置,我可以在此模型中添加任何其他注意机制吗?
答案 0 :(得分:0)
我想您正在做的事情是增加注意力的一种正确方法,因为注意力本身本身不过是可视化为密集层的权重。另外,我认为紧接在编码器之后才是正确的做法,因为它将使您关注任务所需的数据分发中最“信息化”的部分。