pikaliov

penalize_func

Apr 9th, 2020
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.03 KB | None | 0 0
  1.     def equal_f(self, inp, val):
  2.         res = tf.reduce_sum(tf.cast(tf.equal(inp, val), tf.int32)) + 1
  3.         return res
  4.  
  5.     def call(self, inp, training=None, y=None):
  6.         seq_length = tf.math.reduce_sum(tf.cast(tf.math.not_equal(inp, 0), dtype=tf.int32), axis=1)
  7.         mask = tf.cast(tf.math.not_equal(inp, 0), tf.float32)[:, tf.newaxis, tf.newaxis, :]  #(batch_size, 1, 1, seq_len)
  8.         enc_output = self.encoder(inp, training, mask)  # (batch_size, inp_seq_len, d_model)
  9.         logits = self.final_layer(enc_output)  # (batch_size, tar_seq_len, target_vocab_size)
  10.  
  11.         if y is not None:
  12.             range_labels = tf.range(0, self.target_vocab_size)
  13.             res = lambda val: self.equal_f(y, val)
  14.             count_arr = tf.cast(tf.map_fn(res, range_labels), dtype=tf.float32)
  15.             priores = count_arr / tf.reduce_sum(count_arr)
  16.             logits = tf.nn.log_softmax(logits) - tf.math.log(priores)
  17.  
  18.         pred = tf.cast(tf.argmax(logits, axis=-1), dtype=tf.int32)
  19.         return pred, logits, seq_length
Advertisement
Add Comment
Please, Sign In to add comment