Esempio n. 1
0
def gen(output, recipe, bwd=True, use_gpu=False):
    from ch2o import test_args
    test_args.get_test_args([output, '--allow-unused-params'])

    (idim, odim, args), (xs, ilens, ys) = recipe
    ch2o.generate_testcase(lambda: E2E(idim, odim, args),
                           [xs, ilens, ys], backprop=bwd, use_gpu=use_gpu)
    def gen_test(model_fn, subname=None):
        model = model_fn()
        # Check if our modification is valid.
        expected, _ = model.original(hs, ys)
        actual = model.forward(hs, ys)
        assert np.allclose(expected.array, actual.array)

        ch2o.generate_testcase(model_fn, [hs, ys], subname=subname)
Esempio n. 3
0
def gen_test():
    (idim, odim, args), (xs, ilens, ys) = test_recipe()

    def gen_test(model_fn, subname=None):
        model = model_fn()
        # Check if our modification is valid.
        expected = model.original(xs, ilens, ys)
        actual = model.forward(xs, ilens, ys)
        assert np.allclose(expected.array, actual.array)

        ch2o.generate_testcase(model_fn, [xs, ilens, ys], subname=subname)

    gen_test(lambda: E2E(idim, odim, args))

    ch2o.generate_testcase(lambda: E2E(idim, odim, args, nobias=True),
                           [xs, ilens, ys], backprop=True)
Esempio n. 4
0
            # the size of the inputs to each layer will be inferred
            self.l1 = L.Linear(None, n_units)  # n_in -> n_units
            self.l2 = L.Linear(None, n_units)  # n_units -> n_units
            self.l3 = L.Linear(None, n_out)  # n_units -> n_out

    def forward(self, x, t):
        h1 = F.relu(self.l1(x))
        h2 = F.relu(self.l2(h1))
        h3 = self.l3(h2)
        loss = F.softmax_cross_entropy(h3, t)
        # loss = h3
        return loss


# ======================================from MLP

if __name__ == '__main__':
    import numpy as np
    np.random.seed(314)

    out_n = 4
    batch_size = 100
    model = MLP(8, out_n)

    v = np.random.rand(batch_size, 3).astype(np.float32)
    w = np.random.randint(out_n, size=batch_size)
    import ch2o
    ch2o.generate_testcase(model, [v, w])

    ch2o.generate_testcase(model, [v, w], backprop=True)
Esempio n. 5
0
# coding: utf-8

import chainer


class A(chainer.Chain):
    def __init__(self):
        super(A, self).__init__()

    def forward(self, x, y):
        z = (0.5 * 0.2) + 0.3 * x + y
        return z


import ch2o
import numpy as np
if __name__ == '__main__':
    model = A()

    v = np.random.rand(3, 5).astype(np.float32)
    w = np.random.rand(3, 5).astype(np.float32)

    ch2o.generate_testcase(model, [v, w])
# coding: utf-8

import numpy as np
import chainer
import chainer.functions as F


class A(chainer.Chain):
    def __init__(self):
        super(A, self).__init__()

    def forward(self, x):
        return chainer.Variable(x)


# ======================================

import ch2o

if __name__ == '__main__':
    ch2o.generate_testcase(A(), [np.array(42)])
Esempio n. 7
0
# coding: utf-8

import chainer
import chainer.functions as F


class ResizeImages(chainer.Chain):
    def forward(self, x):
        y1 = F.resize_images(x, (257, 513))
        return y1


# ======================================

import ch2o
import numpy as np

if __name__ == '__main__':
    x = np.random.rand(1, 1, 129, 257).astype(np.float32)
    # x = np.random.rand(1, 256, 129, 257).astype(np.float32)
    ch2o.generate_testcase(ResizeImages, [x])
Esempio n. 8
0
# coding: utf-8

import numpy as np
import chainer
import chainer.functions as F


class Len(chainer.Chain):
    def forward(self, x):
        return len(x)


class LenList(chainer.Chain):
    def forward(self, xs):
        return len(xs)


# ======================================

import ch2o

if __name__ == '__main__':
    np.random.seed(314)

    ch2o.generate_testcase(Len(), [np.random.rand(3, 5, 4)])

    ch2o.generate_testcase(LenList(), [[np.array(x) for x in [3, 5, 4]]],
                           subname='list')
