Exemplo n.º 1
0
    def __init__(self, n_vocab, n_units):
        super(ContinuousBoW, self).__init__()

        with self.init_scope():
            self.embed = L.EmbedID(n_vocab,
                                   n_units,
                                   initialW=I.Uniform(1. / n_units))
            self.out = L.Linear(n_units, n_vocab, initialW=0)
    def __init__(self, n_vocab, n_units, loss_func):
        super(SkipGram, self).__init__()

        with self.init_scope():
            self.embed = L.EmbedID(n_vocab,
                                   n_units,
                                   initialW=I.Uniform(1. / n_units))
            self.loss_func = loss_func
Exemplo n.º 3
0
 def __init__(self, emb_dim, n_out, depth=[2, 2, 2, 2]):
     super(VDCNN, self).__init__()
     with self.init_scope():
         self.embed = L.EmbedID(emb_dim,
                                16,
                                initializers.Normal(),
                                ignore_label=-1)
         self.conv1 = L.ConvolutionND(1,
                                      16,
                                      64,
                                      3,
                                      1,
                                      1,
                                      initialW=initializers.HeUniform(),
                                      initial_bias=initializers.Uniform(
                                          np.sqrt(6 / (16 * 3))))
         self.cb2 = BuildBlock(depth[0], 64, 128)
         self.cb3 = BuildBlock(depth[1], 128, 256)
         self.cb4 = BuildBlock(depth[2], 256, 512)
         self.cb5 = BuildBlock(depth[3], 512, 512)
         self.fc6 = L.Linear(
             4096,
             2048,
             initialW=initializers.Uniform(1 / np.sqrt(4096)),
             initial_bias=initializers.Uniform(1 / np.sqrt(4096)))
         self.fc7 = L.Linear(
             2048,
             2048,
             initialW=initializers.Uniform(1 / np.sqrt(2048)),
             initial_bias=initializers.Uniform(1 / np.sqrt(2048)))
         self.fc8 = L.Linear(
             2048,
             n_out,
             initialW=initializers.Uniform(1 / np.sqrt(2048)),
             initial_bias=initializers.Uniform(1 / np.sqrt(2048)))
Exemplo n.º 4
0
    def __init__(self, input_space, output_space, embedding_size,\
                lstm_size, lstm_dropout, zero_bias=False):
        """
        Initialise this network
        Args:
            input_space     -- here space of observations x
            output_space    -- here space of actions a
            embedding_size  -- size of the embedding layers, both for
                               observations -> embeddings and embeddings -> joint embeddings
            lstm_size       -- size of the lstm hidden layer for both the behaviour
                               and inference policies
            lstm_dropout    -- lstm dropout ratio between 0 and 1, i.e. percentage of units
                               dropped to improve generalisation
            zero_bias       -- False by default, set True for low uniformly distributed initial bias
        """

        super(ClosedLoopEmpowermentNet, self).__init__()

        # TODO: Potentially relax assumptions;
        # This is presently custom-tailored to the Gridworld environment
        assert isinstance(output_space, gym.spaces.Discrete)
        assert isinstance(input_space, gym.spaces.Box)
        assert input_space.shape is not None
        assert embedding_size > 0
        assert lstm_size > 0

        self._input_space = input_space
        self._output_space = output_space
        self._lstm_size = lstm_size

        in_channels = input_space.shape[0]
        initial_bias = None if zero_bias else I.Uniform(1e-4)

        # Define the network
        with self.init_scope():
            # Embeddings are fully connected, one-layer linear networks
            # Will be wrapped in rectified linear units later for non-linearity
            # Adding initial bias means that most rectified units will have input > 0
            # and thus be active in beginning

            # Embedding for input observations x_t -> u_t
            self.lin_embed_p = L.Linear(in_size=in_channels, out_size=embedding_size,\
                                        initial_bias=initial_bias)

            # Embedding for joint embeddings (u_t, u_f) -> v
            self.lin_embed_q = L.Linear(in_size=embedding_size*2, out_size=embedding_size,\
                                        initial_bias=initial_bias)

            # LSTM for inference policy, taking input of form [h^p_t, v, a_{t-1}]
            self.lstm_q = L.NStepLSTM(n_layers=1,\
                                      in_size=(lstm_size + embedding_size + self._output_space.n),\
                                      out_size=lstm_size, dropout=lstm_dropout)

            # Linear transformation of LSTM output to actions for inference policy
            self.lin_pi_q = L.Linear(in_size=lstm_size,
                                     out_size=self._output_space.n,
                                     initial_bias=initial_bias)
