Example #1
0
    def test_addgrads(self):
        l1 = chainer.Link()
        with l1.init_scope():
            l1.x = chainer.Parameter(shape=(2, 3))
        l2 = chainer.Link()
        with l2.init_scope():
            l2.x = chainer.Parameter(shape=2)
        l3 = chainer.Link()
        with l3.init_scope():
            l3.x = chainer.Parameter(shape=3)
        c1 = chainer.Chain()
        with c1.init_scope():
            c1.l1 = l1
            c1.l2 = l2
        c2 = chainer.Chain()
        with c2.init_scope():
            c2.c1 = c1
            c2.l3 = l3
        l1.x.grad.fill(1)
        l2.x.grad.fill(2)
        l3.x.grad.fill(3)

        self.l1.x.grad.fill(-1)
        self.l2.x.grad.fill(-2)
        self.l3.cleargrads()

        self.c2.addgrads(c2)
        numpy.testing.assert_array_equal(self.l1.x.grad, numpy.zeros((2, 3)))
        numpy.testing.assert_array_equal(self.l2.x.grad, numpy.zeros(2))
        numpy.testing.assert_array_equal(self.l3.x.grad, numpy.full(3, 3.))
Example #2
0
def test_namedpersistent():
    # This test case is adopted from
    # https://github.com/chainer/chainer/pull/6788

    l1 = chainer.Link()
    with l1.init_scope():
        l1.x = chainer.Parameter(shape=(2, 3))

    l2 = chainer.Link()
    with l2.init_scope():
        l2.x = chainer.Parameter(shape=2)
    l2.add_persistent('l2_a', numpy.array([1, 2, 3], dtype=numpy.float32))

    l3 = chainer.Link()
    with l3.init_scope():
        l3.x = chainer.Parameter()
    l3.add_persistent('l3_a', numpy.array([1, 2, 3], dtype=numpy.float32))

    c1 = chainer.Chain()
    with c1.init_scope():
        c1.l1 = l1
    c1.add_link('l2', l2)
    c1.add_persistent('c1_a', numpy.array([1, 2, 3], dtype=numpy.float32))

    c2 = chainer.Chain()
    with c2.init_scope():
        c2.c1 = c1
        c2.l3 = l3
    c2.add_persistent('c2_a', numpy.array([1, 2, 3], dtype=numpy.float32))
    namedpersistent = list(chainerrl.misc.namedpersistent(c2))
    assert ([(name, id(p)) for name, p in namedpersistent
             ] == [('/c2_a', id(c2.c2_a)), ('/c1/c1_a', id(c2.c1.c1_a)),
                   ('/c1/l2/l2_a', id(c2.c1.l2.l2_a)),
                   ('/l3/l3_a', id(c2.l3.l3_a))])
Example #3
0
    def test_copyparams(self):
        l1 = chainer.Link()
        with l1.init_scope():
            l1.x = chainer.Parameter(shape=(2, 3))
        l2 = chainer.Link()
        with l2.init_scope():
            l2.x = chainer.Parameter(shape=2)
        l3 = chainer.Link()
        with l3.init_scope():
            l3.x = chainer.Parameter(shape=3)
        c1 = chainer.Chain()
        with c1.init_scope():
            c1.l1 = l1
            c1.l2 = l2
        c2 = chainer.Chain()
        with c2.init_scope():
            c2.c1 = c1
            c2.l3 = l3
        l1.x.data.fill(0)
        l2.x.data.fill(1)
        l3.x.data.fill(2)

        self.c2.copyparams(c2)

        numpy.testing.assert_array_equal(self.l1.x.data, l1.x.data)
        numpy.testing.assert_array_equal(self.l2.x.data, l2.x.data)
        numpy.testing.assert_array_equal(self.l3.x.data, l3.x.data)
Example #4
0
    def setUp(self):
        self.l1 = chainer.Link(x=(2, 3))
        self.l2 = chainer.Link(x=2)
        self.l3 = chainer.Link(x=3)

        self.c1 = chainer.Chain(l1=self.l1)
        self.c1.add_link('l2', self.l2)
        self.c2 = chainer.Chain(c1=self.c1, l3=self.l3)