Esempio n. 9
0
class ListSlice(chainer.Chain):
    def forward(self, x):
        # Use `shape` to make a sequence.
        xs = x.shape
        y1 = np.array(xs[2])
        y2 = np.array(xs[-2])
        y3 = np.array(xs[:2])
        y4 = np.array(xs[1:3])
        return y1, y2, y3, y4


# ======================================

import ch2o
import numpy as np

if __name__ == '__main__':
    n_maxlen = 10

    model = A()

    u = np.random.rand(n_maxlen + 6).astype(np.float32)
    v = np.random.rand(n_maxlen + 6, n_maxlen + 6).astype(np.float32)
    w = np.random.randint(0, n_maxlen, size=2)

    ch2o.generate_testcase(model, [u, v, w])

    x = np.random.rand(4, 3, 5, 7)
    ch2o.generate_testcase(ListSlice(), [x], subname='list')
Esempio n. 10
0
# coding: utf-8

import numpy as np
import chainer
import chainer.functions as F


class Softmax(chainer.Chain):
    def forward(self, x):
        return F.softmax(x)


class SoftmaxAxis(chainer.Chain):
    def forward(self, x):
        return F.softmax(x, axis=2)


# ======================================

import ch2o

if __name__ == '__main__':
    np.random.seed(314)
    a = np.random.rand(3, 5, 4).astype(np.float32)

    ch2o.generate_testcase(Softmax(), [a])

    ch2o.generate_testcase(SoftmaxAxis(), [a], subname='axis')
Esempio n. 11
0
        self.spatial_scale = spatial_scale
        self.sampling_ratio = sampling_ratio

    def forward(self, x, rois, roi_indices):
        return self.fn(x, rois, roi_indices, 7, 1.2, 2)


# ======================================

import ch2o

if __name__ == '__main__':
    import numpy as np

    x = np.arange(2 * 3 * 5 * 5).reshape((2, 3, 5, 5)).astype(np.float32)
    rois = np.array([[0, 1, 3, 4], [1, 0.3, 4, 2.6]]).astype(np.float32)
    roi_indices = np.array([0, 1]).astype(np.int32)

    ch2o.generate_testcase(ROIPool2D(F.roi_max_pooling_2d, 7, 1.2),
                           [x, rois, roi_indices],
                           subname='max_pool')
    ch2o.generate_testcase(ROIPool2D(F.roi_average_pooling_2d, 7, 1.2),
                           [x, rois, roi_indices],
                           subname='avg_pool')
    ch2o.generate_testcase(ROIAlign2D(F.roi_max_align_2d, 7, 1.2, 2),
                           [x, rois, roi_indices],
                           subname='max_align')
    ch2o.generate_testcase(ROIAlign2D(F.roi_average_align_2d, 7, 1.2, 3),
                           [x, rois, roi_indices],
                           subname='avg_align')
Esempio n. 12
0
import chainer
import chainer.functions as F


class Unpooling2D(chainer.Chain):
    def forward(self, x):
        y = F.unpooling_2d(x, 2, cover_all=False)
        return y


class Unpooling2D_3x4(chainer.Chain):
    def forward(self, x):
        y = F.unpooling_2d(x, (3, 4), cover_all=False)
        return y


# ======================================

import ch2o
import numpy as np

if __name__ == '__main__':
    x = np.random.rand(2, 3, 11, 7).astype(np.float32)
    ch2o.generate_testcase(Unpooling2D, [x])
    ch2o.generate_testcase(Unpooling2D_3x4, [x], subname='3x4')

    # The largest input in FPN.
    x = np.random.rand(1, 256, 100, 100).astype(np.float32)
    ch2o.generate_testcase(Unpooling2D, [x], subname='large')
Esempio n. 13
0
    def forward(self, x):
        x = self.l(x) * self.p
        return x


class A(chainer.Chain):
    def __init__(self):
        super(A, self).__init__()
        with self.init_scope():
            self.l0 = L.Linear(3)
            self.l1 = B(5, 3.1)
            self.l2 = B(4, 4.2)

    def forward(self, x):
        x = self.l0(x)
        x = self.l1(x) + self.l2.p
        x = self.l2(x) + self.l1.p
        return x


