def test_forward_does_a_bilinear_product(self): # pylint: disable=protected-access bilinear = BilinearSimilarity(2, 2) bilinear._weight_matrix = Parameter(torch.FloatTensor([[-.3, .5], [2.0, -1.0]])) bilinear._bias = Parameter(torch.FloatTensor([.1])) a_vectors = Variable(torch.FloatTensor([[1, 1], [-1, -1]])) b_vectors = Variable(torch.FloatTensor([[1, 0], [0, 1]])) result = bilinear(a_vectors, b_vectors).data.numpy() assert result.shape == (2,) assert_almost_equal(result, [1.8, .6])
def test_forward_does_a_bilinear_product(self): # pylint: disable=protected-access bilinear = BilinearSimilarity(2, 2) bilinear._weight_matrix = Parameter( torch.FloatTensor([[-.3, .5], [2.0, -1.0]])) bilinear._bias = Parameter(torch.FloatTensor([.1])) a_vectors = torch.FloatTensor([[1, 1], [-1, -1]]) b_vectors = torch.FloatTensor([[1, 0], [0, 1]]) result = bilinear(a_vectors, b_vectors).data.numpy() assert result.shape == (2, ) assert_almost_equal(result, [1.8, .6])
def test_forward_works_with_higher_order_tensors(self): # pylint: disable=protected-access bilinear = BilinearSimilarity(4, 7) weights = numpy.random.rand(4, 7) bilinear._weight_matrix = Parameter(torch.from_numpy(weights).float()) bilinear._bias = Parameter(torch.from_numpy(numpy.asarray([0])).float()) a_vectors = numpy.random.rand(5, 4, 3, 6, 4) b_vectors = numpy.random.rand(5, 4, 3, 6, 7) a_variables = Variable(torch.from_numpy(a_vectors).float()) b_variables = Variable(torch.from_numpy(b_vectors).float()) result = bilinear(a_variables, b_variables).data.numpy() assert result.shape == (5, 4, 3, 6) expected_result = numpy.dot(numpy.dot(numpy.transpose(a_vectors[3, 2, 1, 3]), weights), b_vectors[3, 2, 1, 3]) assert_almost_equal(result[3, 2, 1, 3], expected_result, decimal=5)
def test_forward_works_with_higher_order_tensors(self): # pylint: disable=protected-access bilinear = BilinearSimilarity(4, 7) weights = numpy.random.rand(4, 7) bilinear._weight_matrix = Parameter(torch.from_numpy(weights).float()) bilinear._bias = Parameter( torch.from_numpy(numpy.asarray([0])).float()) a_vectors = numpy.random.rand(5, 4, 3, 6, 4) b_vectors = numpy.random.rand(5, 4, 3, 6, 7) a_variables = torch.from_numpy(a_vectors).float() b_variables = torch.from_numpy(b_vectors).float() result = bilinear(a_variables, b_variables).data.numpy() assert result.shape == (5, 4, 3, 6) expected_result = numpy.dot( numpy.dot(numpy.transpose(a_vectors[3, 2, 1, 3]), weights), b_vectors[3, 2, 1, 3]) assert_almost_equal(result[3, 2, 1, 3], expected_result, decimal=5)