【问题标题】:Many to many LSTM with attention at each time step多对多 LSTM 在每个时间步都有关注
【发布时间】:2019-08-09 12:15:12
【问题描述】:

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

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

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

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

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 注意力模型,但这似乎有点牵强。

【问题讨论】:

    标签: python tensorflow deep-learning many-to-many


    【解决方案1】:

    似乎这段代码运行良好。错误在下游。如果有人使用此代码来实现多对多注意力,请注意,由于您要为每个时间步学习两个额外的权重矩阵,因此训练需要很长时间。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-08
      • 2020-04-11
      • 1970-01-01
      • 2016-08-17
      • 1970-01-01
      • 2019-06-24
      • 2019-12-22
      相关资源
      最近更新 更多