Example #5
0
    def setUp(self):
        self.l1 = chainer.Link(x=(2, 3))
        self.l2 = chainer.Link(x=2)
        self.l3 = chainer.Link(x=None)

        self.c1 = chainer.Chain(l1=self.l1)
        self.c1.add_link('l2', self.l2)
        self.c2 = chainer.Chain(c1=self.c1)
        with self.c2.init_scope():
            self.c2.l3 = self.l3
Example #6
0
 def __init__(self):
     self.caffe = chainer.Chain(
         conv1=L.Convolution2D(3, 96, ksize=11, stride=4),
         conv2=L.Convolution2D(96, 256, ksize=5, pad=2),
         conv3=L.Convolution2D(256, 384, ksize=3, pad=1),
         conv4=L.Convolution2D(384, 384, ksize=3, pad=1),
         conv5=L.Convolution2D(384, 256, ksize=3, pad=1),
         fc6=L.Linear(9216, 4096),
         fc7=L.Linear(4096, 4096),
     )
     self.fine = chainer.Chain(fc8ft=F.Linear(4096, 17), )
Example #7
0
    def test_copy_param_scalar(self):
        a = chainer.Chain()
        with a.init_scope():
            a.p = chainer.Parameter(np.array(1))
        b = chainer.Chain()
        with b.init_scope():
            b.p = chainer.Parameter(np.array(2))

        self.assertNotEqual(a.p.array, b.p.array)

        # Copy b's parameters to a
        copy_param.copy_param(a, b)

        self.assertEqual(a.p.array, b.p.array)
Example #8
0
    def test_copyparams(self):
        l1 = chainer.Link(x=(2, 3))
        l2 = chainer.Link(x=2)
        l3 = chainer.Link(x=3)
        c1 = chainer.Chain(l1=l1, l2=l2)
        c2 = chainer.Chain(c1=c1, l3=l3)
        l1.x.data.fill(0)
        l2.x.data.fill(1)
        l3.x.data.fill(2)

        self.c2.copyparams(c2)

        numpy.testing.assert_array_equal(self.l1.x.data, l1.x.data)
        numpy.testing.assert_array_equal(self.l2.x.data, l2.x.data)
        numpy.testing.assert_array_equal(self.l3.x.data, l3.x.data)
    def __init__(self, hyperparams: HyperParameters, hdf5_path=None):
        assert isinstance(hyperparams, HyperParameters)

        self.generation_network, self.generation_network_params = self.build_generation_network(
            total_timestep=hyperparams.generator_total_timestep,
            channels_chz=hyperparams.channels_chz,
            channels_u=hyperparams.generator_u_channels)

        self.inference_network, self.inference_network_params = self.build_inference_network(
            channels_chz=hyperparams.channels_chz)

        self.representation_network, self.representation_network_params = self.build_representation_network(
            architecture=hyperparams.representation_architecture,
            channels_r=hyperparams.channels_r)

        if hdf5_path:
            try:
                load_hdf5(
                    os.path.join(hdf5_path, "generation.hdf5"),
                    self.generation_network_params)
                load_hdf5(
                    os.path.join(hdf5_path, "inference.hdf5"),
                    self.inference_network_params)
                load_hdf5(
                    os.path.join(hdf5_path, "representation.hdf5"),
                    self.representation_network_params)
            except:
                pass

        self.parameters = chainer.Chain(
            g=self.generation_network_params,
            i=self.inference_network_params,
            r=self.representation_network_params,
        )
def load_model():
    models = chainer.Chain(conv1=L.Convolution2D(in_channels=None,
                                                 out_channels=6,
                                                 ksize=3,
                                                 pad=1),
                           conv2=L.Convolution2D(in_channels=None,
                                                 out_channels=12,
                                                 ksize=3,
                                                 pad=1),
                           conv3=L.Convolution2D(in_channels=None,
                                                 out_channels=24,
                                                 ksize=3,
                                                 pad=1),
                           conv4=L.Convolution2D(in_channels=None,
                                                 out_channels=48,
                                                 ksize=3,
                                                 pad=1),
                           conv5=L.Convolution2D(in_channels=None,
                                                 out_channels=96,
                                                 ksize=3,
                                                 pad=1),
                           bn1=L.BatchNormalization(6),
                           bn2=L.BatchNormalization(12),
                           bn3=L.BatchNormalization(24),
                           bn4=L.BatchNormalization(48),
                           bn5=L.BatchNormalization(96),
                           linear5=L.Linear(None, 10))
    serializers.load_npz('./Model_check_point/resizebn_64_36.model', models)
    #models.to_gpu(0)
    return models