# ======================================

import ch2o

if __name__ == '__main__':
    import numpy as np
    np.random.seed(314)

    v = np.random.rand(3, 7).astype(np.float32)
    ch2o.generate_testcase(A, [v])
    def g(self, x):
        return self.a + x


def h(x, y):
    return x + y


class A(chainer.Chain):

    def __init__(self):
        super(A, self).__init__()

    def forward(self, x, y, z):
        p = F(x).g(y)
        return h(p, z)


# ======================================

import ch2o
import numpy as np

if __name__ == '__main__':
    model = A()

    a = np.random.rand(3, 4).astype(np.float32)
    b = np.random.rand(3, 4).astype(np.float32)
    c = np.random.rand(3, 4).astype(np.float32)
    ch2o.generate_testcase(model, [a, b, c])
Esempio n. 15
0
class MeanKeepdims(chainer.Chain):
    def forward(self, x):
        return F.mean(x, axis=0, keepdims=True)


class MeanTupleAxis(chainer.Chain):
    def forward(self, x):
        return F.mean(x, axis=(1, 2))


class MeanAllAxis(chainer.Chain):
    def forward(self, x):
        return F.mean(x)


# ======================================

import ch2o

if __name__ == '__main__':
    np.random.seed(314)
    a = np.random.rand(3, 5, 4).astype(np.float32)

    ch2o.generate_testcase(Mean(), [a])

    ch2o.generate_testcase(MeanKeepdims(), [a], subname='keepdims')

    ch2o.generate_testcase(MeanTupleAxis(), [a], subname='tuple_axis')

    ch2o.generate_testcase(MeanAllAxis(), [a], subname='all_axis')
Esempio n. 16
0
import chainer
import chainer.functions as F


class Shape(chainer.Chain):
    def forward(self, x):
        y1 = x.shape
        return list(y1)


class ShapeConcat(chainer.Chain):
    def forward(self, x):
        y1 = x.shape
        return np.array(y1 + (42, ))


# ======================================

import ch2o

if __name__ == '__main__':
    import numpy as np
    np.random.seed(314)

    x = np.random.rand(12, 6, 4).astype(np.float32)

    ch2o.generate_testcase(Shape(), [x])

    ch2o.generate_testcase(ShapeConcat(), [x], subname='concat')
Esempio n. 17
0
# coding: utf-8

import numpy as np
import chainer
import chainer.functions as F


class A(chainer.Chain):
    def forward(self):
        y1 = np.zeros((3, 4), dtype=np.float32)
        return y1


# ======================================

import ch2o

if __name__ == '__main__':
    ch2o.generate_testcase(A(), [])
Esempio n. 18
0
    def __init__(self, num_hidden):
        super(LinkInFor, self).__init__()
        with self.init_scope():
            self.l = L.Linear(num_hidden, num_hidden)

    def forward(self, x, h, indices):
        for i in indices:
            h = h + self.l(x[:, i])
        return h


import ch2o

if __name__ == '__main__':
    import numpy as np
    np.random.seed(314)

    batch_size = 3
    num_hidden = 5
    sequence_length = 4

    model = LinkInFor(num_hidden)

    x = np.random.rand(batch_size, sequence_length,
                       num_hidden).astype(np.float32)
    h = np.random.rand(batch_size, num_hidden).astype(np.float32)

    args = [x, h, np.arange(sequence_length)]
    dprint(model(*args))
    ch2o.generate_testcase(model, args)
Esempio n. 19
0
# coding: utf-8

import chainer

# Network definition


class A(chainer.Chain):

    def __init__(self):
        super(A, self).__init__()

    def forward(self, x):
        return x

# ======================================


import ch2o

if __name__ == '__main__':
    import numpy as np
    np.random.seed(314)

    model = A()

    x = np.random.rand(5, 7).astype(np.float32)
    x = [x]
    ch2o.generate_testcase(model, x)
Esempio n. 20
0
# coding: utf-8

import chainer
import chainer.functions as F


class Size(chainer.Chain):
    def forward(self, x):
        y1 = x.size
        return y1


# ======================================

import ch2o

