def attend(self, input_mat, state, w1dt, input_len, batch_size):
        global attention_w2
        global attention_v
        w2 = dy.parameter(attention_w2)
        v = dy.parameter(attention_v)
        w2dt = w2 * dy.concatenate(list(state.s()))
        unnormalized = dy.transpose(v * dy.tanh(dy.colwise_add(w1dt, w2dt)))
        unnormalized = dy.reshape(unnormalized, (input_len, ), batch_size)
        att_weights = dy.softmax(unnormalized)

        context = input_mat * att_weights
        return context, att_weights
Ejemplo n.º 2
0
def attend_batch(input_mat, state, w1dt, batch_size, input_length):
	#print "in attend batch"
	global attention_w2
	global attention_v
	w2 = dy.parameter(attention_w2)
	v = dy.parameter(attention_v)
        #print "Calculating w2dt"
	w2dt = w2*dy.concatenate(list(state.s()))
        unnormalized = dy.transpose(v * dy.tanh(dy.colwise_add(w1dt, w2dt)))
        attention_reshaped = dy.reshape(unnormalized, (input_length, ), batch_size)
	att_weights = dy.softmax(attention_reshaped)
	context = input_mat * att_weights
	return context
Ejemplo n.º 3
0
def attend(input_mat, state, w1dt):
	global attention_w2
	global attention_v
	w2 = dy.parameter(attention_w2)
	v = dy.parameter(attention_v)

	# input_mat: (encoder_state x seqlen) => input vecs concatenated as cols
	# w1dt: (attdim x seqlen)
	# w2dt: (attdim x attdim)
	w2dt = w2*dy.concatenate(list(state.s()))
	# att_weights: (seqlen,) row vector
	unnormalized = dy.transpose(v * dy.tanh(dy.colwise_add(w1dt, w2dt)))
	att_weights = dy.softmax(unnormalized)
	# context: (encoder_state)
	context = input_mat * att_weights
	return context
Ejemplo n.º 4
0
 def _mlp(self, W1_att_f, W1_att_e, w2_att, h_fs_matrix, h_e, F):
     E = W1_att_e * h_e
     a = dy.colwise_add(W1_att_f * h_fs_matrix, E)
     res = dy.transpose(dy.tanh(a)) * w2_att
     return res