Example #11
0
 def __init__(self, params):
     params.check()
     self.params = params
     self.chain = chainer.Chain()
     self.create_network()
     self.setup_optimizer()
     self._gpu = False
    def build_inference_network(self, generation_steps, channels_chz, channels_map_x):
        inference_parameters = chainer.Chain()
        cores = []
        with inference_parameters.init_scope():
            # LSTM core
            for t in range(generation_steps):
                params = gqn.nn.chainer.inference.CoreParameters(
                    channels_chz=channels_chz)
                core = gqn.nn.chainer.inference.CoreNetwork(params=params)
                cores.append(core)
                setattr(inference_parameters, "core_%d" % t, params)

            # z posterior sampler
            params = gqn.nn.chainer.inference.PosteriorParameters(
                channels_z=channels_chz)
            posterior = gqn.nn.chainer.inference.PosteriorNetwork(params)
            inference_parameters.posterior = params

            # x downsampler
            params = gqn.nn.chainer.inference.DownsamplerParameters(
                channels=channels_map_x)
            downsampler = gqn.nn.chainer.inference.Downsampler(params)
            inference_parameters.downsampler = params

        return cores, posterior, downsampler, inference_parameters
 def _create_chain(self):
     chain = chainer.Chain(l_conv1_1=L.Convolution2D(None,
                                                     64, (3, 3),
                                                     pad=1),
                           l_norm1_1=L.BatchNormalization(64),
                           l_conv1_2=L.Convolution2D(64, 64, (3, 3), pad=1),
                           l_norm1_2=L.BatchNormalization(64),
                           l_conv1_3=L.Convolution2D(64, 64, (3, 3), pad=1),
                           l_norm1_3=L.BatchNormalization(64),
                           l_conv1_r=L.Convolution2D(None,
                                                     64, (3, 3),
                                                     pad=1),
                           l_norm1_r=L.BatchNormalization(64),
                           l_conv2_1=L.Convolution2D(64, 128, (3, 3),
                                                     pad=1),
                           l_norm2_1=L.BatchNormalization(128),
                           l_conv2_2=L.Convolution2D(128,
                                                     128, (3, 3),
                                                     pad=1),
                           l_norm2_2=L.BatchNormalization(128),
                           l_conv2_3=L.Convolution2D(128,
                                                     128, (3, 3),
                                                     pad=1),
                           l_norm2_3=L.BatchNormalization(128),
                           l_conv2_r=L.Convolution2D(64, 128, (3, 3),
                                                     pad=1),
                           l_norm2_r=L.BatchNormalization(128),
                           l_conv3_1=L.Convolution2D(128,
                                                     256, (3, 3),
                                                     pad=1),
                           l_norm3_1=L.BatchNormalization(256),
                           l_conv3_2=L.Convolution2D(256,
                                                     256, (3, 3),
                                                     pad=1),
                           l_norm3_2=L.BatchNormalization(256),
                           l_conv3_3=L.Convolution2D(256,
                                                     256, (3, 3),
                                                     pad=1),
                           l_norm3_3=L.BatchNormalization(256),
                           l_conv3_r=L.Convolution2D(128,
                                                     256, (3, 3),
                                                     pad=1),
                           l_norm3_r=L.BatchNormalization(256),
                           l_conv4_1=L.Convolution2D(256,
                                                     512, (3, 3),
                                                     pad=1),
                           l_norm4_1=L.BatchNormalization(512),
                           l_conv4_2=L.Convolution2D(512,
                                                     512, (3, 3),
                                                     pad=1),
                           l_norm4_2=L.BatchNormalization(512),
                           l_conv4_3=L.Convolution2D(512,
                                                     512, (3, 3),
                                                     pad=1),
                           l_norm4_3=L.BatchNormalization(512),
                           l_conv4_r=L.Convolution2D(256,
                                                     512, (3, 3),
                                                     pad=1),
                           l_norm4_r=L.BatchNormalization(512))
     return chain
    def build_generation_network(self, generation_steps, channels_chz,
                                 channels_u):
        generation_parameters = chainer.Chain()
        cores = []
        with generation_parameters.init_scope():
            # LSTM core
            for t in range(generation_steps):
                params = gqn.nn.chainer.generator.CoreParameters(
                    channels_chz=channels_chz, channels_u=channels_u)
                core = gqn.nn.chainer.generator.CoreNetwork(params=params)
                cores.append(core)
                setattr(generation_parameters, "core_%d" % t, params)

            # z prior sampler
            params = gqn.nn.chainer.generator.PriorParameters(
                channels_z=channels_chz)
            prior = gqn.nn.chainer.generator.PriorNetwork(params)
            generation_parameters.prior = params

            # observation sampler
            params = gqn.nn.chainer.generator.ObservationParameters()
            observation = gqn.nn.chainer.generator.ObservationNetwork(params)
            generation_parameters.observation = params

        return cores, prior, observation, generation_parameters
