2017-02-28 7 views
1

TensorFlowで私の注意の実装が正しいかどうかチェックしたいと思います。Tensorflowでの注意の実装

基本的には、https://arxiv.org/pdf/1509.06664v1.pdfに記載されている注意を使用しています。 (ちょうどベースラインの注意ではなく、言葉による注意)。これまでのところ、最後の隠れ状態h_Nを使わずに実装しました。

def attention(hidden_states): 
    ''' 
    hidden states (inputs) are seq_len x batch_size x dim 
    returns r, the weighted representation of the hidden states by attention vector a 

    Note:I do not use the h_N vector and also I skip the last projection layer. 
    ''' 
    shape = hidden_states.get_shape().as_list() 

    with tf.variable_scope('attention_{}'.format(name), reuse=reuse) as f: 
     initializer = tf.random_uniform_initializer() 

     # Initialize Parameters 
     weights_Y = tf.get_variable("weights_Y", [self.in_size, self.in_size], initializer=initializer) 
     weights_w = tf.get_variable("weights_w", [self.in_size, 1], initializer=initializer) 

     # Equation => M = tanh(W^{Y}Y) 
     tmp = tf.reshape(hidden_states, [-1, shape[2]]) 
     Y = tf.matmul(tmp, weights_Y) 
     Y = tf.reshape(Y, [shape[0], -1, shape[2]]) 
     Y = tf.tanh(Y, name='M_matrix') 

     # Equation => a = softmax(Y w^T) 
     Y = tf.reshape(Y, [-1, shape[2]]) 
     a = tf.matmul(Y, weights_w) 
     a = tf.reshape(a, [-1, shape[0]]) 
     a = tf.nn.softmax(a, name='attention_vector') 

     # Equation => r = Ya^T 
     # This is the part I weight all hidden states by the attention vector 
     a = tf.expand_dims(a, 2) 
     H = tf.transpose(hidden_states, [1,2,0]) 
     r = tf.matmul(H, a, name='R_vector') 
     r = tf.reshape(r, [-1, shape[2]]) 

     # I skip the last projection layer since I do not use h_N 
     return r 

このグラフは、適切にコンパイル、実行、および訓練します。 (損失は減少しているなど)、パフォーマンスは私の予想以上に低いです。もし私が正しいことをしているなら、私が小切手を手に入れることができれば嬉しいです。一般

、ある乗算について

1)[?、seq_len、暗く] [DIM、DIM]を乗じた行列。 [?、seq_len、dim]から[-1、dim]までのtf.reshapeを使用し、[dim、dim]で形状[-1、dim]のmatmulを適用し、[?、seq_len、マムムールの後で曇っている?

2)私は注意ベクトルのa(?、seq_len)を取得しました。だから私は(?、seq_len)x(?、dim、seq_len)をする必要があります。

(?、seq_len)から(?、seq_len、1)にキャストしてexpand_dimsしてからmatmulを実行するのは正しいですか?(これは以前のバージョンではbatch_matmulがしたと思います)

ありがとうございます!

答えて

0

TF1.0のtf.einsumは効率的に実装されていますが、計算が非常にエレガントになります。

import tensorflow as tf 
import numpy as np 

batch_size = 3 
seq_len = 5 
dim = 2 
# [batch_size x seq_len x dim] -- hidden states 
Y = tf.constant(np.random.randn(batch_size, seq_len, dim), tf.float32) 
# [batch_size x dim]   -- h_N 
h = tf.constant(np.random.randn(batch_size, dim), tf.float32) 

initializer = tf.random_uniform_initializer() 
W = tf.get_variable("weights_Y", [dim, dim], initializer=initializer) 
w = tf.get_variable("weights_w", [dim], initializer=initializer) 

# [batch_size x seq_len x dim] -- tanh(W^{Y}Y) 
M = tf.tanh(tf.einsum("aij,jk->aik", Y, W)) 
# [batch_size x seq_len]  -- softmax(Y w^T) 
a = tf.nn.softmax(tf.einsum("aij,j->ai", M, w)) 
# [batch_size x dim]   -- Ya^T 
r = tf.einsum("aij,ai->aj", Y, a) 

with tf.Session() as sess: 
    sess.run(tf.global_variables_initializer()) 
    a_val, r_val = sess.run([a, r]) 
    print("a:", a_val, "\nr:", r_val) 
+0

私が間違っていれば正解ですが、注意の仕組みがエンコーダとデコーダの出力を必要とすると思います。エンコーダネットワークの出力にsoftmaxを適用するのと同じくらい簡単だとは思いません。 – YellowPillow

関連する問題