Exemple #1
0
    def one_test_dot(self, size, n_template, template_size, stride, padding):
        rng = xp.random.RandomState(0)
        d = rng.randn(n_template, template_size)

        dmat = template._temp2mat(d, size, stride, padding, xp=xp)
        assert dmat.shape[1] == template._coef_size(d.shape[-1], size, stride,
                                                    padding)
        x = rng.randn(dmat.shape[0], dmat.shape[1])
        xmat = template._coef2mat(x, size, d.shape[-1], stride, padding, xp=xp)
        print(xmat.shape)
        assert allclose(xp.tensordot(x, dmat, 2),
                        xp.tensordot(d, xmat, 2))
        # matrix dot expression
        fit = xp.tensordot(x, dmat, 2)
        assert allclose(xp.dot(x.reshape(-1),
                               dmat.reshape(-1, dmat.shape[-1])), fit)
        assert allclose(xp.dot(d.reshape(-1),
                               xmat.reshape(-1, xmat.shape[-1])), fit)
        # with batch x
        batch_size = 4
        x = rng.randn(batch_size, dmat.shape[0], dmat.shape[1])
        xmat = template._coef2mat(x, size, d.shape[-1], stride, padding, xp=xp)

        assert allclose(xp.tensordot(x, dmat, 2),
                        xp.tensordot(d, xmat, ((0, 1), (-3, -2))))
Exemple #2
0
 def error(self, x, D, alpha, mask=None):
     mask = xp.ones(self.y.shape, dtype=float) if mask is None else mask
     alpha = alpha * xp.sum(mask, axis=-1, keepdims=True)
     D = normalize.l2(D, axis=-1, xp=xp)
     loss = xp.sum(0.5 / alpha *
                   xp.square(xp.abs(self.y - xp.tensordot(x, D, axes=1))) *
                   mask)
     return loss + xp.sum(xp.abs(x))