Example #15
0
 def make_network(self, scale, bias=0):
     dim = self.dim
     self.lmNet = chainer.Chain()
     self.lmNet.add_link(
         'Embed',
         L.EmbedID(len(self.vocab), self.dim, initialW=I.Uniform(scale)))
     for i in range(self.layerNum):
         self.lmNet.add_link(
             'x2h%s_0' % (i),
             L.Linear(dim, dim, initialW=I.Uniform(scale), nobias=True))
         self.lmNet.add_link(
             'x2t%s_0' % (i),
             L.Linear(dim, dim, initialW=I.Uniform(scale), nobias=True))
         for j in range(self.depth):
             self.lmNet.add_link(
                 'h2h%s_%s' % (i, j),
                 L.Linear(dim, dim, initialW=I.Uniform(scale)))
             self.lmNet.add_link(
                 'h2t%s_%s' % (i, j),
                 L.Linear(dim,
                          dim,
                          initialW=I.Uniform(scale),
                          initial_bias=bias))
     self.lmNet.add_link(
         'Output', L.Linear(dim, len(self.vocab),
                            initialW=I.Uniform(scale)))
Example #16
0
 def make_network(self, scale):
     dim = self.dim
     self.lmNet = chainer.Chain()
     self.lmNet.add_link('Embed', L.EmbedID(len(self.vocab), dim, initialW=I.Uniform(scale)))
     for j in range(self.layerNum):
         self.lmNet.add_link('LSTM%s'%(j), L.LSTM(dim, dim, lateral_init=I.Uniform(scale), upward_init=I.Uniform(scale), forget_bias_init=0))
     self.lmNet.add_link('Output', L.Linear(dim, len(self.vocab), initialW=I.Uniform(scale)))
Example #17
0
    def test_soft_copy_param_scalar(self):
        a = chainer.Chain()
        with a.init_scope():
            a.p = chainer.Parameter(np.array(0.5))
        b = chainer.Chain()
        with b.init_scope():
            b.p = chainer.Parameter(np.array(1))

        # a = (1 - tau) * a + tau * b
        copy_param.soft_copy_param(target_link=a, source_link=b, tau=0.1)

        np.testing.assert_almost_equal(a.p.array, 0.55)
        np.testing.assert_almost_equal(b.p.array, 1.0)

        copy_param.soft_copy_param(target_link=a, source_link=b, tau=0.1)

        np.testing.assert_almost_equal(a.p.array, 0.595)
        np.testing.assert_almost_equal(b.p.array, 1.0)
Example #18
0
    def test_addgrads(self):
        l1 = chainer.Link(x=(2, 3))
        l2 = chainer.Link(x=2)
        l3 = chainer.Link(x=3)
        c1 = chainer.Chain(l1=l1, l2=l2)
        c2 = chainer.Chain(c1=c1, l3=l3)
        l1.x.grad.fill(1)
        l2.x.grad.fill(2)
        l3.x.grad.fill(3)

        self.l1.x.grad.fill(-1)
        self.l2.x.grad.fill(-2)
        self.l3.x.grad.fill(-3)

        self.c2.addgrads(c2)
        numpy.testing.assert_array_equal(self.l1.x.grad, numpy.zeros((2, 3)))
        numpy.testing.assert_array_equal(self.l2.x.grad, numpy.zeros(2))
        numpy.testing.assert_array_equal(self.l3.x.grad, numpy.zeros(3))
