我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用torch.nn.functional.cosine_similarity()。
def forward(self, repres, max_att): """ Args: repres - [bsz, a_len|q_len, cont_dim] max_att - [bsz, q_len|a_len, cont_dim] Return: size - [bsz, sentence_len, mp_dim] """ bsz = repres.size(0) sent_len = repres.size(1) repres = repres.view(-1, self.cont_dim) max_att = max_att.view(-1, self.cont_dim) repres = multi_perspective_expand_for_2D(repres, self.weight) max_att = multi_perspective_expand_for_2D(max_att, self.weight) temp = cosine_similarity(repres, max_att, repres.dim()-1) return temp.view(bsz, sent_len, self.mp_dim)
def test_cosine_similarity(self): input1 = Variable(torch.randn(4, 4), requires_grad=True) input2 = Variable(torch.randn(4, 4), requires_grad=True) self.assertTrue(gradcheck(lambda x, y: F.cosine_similarity(x, y), (input1, input2))) input1 = Variable(torch.randn(4, 5, 6), requires_grad=True) input2 = Variable(torch.randn(4, 5, 6), requires_grad=True) self.assertTrue(gradcheck(lambda x, y: F.cosine_similarity(x, y, dim=0), (input1, input2))) self.assertTrue(gradcheck(lambda x, y: F.cosine_similarity(x, y, dim=-1), (input1, input2))) # Check cosine_similarity input/output shapes input_size = (1, 3, 2, 1) expected_size = (1, 2, 1) input1 = Variable(torch.randn(input_size), requires_grad=True) input2 = Variable(torch.randn(input_size), requires_grad=True) self.assertEqual(F.cosine_similarity(input1, input2, dim=1).size(), expected_size)
def forward(self, q_corpora, q_words, a_corpora, a_words): """ Module main forward """ # Step 1 - Get Mask from q_corpora and a_corpora self.q_mask = q_corpora.ge(PF_POS) self.a_mask = a_corpora.ge(PF_POS) # Step 2 - Word Representation Layer self.q_repres = self._word_repre_layer((q_corpora, q_words)) self.a_repres = self._word_repre_layer((a_corpora, a_words)) # Step 3 - Cosine Similarity and mask iqr_temp = self.q_repres.unsqueeze(1) # [bsz, 1, q_len, context_dim] ipr_temp = self.a_repres.unsqueeze(2) # [bsz, a_len, 1, context_dim] # [bsz, a_len, q_len] simi = F.cosine_similarity(iqr_temp, ipr_temp, dim=3) simi_mask = self._cosine_similarity_mask(simi) # Step 4 - Matching Layer q_aware_reps, a_aware_reps = self._bilateral_match(simi_mask) q_aware_reps = F.dropout(q_aware_reps, p=self.dropout) a_aware_reps = F.dropout(a_aware_reps, p=self.dropout) # Step 5 - Aggregation Layer aggre = self._aggre(q_aware_reps, a_aware_reps) # Step 6 - Prediction Layer predict = F.tanh(self.l1(aggre)) predict = F.dropout(predict, p=self.dropout) return F.softmax(self.l2(predict))
def forward(self, cont_repres, other_cont_first): """ Args: cont_repres - [batch_size, this_len, context_lstm_dim] other_cont_first - [batch_size, context_lstm_dim] Return: size - [batch_size, this_len, mp_dim] """ def expand(context, weight): """ Args: [batch_size, this_len, context_lstm_dim] [mp_dim, context_lstm_dim] Return: [batch_size, this_len, mp_dim, context_lstm_dim] """ # [1, 1, mp_dim, context_lstm_dim] weight = weight.unsqueeze(0) weight = weight.unsqueeze(0) # [batch_size, this_len, 1, context_lstm_dim] context = context.unsqueeze(2) return torch.mul(context, weight) cont_repres = expand(cont_repres, self.weight) other_cont_first = multi_perspective_expand_for_2D(other_cont_first, self.weight) # [batch_size, 1, mp_dim, context_lstm_dim] other_cont_first = other_cont_first.unsqueeze(1) return cosine_similarity(cont_repres, other_cont_first, cont_repres.dim()-1)
def forward(self, cont_repres, other_cont_repres): """ Args: cont_repres - [batch_size, this_len, context_lstm_dim] other_cont_repres - [batch_size, other_len, context_lstm_dim] Return: size - [bsz, this_len, mp_dim*2] """ bsz = cont_repres.size(0) this_len = cont_repres.size(1) other_len = other_cont_repres.size(1) cont_repres = cont_repres.view(-1, self.cont_dim) other_cont_repres = other_cont_repres.view(-1, self.cont_dim) cont_repres = multi_perspective_expand_for_2D(cont_repres, self.weight) other_cont_repres = multi_perspective_expand_for_2D(other_cont_repres, self.weight) cont_repres = cont_repres.view(bsz, this_len, self.mp_dim, self.cont_dim) other_cont_repres = other_cont_repres.view(bsz, other_len, self.mp_dim, self.cont_dim) # [bsz, this_len, 1, self.mp_dim, self.cont_dim] cont_repres = cont_repres.unsqueeze(2) # [bsz, 1, other_len, self.mp_dim, self.cont_dim] other_cont_repres = other_cont_repres.unsqueeze(1) # [bsz, this_len, other_len, self.mp_dim]fanruan simi = cosine_similarity(cont_repres, other_cont_repres, cont_repres.dim()-1) t_max, _ = simi.max(2) t_mean = simi.mean(2) return torch.cat((t_max, t_mean), 2)
def _algo_1_horiz_comp(self, sent1_block_a, sent2_block_a): comparison_feats = [] for pool in ('max', 'min', 'mean'): for ws in self.filter_widths: x1 = sent1_block_a[ws][pool] x2 = sent2_block_a[ws][pool] batch_size = x1.size()[0] comparison_feats.append(F.cosine_similarity(x1, x2).contiguous().view(batch_size, 1)) comparison_feats.append(F.pairwise_distance(x1, x2)) return torch.cat(comparison_feats, dim=1)
def _algo_2_vert_comp(self, sent1_block_a, sent2_block_a, sent1_block_b, sent2_block_b): comparison_feats = [] ws_no_inf = [w for w in self.filter_widths if not np.isinf(w)] for pool in ('max', 'min', 'mean'): for ws1 in self.filter_widths: x1 = sent1_block_a[ws1][pool] batch_size = x1.size()[0] for ws2 in self.filter_widths: x2 = sent2_block_a[ws2][pool] if (not np.isinf(ws1) and not np.isinf(ws2)) or (np.isinf(ws1) and np.isinf(ws2)): comparison_feats.append(F.cosine_similarity(x1, x2).contiguous().view(batch_size, 1)) comparison_feats.append(F.pairwise_distance(x1, x2)) comparison_feats.append(torch.abs(x1 - x2)) for pool in ('max', 'min'): for ws in ws_no_inf: oG_1B = sent1_block_b[ws][pool] oG_2B = sent2_block_b[ws][pool] for i in range(0, self.n_per_dim_filters): x1 = oG_1B[:, :, i] x2 = oG_2B[:, :, i] batch_size = x1.size()[0] comparison_feats.append(F.cosine_similarity(x1, x2).contiguous().view(batch_size, 1)) comparison_feats.append(F.pairwise_distance(x1, x2)) comparison_feats.append(torch.abs(x1 - x2)) return torch.cat(comparison_feats, dim=1)
def test_cosine_similarity(self): input1 = Variable(torch.randn(4, 4), requires_grad=True) input2 = Variable(torch.randn(4, 4), requires_grad=True) self.assertTrue(gradcheck(lambda x, y: F.cosine_similarity(x, y), (input1, input2))) input1 = Variable(torch.randn(4, 5, 6), requires_grad=True) input2 = Variable(torch.randn(4, 5, 6), requires_grad=True) self.assertTrue(gradcheck(lambda x, y: F.cosine_similarity(x, y, dim=0), (input1, input2))) self.assertTrue(gradcheck(lambda x, y: F.cosine_similarity(x, y, dim=-1), (input1, input2)))