Exemplo n.º 5
0
 def __init__(self, item_num, hidden_num, rating_num, encoding_size=-1):
     if encoding_size <= 0:
         super(NadeIn,
               self).__init__(a=L.Linear(item_num * rating_num,
                                         hidden_num,
                                         initialW=I.Uniform(0.06),
                                         initial_bias=I.Constant(0)), )
     else:
         super(NadeIn, self).__init__(
             a=L.Linear(item_num * rating_num,
                        encoding_size,
                        nobias=True,
                        initialW=I.Uniform(0.06)),
             b=L.Linear(encoding_size,
                        hidden_num,
                        initialW=I.Uniform(0.06),
                        initial_bias=I.Constant(0)),
         )
Exemplo n.º 6
0
 def __init__(self):
     super(Actor, self).__init__(
         h1 = L.Linear(O_DIM, 400),
         h2 = L.Linear(400, 300),
         h3 = L.Linear(300, A_DIM, initialW=initializers.Uniform(scale=0.003)),
     )
     self.optimizer = optimizers.Adam(alpha=ACTOR_LEARNING_RATE)
     self.optimizer.setup(self)
     self.optimizer.add_hook(optimizer_hooks.GradientClipping(2.0))
Exemplo n.º 7
0
    def setUp(self):
        N = 2
        in_channels = 3
        out_channels = 2
        ndim = len(self.dims)
        ksize = (3, ) * ndim
        stride = (2, ) * ndim
        pad = (1, ) * ndim

        if self.used_outsize == 'case1' or self.used_outsize == 'None':
            # Use output size determined with get_deconv_outsize.
            outs = tuple(
                conv.get_deconv_outsize(d, k, s, p)
                for (d, k, s, p) in zip(self.dims, ksize, stride, pad))
        elif self.used_outsize == 'case2':
            # Use possible output size other than the one determined with
            # get_deconv_outsize.
            outs = tuple(
                conv.get_deconv_outsize(d, k, s, p) + 1
                for (d, k, s, p) in zip(self.dims, ksize, stride, pad))

        if self.used_outsize != 'None':
            outsize = outs
        else:
            outsize = None

        if not self.nobias:
            initial_bias = initializers.Uniform(scale=1, dtype=self.dtype)
        else:
            initial_bias = None

        self.link = deconvolution_nd.DeconvolutionND(ndim,
                                                     in_channels,
                                                     out_channels,
                                                     ksize,
                                                     stride=stride,
                                                     pad=pad,
                                                     outsize=outsize,
                                                     initial_bias=initial_bias,
                                                     nobias=self.nobias)
        self.link.cleargrads()

        x_shape = (N, in_channels) + self.dims
        self.x = numpy.random.uniform(-1, 1, x_shape).astype(self.dtype)
        gy_shape = (N, out_channels) + outs
        self.gy = numpy.random.uniform(-1, 1, gy_shape).astype(self.dtype)

        self.check_forward_options = {}
        self.check_backward_options = {'eps': 1e-2, 'atol': 1e-4, 'rtol': 1e-3}
        if self.dtype == numpy.float16:
            self.check_forward_options = {'atol': 5e-3, 'rtol': 5e-2}
            self.check_backward_options = {
                'eps': 2**-3,
                'atol': 1e-2,
                'rtol': 1e-1
            }
