Esempio n. 1
0
 def check_inconsistent_input_size(self, h_data, xs_data, ws_data, bs_data):
     h = _wrap_variable(h_data)
     xs = _wrap_variable(xs_data)
     ws = _wrap_variable(ws_data)
     bs = _wrap_variable(bs_data)
     with self.assertRaises(ValueError):
         functions.n_step_rnn(
             self.n_layers, self.dropout, h, ws, bs, xs,
             activation=self.activation)
Esempio n. 2
0
    def check_forward(self, h_data, xs_data, ws_data, bs_data):
        h = chainer.Variable(h_data)
        xs = [chainer.Variable(x) for x in xs_data]
        ws = [[chainer.Variable(w) for w in ws] for ws in ws_data]
        bs = [[chainer.Variable(b) for b in bs] for bs in bs_data]
        hy, ys = functions.n_step_rnn(self.n_layers,
                                      self.dropout,
                                      h,
                                      ws,
                                      bs,
                                      xs,
                                      activation=self.activation)

        e_hy = self.hx.copy()
        for ind in range(self.length):
            x = self.xs[ind]
            batch = x.shape[0]
            for layer in range(self.n_layers):
                w = self.ws[layer]
                b = self.bs[layer]
                h_prev = e_hy[layer, :batch]
                if self.activation == 'tanh':
                    e_h = numpy.tanh(
                        x.dot(w[0].T) + h_prev.dot(w[1].T) + b[0] + b[1])
                elif self.activation == 'relu':
                    e_h = _relu(
                        x.dot(w[0].T) + h_prev.dot(w[1].T) + b[0] + b[1])

                e_hy[layer, :batch] = e_h

                x = e_h

            testing.assert_allclose(ys[ind].data, x, rtol=1e-4, atol=1e-4)

        testing.assert_allclose(hy.data, e_hy, rtol=1e-4, atol=1e-4)
Esempio n. 3
0
    def check_forward(
            self, h_data, xs_data, ws_data, bs_data):
        h = _wrap_variable(h_data)
        xs = _wrap_variable(xs_data)
        ws = _wrap_variable(ws_data)
        bs = _wrap_variable(bs_data)
        hy, ys = functions.n_step_rnn(
            self.n_layers, self.dropout, h, ws, bs, xs,
            activation=self.activation)

        e_hy = self.hx.copy()
        for ind in range(self.length):
            x = self.xs[ind]
            batch = x.shape[0]
            for layer in range(self.n_layers):
                w = self.ws[layer]
                b = self.bs[layer]
                h_prev = e_hy[layer, :batch]
                if self.activation == 'tanh':
                    e_h = numpy.tanh(x.dot(w[0].T) +
                                     h_prev.dot(w[1].T) + b[0] + b[1])
                elif self.activation == 'relu':
                    e_h = _relu(x.dot(w[0].T) +
                                h_prev.dot(w[1].T) + b[0] + b[1])

                e_hy[layer, :batch] = e_h

                x = e_h

            testing.assert_allclose(
                ys[ind].data, x, rtol=1e-4, atol=1e-4)

        testing.assert_allclose(hy.data, e_hy, rtol=1e-4, atol=1e-4)
Esempio n. 4
0
    def check_forward(
            self, h_data, xs_data, ws_data, bs_data, volatile):
        h = chainer.Variable(h_data, volatile=volatile)
        xs = [chainer.Variable(x, volatile=volatile) for x in xs_data]
        ws = [[chainer.Variable(w, volatile=volatile) for w in ws]
              for ws in ws_data]
        bs = [[chainer.Variable(b, volatile=volatile) for b in bs]
              for bs in bs_data]
        hy, ys = functions.n_step_rnn(
            self.n_layers, self.dropout, h, ws, bs, xs,
            use_cudnn=self.use_cudnn, activation=self.activation)

        e_hy = self.hx.copy()
        for ind in range(self.length):
            x = self.xs[ind]
            batch = x.shape[0]
            for layer in range(self.n_layers):
                w = self.ws[layer]
                b = self.bs[layer]
                h_prev = e_hy[layer, :batch]
                if self.activation == 'tanh':
                    e_h = numpy.tanh(x.dot(w[0].T) +
                                     h_prev.dot(w[1].T) + b[0] + b[1])
                elif self.activation == 'relu':
                    e_h = _relu(x.dot(w[0].T) +
                                h_prev.dot(w[1].T) + b[0] + b[1])

                e_hy[layer, :batch] = e_h

                x = e_h

            testing.assert_allclose(
                ys[ind].data, x, rtol=1e-4, atol=1e-4)

        testing.assert_allclose(hy.data, e_hy, rtol=1e-4, atol=1e-4)
Esempio n. 5
0
 def forward(self, inputs, device):
     h, ws, bs, xs = self.process_inputs(inputs)
     out = F.n_step_rnn(self.n_layers, 0.0, h, ws, bs, xs, self.activation)
     rets = []
     rets.append(out[0])
     for i in range(len(out[1])):
         rets.append(out[1][i])
     return tuple(rets)
Esempio n. 6
0
 def call_forward(self, train):
     hx = _wrap_variable(_to_gpu(self.hx))
     xs = _wrap_variable(_to_gpu(self.xs))
     ws = _wrap_variable(_to_gpu(self.ws))
     bs = _wrap_variable(_to_gpu(self.bs))
     with chainer.using_config('enable_backprop', train), \
             chainer.using_config('train', train):
         return functions.n_step_rnn(
             self.n_layers, self.dropout, hx, ws, bs, xs)
