def forward(self, inputs, device): h, ws, bs, xs = self.process_inputs(inputs) if h.array.dtype == numpy.float64: with chainer.using_config('use_cudnn', 'never'): out = F.n_step_bigru(self.n_layers, 0.0, h, ws, bs, xs) else: out = F.n_step_bigru(self.n_layers, 0.0, h, ws, bs, xs) rets = [] rets.append(out[0]) for i in range(len(out[1])): rets.append(out[1][i]) return tuple(rets)
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_bigru( self.n_layers, self.dropout, h, ws, bs, xs) xs_next = self.xs e_hy = self.hx.copy() for layer in range(self.n_layers): # forward di = 0 xf = [] layer_idx = layer * 2 + di w = self.ws[layer_idx] b = self.bs[layer_idx] for ind in range(self.length): x = xs_next[ind] batch = x.shape[0] h_prev = e_hy[layer_idx, :batch] # GRU z = sigmoid(x.dot(w[1].T) + h_prev.dot(w[4].T) + b[1] + b[4]) r = sigmoid(x.dot(w[0].T) + h_prev.dot(w[3].T) + b[0] + b[3]) h_bar = numpy.tanh(x.dot(w[2].T) + r * ((h_prev).dot(w[5].T) + b[5]) + b[2]) e_h = (1 - z) * h_bar + z * h_prev e_hy[layer_idx, :batch] = e_h xf.append(e_h) # backward di = 1 xb = [] layer_idx = layer * 2 + di w = self.ws[layer_idx] b = self.bs[layer_idx] for ind in reversed(range(self.length)): x = xs_next[ind] batch = x.shape[0] h_prev = e_hy[layer_idx, :batch] # GRU z = sigmoid(x.dot(w[1].T) + h_prev.dot(w[4].T) + b[1] + b[4]) r = sigmoid(x.dot(w[0].T) + h_prev.dot(w[3].T) + b[0] + b[3]) h_bar = numpy.tanh(x.dot(w[2].T) + r * ((h_prev).dot(w[5].T) + b[5]) + b[2]) e_h = (1 - z) * h_bar + z * h_prev e_hy[layer_idx, :batch] = e_h xb.append(e_h) xb.reverse() xs_next = [numpy.concatenate([hfi, hbi], axis=1) for (hfi, hbi) in zip(xf, xb)] for k, (ysi, xsi) in enumerate(zip(ys, xs_next)): testing.assert_allclose(ysi.data, xsi, rtol=1e-4, atol=1e-4) testing.assert_allclose(hy.data, e_hy, rtol=1e-4, atol=1e-4)
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_bigru( self.n_layers, self.dropout, hx, ws, bs, xs)
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_bigru(self.n_layers, self.dropout, h, ws, bs, xs)
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_bigru( self.n_layers, self.dropout, h, ws, bs, xs)
def f(*inputs): (hx, ), inputs = _split(inputs, 1) ws = [] for i in range(self.n_layers * 2): weights, inputs = _split(inputs, 6) ws.append(weights) bs = [] for i in range(self.n_layers * 2): biases, inputs = _split(inputs, 6) bs.append(biases) xs = inputs hy, ys = functions.n_step_bigru( self.n_layers, self.dropout, hx, ws, bs, xs) return (hy, ) + ys
def f(*inputs): (hx, ), inputs = _split(inputs, 1) ws = [] for i in range(self.n_layers * 2): weights, inputs = _split(inputs, 6) ws.append(weights) bs = [] for i in range(self.n_layers * 2): biases, inputs = _split(inputs, 6) bs.append(biases) xs = inputs hy, ys = functions.n_step_bigru(self.n_layers, self.dropout, hx, ws, bs, xs) return (hy, ) + ys
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_bigru(self.n_layers, self.dropout, h, ws, bs, xs, train=train, use_cudnn=self.use_cudnn)