Exemple #3
0
    def test_same(self):
        # stride 1
        # n_template: 2, template_size: 3
        D = xp.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32)
        x = xp.array([[1, 2, 3, 4, 5, 6, 7, 8, 9],
                      [10, 20, 30, 40, 50, 60, 70, 80, 90]], dtype=np.float32)
        size, stride, padding = 7, 1, 'SAME'
        dmat = template._temp2mat(D, size, stride, padding, xp=xp)
        assert dmat.shape[1] == template._coef_size(D.shape[-1], size, stride,
                                                    padding)

        expected = xp.array([[[3, 0, 0, 0, 0, 0, 0],
                              [2, 3, 0, 0, 0, 0, 0],
                              [1, 2, 3, 0, 0, 0, 0],
                              [0, 1, 2, 3, 0, 0, 0],
                              [0, 0, 1, 2, 3, 0, 0],
                              [0, 0, 0, 1, 2, 3, 0],
                              [0, 0, 0, 0, 1, 2, 3],
                              [0, 0, 0, 0, 0, 1, 2],
                              [0, 0, 0, 0, 0, 0, 1]],
                             [[6, 0, 0, 0, 0, 0, 0],
                              [5, 6, 0, 0, 0, 0, 0],
                              [4, 5, 6, 0, 0, 0, 0],
                              [0, 4, 5, 6, 0, 0, 0],
                              [0, 0, 4, 5, 6, 0, 0],
                              [0, 0, 0, 4, 5, 6, 0],
                              [0, 0, 0, 0, 4, 5, 6],
                              [0, 0, 0, 0, 0, 4, 5],
                              [0, 0, 0, 0, 0, 0, 4]]], dtype=xp.float32)
        assert allclose(dmat, expected)

        xmat = template._coef2mat(x, size, D.shape[-1], stride, padding,
                                  xp=xp)
        expected = xp.array([[[3, 4, 5, 6, 7, 8, 9],
                              [2, 3, 4, 5, 6, 7, 8],
                              [1, 2, 3, 4, 5, 6, 7]],
                             [[30, 40, 50, 60, 70, 80, 90],
                              [20, 30, 40, 50, 60, 70, 80],
                              [10, 20, 30, 40, 50, 60, 70]]], dtype=xp.float32)
        assert allclose(xmat, expected)
        assert allclose(xp.tensordot(x, dmat, 2),
                        xp.tensordot(D, xmat, 2))

        # stride 2
        size, stride, padding = 7, 2, 'SAME'
        dmat = template._temp2mat(D, size, stride, padding, xp=xp)
        assert dmat.shape[1] == template._coef_size(D.shape[-1], size, stride,
                                                    padding)
        expected = xp.array([[[3, 0, 0, 0, 0, 0, 0],
                              [1, 2, 3, 0, 0, 0, 0],
                              [0, 0, 1, 2, 3, 0, 0],
                              [0, 0, 0, 0, 1, 2, 3],
                              [0, 0, 0, 0, 0, 0, 1]],
                             [[6, 0, 0, 0, 0, 0, 0],
                              [4, 5, 6, 0, 0, 0, 0],
                              [0, 0, 4, 5, 6, 0, 0],
                              [0, 0, 0, 0, 4, 5, 6],
                              [0, 0, 0, 0, 0, 0, 4]]], dtype=xp.float32)
        assert allclose(dmat, expected)

        x = xp.array([[1, 2, 3, 4, 5],
                      [10, 20, 30, 40, 50]], dtype=np.float32)
        xmat = template._coef2mat(x, size, D.shape[-1], stride, padding,
                                  xp=xp)
        expected = xp.array([[[2, 0, 3, 0, 4, 0, 5],
                              [0, 2, 0, 3, 0, 4, 0],
                              [1, 0, 2, 0, 3, 0, 4]],
                             [[20, 0, 30, 0, 40, 0, 50],
                              [0, 20, 0, 30, 0, 40, 0],
                              [10, 0, 20, 0, 30, 0, 40]]], dtype=xp.float32)
        assert allclose(xmat, expected)
        assert allclose(xp.tensordot(x, dmat, 2), xp.tensordot(D, xmat, 2))

        # stride 2
        size, stride, padding = 6, 2, 'SAME'
        dmat = template._temp2mat(D, size, stride, padding, xp=xp)
        print(template._coef_size(D.shape[-1], size, stride,
                                                    padding))
        assert dmat.shape[1] == template._coef_size(D.shape[-1], size, stride,
                                                    padding)
        expected = xp.array([[[2, 3, 0, 0, 0, 0],
                              [0, 1, 2, 3, 0, 0],
                              [0, 0, 0, 1, 2, 3],
                              [0, 0, 0, 0, 0, 1]],
                             [[5, 6, 0, 0, 0, 0],
                              [0, 4, 5, 6, 0, 0],
                              [0, 0, 0, 4, 5, 6],
                              [0, 0, 0, 0, 0, 4]]], dtype=xp.float32)
        assert allclose(dmat, expected)

        x = xp.array([[1, 2, 3, 4],
                      [10, 20, 30, 40]], dtype=np.float32)
        xmat = template._coef2mat(x, size, D.shape[-1], stride, padding, xp=xp)
        expected = xp.array([[[2, 0, 3, 0, 4, 0],
                              [0, 2, 0, 3, 0, 4],
                              [1, 0, 2, 0, 3, 0]],
                             [[20, 0, 30, 0, 40, 0],
                              [0, 20, 0, 30, 0, 40],
                              [10, 0, 20, 0, 30, 0]]], dtype=xp.float32)
        #assert allclose(xmat, expected)
        assert allclose(xp.tensordot(x, dmat, 2), xp.tensordot(D, xmat, 2))
