Model
| 50 | |
| 51 | |
| 52 | class Model(dygraph.Layer): |
| 53 | """"Model""" |
| 54 | def __init__(self, args): |
| 55 | super(Model, self).__init__() |
| 56 | self.args = args |
| 57 | if args.encoding_model == "lstm": |
| 58 | self.embed = LSTMEmbed(args) |
| 59 | elif args.encoding_model == "transformer": |
| 60 | self.embed = TranEmbed(args) |
| 61 | elif args.encoding_model == "ernie-lstm": |
| 62 | self.embed = LSTMByWPEmbed(args) |
| 63 | elif args.encoding_model.startswith("ernie"): |
| 64 | self.embed = ErnieEmbed(args) |
| 65 | mlp_input_size = self.embed.mlp_input_size |
| 66 | |
| 67 | # mlp layer |
| 68 | self.mlp_arc_h = MLP(n_in=mlp_input_size, n_out=args.n_mlp_arc, dropout=args.mlp_dropout) |
| 69 | self.mlp_arc_d = MLP(n_in=mlp_input_size, n_out=args.n_mlp_arc, dropout=args.mlp_dropout) |
| 70 | self.mlp_rel_h = MLP(n_in=mlp_input_size, n_out=args.n_mlp_rel, dropout=args.mlp_dropout) |
| 71 | self.mlp_rel_d = MLP(n_in=mlp_input_size, n_out=args.n_mlp_rel, dropout=args.mlp_dropout) |
| 72 | |
| 73 | # biaffine layers |
| 74 | self.arc_attn = Biaffine(n_in=args.n_mlp_arc, bias_x=True, bias_y=False) |
| 75 | self.rel_attn = Biaffine(n_in=args.n_mlp_rel, n_out=args.n_rels, bias_x=True, bias_y=True) |
| 76 | |
| 77 | def forward(self, words, feats=None): |
| 78 | """Forward network""" |
| 79 | # batch_size, seq_len = words.shape |
| 80 | # get embedding |
| 81 | words, x = self.embed(words, feats) |
| 82 | mask = layers.logical_and(words != self.args.pad_index, words != self.args.eos_index) |
| 83 | |
| 84 | # apply MLPs to the BiLSTM output states |
| 85 | arc_h = self.mlp_arc_h(x) |
| 86 | arc_d = self.mlp_arc_d(x) |
| 87 | rel_h = self.mlp_rel_h(x) |
| 88 | rel_d = self.mlp_rel_d(x) |
| 89 | |
| 90 | # get arc and rel scores from the bilinear attention |
| 91 | # [batch_size, seq_len, seq_len] |
| 92 | s_arc = self.arc_attn(arc_d, arc_h) |
| 93 | # [batch_size, seq_len, seq_len, n_rels] |
| 94 | s_rel = layers.transpose(self.rel_attn(rel_d, rel_h), perm=(0, 2, 3, 1)) |
| 95 | # set the scores that exceed the length of each sentence to -1e5 |
| 96 | s_arc_mask = paddle.unsqueeze(mask, 1) |
| 97 | s_arc = s_arc * s_arc_mask + paddle.scale( |
| 98 | paddle.cast(s_arc_mask, 'int32'), scale=1e5, bias=-1, bias_after_scale=False) |
| 99 | |
| 100 | return s_arc, s_rel, words |
| 101 | |
| 102 | |
| 103 | def epoch_train(args, model, optimizer, loader, epoch): |