Advertisement
pikaliov

logits_penalize_part_crf

Apr 9th, 2020
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.81 KB | None | 0 0
  1. range_labels = tf.range(0, self.target_vocab_size)
  2. res = lambda inp, val: tf.reduce_sum(tf.cast(tf.equal(inp, val), tf.float32))
  3. count_arr = tf.convert_to_tensor([res(y, i) + 1 for i in range_labels])
  4. priores = count_arr / tf.reduce_sum(count_arr)
  5. priores = tf.clip_by_value(priores, -1e12, 1e12)
  6. label_sequences = tf.convert_to_tensor(y, dtype=tf.int32)
  7. logits = tf.nn.log_softmax(logits) - tf.math.log(priores)
  8. log_likelihood, trans_params = tfa.text.crf.crf_log_likelihood(inputs=logits,
  9.                                                                            tag_indices=label_sequences,
  10.                                                                            sequence_lengths=seq_length)
  11. self.transition_params = tf.Variable(self.transition_params, trainable=False)
  12. loss = tf.reduce_mean(-log_likelihood)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement