Esempio n. 1
0
    def __init__(self, n_layers, in_size, out_size, dropout, use_bi_direction,
                 activation, **kwargs):
        argument.check_unexpected_kwargs(
            kwargs,
            use_cudnn='use_cudnn argument is not supported anymore. '
            'Use chainer.using_config')
        argument.assert_kwargs_empty(kwargs)

        weights = []
        direction = 2 if use_bi_direction else 1
        for i in six.moves.range(n_layers):
            for di in six.moves.range(direction):
                weight = link.Link()
                for j in six.moves.range(2):
                    if i == 0 and j < 1:
                        w_in = in_size
                    elif i > 0 and j < 1:
                        w_in = out_size * direction
                    else:
                        w_in = out_size
                    weight.add_param('w%d' % j, (out_size, w_in))
                    weight.add_param('b%d' % j, (out_size, ))
                    getattr(weight, 'w%d' % j).data[...] = numpy.random.normal(
                        0, numpy.sqrt(1. / w_in), (out_size, w_in))
                    getattr(weight, 'b%d' % j).data[...] = 0
                weights.append(weight)

        super(NStepRNNBase, self).__init__(*weights)

        self.n_layers = n_layers
        self.dropout = dropout
        self.activation = activation
        self.out_size = out_size
        self.direction = direction
        self.rnn = rnn.n_step_birnn if use_bi_direction else rnn.n_step_rnn
Esempio n. 2
0
    def __init__(self, n_layers, in_size, out_size, dropout, use_bi_direction,
                 **kwargs):
        argument.check_unexpected_kwargs(
            kwargs,
            use_cudnn='use_cudnn argument is not supported anymore. '
            'Use chainer.using_config')
        argument.assert_kwargs_empty(kwargs)

        weights = []
        direction = 2 if use_bi_direction else 1
        for i in six.moves.range(n_layers):
            for di in six.moves.range(direction):
                weight = link.Link()
                with weight.init_scope():
                    for j in six.moves.range(6):
                        if i == 0 and j < 3:
                            w_in = in_size
                        elif i > 0 and j < 3:
                            w_in = out_size * direction
                        else:
                            w_in = out_size
                        w = variable.Parameter(GlorotNormal(),
                                               (out_size, w_in))
                        b = variable.Parameter(0, (out_size, ))
                        setattr(weight, 'w%d' % j, w)
                        setattr(weight, 'b%d' % j, b)
                weights.append(weight)

        super(NStepGRUBase, self).__init__(*weights)

        self.n_layers = n_layers
        self.dropout = dropout
        self.out_size = out_size
        self.direction = direction
        self.rnn = rnn.n_step_bigru if use_bi_direction else rnn.n_step_gru
    def __init__(self, n_layers, in_size, out_size, dropout, use_cudnn,
                 use_bi_direction):
        weights = []
        direction = 2 if use_bi_direction else 1
        for i in six.moves.range(n_layers):
            for di in six.moves.range(direction):
                weight = link.Link()
                for j in six.moves.range(8):
                    if i == 0 and j < 4:
                        w_in = in_size
                    elif i > 0 and j < 4:
                        w_in = out_size * direction
                    else:
                        w_in = out_size
                    weight.add_param('w%d' % j, (out_size, w_in))
                    weight.add_param('b%d' % j, (out_size, ))
                    getattr(weight, 'w%d' % j).data[...] = numpy.random.normal(
                        0, numpy.sqrt(1. / w_in), (out_size, w_in))
                    getattr(weight, 'b%d' % j).data[...] = 0
                weights.append(weight)

        super(NStepLSTMBase, self).__init__(*weights)

        self.n_layers = n_layers
        self.dropout = dropout
        self.use_cudnn = use_cudnn
        self.out_size = out_size
        self.direction = direction
        self.rnn = rnn.n_step_bilstm if use_bi_direction else rnn.n_step_lstm
    def __init__(self,
                 n_layers,
                 in_size,
                 out_size,
                 dropout,
                 *,
                 initialW=None,
                 initial_bias=None,
                 **kwargs):
        if kwargs:
            argument.check_unexpected_kwargs(
                kwargs,
                use_cudnn='use_cudnn argument is not supported anymore. '
                'Use chainer.using_config',
                use_bi_direction='use_bi_direction is not supported anymore',
                activation='activation is not supported anymore')
            argument.assert_kwargs_empty(kwargs)

        weights = []
        if self.use_bi_direction:
            direction = 2
        else:
            direction = 1

        W_initializer = initializers._get_initializer(initialW)
        if initial_bias is None:
            initial_bias = 0
        bias_initializer = initializers._get_initializer(initial_bias)

        for i in six.moves.range(n_layers):
            for di in six.moves.range(direction):
                weight = link.Link()
                with weight.init_scope():
                    for j in six.moves.range(self.n_weights):
                        if i == 0 and j < self.n_weights // 2:
                            w_in = in_size
                        elif i > 0 and j < self.n_weights // 2:
                            w_in = out_size * direction
                        else:
                            w_in = out_size
                        w = variable.Parameter(W_initializer, (out_size, w_in))
                        b = variable.Parameter(bias_initializer, out_size)
                        setattr(weight, 'w%d' % j, w)
                        setattr(weight, 'b%d' % j, b)
                weights.append(weight)

        super(NStepRNNBase, self).__init__(*weights)

        self.ws = [[
            getattr(layer, 'w%d' % i) for i in six.moves.range(self.n_weights)
        ] for layer in self]
        self.bs = [[
            getattr(layer, 'b%d' % i) for i in six.moves.range(self.n_weights)
        ] for layer in self]

        self.n_layers = n_layers
        self.dropout = dropout
        self.out_size = out_size
        self.direction = direction
Esempio n. 5
0
    def __init__(self, n_layers, in_size, out_size, dropout, initialW,
                 initial_bias, use_bi_direction, **kwargs):
        argument.check_unexpected_kwargs(
            kwargs,
            use_cudnn='use_cudnn argument is not supported anymore. '
            'Use chainer.using_config')
        argument.assert_kwargs_empty(kwargs)

        if initial_bias is None:
            initial_bias = initializers.constant.Zero()
        initialW = initializers._get_initializer(initialW)

        weights = []
        direction = 2 if use_bi_direction else 1
        for i in six.moves.range(n_layers):
            for di in six.moves.range(direction):
                weight = link.Link()
                with weight.init_scope():
                    for j in six.moves.range(8):
                        if i == 0 and j < 4:
                            w_in = in_size
                        elif i > 0 and j < 4:
                            w_in = out_size * direction
                        else:
                            w_in = out_size
                        name_w = 'w{}'.format(j)
                        name_b = 'b{}'.format(j)
                        w = variable.Parameter(initialW, (out_size, w_in))
                        b = variable.Parameter(initial_bias, (out_size, ))
                        setattr(weight, name_w, w)
                        setattr(weight, name_b, b)
                weights.append(weight)

        super(NStepLSTMBase, self).__init__(*weights)

        self.n_layers = n_layers
        self.dropout = dropout
        self.out_size = out_size
        self.direction = direction
        self.rnn = rnn.n_step_bilstm if use_bi_direction else rnn.n_step_lstm
Esempio n. 6
0
    def __init__(
            self, n_layers, in_size, out_size, dropout, use_cudnn=True):
        weights = []
        for i in six.moves.range(n_layers):
            weight = link.Link()
            for j in six.moves.range(8):
                if i == 0 and j < 4:
                    w_in = in_size
                else:
                    w_in = out_size
                weight.add_param('w%d' % j, (out_size, w_in))
                weight.add_param('b%d' % j, (out_size,))
                getattr(weight, 'w%d' % j).data[...] = numpy.random.normal(
                    0, numpy.sqrt(1. / w_in), (out_size, w_in))
                getattr(weight, 'b%d' % j).data[...] = 0
            weights.append(weight)

        super(NStepLSTM, self).__init__(*weights)

        self.n_layers = n_layers
        self.dropout = dropout
        self.use_cudnn = use_cudnn
Esempio n. 7
0
    def __init__(
        self,
        n_layers,
        in_size,
        out_size,
        dropout,
        initialW=None,
        initial_bias=None,
        **kwargs
    ):
        if kwargs:
            argument.check_unexpected_kwargs(
                kwargs,
                use_cudnn="use_cudnn argument is not supported anymore. "
                "Use chainer.using_config",
                use_bi_direction="use_bi_direction is not supported anymore",
                activation="activation is not supported anymore",
            )
            argument.assert_kwargs_empty(kwargs)

        if initialW is None:
            W_initializer = normal.LeCunNormal()
        else:
            W_initializer = initializers._get_initializer(initialW)

        if initial_bias is None:
            initial_bias = 0
        bias_initializer = initializers._get_initializer(initial_bias)

        weights = []
        if self.use_bi_direction:
            direction = 2
        else:
            direction = 1

        for i in range(n_layers):
            for di in range(direction):
                weight = link.Link()
                with weight.init_scope():
                    for j in range(self.n_weights):
                        if i == 0 and j < self.n_weights // 2:
                            w_in = in_size
                        elif i > 0 and j < self.n_weights // 2:
                            w_in = out_size * direction
                        else:
                            w_in = out_size
                        w = variable.Parameter(W_initializer, (out_size, w_in))
                        b = variable.Parameter(bias_initializer, (out_size,))
                        setattr(weight, "w%d" % j, w)
                        setattr(weight, "b%d" % j, b)
                weights.append(weight)

        super().__init__(*weights)

        self.ws = [
            [getattr(layer, "w%d" % i) for i in range(self.n_weights)] for layer in self
        ]
        self.bs = [
            [getattr(layer, "b%d" % i) for i in range(self.n_weights)] for layer in self
        ]

        self.n_layers = n_layers
        self.dropout = dropout
        self.out_size = out_size
        self.direction = direction