Esempio n. 7
0
 def call_forward(self, train):
     hx = _wrap_variable(_to_gpu(self.hx))
     xs = _wrap_variable(_to_gpu(self.xs))
     ws = _wrap_variable(_to_gpu(self.ws))
     bs = _wrap_variable(_to_gpu(self.bs))
     with chainer.using_config('enable_backprop', train), \
             chainer.using_config('train', train):
         return functions.n_step_rnn(self.n_layers, self.dropout, hx, ws,
                                     bs, xs)
Esempio n. 8
0
 def forward(self, train):
     with chainer.using_config('use_cudnn', self.use_cudnn), \
             chainer.using_config('enable_backprop', train), \
             chainer.using_config('train', train):
         h = chainer.Variable(self.hx)
         xs = [chainer.Variable(x) for x in self.xs]
         ws = [[chainer.Variable(w) for w in ws] for ws in self.ws]
         bs = [[chainer.Variable(b) for b in bs] for bs in self.bs]
         return functions.n_step_rnn(self.n_layers, self.dropout, h, ws, bs,
                                     xs)
Esempio n. 9
0
 def forward(self, train):
     volatile = not train
     h = chainer.Variable(self.hx, volatile=volatile)
     xs = [chainer.Variable(x, volatile=volatile) for x in self.xs]
     ws = [[chainer.Variable(w, volatile=volatile) for w in ws]
           for ws in self.ws]
     bs = [[chainer.Variable(b, volatile=volatile) for b in bs]
           for bs in self.bs]
     return functions.n_step_rnn(
         self.n_layers, self.dropout, h, ws, bs, xs,
         train=train, use_cudnn=self.use_cudnn)
Esempio n. 10
0
 def forward(self, train):
     with chainer.using_config('use_cudnn', self.use_cudnn), \
             chainer.using_config('enable_backprop', train), \
             chainer.using_config('train', train):
         h = chainer.Variable(self.hx)
         xs = [chainer.Variable(x) for x in self.xs]
         ws = [[chainer.Variable(w) for w in ws]
               for ws in self.ws]
         bs = [[chainer.Variable(b) for b in bs]
               for bs in self.bs]
         return functions.n_step_rnn(
             self.n_layers, self.dropout, h, ws, bs, xs)
Esempio n. 11
0
 def f(*inputs):
     (hx, ), inputs = _split(inputs, 1)
     ws = []
     for i in range(self.n_layers):
         weights, inputs = _split(inputs, 2)
         ws.append(weights)
     bs = []
     for i in range(self.n_layers):
         biases, inputs = _split(inputs, 2)
         bs.append(biases)
     xs = inputs
     hy, ys = functions.n_step_rnn(
         self.n_layers, self.dropout, hx, ws, bs, xs,
         activation=self.activation)
     return (hy, ) + ys
Esempio n. 12
0
 def f(*inputs):
     (hx, ), inputs = _split(inputs, 1)
     ws = []
     for i in range(self.n_layers):
         weights, inputs = _split(inputs, 2)
         ws.append(weights)
     bs = []
     for i in range(self.n_layers):
         biases, inputs = _split(inputs, 2)
         bs.append(biases)
     xs = inputs
     hy, ys = functions.n_step_rnn(
         self.n_layers, self.dropout, hx, ws, bs, xs,
         activation=self.activation)
     return (hy, ) + ys
Esempio n. 13
0
 def forward(self, train):
     volatile = not train
     h = chainer.Variable(self.hx, volatile=volatile)
     xs = [chainer.Variable(x, volatile=volatile) for x in self.xs]
     ws = [[chainer.Variable(w, volatile=volatile) for w in ws]
           for ws in self.ws]
     bs = [[chainer.Variable(b, volatile=volatile) for b in bs]
           for bs in self.bs]
     return functions.n_step_rnn(self.n_layers,
                                 self.dropout,
                                 h,
                                 ws,
                                 bs,
                                 xs,
                                 train=train,
                                 use_cudnn=self.use_cudnn)
Esempio n. 14
0
    def check_call_cudnn_backward_inference(self, use_cudnn):
        with chainer.using_config('use_cudnn', use_cudnn), \
                chainer.using_config('train', False):
            hx = _wrap_variable(_to_gpu(self.hx))
            xs = _wrap_variable(_to_gpu(self.xs))
            ws = _wrap_variable(_to_gpu(self.ws))
            bs = _wrap_variable(_to_gpu(self.bs))
            hy, ys = functions.n_step_rnn(self.n_layers, self.dropout, hx, ws,
                                          bs, xs)

            hy.grad = _to_gpu(self.dhy)
            if chainer.should_use_cudnn('>=auto', 5000):
                with self.assertRaises(RuntimeError):
                    hy.backward()
            else:
                with testing.patch('cupy.cudnn.rnn_backward_weights') as func:
                    hy.backward()
                assert not func.called
Esempio n. 15
0
    def check_call_cudnn_backward_inference(self, use_cudnn):
        with chainer.using_config('use_cudnn', use_cudnn), \
                chainer.using_config('train', False):
            hx = _wrap_variable(_to_gpu(self.hx))
            xs = _wrap_variable(_to_gpu(self.xs))
            ws = _wrap_variable(_to_gpu(self.ws))
            bs = _wrap_variable(_to_gpu(self.bs))
            hy, ys = functions.n_step_rnn(
                self.n_layers, self.dropout, hx, ws, bs, xs)

            hy.grad = _to_gpu(self.dhy)
            if chainer.should_use_cudnn('>=auto', 5000):
                with self.assertRaises(RuntimeError):
                    hy.backward()
            else:
                with testing.patch('cupy.cudnn.rnn_backward_weights') as func:
                    hy.backward()
                assert not func.called