Exemplo n.º 8
0
    def __init__(self, grid_weight_share=False):
        """
        """
        super(CapsNet, self).__init__()
        self.grid_weight_share = grid_weight_share
        self.recon_loss_weight = 0.0005
        init_scale = 0.1

        # init_W = initializers.HeNormal()
        init_W = initializers.Uniform(scale=init_scale)
        conv1_param = dict(in_channels=1,
                           out_channels=256,
                           ksize=9,
                           stride=1,
                           nobias=True,
                           initialW=init_W)
        pcaps_param = dict(in_caps=1,
                           in_dims=256,
                           out_caps=32,
                           out_dims=8,
                           n_iters=1,
                           ksize=9,
                           stride=2,
                           nobias=True,
                           initialW=init_W)
        if self.grid_weight_share:
            dcapsconv_param = dict(in_caps=32,
                                   in_dims=8,
                                   out_caps=10,
                                   out_dims=16,
                                   n_iters=3,
                                   flat_output=True,
                                   ksize=1,
                                   stride=1,
                                   nobias=True,
                                   initialW=init_W)
        else:
            dcapslin_param = dict(in_caps=32 * 6 * 6,
                                  in_dims=8,
                                  out_caps=10,
                                  out_dims=16,
                                  n_iters=3,
                                  initialW=init_W)
        with self.init_scope():
            self.conv1 = L.Convolution2D(**conv1_param)
            self.primarycaps = CapsConv(**pcaps_param)

            if self.grid_weight_share:
                self.digitcaps = CapsConv(**dcapsconv_param)
            else:
                self.digitcaps = CapsLinear(**dcapslin_param)

            # for reconstruction
            self.fc1 = L.Linear(16 * 10, 512, initialW=init_W)
            self.fc2 = L.Linear(512, 1024, initialW=init_W)
            self.fc3 = L.Linear(1024, 784, initialW=init_W)
Exemplo n.º 9
0
    def __init__(self, n_vocab, n_embed, n_units):
        super(CandW, self).__init__()

        with self.init_scope():
            self.embed = L.EmbedID(n_vocab,
                                   n_embed,
                                   initialW=initializers.Uniform(1. / n_embed))
            self.l1 = L.Linear(n_embed * (args.window * 2 + 1),
                               n_units)  # n_embed * (window*2 + 1)-> n_units
            self.l2 = L.Linear(n_units, 1)  # n_units -> 1
Exemplo n.º 10
0
    def setUp(self):
        ndim = len(self.dims)
        self.ksize = (3, ) * ndim
        self.stride = (2, ) * ndim
        self.pad = (1, ) * ndim

        if self.in_channels == 'omit':
            self.link = convolution_nd.ConvolutionND(
                ndim,
                2,
                self.ksize,
                stride=self.stride,
                pad=self.pad,
                groups=self.groups,
                initial_bias=initializers.Uniform(scale=1., dtype=self.dtype))
        else:
            self.link = convolution_nd.ConvolutionND(
                ndim,
                self.in_channels,
                2,
                self.ksize,
                stride=self.stride,
                pad=self.pad,
                groups=self.groups,
                initial_bias=initializers.Uniform(scale=1., dtype=self.dtype))
        self.link.cleargrads()

        x_shape = (2, 4) + self.dims
        self.x = numpy.random.uniform(-1, 1, x_shape).astype(self.dtype)
        gy_shape = (2, 2) + tuple(
            conv.get_conv_outsize(d, k, s, p)
            for (d, k, s,
                 p) in zip(self.dims, self.ksize, self.stride, self.pad))
        self.gy = numpy.random.uniform(-1, 1, gy_shape).astype(self.dtype)

        self.check_backward_options = {'eps': 1e-2, 'atol': 1e-3, 'rtol': 1e-3}
        if self.dtype == numpy.float16:
            self.check_backward_options = {
                'eps': 2**-4,
                'atol': 2**-4,
                'rtol': 2**-4
            }