Example #19
0
    def setUp(self):
        self.l1 = chainer.Link()
        with self.l1.init_scope():
            self.l1.x = chainer.Parameter(shape=(2, 3))
        self.l2 = chainer.Link()
        with self.l2.init_scope():
            self.l2.x = chainer.Parameter(shape=2)
        self.l3 = chainer.Link()
        with self.l3.init_scope():
            self.l3.x = chainer.Parameter()

        self.c1 = chainer.Chain()
        with self.c1.init_scope():
            self.c1.l1 = self.l1
        with testing.assert_warns(DeprecationWarning):
            self.c1.add_link('l2', self.l2)
        self.c2 = chainer.Chain()
        with self.c2.init_scope():
            self.c2.c1 = self.c1
            self.c2.l3 = self.l3
Example #20
0
    def test_chain(self):
        ch = chainer.Chain()
        with ch.init_scope():
            ch.l1 = chainer.links.Linear(3, 4)
            ch.l2 = chainer.links.Linear(5)
            ch.l3 = chainer.links.PReLU()
        self.assertEqual(names_of_links(ch), {'/l1', '/l2', '/l3'})

        to_factorized_noisy(ch)
        self.assertEqual(names_of_links(ch), {
            '/l1', '/l1/mu', '/l1/sigma', '/l2', '/l2/mu', '/l2/sigma', '/l3'
        })
Example #21
0
    def _test(self, gpu):

        model = chainer.Chain(
            a=L.Linear(1, 2, initialW=3, initial_bias=3),
            b=chainer.Chain(c=L.Linear(2, 3, initialW=4, initial_bias=4)),
        )
        if gpu >= 0:
            model.to_gpu(gpu)
            xp = model.xp
        else:
            xp = np
        optimizer = chainer.optimizers.SGD(self.lr)
        optimizer.setup(model)
        optimizer.add_hook(
            chainerrl.optimizers.NonbiasWeightDecay(
                rate=self.weight_decay_rate))
        optimizer.update(lambda: chainer.Variable(xp.asarray(0.0)))
        decay_factor = 1 - self.lr * self.weight_decay_rate
        xp.testing.assert_allclose(model.a.W.array, 3 * decay_factor)
        xp.testing.assert_allclose(model.a.b.array, 3)
        xp.testing.assert_allclose(model.b.c.W.array, 4 * decay_factor)
        xp.testing.assert_allclose(model.b.c.b.array, 4)
Example #22
0
 def _create_chain(self):
     chain = chainer.Chain(
         l_key=L.Linear(self.cell_size, self.nb_reads * self.memory_shape[1]),
         l_add=L.Linear(self.cell_size, self.nb_reads * self.memory_shape[1]),
         l_sigma=L.Linear(self.cell_size, 1),
         l_ho=L.Linear(self.cell_size, self.nb_class),
         l_ro=L.Linear(self.nb_reads * self.memory_shape[1], self.nb_class),
         # for LSTM
         lstm_xh=L.Linear(self.input_size, 4 * self.cell_size),
         lstm_yh=L.EmbedID(self.nb_class, 4 * self.cell_size),
         lstm_rh=L.Linear(self.nb_reads * self.memory_shape[1], 4 * self.cell_size),
         lstm_hh=L.Linear(self.cell_size, 4 * self.cell_size),
         )
     return chain
    def __init__(self,
                 n_input_channels,
                 n_hidden_layers,
                 n_hidden_channels,
                 action_size,
                 min_action=None,
                 max_action=None,
                 bound_action=True,
                 nonlinearity=F.relu,
                 last_wscale=1.):
        self.n_input_channels = n_input_channels
        self.n_hidden_layers = n_hidden_layers
        self.n_hidden_channels = n_hidden_channels
        self.action_size = action_size
        self.min_action = min_action
        self.max_action = max_action
        self.bound_action = bound_action

        if self.bound_action:

            def action_filter(x):
                return bound_by_tanh(x, self.min_action, self.max_action)
        else:
            action_filter = None

        model = chainer.Chain(
            fc=MLP(
                self.n_input_channels,
                n_hidden_channels,
                (self.n_hidden_channels, ) * self.n_hidden_layers,
                nonlinearity=nonlinearity,
            ),
            lstm=L.LSTM(n_hidden_channels, n_hidden_channels),
            out=L.Linear(n_hidden_channels,
                         action_size,
                         initialW=LeCunNormal(last_wscale)),
        )

        def model_call(model, x):
            h = nonlinearity(model.fc(x))
            h = model.lstm(h)
            h = model.out(h)
            return h

        super().__init__(model=model,
                         model_call=model_call,
                         action_filter=action_filter)
