def test_serialization(self):
     lin1 = links.SimplifiedDropconnect(None, self.out_size)
     x = chainer.Variable(self.x)
     # Must call the link to initialize weights.
     lin1(x)
     w1 = lin1.W.data
     fd, temp_file_path = tempfile.mkstemp()
     os.close(fd)
     npz.save_npz(temp_file_path, lin1)
     lin2 = links.SimplifiedDropconnect(None, self.out_size)
     npz.load_npz(temp_file_path, lin2)
     w2 = lin2.W.data
     self.assertEqual((w1 == w2).all(), True)
Esempio n. 2
0
    def setUp(self):
        in_size = numpy.prod(self.in_shape)

        self.link = links.SimplifiedDropconnect(
            in_size,
            self.out_size,
            initialW=chainer.initializers.Normal(1, self.W_dtype),
            initial_bias=chainer.initializers.Normal(1, self.x_dtype))
        self.link.cleargrads()

        x_shape = (4, ) + self.in_shape
        self.x = numpy.random.uniform(-1, 1, x_shape).astype(self.x_dtype)
        self.gy = numpy.random.uniform(-1, 1,
                                       (4, self.out_size)).astype(self.x_dtype)
        W = self.link.W.data
        b = self.link.b.data

        mask_shape = (4, ) + self.link.W.shape
        self.mask = gen_mask(self.ratio, mask_shape)

        W = (W * self.mask) * (1. / (1 - self.ratio))
        x = self.x.reshape(4, -1)

        # numpy 1.9 does not support matmul.
        # So we use numpy.einsum instead of numpy.matmul.
        self.y_expect = numpy.einsum('ijk,ikl->ijl', W, x[:, :, None]).reshape(
            4, -1) + b

        self.check_forward_options = {}
        self.check_backward_options = {}
        if self.x_dtype == numpy.float16:
            self.check_forward_options = {'atol': 1e-3, 'rtol': 1e-2}
            self.check_backward_options = {'atol': 1e-2, 'rtol': 5e-2}
        elif self.W_dtype == numpy.float16:
            self.check_backward_options = {'atol': 1e-3, 'rtol': 1e-2}
 def __init__(self, in_size: int):
     super(Model, self).__init__()
     with self.init_scope():
         self.l1 = L.SimplifiedDropconnect(None,
                                           64,
                                           nobias=True,
                                           ratio=0.95)
Esempio n. 4
0
    def setUp(self):
        in_size = numpy.prod(self.in_shape)

        self.link = links.SimplifiedDropconnect(
            in_size, self.out_size,
            initialW=chainer.initializers.Normal(1, numpy.float32),
            initial_bias=chainer.initializers.Normal(1, numpy.float32))
        self.link.cleargrads()

        x_shape = (4,) + self.in_shape
        self.x = numpy.ones(x_shape).astype(numpy.float32)
        self.W = self.link.W.data
        self.b = self.link.b.data
    def setUp(self):
        self.link = links.SimplifiedDropconnect(self.in_size_or_none,
                                                self.out_size)
        temp_x = numpy.random.uniform(-1, 1,
                                      (4, self.in_size)).astype(numpy.float32)
        self.link(chainer.Variable(temp_x))
        W = self.link.W.data
        W[...] = numpy.random.uniform(-1, 1, W.shape)
        b = self.link.b.data
        b[...] = numpy.random.uniform(-1, 1, b.shape)
        self.link.cleargrads()
        mask_shape = (4, self.out_size, self.in_size)
        self.mask = gen_mask(self.ratio, mask_shape)

        x_shape = (4, ) + self.in_shape
        self.x = numpy.random.uniform(-1, 1, x_shape).astype(numpy.float32)
        self.gy = numpy.random.uniform(-1, 1, (4, self.out_size)).astype(
            numpy.float32)
        W = (W * self.mask) * (1. / (1 - self.ratio))

        # numpy 1.9 does not support matmul.
        # So we use numpy.einsum instead of numpy.matmul.
        self.y_expect = numpy.einsum('ijk,ikl->ijl', W,
                                     self.x[:, :, None]).reshape(4, -1) + b
 def test_invalid_mask_size2(self):
     link = links.SimplifiedDropconnect(3, 2)
     x = numpy.random.uniform(-1, 1, (4, 3)).astype(numpy.float32)
     mask = numpy.random.uniform(-1, 1, (4, 3, 2)).astype(numpy.float32)
     with self.assertRaises(type_check.InvalidType):
         link(chainer.Variable(x), use_batchwise_mask=False, mask=mask)
 def test_invalid_input_size(self):
     link = links.SimplifiedDropconnect(3, 2)
     x = numpy.random.uniform(-1, 1, (4, 1, 2)).astype(numpy.float32)
     with self.assertRaises(type_check.InvalidType):
         link(chainer.Variable(x))
Esempio n. 8
0
 def setUp(self):
     self.link = links.SimplifiedDropconnect(3, 2)
     self.x = numpy.random.uniform(-1, 1, (4, 1, 2)).astype(numpy.float32)