Exemplo n.º 11
0
 def __init__(self, item_num, hidden_num, rating_num, encoding_size=-1):
     if encoding_size <= 0:
         super(NadeOut,
               self).__init__(p=L.Linear(hidden_num,
                                         rating_num * item_num,
                                         initialW=I.Uniform(0.06),
                                         initial_bias=I.Constant(0)), )
     else:
         super(NadeOut, self).__init__(
             p=L.Linear(hidden_num,
                        encoding_size,
                        nobias=True,
                        initialW=I.Uniform(0.06)),
             q=L.Linear(encoding_size,
                        rating_num * item_num,
                        initialW=I.Uniform(0.06),
                        initial_bias=I.Constant(0)),
         )
     self.item_num = item_num
     self.rating_num = rating_num
    def __init__(self, n_vocab, n_units, loss_func, ori_con_data):
        super(ContinuousBoW, self).__init__()

        with self.init_scope():
            self.embed = L.EmbedID(n_vocab, n_units, initialW=I.Uniform(1. / n_units))

            # 派生単語と元の単語の初期ベクトルを統一する
            for i in range(len(ori_con_data)):
                self.embed.W.data[ori_con_data[i][0]] = self.embed.W.data[ori_con_data[i][1]]

            self.loss_func = loss_func
Exemplo n.º 13
0
 def __init__(self):
     super(Critic, self).__init__(
         h1=L.Linear(O_DIM, 400),
         h2_s=L.Linear(400, 300, nobias=True),
         h2_a=L.Linear(A_DIM, 300),
         h3=L.Linear(300, 1, initialW=initializers.Uniform(scale=0.003)),
     )
     self.optimizer = optimizers.Adam(alpha=CRITIC_LEARNING_RATE)
     self.optimizer.setup(self)
     #self.optimizer.add_hook(optimizer_hooks.WeightDecay(CRITIC_WEIGHT_DECAY))
     self.optimizer.add_hook(optimizer_hooks.GradientClipping(2.0))
 def __init__(self, n_vocab_char, n_units, n_units_char):
     super(RNN, self).__init__()
     with self.init_scope():
         self.embed = L.EmbedID(n_vocab_char,
                                n_units_char,
                                initialW=I.Uniform(
                                    1. / n_units_char))  # word embedding
         self.mid = L.LSTM(n_units_char,
                           n_units_char)  # the first LSTM layer
         self.out = L.Linear(n_units_char,
                             n_units)  # the feed-forward output layer
Exemplo n.º 15
0
    def __init__(self, nc_size, base=32):
        self.nc_size = nc_size
        wscale = math.sqrt(1 / (base * 32) / 3 / 3)
        wout = initializers.Uniform(scale=wscale)
        wscale = math.sqrt(1 / (base * 32) / 2 / 2)
        wcls = initializers.Uniform(scale=wscale)
        super(Discriminator, self).__init__()

        with self.init_scope():
            self.c0 = CBR(3, base, 4, 2, 1, down=True, act=F.leaky_relu)
            self.c1 = CBR(base, base * 2, 4, 2, 1, down=True, act=F.leaky_relu)
            self.c2 = CBR(base * 2,
                          base * 4,
                          4,
                          2,
                          1,
                          down=True,
                          act=F.leaky_relu)
            self.c3 = CBR(base * 4,
                          base * 8,
                          4,
                          2,
                          1,
                          down=True,
                          act=F.leaky_relu)
            self.c4 = CBR(base * 8,
                          base * 16,
                          4,
                          2,
                          1,
                          down=True,
                          act=F.leaky_relu)
            self.c5 = CBR(base * 16,
                          base * 32,
                          4,
                          2,
                          1,
                          down=True,
                          act=F.leaky_relu)
            self.lembed = L.Linear(None, base * 32, initialW=wout)
            self.l1 = L.Linear(None, 1, initialW=wout)
Exemplo n.º 16
0
    def __init__(self, n_vocab, n_embed, n_units, n_senti):
        super(SentimentEmbed, self).__init__()

        with self.init_scope():
            self.embed = L.EmbedID(n_vocab,
                                   n_embed,
                                   initialW=initializers.Uniform(1. / n_embed))
            self.l1 = L.Linear(n_embed * (args.window * 2 + 1),
                               n_units)  # n_embed * (window*2 + 1)-> n_units
            self.lc = L.Linear(n_units, 1)  # n_units -> 1
            self.ls = L.Linear(n_units, 2)  # n_units -> 1
            self.lws = L.Linear(n_embed, n_senti)  # n_embed -> n_senti