Example #24
0
    def make_model(self, env):
        n_dim_obs = env.observation_space.low.size
        n_dim_action = env.action_space.low.size
        n_hidden_channels = 50

        policy = policies.FCGaussianPolicy(n_input_channels=n_dim_obs,
                                           n_hidden_layers=2,
                                           n_hidden_channels=n_hidden_channels,
                                           action_size=n_dim_action,
                                           min_action=env.action_space.low,
                                           max_action=env.action_space.high)

        q_func = q_function.FCSAQFunction(n_dim_obs=n_dim_obs,
                                          n_dim_action=n_dim_action,
                                          n_hidden_layers=2,
                                          n_hidden_channels=n_hidden_channels)

        return chainer.Chain(policy=policy, q_function=q_func)
Example #25
0
    def _create_chain(self):
        chain = chainer.Chain(
            l_conv1_1=L.Convolution2D(None,64,(3,3), pad=1),
            l_norm1_1=L.BatchNormalization(64),
            l_conv1_2=L.Convolution2D(64,64,(3,3), pad=1),
  
            l_norm1_2=L.BatchNormalization(64),
            l_conv1_3=L.Convolution2D(64,64,(3,3), pad=1),
            l_norm1_3=L.BatchNormalization(64),
            l_conv1_r=L.Convolution2D(None,64,(3,3), pad=1),
            l_norm1_r=L.BatchNormalization(64),
            
            l_conv2_1=L.Convolution2D(64,128,(3,3), pad=1),
            l_norm2_1=L.BatchNormalization(128),
            l_conv2_2=L.Convolution2D(128,128,(3,3), pad=1),
            l_norm2_2=L.BatchNormalization(128),
            l_conv2_3=L.Convolution2D(128,128,(3,3), pad=1),

            l_norm2_3=L.BatchNormalization(128),  
            l_conv2_r=L.Convolution2D(64,128,(3,3), pad=1),
            l_norm2_r=L.BatchNormalization(128),

            l_conv3_1=L.Convolution2D(128,256,(3,3), pad=1),
            l_norm3_1=L.BatchNormalization(256),
            l_conv3_2=L.Convolution2D(256,256,(3,3), pad=1),
            l_norm3_2=L.BatchNormalization(256),
            l_conv3_3=L.Convolution2D(256,256,(3,3), pad=1),
            l_norm3_3=L.BatchNormalization(256),
            l_conv3_r=L.Convolution2D(128,256,(3,3), pad=1),
            l_norm3_r=L.BatchNormalization(256),
            
            l_conv4_1=L.Convolution2D(256,128,(3,3), pad=1),
            l_norm4_1=L.BatchNormalization(128),
            l_conv4_2=L.Convolution2D(128,128,(3,3), pad=1),
            l_norm4_2=L.BatchNormalization(128),
            l_conv4_3=L.Convolution2D(128,128,(3,3), pad=1),
            l_norm4_3=L.BatchNormalization(128),
            l_conv4_r=L.Convolution2D(256,128,(3,3), pad=1),
            l_norm4_r=L.BatchNormalization(128),
            
            l_phi=L.Linear(self.dimension, self.nb_class_train),
            )
        return chain
Example #26
0
    def __init__(self, hyperparams: HyperParameters):
        assert isinstance(hyperparams, HyperParameters)

        self.generation_network, self.generation_network_params = self.build_generation_network(
            total_timestep=hyperparams.generator_total_timestep,
            channels_chrz=hyperparams.channels_chz,
            channels_u=hyperparams.generator_u_channels)

        self.inference_network, self.inference_network_params = self.build_inference_network(
            channels_chrz=hyperparams.channels_chz)

        self.representation_network, self.representation_network_params = self.build_representation_network(
            architecture=hyperparams.representation_architecture,
            channels_r=hyperparams.channels_r)

        self.parameters = chainer.Chain(
            g=self.generation_network_params,
            i=self.inference_network_params,
            r=self.representation_network_params,
        )