if __name__ == '__main__':
    import numpy as np
    np.random.seed(314)

    x = np.random.rand(12, 6, 4).astype(np.float32)

    ch2o.generate_testcase(Size(), [x])
# coding: utf-8

import chainer
import chainer.functions as F


class A(chainer.Chain):

    def __init__(self):
        super(A, self).__init__()

    def forward(self, x):
        # TODO(satos) テストケース増やす
        # y1 = F.max_pooling_2d(x, (1, 3), stride=(1, 4), pad=(0, 1))
        y1 = F.average_pooling_2d(x, (1, 3), stride=(1, 4))
        return y1


# ======================================

import ch2o
import numpy as np

if __name__ == '__main__':

    model = A()

    x = v = np.random.rand(2, 3, 1, 13).astype(np.float32)
    ch2o.generate_testcase(model, [x])
Esempio n. 22
0
# coding: utf-8

import chainer
import chainer.functions as F


class ConcatTuple(chainer.Chain):
    def forward(self, x, y):
        return F.concat((x, y))


class ConcatList(chainer.Chain):
    def forward(self, x, y):
        return F.concat([x, y])


# ======================================

import ch2o
import numpy as np

if __name__ == '__main__':
    v = np.random.rand(7, 4, 2).astype(np.float32)
    w = np.random.rand(7, 3, 2).astype(np.float32)

    ch2o.generate_testcase(ConcatTuple, [v, w])

    ch2o.generate_testcase(ConcatList, [v, w], subname='list')
Esempio n. 23
0
class SeparateAxis0(chainer.Chain):
    def forward(self, x):
        return list(F.separate(x, axis=0))


class SeparateAxis1(chainer.Chain):
    def forward(self, x):
        return list(F.separate(x, axis=1))


class SeparateAxis2(chainer.Chain):
    def forward(self, x):
        return list(F.separate(x, axis=2))


# ======================================

import ch2o

if __name__ == '__main__':
    import numpy as np
    np.random.seed(12)

    x = np.random.rand(2, 3, 4).astype(np.float32)

    ch2o.generate_testcase(Separate, [x])
    ch2o.generate_testcase(SeparateAxis0, [x], subname='axis_0')
    ch2o.generate_testcase(SeparateAxis1, [x], subname='axis_1')
    ch2o.generate_testcase(SeparateAxis2, [x], subname='axis_2')
    np.random.seed(314)

    idim = 5
    elayers = 2
    cdim = 3
    hdim = 7
    batch_size = 3
    sequence_length = 4
    num_vocabs = 10

    model = BLSTM(idim, elayers, cdim, hdim, 0)
    labels, ilens = sequence_utils.gen_random_sequence(batch_size,
                                                       sequence_length,
                                                       num_vocabs)
    xs = []
    for l in ilens:
        xs.append(np.random.rand(l, idim).astype(dtype=np.float32))

    # Check if our modification is valid.
    expected = model.original(xs, ilens)
    actual = model.forward(xs, ilens)
    for e, a in zip(expected[0], actual[0]):
        assert np.allclose(e.array, a.array)
    assert np.allclose(expected[1], actual[1])

    ch2o.generate_testcase(model, [xs, ilens])

    ch2o.generate_testcase(BLSTMBackprop(idim, elayers, cdim, hdim, 0),
                           [xs, ilens],
                           backprop=True)
Esempio n. 25
0
    def forward(self, x):
        y1 = F.average_pooling_2d(x, (1, 3), stride=(1, 4))
        return y1


class AvgPoolPad(chainer.Chain):
    def forward(self, x):
        y1 = F.average_pooling_2d(x, (3, 4), stride=1, pad=(2, 3))
        return y1


class AvgPoolNoStride(chainer.Chain):
    def forward(self, x):
        y1 = F.average_pooling_2d(x, (3, 4))
        return y1


# ======================================

import ch2o
import numpy as np

if __name__ == '__main__':

    x = np.random.rand(2, 3, 19, 13).astype(np.float32)
    ch2o.generate_testcase(AvgPool, [x])

    ch2o.generate_testcase(AvgPoolPad, [x], subname='pad')

    ch2o.generate_testcase(AvgPoolNoStride, [x], subname='no_stride')
Esempio n. 26
0
    def forward(self, x, y):
        y1 = F.stack((x, y), axis=0)
        return y1