Exemplo n.º 17
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)))
Exemplo n.º 18
0
    def __init__(self, nc, dtype=np.float32):
        super(InstanceNormalization, self).__init__()
        self.nc = nc
        self.dtype = dtype
        self.bn = None
        self.prev_batch = None
        # self._device_id =  cuda.get_device()

        self.add_param('gamma', nc, dtype=dtype)
        initializers.Uniform(self.gamma.data)

        self.add_param('beta', nc, dtype=dtype)
        initializers.Zero(self.beta.data)
Exemplo n.º 19
0
    def get_initializers(self):
        if self.initialW == 'zero':
            weight_initializer = initializers.constant.Zero()
        elif self.initialW == 'random':
            weight_initializer = initializers.GlorotUniform(
                rng=numpy.random.RandomState(seed=0))

        if self.initial_bias == 'zero':
            bias_initializer = initializers.constant.Zero()
        elif self.initial_bias == 'random':
            bias_initializer = initializers.Uniform(
                rng=numpy.random.RandomState(seed=0))

        return weight_initializer, bias_initializer
Exemplo n.º 20
0
    def __init__(self, obs_size, num_actions, nhidden):
        """Initialize weights"""
        # use LeCunUniform weight initialization for weights
        self.initializer = initializers.LeCunUniform()
        self.bias_initializer = initializers.Uniform(1e-4)

        super(QNetwork, self).__init__(
            feature_layer = L.Linear(obs_size, nhidden,
                                initialW = self.initializer,
                                initial_bias = self.bias_initializer),
            action_values = L.Linear(nhidden, num_actions, 
                                initialW=self.initializer,
                                initial_bias = self.bias_initializer)
        )
Exemplo n.º 21
0
    def __init__(self,
                 input_space,
                 output_space,
                 zero_bias=False,
                 in_channels=None):
        super(ChainerDQN, self).__init__()
        assert isinstance(input_space, gym.Space)
        assert input_space.shape is not None
        if isinstance(output_space, gym.spaces.Discrete):
            out_units = output_space.n
        elif isinstance(output_space, gym.spaces.Box):
            assert len(output_space.shape) == 1
            out_units = output_space.shape[0]
        else:
            raise NotImplementedError

        self._input_space = input_space
        self._output_space = output_space
        initial_bias = None if zero_bias else I.Uniform(1e-4)
        in_channels = get_in_channels(input_space, in_channels)
        with self.init_scope():
            self.conv0 = L.Convolution2D(in_channels,
                                         32,
                                         ksize=8,
                                         stride=4,
                                         initialW=I.HeUniform(),
                                         initial_bias=initial_bias)
            self.conv1 = L.Convolution2D(None,
                                         64,
                                         ksize=4,
                                         stride=2,
                                         initialW=I.HeUniform(),
                                         initial_bias=initial_bias)
            self.conv2 = L.Convolution2D(None,
                                         64,
                                         ksize=3,
                                         stride=1,
                                         initialW=I.HeUniform(),
                                         initial_bias=initial_bias)
            self.fc0 = L.Linear(None,
                                512,
                                initialW=I.HeNormal(scale=0.01),
                                initial_bias=initial_bias)
            self.fc1 = L.Linear(None,
                                out_units,
                                initialW=I.HeNormal(scale=0.01),
                                initial_bias=initial_bias)

        self(np.zeros((1, ) + input_space.shape, input_space.dtype))
Exemplo n.º 22
0
 def __init__(self,
              wordCounts,
              embed_size,
              lossfun=negative_sampling.NegativeSampling):
     # 这句话的定义是实现其父类chainer.link.Chain的init
     super(SkipModel, self).__init__()
     with self.init_scope():
         # self.embedings = np.random.uniform(0,1,(dataMes.vocabSize,embed_size)).astype(np.float32)
         self.embedings = L.EmbedID(len(wordCounts),
                                    embed_size,
                                    initialW=I.Uniform(1. / embed_size))
         # 负采样函数需要获取词频数据 self.wordCnt, 和词向量维度信息embed_size,这样才能构建一个第二层映射矩阵
         self.lossfun = negative_sampling.NegativeSampling(embed_size,
                                                           wordCounts,
                                                           sample_size=5)
    def __init__(self,
                 n_vocab,
                 n_units,
                 loss_func,
                 n_vocab_char=None,
                 n_units_char=None):
        super(SkipGram, self).__init__()

        with self.init_scope():
            if args.subword == 'none':
                self.embed = L.EmbedID(n_vocab,
                                       n_units,
                                       initialW=I.Uniform(1. / n_units))
            if args.subword == 'rnn':
                self.rnn = RNN(n_vocab_char, n_units, n_units_char)
            self.loss_func = loss_func