Example #27
0
 def initModel(self):
     self.model = chainer.Chain(
         encoderEmbed=L.EmbedID(self.encVocabSize,
                                self.eDim_enc),  # encoder embedding layer
         decoderEmbed=L.EmbedID(self.decVocabSize,
                                self.eDim_dec),  # decoder embedding layer
         decOutputL=L.Linear(self.hDim, self.decVocabSize),  # output layer
         encoder_bak=L.NStepLSTM(
             n_layers=self.n_layers,
             in_size=self.eDim_enc,
             out_size=self.hDim,
             dropout=self.dropout_rate),  # encoder backward
         encoder_fwd=L.NStepLSTM(
             n_layers=self.n_layers,
             in_size=self.eDim_enc,
             out_size=self.hDim,
             dropout=self.dropout_rate),  # encoder forward
         decoder_=NLayerLSTM(eDim=self.eDim_dec, hDim=self.hDim),  # decoder
         attnIn=L.Linear(self.hDim, self.hDim, nobias=True),  # attn
         attnOut=L.Linear(self.hDim + self.hDim, self.hDim,
                          nobias=True)  # attn
     )
Example #28
0
    def __init__(self,
                 n_input_channels,
                 n_hidden_layers,
                 n_hidden_channels,
                 action_size,
                 min_action=None,
                 max_action=None,
                 bound_action=True):
        self.n_input_channels = n_input_channels
        self.n_hidden_layers = n_hidden_layers
        self.n_hidden_channels = n_hidden_channels
        self.action_size = action_size
        self.min_action = min_action
        self.max_action = max_action
        self.bound_action = bound_action

        if self.bound_action:
            action_filter = lambda x: bound_by_tanh(x, self.min_action, self.
                                                    max_action)
        else:
            action_filter = None

        model = chainer.Chain(
            fc=MLP(self.n_input_channels, n_hidden_channels,
                   (self.n_hidden_channels, ) * self.n_hidden_layers),
            lstm=L.LSTM(n_hidden_channels, n_hidden_channels),
            out=L.Linear(n_hidden_channels, action_size),
        )

        def model_call(model, x):
            h = F.relu(model.fc(x))
            h = model.lstm(h)
            h = model.out(h)
            return h

        super().__init__(model=model,
                         model_call=model_call,
                         action_filter=action_filter)
    def __init__(self, hyperparams: HyperParameters, hdf5_path=None):
        assert isinstance(hyperparams, HyperParameters)
        self.generation_steps = hyperparams.generator_generation_steps
        self.hyperparams = hyperparams

        self.generation_cores, self.generation_prior, self.generation_observation, self.generation_params = self.build_generation_network(
            generation_steps=self.generation_steps,
            channels_chz=hyperparams.channels_chz,
            channels_u=hyperparams.generator_u_channels)

        self.inference_cores, self.inference_posterior, self.inference_downsampler, self.inference_params = self.build_inference_network(
            generation_steps=self.generation_steps,
            channels_chz=hyperparams.channels_chz,
            channels_map_x=hyperparams.inference_channels_map_x)

        self.representation_network, self.representation_params = self.build_representation_network(
            architecture=hyperparams.representation_architecture,
            channels_r=hyperparams.channels_r)

        if hdf5_path:
            try:
                load_hdf5(
                    os.path.join(hdf5_path, "generation.hdf5"),
                    self.generation_params)
                load_hdf5(
                    os.path.join(hdf5_path, "inference.hdf5"),
                    self.inference_params)
                load_hdf5(
                    os.path.join(hdf5_path, "representation.hdf5"),
                    self.representation_params)
            except:
                pass

        self.parameters = chainer.Chain()
        with self.parameters.init_scope():
            self.parameters.generation = self.generation_params
            self.parameters.inference = self.inference_params
            self.parameters.representation = self.representation_params
    print("------------")
    # print("x_train", x_train)
    # print("y_train", y_train)
    # print(y_train)
    # print(y_test)

    N = len(x_train)  # train data size
    in_units = x_train.shape[1]  # 入力層のユニット数 (語彙数)
    print("学習データ数:", N)

    n_units = args.units  # 隠れ層のユニット数
    n_label = 2  # 出力層のユニット数

    #モデルの定義
    model = chainer.Chain(l1=L.Linear(in_units, n_units),
                          l2=L.Linear(n_units, n_units),
                          l3=L.Linear(n_units, n_label))

    # #GPUを使うかどうか
    # if args.gpu > 0:
    # cuda.check_cuda_available()
    # cuda.get_device(args.gpu).use()
    # model.to_gpu()
    # xp = np if args.gpu <= 0 else cuda.cupy #args.gpu <= 0: use cpu, otherwise: use gpu

    xp = np

    batchsize = args.batchsize
    n_epoch = args.epoch

    def forward(x, t, train=True):