-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatt_self_rcnn.py
33 lines (21 loc) · 1015 Bytes
/
att_self_rcnn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import tensorflow as tf
from arenets.attention import common
from arenets.context.architectures.rcnn import RCNN
class AttentionSelfRCNN(RCNN):
def __init__(self):
super(AttentionSelfRCNN, self).__init__()
self.__att_alphas = None
def get_attention_alphas(self, rnn_outputs):
raise NotImplementedError()
# region public methods
def iter_input_dependent_hidden_parameters(self):
for name, value in super(AttentionSelfRCNN, self).iter_input_dependent_hidden_parameters():
yield name, value
yield common.ATTENTION_WEIGHTS_LOG_PARAMETER, self.__att_alphas
# endregion
def modify_rnn_outputs_optional(self, output_fw, output_bw):
rnn_outputs = tf.add(output_fw, output_bw)
self.__att_alphas = self.get_attention_alphas(rnn_outputs)
output_fw_w = output_fw * tf.expand_dims(self.__att_alphas, -1)
output_bw_w = output_bw * tf.expand_dims(self.__att_alphas, -1)
return output_fw_w, output_bw_w