class StackAxis1(chainer.Chain):
    def forward(self, x, y):
        y1 = F.stack((x, y), axis=1)
        return y1


class StackAxis2(chainer.Chain):
    def forward(self, x, y):
        y1 = F.stack((x, y), axis=2)
        return y1


# ======================================

import ch2o
import numpy as np

if __name__ == '__main__':
    v = np.random.rand(5, 4, 2).astype(np.float32)
    w = np.random.rand(5, 4, 2).astype(np.float32)

    ch2o.generate_testcase(Stack, [v, w])
    ch2o.generate_testcase(StackAxis0, [v, w], subname='axis0')
    ch2o.generate_testcase(StackAxis1, [v, w], subname='axis1')
    ch2o.generate_testcase(StackAxis2, [v, w], subname='axis2')
Esempio n. 27
0
import chainer
import chainer.functions as F


class Squeeze(chainer.Chain):
    def forward(self, x):
        return F.squeeze(x, 1)


class SqueezeAxes(chainer.Chain):
    def forward(self, x):
        return F.squeeze(x, axis=(1, 3))


class SqueezeNoAxes(chainer.Chain):
    def forward(self, x):
        return F.squeeze(x)


# ======================================

import ch2o
import numpy as np

if __name__ == '__main__':
    x = np.random.rand(3, 1, 4, 1, 5, 1).astype(np.float32)

    ch2o.generate_testcase(Squeeze(), [x])
    ch2o.generate_testcase(SqueezeAxes(), [x], subname='axes')
    ch2o.generate_testcase(SqueezeNoAxes(), [x], subname='noaxes')
Esempio n. 28
0
    def forward(self, xs):
        return xs[:, :]


# ======================================


import ch2o
import numpy as np


if __name__ == '__main__':
    n_maxlen = 10

    model = A()

    u = np.random.rand(n_maxlen+6).astype(np.float32)
    v = np.random.rand(n_maxlen+6, n_maxlen+6).astype(np.float32)
    w = np.random.randint(0, n_maxlen, size=2)

    ch2o.generate_testcase(model, [u, v, w])

    x = np.random.rand(7, 5, 3, 4)
    ch2o.generate_testcase(ListSlice(), [x], subname='list')

    ch2o.generate_testcase(SliceStep(), [x], subname='step')
    ch2o.generate_testcase(SliceStepSecond(), [x], subname='step_second')

    ch2o.generate_testcase(SliceAll(), [x], subname='all')
    ch2o.generate_testcase(SliceAllSecond(), [x], subname='all_second')
Esempio n. 29
0
# coding: utf-8

import chainer
import chainer.functions as F


class A(chainer.Chain):
    def forward(self, x, y):
        y1 = F.hstack((x, y))
        return y1


class B(chainer.Chain):
    def forward(self, xs):
        y1 = F.hstack(xs)
        return y1


# ======================================

import ch2o
import numpy as np

if __name__ == '__main__':
    v = np.random.rand(5, 3, 2).astype(np.float32)
    w = np.random.rand(5, 4, 2).astype(np.float32)

    ch2o.generate_testcase(A(), [v, w])
    ch2o.generate_testcase(B(), [[v, w]], subname='list')
    import numpy as np
    np.random.seed(314)

    eprojs = 3
    dunits = 4
    att_dim = 5
    batch_size = 3
    sequence_length = 4
    num_vocabs = 10

    model_fn = lambda: AttDot(eprojs, dunits, att_dim)
    labels, ilens = sequence_utils.gen_random_sequence(batch_size,
                                                       sequence_length,
                                                       num_vocabs)
    xs = []
    for l in ilens:
        xs.append(np.random.rand(l, eprojs).astype(np.float32))

    # Check if our modification is valid.
    expected = model_fn().original(xs, None, None)
    actual = model_fn().forward(xs, None, None)
    for e, a in zip(expected, actual):
        assert np.allclose(e.array, a.array)

    ch2o.generate_testcase(model_fn, [xs, None, None])

    z = np.random.rand(batch_size, dunits).astype(np.float32)
    ch2o.generate_testcase(lambda: AttDotBackprop(eprojs, dunits, att_dim),
                           [xs, z, None],
                           backprop=True)