text
stringlengths
1
93.6k
attention_pool = tf.reduce_sum(scores * passage,1)
_, state = cell(attention_pool, initial_state)
inputs = [passage, state]
p2_logits = attention(inputs, Params.attn_size, weights_p, memory_len = passage_len, scope = "attention", reuse = True)
return tf.stack((p1_logits,p2_logits),1)
def attention_rnn(inputs, inputs_len, units, attn_cell, bidirection = True, scope = "gated_attention_rnn", is_training = True):
with tf.variable_scope(scope):
if bidirection:
outputs = bidirectional_GRU(inputs,
inputs_len,
cell = attn_cell,
scope = scope + "_bidirectional",
output = 0,
is_training = is_training)
else:
outputs, _ = tf.nn.dynamic_rnn(attn_cell, inputs,
sequence_length = inputs_len,
dtype=tf.float32)
return outputs
def question_pooling(memory, units, weights, memory_len = None, scope = "question_pooling"):
with tf.variable_scope(scope):
shapes = memory.get_shape().as_list()
V_r = tf.get_variable("question_param", shape = (Params.max_q_len, units), initializer = tf.contrib.layers.xavier_initializer(), dtype = tf.float32)
inputs_ = [memory, V_r]
attn = attention(inputs_, units, weights, memory_len = memory_len, scope = "question_attention_pooling")
attn = tf.expand_dims(attn, -1)
return tf.reduce_sum(attn * memory, 1)
def gated_attention(memory, inputs, states, units, params, self_matching = False, memory_len = None, scope="gated_attention"):
with tf.variable_scope(scope):
weights, W_g = params
inputs_ = [memory, inputs]
states = tf.reshape(states,(Params.batch_size,Params.attn_size))
if not self_matching:
inputs_.append(states)
scores = attention(inputs_, units, weights, memory_len = memory_len)
scores = tf.expand_dims(scores,-1)
attention_pool = tf.reduce_sum(scores * memory, 1)
inputs = tf.concat((inputs,attention_pool),axis = 1)
g_t = tf.sigmoid(tf.matmul(inputs,W_g))
return g_t * inputs
def mask_attn_score(score, memory_sequence_length, score_mask_value = -1e8):
score_mask = tf.sequence_mask(
memory_sequence_length, maxlen=score.shape[1])
score_mask_values = score_mask_value * tf.ones_like(score)
return tf.where(score_mask, score, score_mask_values)
def attention(inputs, units, weights, scope = "attention", memory_len = None, reuse = None):
with tf.variable_scope(scope, reuse = reuse):
outputs_ = []
weights, v = weights
for i, (inp,w) in enumerate(zip(inputs,weights)):
shapes = inp.shape.as_list()
inp = tf.reshape(inp, (-1, shapes[-1]))
if w is None:
w = tf.get_variable("w_%d"%i, dtype = tf.float32, shape = [shapes[-1],Params.attn_size], initializer = tf.contrib.layers.xavier_initializer())
outputs = tf.matmul(inp, w)
# Hardcoded attention output reshaping. Equation (4), (8), (9) and (11) in the original paper.
if len(shapes) > 2:
outputs = tf.reshape(outputs, (shapes[0], shapes[1], -1))
elif len(shapes) == 2 and shapes[0] is Params.batch_size:
outputs = tf.reshape(outputs, (shapes[0],1,-1))
else:
outputs = tf.reshape(outputs, (1, shapes[0],-1))
outputs_.append(outputs)
outputs = sum(outputs_)
if Params.bias:
b = tf.get_variable("b", shape = outputs.shape[-1], dtype = tf.float32, initializer = tf.contrib.layers.xavier_initializer())
outputs += b
scores = tf.reduce_sum(tf.tanh(outputs) * v, [-1])
if memory_len is not None:
scores = mask_attn_score(scores, memory_len)
return tf.nn.softmax(scores) # all attention output is softmaxed now
def cross_entropy(output, target):
cross_entropy = target * tf.log(output + 1e-8)
cross_entropy = -tf.reduce_sum(cross_entropy, 2) # sum across passage timestep
cross_entropy = tf.reduce_mean(cross_entropy, 1) # average across pointer networks output
return tf.reduce_mean(cross_entropy) # average across batch size
def total_params():
total_parameters = 0
for variable in tf.trainable_variables():
shape = variable.get_shape()
variable_parametes = 1
for dim in shape:
variable_parametes *= dim.value
total_parameters += variable_parametes
print("Total number of trainable parameters: {}".format(total_parameters))
# <FILESEP>
import torch
import pandas as pd
from train_util import AddEgoIds, extract_param, add_arange_ids, get_loaders, evaluate_homo, evaluate_hetero
from training import get_model
from torch_geometric.nn import to_hetero, summary
import wandb