MCPcopy Create free account
hub / github.com/NJUNLP/GTS / forward

Method forward

code/NNModel/attention_module.py:73–88  ·  view source on GitHub ↗
(self, query, value, mask)

Source from the content-addressed store, hash-verified

71 self.v = torch.nn.Linear(50, 1, bias=False)
72
73 def forward(self, query, value, mask):
74 # attention_states = self.linear_q(query)
75 # attention_states_T = self.linear_k(values)
76 attention_states = query
77 attention_states_T = value
78 attention_states_T = attention_states_T.permute([0, 2, 1])
79
80 weights=torch.bmm(attention_states, attention_states_T)
81 weights = weights.masked_fill(mask.unsqueeze(1).expand_as(weights)==0, float("-inf")) # mask掉每行后面的列
82 attention = F.softmax(weights,dim=2)
83
84 # value=self.linear_v(states)
85 merged=torch.bmm(attention, value)
86 merged=merged * mask.unsqueeze(2).float().expand_as(merged)
87
88 return merged
89
90 def forward_perceptron(self, query, value, mask):
91 attention_states = query

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected