Example #1
0
    def check_forward(self, e1_data, e2_data, W_data, V1_data, V2_data,
                      b_data):
        e1 = chainer.Variable(e1_data)
        e2 = chainer.Variable(e2_data)
        W = chainer.Variable(W_data)

        e1_data = e1_data.reshape(e1_data.shape[0], -1)
        e2_data = e2_data.reshape(e2_data.shape[0], -1)
        xp = cuda.get_array_module(e1)
        y_expect = xp.einsum('ij,ik,jkl->il', e1_data, e2_data, W_data)

        flags = V1_data is None, V2_data is None, b_data is None
        if any(flags):
            if not all(flags):
                raise ValueError(
                    'Test either all or none of the optional parameters.')
            y = functions.bilinear(e1, e2, W)
        else:
            V1 = chainer.Variable(V1_data)
            V2 = chainer.Variable(V2_data)
            b = chainer.Variable(b_data)
            y = functions.bilinear(e1, e2, W, V1, V2, b)

            y_expect = xp.einsum('ij,ik,jkl->il', e1_data, e2_data, W_data)
            y_expect += e1_data.dot(V1_data)
            y_expect += e2_data.dot(V2_data)
            y_expect += b_data

        testing.assert_allclose(y_expect, cuda.to_cpu(y.data))
        assert y.data.dtype == e1_data.dtype
Example #2
0
    def check_forward(self, e1_data, e2_data, W_data, V1_data, V2_data,
                      b_data):
        e1 = chainer.Variable(e1_data)
        e2 = chainer.Variable(e2_data)
        W = chainer.Variable(W_data)

        e1_data = e1_data.reshape(e1_data.shape[0], -1)
        e2_data = e2_data.reshape(e2_data.shape[0], -1)
        xp = cuda.get_array_module(e1)
        y_expect = xp.einsum('ij,ik,jkl->il', e1_data, e2_data, W_data)

        flags = V1_data is None, V2_data is None, b_data is None
        if any(flags):
            if not all(flags):
                raise ValueError(
                    'Test either all or none of the optional parameters.')
            y = functions.bilinear(e1, e2, W)
        else:
            V1 = chainer.Variable(V1_data)
            V2 = chainer.Variable(V2_data)
            b = chainer.Variable(b_data)
            y = functions.bilinear(e1, e2, W, V1, V2, b)

            y_expect = xp.einsum('ij,ik,jkl->il', e1_data, e2_data, W_data)
            y_expect += e1_data.dot(V1_data)
            y_expect += e2_data.dot(V2_data)
            y_expect += b_data

        testing.assert_allclose(y_expect, cuda.to_cpu(y.data))
        assert y.data.dtype == e1_data.dtype
Example #3
0
 def forward(self, inputs, device):
     if self.test_partial:
         e1, e2, W = inputs
         V1 = None
         V2 = None
         b = None
     else:
         e1, e2, W, V1, V2, b = inputs
     flags = V1 is None, V2 is None, b is None
     if any(flags):
         if not all(flags):
             raise ValueError(
                 'Test either all or none of the optional parameters.')
         y = functions.bilinear(e1, e2, W)
     else:
         y = functions.bilinear(e1, e2, W, V1, V2, b)
     return y,
Example #4
0
 def forward(self, inputs, device):
     if self.test_partial:
         e1, e2, W = inputs
         V1 = None
         V2 = None
         b = None
     else:
         e1, e2, W, V1, V2, b = inputs
     flags = V1 is None, V2 is None, b is None
     if any(flags):
         if not all(flags):
             raise ValueError(
                 'Test either all or none of the optional parameters.')
         y = functions.bilinear(e1, e2, W)
     else:
         y = functions.bilinear(e1, e2, W, V1, V2, b)
     return y,
Example #5
0
 def test_invalid_full_partial_ambiguous(self):
     with self.assertRaises(ValueError):
         functions.bilinear(self.e1, self.e2, self.W, self.V1)
Example #6
0
 def test_invalid_full_partial_ambiguous(self):
     with self.assertRaises(ValueError):
         functions.bilinear(self.e1, self.e2, self.W, self.V1)
Example #7
0
 def f(*inputs):
     y = functions.bilinear(*inputs)
     return y * y
Example #8
0
 def f(*inputs):
     y = functions.bilinear(*inputs)
     return y * y