def __call__(self, adj, nodes, roles, targ): """ Puts the tensors through encoders and decoders :param adj: Adjacency matrices of input example :type adj: tf.tensor :param nodes: node features :type nodes: tf.tensor :param targ: target sequences :type targ: tf.tensor :return: output probability distribution :rtype: tf.tensor """ enc_output = self.encoder(nodes, adj, roles, self.num_heads, self.encoder.trainable, None) batch = enc_output.shape[0] self.enc_output_hidden = tf.reshape(enc_output, shape=[batch, -1]) enc_hidden = self.hidden(self.enc_output_hidden) dec_input = tf.expand_dims([self.target_lang.word_index['<start>']] * self.args.batch_size, 1) loss = 0 for t in range(1, targ.shape[1]): # pass encoder output to decoder predictions, dec_hidden, _ = self.decoder(dec_input, enc_hidden, enc_output) loss += loss_function(targ[:, t], predictions, self.loss_object) #using teacher forcing dec_input = tf.expand_dims(targ[:, t], 1) return predictions, dec_hidden, loss
def eval_step(adj, nodes, targ): model.trainable = False tar_real = targ[:, 1:] tar_inp = targ[:, :-1] mask = create_transgat_masks(tar_inp) predictions, att_weights = model(adj, nodes, tar_inp, mask) eval_loss = loss_function(tar_real, predictions, loss_object) train_loss(eval_loss) train_accuracy(tar_real, predictions) eval_loss = train_loss.result() acc = train_accuracy.result() model.trainable = True return eval_loss, acc
def train_step(adj, nodes, targ): tar_real = targ[:, 1:] tar_inp = targ[:, :-1] with tf.GradientTape() as tape: mask = create_transgat_masks(tar_inp) predictions, att_weights = model(adj, nodes, tar_inp, mask) batch_loss = loss_function(tar_real, predictions, loss_object) gradients = tape.gradient(batch_loss, model.trainable_weights) optimizer.apply_gradients(zip(gradients, model.trainable_weights)) train_loss(batch_loss) train_accuracy(tar_real, predictions) batch_loss = train_loss.result() acc = train_accuracy.result() return batch_loss, acc
def __call__(self, inp, targ, enc_hidden): loss = 0 enc_output, enc_hidden = self.encoder(inp, enc_hidden) dec_hidden = enc_hidden dec_input = tf.expand_dims([self.target_lang.word_index['<start>']] * self.batch_size, 1) # Teacher forcing - feeding the target as the next input for t in range(1, targ.shape[1]): # passing enc_output to the decoder predictions, dec_hidden, _ = self.decoder(dec_input, dec_hidden, enc_output) loss += loss_function(targ[:, t], predictions, self.loss_object) # using teacher forcing dec_input = tf.expand_dims(targ[:, t], 1) return predictions, dec_hidden, loss
def eval_step(inp, tar): model.trainable = False tar_inp = tar[:, :-1] tar_real = tar[:, 1:] enc_padding_mask, combined_mask, dec_padding_mask = create_masks( inp, tar_inp) predictions, _ = model(inp, tar_inp, True, enc_padding_mask, combined_mask, dec_padding_mask) loss = loss_function(tar_real, predictions, loss_object) train_loss(loss) train_accuracy(tar_real, predictions) loss = train_loss.result() acc = train_accuracy.result() model.trainable = True return loss, acc
def train_step(inp, tar): tar_inp = tar[:, :-1] tar_real = tar[:, 1:] enc_padding_mask, combined_mask, dec_padding_mask = create_masks( inp, tar_inp) with tf.GradientTape() as tape: predictions, _ = model(inp, tar_inp, True, enc_padding_mask, combined_mask, dec_padding_mask) loss = loss_function(tar_real, predictions, loss_object) gradients = tape.gradient(loss, model.trainable_variables) optimizer.apply_gradients(zip(gradients, model.trainable_variables)) train_loss(loss) train_accuracy(tar_real, predictions) loss = train_loss.result() acc = train_accuracy.result() return loss, acc