首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >多对多LSTM,每个时间步都要注意

多对多LSTM,每个时间步都要注意
EN

Stack Overflow用户
提问于 2019-03-19 11:06:03
回答 1查看 438关注 0票数 1

我正在做时间序列图像分类,我需要在每个时间步(多对多)输出一个分类。

我的Tensorflow图采用了Batch size,Time step,Image,并实现了一个深度CNN-LSTM,目前它在分类之前是一个时间分布的密集层。

在我之前的工作中,我通过加权时间步长的隐藏状态,成功地将注意力添加到更好的模型时间依赖性上。但是,我找不到任何尝试在多对多RNN中使用注意力的实现。

我尝试了下面的代码,它可以编译和运行,但比没有运行的模型性能更差。这里的想法是学习每个步骤的注意力权重,以基于当前时间步长对每隔一个时间步长进行加权。我有780万个训练样本,所以我不担心这是过度拟合-事实上,它增加了训练误差超过模型没有!

代码语言:javascript
复制
def create_multi_attention(inputs, attention_size, time_major=False):
hidden_size = inputs.shape[2].value
print("Attention In: {}".format(inputs.shape))

w_omegas, b_omegas, u_omegas = [], [], []
for i in range(0, MAX_TIME_STEPS):
    w_omegas.append(create_weights([hidden_size, attention_size]))
    b_omegas.append(tf.Variable(tf.constant(0.05, shape = [attention_size])))
    u_omegas.append(create_weights([attention_size]))

# Trainable parameters
layers_all = []
for i in range(0, MAX_TIME_STEPS):  
    v = tf.tanh(tf.tensordot(inputs, w_omegas[i], axes=1) + b_omegas[i])       
    vu = tf.tensordot(v, u_omegas[i], axes=1, name='vu')
    alphas = tf.nn.softmax(vu, name='alphas')  

    output = tf.reshape(tf.reduce_sum(inputs * tf.expand_dims(alphas, -1), 1), (-1, 1, hidden_size))        
    layers_all.append(output)

output = tf.concat(layers_all, axis = 1) #[Batch, Time steps, LSTM output size]
print("Attention Out: {}".format(output.shape))
return output

我很乐意为论文提供任何意见、想法或观点!我曾考虑过尝试seq2seq注意力模型,但这似乎有点牵强。

EN

回答 1

Stack Overflow用户

发布于 2019-03-27 04:00:42

看起来这段代码运行得很好。错误发生在下游。如果有人使用此代码来实现多对多注意,请注意,它将需要非常长的时间进行训练,因为您将为每个时间步学习两个额外的权重矩阵。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55233040

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档