Exemple #4
0
    def test_valid(self):
        # stride 1
        # n_template: 2, template_size: 3
        D = xp.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32)
        x = xp.array([[1, 2, 3, 4, 5], [10, 20, 30, 40, 50]], dtype=np.float32)
        size, stride, padding = 7, 1, 'VALID'
        dmat = template._temp2mat(D, size, stride, padding, xp=xp)
        assert dmat.shape[1] == template._coef_size(D.shape[-1], size, stride,
                                                    padding)
        expected = xp.array([[[1, 2, 3, 0, 0, 0, 0],
                              [0, 1, 2, 3, 0, 0, 0],
                              [0, 0, 1, 2, 3, 0, 0],
                              [0, 0, 0, 1, 2, 3, 0],
                              [0, 0, 0, 0, 1, 2, 3]],
                             [[4, 5, 6, 0, 0, 0, 0],
                              [0, 4, 5, 6, 0, 0, 0],
                              [0, 0, 4, 5, 6, 0, 0],
                              [0, 0, 0, 4, 5, 6, 0],
                              [0, 0, 0, 0, 4, 5, 6]]], dtype=xp.float32)
        assert allclose(dmat, expected)

        xmat = template._coef2mat(x, size, D.shape[-1], stride, padding,
                                  xp=xp)
        expected = xp.array([[[1, 2, 3, 4, 5, 0, 0],
                              [0, 1, 2, 3, 4, 5, 0],
                              [0, 0, 1, 2, 3, 4, 5]],
                             [[10, 20, 30, 40, 50, 0, 0],
                              [0, 10, 20, 30, 40, 50, 0],
                              [0, 0, 10, 20, 30, 40, 50]]], dtype=xp.float32)
        assert allclose(xmat, expected)
        assert allclose(xp.tensordot(x, dmat, 2), xp.tensordot(D, xmat, 2))

        # stride 2
        size, stride, padding = 7, 2, 'VALID'
        dmat = template._temp2mat(D, size, stride, padding, xp=xp)
        assert dmat.shape[1] == template._coef_size(D.shape[-1], size, stride,
                                                    padding)

        x = xp.array([[1, 2, 3], [10, 20, 30]], dtype=np.float32)
        xmat = template._coef2mat(x, size, D.shape[-1], stride, padding, xp=xp)
        expected = xp.array([[[1, 0, 2, 0, 3, 0, 0],
                              [0, 1, 0, 2, 0, 3, 0],
                              [0, 0, 1, 0, 2, 0, 3]],
                             [[10, 0, 20, 0, 30, 0, 0],
                              [0, 10, 0, 20, 0, 30, 0],
                              [0, 0, 10, 0, 20, 0, 30]]], dtype=xp.float32)
        assert allclose(xmat, expected)
        assert allclose(xp.tensordot(x, dmat, 2), xp.tensordot(D, xmat, 2))

        # stride 2
        size, stride, padding = 6, 2, 'VALID'
        dmat = template._temp2mat(D, size, stride, padding, xp=xp)
        assert dmat.shape[1] == template._coef_size(D.shape[-1], size, stride,
                                                    padding)
        expected = xp.array([[[0, 1, 2, 3, 0, 0],
                              [0, 0, 0, 1, 2, 3]],
                             [[0, 4, 5, 6, 0, 0],
                              [0, 0, 0, 4, 5, 6]]], dtype=xp.float32)
        assert allclose(dmat, expected)

        x = xp.array([[1, 2], [10, 20]], dtype=np.float32)
        xmat = template._coef2mat(x, size, D.shape[-1], stride, padding,
                                  xp=xp)
        expected = xp.array([[[0, 1, 0, 2, 0, 0],
                              [0, 0, 1, 0, 2, 0],
                              [0, 0, 0, 1, 0, 2]],
                             [[0, 10, 0, 20, 0, 0],
                              [0, 0, 10, 0, 20, 0],
                              [0, 0, 0, 10, 0, 20]]], dtype=xp.float32)
        assert allclose(xmat, expected)
        assert allclose(xp.tensordot(x, dmat, 2), xp.tensordot(D, xmat, 2))