|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: UTF-8 -*- |
| 3 | + |
| 4 | +# Copyright 2016 Timothy Dozat |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | + |
| 18 | +from __future__ import absolute_import |
| 19 | +from __future__ import division |
| 20 | +from __future__ import print_function |
| 21 | + |
| 22 | +import numpy as np |
| 23 | +import tensorflow as tf |
| 24 | + |
| 25 | +from parser.neural.models.nlp.parsers.base_parser import BaseParser |
| 26 | + |
| 27 | +#*************************************************************** |
| 28 | +class BinParser(BaseParser): |
| 29 | + """""" |
| 30 | + |
| 31 | + #============================================================= |
| 32 | + def __call__(self, vocabs, moving_params=None): |
| 33 | + """""" |
| 34 | + |
| 35 | + top_recur = super(BinParser, self).__call__(vocabs, moving_params=moving_params) |
| 36 | + int_tokens_to_keep = tf.to_int32(self.tokens_to_keep) |
| 37 | + |
| 38 | + with tf.variable_scope('MLP'): |
| 39 | + dep_mlp, head_mlp = self.MLP(top_recur, self.arc_mlp_size + self.rel_mlp_size + self.p_mlp_size, |
| 40 | + n_splits=2) |
| 41 | + arc_dep_mlp, rel_dep_mlp, p_dep_mlp = tf.split(dep_mlp, [self.arc_mlp_size, self.rel_mlp_size, self.p_mlp_size], axis=2) |
| 42 | + arc_head_mlp, rel_head_mlp, p_head_mlp = tf.split(head_mlp, [self.arc_mlp_size, self.rel_mlp_size, self.p_mlp_size], axis=2) |
| 43 | + |
| 44 | + with tf.variable_scope('p'): |
| 45 | + # (n x b x d) o (d x 1 x d) o (n x b x d).T -> (n x b x b) |
| 46 | + arc_ps = self.bilinear(p_dep_mlp, p_head_mlp, 1) |
| 47 | + # (b x 1) |
| 48 | + arc_logits = -tf.nn.softplus(arc_ps) |
| 49 | + |
| 50 | + with tf.variable_scope('Arc'): |
| 51 | + # (n x b x d) o (d x 1 x d) o (n x b x d).T -> (n x b x b) |
| 52 | + arc_logits += self.bilinear(arc_dep_mlp, arc_head_mlp, 1, add_bias2=False) |
| 53 | + # (n x b x b) |
| 54 | + arc_probs = tf.nn.softmax(arc_logits) |
| 55 | + # (n x b) |
| 56 | + arc_preds = tf.to_int32(tf.argmax(arc_logits, axis=-1)) |
| 57 | + # (n x b) |
| 58 | + arc_targets = self.vocabs['heads'].placeholder |
| 59 | + # (n x b) |
| 60 | + arc_correct = tf.to_int32(tf.equal(arc_preds, arc_targets))*int_tokens_to_keep |
| 61 | + # () |
| 62 | + arc_loss = tf.losses.sparse_softmax_cross_entropy(arc_targets, arc_logits, self.tokens_to_keep) |
| 63 | + |
| 64 | + with tf.variable_scope('Rel'): |
| 65 | + # (n x b x d) o (d x r x d) o (n x b x d).T -> (n x b x r x b) |
| 66 | + rel_logits = self.bilinear(rel_dep_mlp, rel_head_mlp, len(self.vocabs['rels'])) |
| 67 | + # (n x b x r x b) |
| 68 | + rel_probs = tf.nn.softmax(rel_logits, dim=2) |
| 69 | + # (n x b x b) |
| 70 | + one_hot = tf.one_hot(arc_preds if moving_params is not None else arc_targets, self.bucket_size) |
| 71 | + # (n x b x b) -> (n x b x b x 1) |
| 72 | + one_hot = tf.expand_dims(one_hot, axis=3) |
| 73 | + # (n x b x r x b) o (n x b x b x 1) -> (n x b x r x 1) |
| 74 | + select_rel_logits = tf.matmul(rel_logits, one_hot) |
| 75 | + # (n x b x r x 1) -> (n x b x r) |
| 76 | + select_rel_logits = tf.squeeze(select_rel_logits, axis=3) |
| 77 | + # (n x b) |
| 78 | + rel_preds = tf.to_int32(tf.argmax(select_rel_logits, axis=-1)) |
| 79 | + # (n x b) |
| 80 | + rel_targets = self.vocabs['rels'].placeholder |
| 81 | + # (n x b) |
| 82 | + rel_correct = tf.to_int32(tf.equal(rel_preds, rel_targets))*int_tokens_to_keep |
| 83 | + # () |
| 84 | + rel_loss = tf.losses.sparse_softmax_cross_entropy(rel_targets, select_rel_logits, self.tokens_to_keep) |
| 85 | + |
| 86 | + n_arc_correct = tf.reduce_sum(arc_correct) |
| 87 | + n_rel_correct = tf.reduce_sum(rel_correct) |
| 88 | + correct = arc_correct * rel_correct |
| 89 | + n_correct = tf.reduce_sum(correct) |
| 90 | + n_seqs_correct = tf.reduce_sum(tf.to_int32(tf.equal(tf.reduce_sum(correct, axis=1), self.sequence_lengths-1))) |
| 91 | + loss = arc_loss + rel_loss |
| 92 | + |
| 93 | + outputs = { |
| 94 | + 'arc_logits': arc_logits, |
| 95 | + 'arc_probs': arc_probs, |
| 96 | + 'arc_preds': arc_preds, |
| 97 | + 'arc_targets': arc_targets, |
| 98 | + 'arc_correct': arc_correct, |
| 99 | + 'arc_loss': arc_loss, |
| 100 | + 'n_arc_correct': n_arc_correct, |
| 101 | + |
| 102 | + 'rel_logits': rel_logits, |
| 103 | + 'rel_probs': rel_probs, |
| 104 | + 'rel_preds': rel_preds, |
| 105 | + 'rel_targets': rel_targets, |
| 106 | + 'rel_correct': rel_correct, |
| 107 | + 'rel_loss': rel_loss, |
| 108 | + 'n_rel_correct': n_rel_correct, |
| 109 | + |
| 110 | + 'n_tokens': self.n_tokens, |
| 111 | + 'n_seqs': self.batch_size, |
| 112 | + 'tokens_to_keep': self.tokens_to_keep, |
| 113 | + 'n_correct': n_correct, |
| 114 | + 'n_seqs_correct': n_seqs_correct, |
| 115 | + 'loss': loss |
| 116 | + } |
| 117 | + |
| 118 | + return outputs |
0 commit comments