我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用chainer.functions.lstm()。
def decode_once(self, x, state, train=True): c = state['c'] h = state['h'] h_tilde = state.get('h_tilde', None) emb = self.trg_emb(x) lstm_in = self.eh(emb) + self.hh(h) if h_tilde is not None: lstm_in += self.ch(h_tilde) c, h = F.lstm(c, lstm_in) a = self.attender(h, train=train) h_tilde = F.concat([a, h]) h_tilde = F.tanh(self.w_c(h_tilde)) o = self.ho(h_tilde) state['c'] = c state['h'] = h state['h_tilde'] = h_tilde return o, state
def decode_once(self, x, state, train=True): l = state.get('lengths', self.lengths) c = state['c'] h = state['h'] h_tilde = state.get('h_tilde', None) emb = self.trg_emb(x) lemb = self.len_emb(l) lstm_in = self.eh(emb) + self.hh(h) + self.lh(lemb) if h_tilde is not None: lstm_in += self.ch(h_tilde) c, h = F.lstm(c, lstm_in) a = self.attender(h, train=train) h_tilde = F.concat([a, h]) h_tilde = F.tanh(self.w_c(h_tilde)) o = self.ho(h_tilde) state['c'] = c state['h'] = h state['h_tilde'] = h_tilde return o, state
def check_forward(self, x_data): xp = self.link.xp x = chainer.Variable(x_data) h1 = self.link(x) c0 = chainer.Variable(xp.zeros((len(self.x), self.out_size), dtype=self.x.dtype)) c1_expect, h1_expect = functions.lstm(c0, self.link.upward(x)) gradient_check.assert_allclose(h1.data, h1_expect.data) gradient_check.assert_allclose(self.link.h.data, h1_expect.data) gradient_check.assert_allclose(self.link.c.data, c1_expect.data) h2 = self.link(x) c2_expect, h2_expect = \ functions.lstm(c1_expect, self.link.upward(x) + self.link.lateral(h1)) gradient_check.assert_allclose(h2.data, h2_expect.data)
def check_forward(self, c_prev_data, x_data): c_prev = chainer.Variable(c_prev_data) x = chainer.Variable(x_data) c, h = functions.lstm(c_prev, x) self.assertEqual(c.data.dtype, self.dtype) self.assertEqual(h.data.dtype, self.dtype) # Compute expected out a_in = self.x[:, [0, 4]] i_in = self.x[:, [1, 5]] f_in = self.x[:, [2, 6]] o_in = self.x[:, [3, 7]] c_expect = _sigmoid(i_in) * numpy.tanh(a_in) + \ _sigmoid(f_in) * self.c_prev h_expect = _sigmoid(o_in) * numpy.tanh(c_expect) gradient_check.assert_allclose( c_expect, c.data, **self.check_forward_options) gradient_check.assert_allclose( h_expect, h.data, **self.check_forward_options)
def __call__(self, e, c, h, cxt=None): lstm_in = self.eh(e) + self.hh(h) c, h = F.lstm(c, lstm_in) return c, h
def step(self, e, c, h, backward): if backward: lstm_in = self.b_eh(e) + self.b_hh(h) else: lstm_in = self.eh(e) + self.hh(h) c, h = F.lstm(c, lstm_in) return c, h
def decode_once(self, x, state, train=True): c = state['c'] h = state['h'] emb = self.trg_emb(x) lstm_in = self.eh(emb) + self.hh(h) c, h = F.lstm(c, lstm_in) o = self.ho(h) state['c'] = c state['h'] = h return o, state
def decode_once(self, x, state, train=True): h = state['h'] c = state['c'] emb = self.trg_emb(x) a = self.attender(h, train=train) lstm_in = self.eh(emb) + self.hh(h) + self.ch(a) c, h = F.lstm(c, lstm_in) o = self.ho(h) state['h'] = h state['c'] = c return o, state
def _encode(self, x_list): batch_size = len(x_list[0]) pc = p = _zeros((batch_size, self.hidden_size)) for x in reversed(x_list): i = self.x_i(_mkivar(x)) pc, p = F.lstm(pc, self.i_p(i) + self.p_p(p)) return pc, p
def _decode_one_step(self, y, qc, q): j = self.y_j(_mkivar(y)) qc, q = F.lstm(qc, self.j_q(j) + self.q_q(q)) z = self.q_z(q) return z, qc, q
def _encode(self, x_list): batch_size = len(x_list[0]) source_length = len(x_list) # Encoding fc = bc = f = b = _zeros((batch_size, self.hidden_size)) i_list = [self.x_i(_mkivar(x)) for x in x_list] f_list = [] b_list = [] for i in i_list: fc, f = F.lstm(fc, self.i_f(i) + self.f_f(f)) f_list.append(f) for i in reversed(i_list): bc, b = F.lstm(bc, self.i_b(i) + self.b_b(b)) b_list.append(b) b_list.reverse() # Making concatenated matrix # {f,b}_mat: shape = [batch, srclen, hidden] f_mat = F.concat([F.expand_dims(f, 1) for f in f_list], 1) b_mat = F.concat([F.expand_dims(b, 1) for b in b_list], 1) # fb_mat: shape = [batch, srclen, 2 * hidden] fb_mat = F.concat([f_mat, b_mat], 2) # fbe_mat: shape = [batch * srclen, atten] fbe_mat = self.fb_e( F.reshape(fb_mat, [batch_size * source_length, 2 * self.hidden_size])) return fb_mat, fbe_mat, fc, bc, f_list[-1], b_list[0]
def _decode_one_step(self, y, pc, p, fb_mat, fbe_mat): j = self.y_j(_mkivar(y)) q = self._context(p, fb_mat, fbe_mat) pc, p = F.lstm(pc, self.j_p(j) + self.q_p(q) + self.p_p(p)) z = self.p_z(p) return z, pc, p
def __call__(self, word): x_list = [XP.iarray([min(ord(x), 0x7f)]) for x in word] ac = self.__EMBED_ZEROS a = self.__EMBED_ZEROS for x in x_list: ac, a = functions.lstm(ac, self.w_xa(x) + self.w_aa(a)) bc = self.__EMBED_ZEROS b = self.__EMBED_ZEROS for x in reversed(x_list): bc, b = functions.lstm(bc, self.w_xb(x) + self.w_bb(b)) return a, b
def __call__(self, c, x, a, b, y): return functions.lstm( c, self.w_xy(x) + self.w_ay(a) + self.w_by(b) + self.w_yy(y), )
def __call__(self, c, x, y): return functions.lstm( c, self.w_xy(x) + self.w_yy(y), )
def __call__(self, c, q, r, z): return functions.lstm( c, self.w_qz(q) + self.w_rz(r) + self.w_zz(z), )
def __call__(self, c, q, s1, s2): return functions.lstm( c, self.w_qs(q) + self.w_s1s(s1) + self.w_s2s(s2), )
def __call__(self, c, x, a, b, y): c, h = functions.lstm( c, self.w_xy(x) + self.w_ay(a) + self.w_by(b) + self.w_yy(y), ) return c, XP.dropout(h)
def __call__(self, c, x, y): c, h = functions.lstm( c, self.w_xy(x) + self.w_yy(y), ) return c, XP.dropout(h)
def __call__(self, c, a, b, s1, r1, s2, r2, z): c, h = functions.lstm( c, self.w_az(a) + self.w_bz(b) + self.w_s1z(s1) + self.w_r1z(r1) + \ self.w_s2z(s2) + self.w_r2z(r2) + self.w_zz(z), ) return c, XP.dropout(h)
def __call__(self, c, a, b, s1, s2): c, h = functions.lstm( c, self.w_as(a) + self.w_bs(b) + self.w_s1s(s1) + self.w_s2s(s2), ) return c, XP.dropout(h)
def __call__(self, c, a, b, s1, s2, z): c, h = functions.lstm( c, self.w_az(a) + self.w_bz(b) + self.w_s1z(s1) + self.w_s2z(s2) + self.w_zz(z), ) return c, XP.dropout(h)
def __call__(self, c, a, b, s1, s2, z): c, h = functions.lstm( c, self.w_as(a) + self.w_bs(b) + self.w_s1s(s1) + self.w_s2s(s2) + self.w_zs(z), ) return c, XP.dropout(h)
def __call__(self, c, x, y): return functions.lstm(c, self.w_xy(x) + self.w_yy(y))
def __call__(self, c, x1, x2, y): return functions.lstm(c, self.w_x1y(x1) + self.w_x2y(x2) + self.w_yy(y))
def __call__(self, c, q, s1, s2, z): return functions.lstm( c, self.w_qs(q) + self.w_s1s(s1) + self.w_s2s(s2) + self.w_zs(z), )
def __call__(self, c, x, a, b, y): u = XP.dropout(functions.tanh(self.w_xu(x) + self.w_au(a) + self.w_bu(b))) c, h = functions.lstm(c, self.w_uy(u) + self.w_yy(y)) return c, XP.dropout(h)
def __call__(self, c, x, y): c, h = functions.lstm(c, self.w_xy(x) + self.w_yy(y)) return c, XP.dropout(h)
def __call__(self, c, a, b, s1, r1, s2, r2, z): u = XP.dropout(functions.tanh( self.w_au(a) + self.w_bu(b) + self.w_s1u(s1) + self.w_r1u(r1) + \ self.w_s2u(s2) + self.w_r2u(r2), )) c, h = functions.lstm(c, self.w_uz(u) + self.w_zz(z)) return c, XP.dropout(h)
def __call__(self, c, a, b, s1, s2): u = XP.dropout(functions.tanh(self.w_au(a) + self.w_bu(b) + self.w_s2u(s2))) c, h = functions.lstm(c, self.w_us(u) + self.w_s1s(s1)) return c, XP.dropout(h)
def __call__(self, c, a, b, s1, s2, z): return functions.lstm( c, self.w_as(a) + self.w_bs(b) + self.w_s1s(s1) + self.w_s2s(s2) + self.w_zs(z), )
def __call__(self, c, a, b, s1, r1, s2, r2, z): return functions.lstm( c, self.w_az(a) + self.w_bz(b) + self.w_s1z(s1) + self.w_r1z(r1) + \ self.w_s2z(s2) + self.w_r2z(r2) + self.w_zz(z), )
def __call__(self, word): x_list = [XP.iarray([min(ord(x), 0x7f)]) for x in word] ac = self.__EMBED_ZEROS a = self.__EMBED_ZEROS for x in x_list: ac, a = functions.lstm(ac, self.w_xa(x) + self.w_aa(a)) a = XP.dropout(a) bc = self.__EMBED_ZEROS b = self.__EMBED_ZEROS for x in reversed(x_list): bc, b = functions.lstm(bc, self.w_xb(x) + self.w_bb(b)) b = XP.dropout(b) return a, b
def __call__(self, c, q, s1, s2): return functions.lstm(c, self.w_qs(q) + self.w_s1s(s1) + self.w_s2s(s2))
def __call__(self, word): x_list = [min(ord(x), 0x7f) for x in word] ac = self.__EMBED_ZEROS a = self.__EMBED_ZEROS for x in x_list: ac, a = functions.lstm(ac, self.__v_xa[x] + self.w_aa(a)) bc = self.__EMBED_ZEROS b = self.__EMBED_ZEROS for x in reversed(x_list): bc, b = functions.lstm(bc, self.__v_xb[x] + self.w_bb(b)) return a, b