Exemple #1
0
def ls_solution(g0, g1):
    """ Get least-squares solution matrix for regression from rows of g0
      to rows of g1. Both g0 and g1 are chainer's Variable.
  """
    g0t = F.transpose(g0)
    if g0.shape[0] >= g0.shape[1]:
        g0pinv = F.matmul(F.inv(F.matmul(g0t, g0)), g0t)
    else:
        g0pinv = F.matmul(g0t, F.inv(F.matmul(g0, g0t)))
    K = F.transpose(F.matmul(g0pinv, g1))
    return K
Exemple #2
0
    def test_singular_gpu(self):
        x = chainer.Variable(
            cuda.to_gpu(numpy.zeros((2, 2), dtype=numpy.float32)))

        # Should raise exception only when debug mode.
        with chainer.using_config('debug', False):
            functions.inv(x)

        with chainer.using_config('debug', True):
            with self.assertRaises(ValueError):
                functions.inv(x)
Exemple #3
0
def norm(x):
    """
    ベクトルの正規化
    x -> x/|x|
    """
    s = F.sum(x**2, axis=1) ** 0.5  # [a,b]^T
    height = s.data.shape[0]
    a = Variable(np.ones((1, height), dtype=np.float32))  # [1,1]
    eye = Variable(np.eye(height, dtype=np.float32))
    b = F.inv(F.matmul(s, a) * eye)  # [1/a, 0; 0, 1/b]
    return F.matmul(b, x)
Exemple #4
0
    def _kdeparts(self, input_obs, input_ins):
        """ Multivariate Kernel Density Estimation (KDE) with Gaussian kernels on the given random variables.
            INPUT:
                input_obs - Variable of input observation random variables to estimate density
                input_ins - Variable of input data instance to calculate the probability value
            OUTPUT:
                const - Constant term in the Gaussian KDE expression
                energy - Expressions in the exponential to calculate Gaussian KDE (energy wrt. every obs. point)
        """
        [n, d] = input_obs.shape

        # Compute Kernel Bandwidth Matrix based on Silverman's Rule of Thumb
        silverman_factor = np.power(n * (d + 2.0) / 4.0, -1. / (d + 4))
        input_centered = input_obs - F.mean(input_obs, axis=0, keepdims=True)
        data_covariance = F.matmul(F.transpose(input_centered), input_centered) / n
        kernel_bw = F.diagonal(data_covariance) * (silverman_factor ** 2) * np.eye(d, d)
        const = 1 / (n * ((2 * np.pi) ** (d/2)) * F.sqrt(F.det(kernel_bw)))

        # Compute energy expressions in the exponent for every observation point
        diff = input_obs - input_ins
        energy = -0.5 * F.diagonal(F.matmul(F.matmul(diff, F.inv(kernel_bw)), F.transpose(diff)))

        return const, energy
Exemple #5
0
 def test_invalid_shape(self):
     x = chainer.Variable(numpy.zeros((1, 2), dtype=numpy.float32))
     with self.assertRaises(type_check.InvalidType):
         functions.inv(x)
Exemple #6
0
 def test_identity_gpu(self):
     eye = cuda.to_gpu(_make_eye(self.x.shape))
     x = chainer.Variable(cuda.to_gpu(self.x))
     y = functions.matmul(x, functions.inv(x))
     testing.assert_allclose(
         y.data, eye, **self.check_forward_options)
Exemple #7
0
 def check_forward(self, x_data):
     x = chainer.Variable(x_data)
     y = functions.inv(x)
     x1 = self.x.astype(self.check_forward_dtype, copy=False)
     testing.assert_allclose(_inv(x1), y.data, **self.check_forward_options)
Exemple #8
0
 def forward(self, inputs, device):
     x, = inputs
     return functions.inv(x),
Exemple #9
0
 def check_forward(self, x_data):
     x = chainer.Variable(x_data)
     y = functions.inv(x)
     testing.assert_allclose(_inv(self.x), y.data,
                             **self.check_forward_options)
Exemple #10
0
 def check_forward(self, x_data):
     x = chainer.Variable(x_data)
     y = functions.inv(x)
     testing.assert_allclose(
         _inv(self.x), y.data, **self.check_forward_options)
Exemple #11
0
 def test_invalid_shape(self):
     with self.assertRaises(TypeError):
         functions.inv(chainer.Variable(numpy.zeros(1, 2)))
Exemple #12
0
 def test_invalid_shape(self):
     with self.assertRaises(TypeError):
         functions.inv(chainer.Variable(numpy.zeros(1, 2)))
Exemple #13
0
 def test_identity_gpu(self):
     eye = cuda.to_gpu(_make_eye(self.x.shape))
     x = chainer.Variable(cuda.to_gpu(self.x))
     y = functions.matmul(x, functions.inv(x))
     gradient_check.assert_allclose(y.data, eye, rtol=1e-4, atol=1e-4)
Exemple #14
0
 def check_forward(self, x_data, atol=1e-7, rtol=1e-7):
     x = chainer.Variable(x_data)
     y = functions.inv(x)
     gradient_check.assert_allclose(
         _inv(self.x), y.data, atol=atol, rtol=rtol)
Exemple #15
0
 def test_identity_cpu(self):
     eye = _make_eye(self.x.shape)
     x = chainer.Variable(self.x)
     y = functions.matmul(x, functions.inv(x))
     gradient_check.assert_allclose(y.data, eye,
                                    **self.check_forward_options)
Exemple #16
0
 def test_singular_cpu(self):
     x = chainer.Variable(numpy.zeros((2, 2), dtype=numpy.float32))
     with self.assertRaises(ValueError):
         functions.inv(x)
Exemple #17
0
 def test_identity_gpu(self):
     eye = cuda.to_gpu(_make_eye(self.x.shape))
     x = chainer.Variable(cuda.to_gpu(self.x))
     y = functions.matmul(x, functions.inv(x))
     testing.assert_allclose(y.data, eye, **self.check_forward_options)
Exemple #18
0
 def invW(self):
     return F.expand_dims(F.inv(self.W[..., 0]), axis=2)
Exemple #19
0
 def test_identity(self, backend_config):
     x, = self.generate_inputs()
     x = chainer.Variable(backend_config.get_array(x))
     y = functions.matmul(x, functions.inv(x))
     testing.assert_allclose(
         y.data, _make_eye(x.shape), **self.check_forward_options)
Exemple #20
0
 def check_forward(self, x_data):
     x = chainer.Variable(x_data)
     y = functions.inv(x)
     x1 = self.x.astype(self.check_forward_dtype, copy=False)
     testing.assert_allclose(_inv(x1), y.data, **self.check_forward_options)