Exemplo n.º 24
0
 def __init__(self, in_channels, out_channels, use_tanh=False):
     if True:
         initialW = initializers.Uniform(scale=0.05)
     else:
         wstd = math.sqrt(in_channels)
         initialW = initializers.Normal(wstd)
     super().__init__(W=Convolution1D(in_channels,
                                      3 * out_channels,
                                      1,
                                      stride=1,
                                      pad=0,
                                      initialW=initialW),
                      bf=Bias(shape=(out_channels, )),
                      br=Bias(shape=(out_channels, )))
     self.use_highway_connections = in_channels == out_channels
     self.use_tanh = use_tanh
     self.reset_state()
Exemplo n.º 25
0
    def __init__(self, vocab, vocab_ngram_tokens, n_units, n_units_char, dropout,
                 subword):  # dropout ratio, zero indicates no dropout
        super(SUMAVG, self).__init__()
        with self.init_scope():
            if subword.startswith('sum'):
                self.f_sumavg = F.sum
            if subword.startswith('avg'):
                self.f_sumavg = F.average

            self.embed = L.EmbedID(
                len(vocab_ngram_tokens.lst_words) + 2, n_units_char,
                initialW=I.Uniform(1. / n_units_char))  # ngram tokens embedding  plus 2 for OOV and end symbol.

            self.n_ngram = vocab_ngram_tokens.metadata["max_gram"] - vocab_ngram_tokens.metadata["min_gram"] + 1
            self.dropout = dropout
            self.vocab = vocab
            self.vocab_ngram_tokens = vocab_ngram_tokens
Exemplo n.º 26
0
    def __init__(self,
                 input_space,
                 output_space,
                 hidden_layer_sizes=(512, 512, 512),
                 activation=F.relu,
                 zero_bias=False):
        """
        Args:
            input_space -- the space that inputs will be drawn from
            output_space -- the space that outputs will be drawn from.

        Keyword Args:
            hidden_layer_sizes -- the number of units in each hidden layer in order
                                  [(512, 512, 512)]
            activation -- the activation function used on all layers [relu]
            zero_bias -- whether the bias should be initialized to zero [False]
        """
        assert isinstance(input_space, gym.Space)
        assert input_space.shape is not None
        if isinstance(output_space, gym.spaces.Discrete):
            out_units = output_space.n
        elif isinstance(output_space, gym.spaces.Box):
            assert len(output_space.shape) == 1
            out_units = output_space.shape[0]
        else:
            raise NotImplementedError

        self._input_space = input_space
        self._output_space = output_space
        self._num_hidden = len(hidden_layer_sizes)
        assert self._num_hidden > 0

        initial_weights = I.HeUniform()
        initial_bias = None if zero_bias else I.Uniform(1e-4)
        links = []
        for units in hidden_layer_sizes:
            links.append(
                L.Linear(None, units, False, initial_weights, initial_bias))

        links.append(
            L.Linear(None, out_units, False, initial_weights, initial_bias))
        self._activation = activation

        super(ChainerMLP, self).__init__(*links)
        self(np.zeros((1, ) + input_space.shape, input_space.dtype))
Exemplo n.º 27
0
 def __init__(self,
              n_vocab_char,
              n_units,
              n_units_char,
              index2charIds,
              dropout=.2):  #dropout ratio, zero indicates no dropout
     super(RNN, self).__init__()
     with self.init_scope():
         self.embed = L.EmbedID(n_vocab_char,
                                n_units_char,
                                initialW=I.Uniform(
                                    1. / n_units_char))  # word embedding
         self.mid = L.LSTM(n_units_char,
                           n_units_char)  # the first LSTM layer
         self.out = L.Linear(n_units_char,
                             n_units)  # the feed-forward output layer
         self.dropout = dropout
         self.index2charIds = index2charIds
