我们从Python开源项目中,提取了以下7个代码示例,用于说明如何使用torch.nn.functional.bilinear()。
def test_bilinear(self): module = nn.Bilinear(10, 10, 8) module2 = legacy.Bilinear(10, 10, 8) module2.weight.copy_(module.weight.data) module2.bias.copy_(module.bias.data) input1 = torch.randn(4, 10) input2 = torch.randn(4, 10) output = module(Variable(input1), Variable(input2)) output2 = module2.forward([input1, input2]) input1_1 = Variable(input1, requires_grad=True) input2_1 = Variable(input2, requires_grad=True) output3 = module(input1_1, input2_1) grad = torch.randn(*output3.size()) output3.backward(grad) gi1 = input1_1.grad.data.clone() gi2 = input2_1.grad.data.clone() self.assertEqual(output.data, output2) self.assertEqual([gi1, gi2], output3) self.assertTrue(gradcheck(lambda x1, x2: F.bilinear(x1, x2, module.weight, module.bias), (input1_1, input2_1)))
def test_bilinear(self): module = nn.Bilinear(10, 10, 8) module2 = legacy.Bilinear(10, 10, 8) module2.weight.copy_(module.weight.data) module2.bias.copy_(module.bias.data) input1 = torch.randn(4, 10) input2 = torch.randn(4, 10) output = module(Variable(input1), Variable(input2)) output2 = module2.forward([input1, input2]) input1_1 = Variable(input1, requires_grad=True) input2_1 = Variable(input2, requires_grad=True) output3 = module(input1_1, input2_1) grad = torch.randn(*output3.size()) output3.backward(grad) gi1 = input1_1.grad.data.clone() gi2 = input2_1.grad.data.clone() self.assertEqual(output.data, output2) # TODO: this assertion is incorrect, fix needed # self.assertEqual([gi1, gi2], output3) self.assertTrue(gradcheck(lambda x1, x2: F.bilinear(x1, x2, module.weight, module.bias), (input1_1, input2_1)))
def test_upsamplingBilinear2d(self): m = nn.Upsample(size=4, mode='bilinear') in_t = torch.ones(1, 1, 2, 2) out_t = m(Variable(in_t)) self.assertEqual(torch.ones(1, 1, 4, 4), out_t.data) input = Variable(torch.randn(1, 1, 2, 2), requires_grad=True) self.assertTrue(gradcheck(lambda x: F.upsample(x, 4, mode='bilinear'), (input,)))
def test_bilinear(self): module = nn.Bilinear(10, 10, 8) module_legacy = legacy.Bilinear(10, 10, 8) module_legacy.weight.copy_(module.weight.data) module_legacy.bias.copy_(module.bias.data) input1 = torch.randn(4, 10) input2 = torch.randn(4, 10) output = module(Variable(input1), Variable(input2)) output_legacy = module_legacy.forward([input1, input2]) self.assertEqual(output.data, output_legacy) input1_1 = Variable(input1, requires_grad=True) input2_1 = Variable(input2, requires_grad=True) module.zero_grad() module_legacy.zeroGradParameters() output = module(input1_1, input2_1) grad_output = torch.randn(*output.size()) gi1_legacy, gi2_legacy = module_legacy.backward([input1, input2], grad_output) output.backward(grad_output) gi1 = input1_1.grad.data.clone() gi2 = input2_1.grad.data.clone() self.assertEqual(gi1, gi1_legacy) self.assertEqual(gi2, gi2_legacy) self.assertEqual(module.weight.grad.data, module_legacy.gradWeight) self.assertEqual(module.bias.grad.data, module_legacy.gradBias) _assertGradAndGradgradChecks(self, lambda x1, x2: F.bilinear(x1, x2, module.weight, module.bias), (input1_1, input2_1))
def forward(self, input_left, input_right): ''' Args: input_left: Tensor the left input tensor with shape = [batch1, batch2, ..., left_features] input_right: Tensor the right input tensor with shape = [batch1, batch2, ..., right_features] Returns: ''' left_size = input_left.size() right_size = input_right.size() assert left_size[:-1] == right_size[:-1], \ "batch size of left and right inputs mis-match: (%s, %s)" % (left_size[:-1], right_size[:-1]) batch = int(np.prod(left_size[:-1])) # convert left and right input to matrices [batch, left_features], [batch, right_features] input_left = input_left.view(batch, self.left_features) input_right = input_right.view(batch, self.right_features) # output [batch, out_features] output = F.bilinear(input_left, input_right, self.U, self.bias) output = output + F.linear(input_left, self.W_l, None) + F.linear(input_right, self.W_r, None) # convert back to [batch1, batch2, ..., out_features] return output.view(left_size[:-1] + (self.out_features, ))
def test_upsamplingBilinear2d(self): m = nn.Upsample(size=4, mode='bilinear') in_t = torch.ones(1, 1, 2, 2) out_t = m(Variable(in_t)) self.assertEqual(torch.ones(1, 1, 4, 4), out_t.data) input = Variable(torch.randn(1, 1, 2, 2), requires_grad=True) gradcheck(lambda x: F.upsample(x, 4, mode='bilinear'), [input])