def gen(output, recipe, bwd=True, use_gpu=False): from chainer_compiler.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)
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)
# 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) # ====================================== from chainer_compiler 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')
return y1 class PadSequenceLength(chainer.Chain): def forward(self, xs): y1 = F.pad_sequence(xs, length=20) return y1 class PadSequencePadding(chainer.Chain): def forward(self, xs): y1 = F.pad_sequence(xs, padding=-1) return y1 # ====================================== from chainer_compiler import ch2o if __name__ == '__main__': import numpy as np np.random.seed(314) ls = np.random.randint(0, 10, size=5) x = [np.random.rand(i).astype(np.float32) for i in ls] ch2o.generate_testcase(PadSequence, [x]) ch2o.generate_testcase(PadSequenceLength, [x], subname='length') ch2o.generate_testcase(PadSequencePadding, [x], subname='padding')
hy, cs, ys = self.l1(None, None, x) return hy, cs, ys # return hy,cs # ====================================== from chainer_compiler import ch2o if __name__ == '__main__': import numpy as np np.random.seed(314) n_batch = 7 n_layer = 3 n_in = 8 n_hidden = 5 n_maxlen = 10 # n_batch = 2 # n_layer = 2 # n_in = 2 # n_hidden = 4 model = A(n_layer, n_in, n_hidden) # ilens = np.random.randint(1,n_maxlen,size=n_batch) ilens = [t for t in range(n_batch)] xs = [np.random.rand(i + 4, n_in).astype(np.float32) for i in ilens] ch2o.generate_testcase(model, [xs])
""" y5 = [] for j in ps: for i in qs: y5.append(p) return y5 """ y1 = [x[3:5] for x in xs] y2 = [x[ps[0]:ps[0] + 3] for x in xs] y3 = [x[p:p + 4] for x in xs] y4 = [xs[i][:i] for i in range(p)] y5 = [3 for x in xs] return y1, y2, y3, y4, y5 # ====================================== from chainer_compiler import ch2o if __name__ == '__main__': import numpy as np np.random.seed(314) model = A() v = np.random.rand(10, 20).astype(np.float32) ps = np.array([3, 4]) qs = np.array([1, 2, 3, 4, 5]) p = np.int64(5) ch2o.generate_testcase(model, [v, ps, p, qs])
def forward(self, xs, ilens): xs, ilens = self.vgg(xs, ilens) return F.pad_sequence(xs) from chainer_compiler import ch2o if __name__ == '__main__': import numpy as np np.random.seed(314) idim = 5 elayers = 2 cdim = 3 hdim = 7 batch_size = 3 sequence_length = 4 num_vocabs = 10 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)) ch2o.generate_testcase(lambda: VGG2L(1), [xs, ilens]) ch2o.generate_testcase(lambda: VGG2LBackprop(1), [xs, ilens], backprop=True)
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) # ====================================== from chainer_compiler 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')
# coding: utf-8 import numpy as np import chainer import chainer.functions as F class Full(chainer.Chain): def forward(self): y1 = np.full((3, 4), 42) return y1 class FullDtype(chainer.Chain): def forward(self): y1 = np.full((3, 4), 42, dtype=np.float32) return y1 # ====================================== from chainer_compiler import ch2o if __name__ == '__main__': ch2o.generate_testcase(Full, []) ch2o.generate_testcase(FullDtype, [], subname='dtype')
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) # ====================================== from chainer_compiler 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])
ys, ilens = sequence_utils.gen_random_sequence(batch_size, sequence_length, odim) 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) def model_fn(): # att = AttDot(eprojs, dunits, att_dim) # dec = Decoder(eprojs, odim, dlayers, dunits, sos, eos, att) dec = Decoder(eprojs, odim, dlayers, dunits, sos, eos, att_dim) return dec gen_test(model_fn) ch2o.generate_testcase(model_fn, [hs, ys], backprop=True) def model_fn(): dec = Decoder(eprojs, odim, dlayers, dunits, sos, eos, att_dim, aconv_chans, aconv_filts) return dec gen_test(model_fn, subname='attloc') ch2o.generate_testcase(model_fn, [hs, ys], subname='attloc', backprop=True)
class BroadcastTo(chainer.Chain): def forward(self, x): y1 = F.broadcast_to(x, (2, 6, 4, 3)) return y1 class BroadcastToBackprop(chainer.Chain): def __init__(self): super(BroadcastToBackprop, self).__init__() with self.init_scope(): self.l1 = L.Linear(None, 5) def forward(self, x): x = self.l1(x) x = F.reshape(x, (6, 5, 1)) y = F.broadcast_to(x, (2, 6, 5, 3)) return y # ====================================== from chainer_compiler import ch2o import numpy as np if __name__ == '__main__': x = np.random.rand(6, 4, 1).astype(np.float32) - 0.5 ch2o.generate_testcase(BroadcastTo, [x]) x = np.random.rand(6, 3).astype(np.float32) - 0.5 ch2o.generate_testcase(BroadcastToBackprop, [x], backprop=True)
roi_indices = data["roi_indices"] if args.gpu: hs = [chainer.cuda.to_gpu(h) for h in hs] rois = chainer.cuda.to_gpu(rois) roi_indices = chainer.cuda.to_gpu(roi_indices) assert(len(hs) == 5) assert(len(rois) == 5) assert(len(roi_indices) == 5) for i, FPN_ROIAlign2D in enumerate((FPN_ROIAlign2D_1st_scale, FPN_ROIAlign2D_2nd_scale, FPN_ROIAlign2D_3rd_scale, FPN_ROIAlign2D_4th_scale, FPN_ROIAlign2D_5th_scale)): ch2o.generate_testcase( FPN_ROIAlign2D(F.roi_average_align_2d), [hs[i], rois[i], roi_indices[i]], output_dir=os.path.join(args.out, "fpn_roi_align_2d_pyramid{}_scale".format(i)), use_gpu=args.gpu) else: 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],
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)
if __name__ == '__main__': import numpy as np np.random.seed(314) batch_size = 3 sequence_length = 4 num_vocabs = 10 num_hidden = 5 model_fn = lambda: MyLSTM(num_hidden, batch_size, sequence_length) labels, lengths = sequence_utils.gen_random_sequence( batch_size, sequence_length, num_vocabs) xs = [] for l in lengths: xs.append(np.random.rand(l, num_hidden).astype(dtype=np.float32)) h = np.zeros((batch_size, num_hidden), dtype=np.float32) c = np.zeros((batch_size, num_hidden), dtype=np.float32) mask = (np.expand_dims(np.arange(sequence_length), 0) < np.expand_dims( lengths, 1)).astype(np.float32) args = [xs, h, c, mask] #print(model_fn()(*args)) #print(run_with_n_step_lstm(xs, h, c, model.l.W, model.l.b)) ch2o.generate_testcase(model_fn, args)
if __name__ == '__main__': import numpy as np np.random.seed(43) batch_size = 3 in_size = 7 out_size = 4 def model_fn(): lstm = StatelessLSTM(in_size, out_size) return lstm c = np.random.rand(batch_size, out_size).astype(np.float32) h = np.random.rand(batch_size, out_size).astype(np.float32) x = np.random.rand(batch_size, in_size).astype(np.float32) model = model_fn() # Check if our modification is valid. expected = model.original(c, h, x) actual = model.forward(c, h, x) for e, a in zip(expected, actual): assert np.allclose(e.array, a.array) ch2o.generate_testcase(model_fn, [c, h, x]) def model_fn(): lstm = StatelessLSTMBackprop(in_size, out_size) return lstm ch2o.generate_testcase(model_fn, [c, h, x], backprop=True)
# 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) from chainer_compiler import ch2o ch2o.generate_testcase(model, [v, w]) ch2o.generate_testcase(model, [v, w], backprop=True)
# 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]) # ====================================== from chainer_compiler 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')
if y is None: y = 42 y += i return y class LazySelfInit(chainer.Chain): def __init__(self): super(LazySelfInit, self).__init__() self.y = None def forward(self, x): for i in range(x): if self.y is None: self.y = 42 self.y += i return self.y # ====================================== from chainer_compiler import ch2o import numpy as np if __name__ == '__main__': ch2o.generate_testcase(LazyInit(), [5], subname='lazy_init') ch2o.generate_testcase(LazySelfInit, [5], subname='lazy_self_init')
def forward(self, x): y1 = self.l1(x) y2 = self.l2(x) return (y1, y2) class B(chainer.Chain): def __init__(self, n_out): super(B, self).__init__() with self.init_scope(): self.l1 = L.Linear(None, n_out) def forward(self, x): return self.l1(x) # ====================================== from chainer_compiler import ch2o if __name__ == '__main__': import numpy as np np.random.seed(314) x = np.random.rand(5, 7).astype(np.float32) ch2o.generate_testcase(A(3), [x]) x = np.random.rand(5, 7).astype(np.float32) ch2o.generate_testcase(B(3), [x], backprop=True)
self.l = L.Linear(None, 5) def forward(self, x): x = self.l(x) x = F.reshape(x, (3, 2, 5)) x = F.sum(x, axis=self.axis, keepdims=self.keepdims) return x # ====================================== from chainer_compiler import ch2o if __name__ == '__main__': np.random.seed(314) a = np.random.rand(6, 2, 3).astype(np.float32) ch2o.generate_testcase(Sum(), [a]) ch2o.generate_testcase(SumKeepdims(), [a], subname='keepdims') ch2o.generate_testcase(SumTupleAxis(), [a], subname='tuple_axis') ch2o.generate_testcase(SumAllAxis(), [a], subname='all_axis') for axis in [0, 1, 2, (0, 2), (1, 2), None]: for keepdims in [False, True]: name = 'axis%s_kd%s' % (axis, keepdims) ch2o.generate_testcase(lambda: SumBackprop(axis, keepdims), [a], subname=name, backprop=True)
def forward(self, x, cond): if cond is not None: x = self.l(x) return x # ====================================== from chainer_compiler import ch2o import numpy as np if __name__ == '__main__': ch2o.generate_testcase(StaticCondTrue(), [42]) ch2o.generate_testcase(StaticCondFalse(), [42], subname='static_false') ch2o.generate_testcase(StaticCondTrueNoElse(), [42], subname='static_true_no_else') ch2o.generate_testcase(StaticCondFalseNoElse(), [42], subname='static_false_no_else') ch2o.generate_testcase(DynamicCond(), [42, False], subname='false') ch2o.generate_testcase(DynamicCond(), [42, True], subname='true') ch2o.generate_testcase(DynamicCondNoElse(), [42, False], subname='false_no_else')
# 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 # ====================================== from chainer_compiler 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')
# 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) # ====================================== from chainer_compiler import ch2o if __name__ == '__main__': ch2o.generate_testcase(A(), [np.array(42)])
return x # ====================================== from chainer_compiler import ch2o import numpy as np if __name__ == '__main__': model = A() v = np.random.rand(10).astype(np.float32) p = np.int64(5) ch2o.generate_testcase(model, [v, p]) model = B() length = 4 xs = [] for i in range(length): xs.append(np.random.rand(length, 5).astype(dtype=np.float32)) args = [xs, length] ch2o.generate_testcase(model, args, subname='closure_bug') ch2o.generate_testcase(C(), [], subname='multi_ref') ch2o.generate_testcase(D(), [], subname='leak') ch2o.generate_testcase(UpdateSelf(), [42], subname='update_self')
F.relu(self.conv2(h))), 3, stride=2) h = F.relu(self.conv3(h)) h = F.relu(self.conv4(h)) h = F.max_pooling_2d(F.relu(self.conv5(h)), 3, stride=2) h = F.dropout(F.relu(self.fc6(h))) h = F.dropout(F.relu(self.fc7(h))) h = self.fc8(h) loss = F.softmax_cross_entropy(h, t) #loss = h # chainer.report({'loss': loss, 'accuracy': F.accuracy(h, t)}, self) return loss # from https://github.com/chainer/chainer/blob/master/examples/imagenet/alex.py if __name__ == '__main__': np.random.seed(314) model = Alex(shrink_ratio=23) # batch * channel * H * W v = np.random.rand(2, 3, 227, 227).astype(np.float32) w = np.random.randint(1000, size=2) from chainer_compiler import ch2o ch2o.generate_testcase(model, [v, w])
# coding: utf-8 import chainer # Network definition class A(chainer.Chain): def __init__(self): super(A, self).__init__() def forward(self, x): return x # ====================================== from chainer_compiler 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)
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 # ====================================== from chainer_compiler 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')
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 # ====================================== from chainer_compiler 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')