Exemplo n.º 28
0
    def __init__(self,
                 n_vocab,
                 n_units,
                 filter_sizes,
                 n_filter,
                 drop_rate,
                 n_class,
                 init_E=None):
        """
        """
        self.n_vocab = n_vocab
        self.n_units = n_units
        self.filter_sizes = filter_sizes
        self.n_filter = n_filter
        self.drop_rate = drop_rate
        self.n_class = n_class
        super(CNNSentenceClassifier, self).__init__()

        with self.init_scope():
            # Embedding
            if init_E is None:
                init_E = initializers.Uniform(1. / n_units)
            self.add_link("embed", L.EmbedID(n_vocab, n_units,
                                             initialW=init_E))

            # Convolutions
            for sz in filter_sizes:
                self.add_link(
                    "conv_{}".format(sz),
                    L.Convolution2D(1,
                                    n_filter,
                                    ksize=(sz, n_units),
                                    stride=1,
                                    pad=0,
                                    initialW=initializers.HeNormal()))

            # FC
            self.add_link(
                "fc",
                L.Linear(len(filter_sizes) * n_filter,
                         n_class,
                         initialW=initializers.HeNormal()))
Exemplo n.º 29
0
    def __init__(self, vocab, vocab_ngram_tokens, n_units, n_units_char, dropout,
                 subword):  # dropout ratio, zero indicates no dropout
        super(RNN, self).__init__()
        with self.init_scope():
            self.embed = L.EmbedID(
                len(vocab_ngram_tokens.lst_words) + 2, n_units_char,
                initialW=I.Uniform(1. / n_units_char))  # ngram tokens embedding  plus 2 for OOV and end symbol.
            if 'lstm' in subword:
                self.mid = L.LSTM(n_units_char, n_units_char * 2)
            self.out = L.Linear(n_units_char * 2, n_units_char)  # the feed-forward output layer
            if 'bilstm' in subword:
                self.mid_b = L.LSTM(n_units_char, n_units_char * 2)
                self.out_b = L.Linear(n_units_char * 2, n_units_char)

            self.n_ngram = vocab_ngram_tokens.metadata["max_gram"] - vocab_ngram_tokens.metadata["min_gram"] + 1
            self.final_out = L.Linear(n_units * (self.n_ngram), n_units)

            self.dropout = dropout
            self.vocab = vocab
            self.vocab_ngram_tokens = vocab_ngram_tokens
            self.subword = subword
Exemplo n.º 30
0
    def __init__(self,
                 subword,
                 vocab,
                 vocab_ngram_tokens,
                 dimensions,
                 loss_func,
                 dropout=0):  # dropout ratio, zero indicates no dropout
        super(SkipGram, self).__init__()

        with self.init_scope():
            self.subword = subword
            self.vocab = vocab
            self.vocab_ngram_tokens = vocab_ngram_tokens
            self.n_ngram = vocab_ngram_tokens.metadata[
                "max_gram"] - vocab_ngram_tokens.metadata["min_gram"] + 1

            if 'none' in subword:
                self.word_embed = L.EmbedID(
                    len(vocab.lst_words) + 2,
                    dimensions,
                    initialW=I.Uniform(
                        1. / dimensions))  # plus 2 for OOV and end symbol.
            else:
                self.word_embed = None

            if subword.startswith('_none'):
                self.f = None
            # if subword.startswith('cnn_'):
            #     self.f = CNN(vocab, vocab_ngram_tokens, dimensions, dimensions, dropout)
            if subword.startswith('cnn1d'):
                self.f = CNN1D(vocab, vocab_ngram_tokens, dimensions,
                               dimensions, dropout, args.subword)
            if subword.startswith('bilstm') or subword.startswith('lstm'):
                self.f = RNN(vocab, vocab_ngram_tokens, dimensions, dimensions,
                             dropout, args.subword)
            if subword.startswith('avg') or subword.startswith('sum'):
                self.f = SUMAVG(vocab, vocab_ngram_tokens, dimensions,
                                dimensions, dropout, args.subword)

            self.loss_func = loss_func