def forward(self, xs, h): N, T = xs.shape N, H = h.shape self.lstm.set_state(h) out = self.embed.forward(xs) hs = np.repeat(h, T, axis=0).reshape(N, T, H) out = np.concatenate((hs, out), axis=2) out = self.lstm.forward(out) out = np.concatenate((hs, out), axis=2) score = self.affine.forward(out) self.cache = H return score
def forward(self, xs): hs1 = self.forward_lstm.forward(xs) hs2 = self.backward_lstm.forward(xs[:, ::-1]) hs2 = hs2[:, ::-1] hs = np.concatenate((hs1, hs2), axis=2) return hs
def forward(self, xs): o1 = self.forward_lstm.forward(xs) o2 = self.backward_lstm.forward(xs[:, ::-1]) o2 = o2[:, ::-1] out = np.concatenate((o1, o2), axis=2) return out
def forward(self, xs, enc_hs): h = enc_hs[:, -1] self.lstm.set_state(h) out = self.embed.forward(xs) dec_hs = self.lstm.forward(out) c = self.attention.forward(enc_hs, dec_hs) out = np.concatenate((c, dec_hs), axis=2) score = self.affine.forward(out) return score
def generate(self, h, start_id, sample_size): sampled = [] char_id = start_id self.lstm.set_state(h) H = h.shape[1] peeky_h = h.reshape(1, 1, H) for _ in range(sample_size): x = np.array([char_id]).reshape((1, 1)) out = self.embed.forward(x) out = np.concatenate((peeky_h, out), axis=2) out = self.lstm.forward(out) out = np.concatenate((peeky_h, out), axis=2) score = self.affine.forward(out) char_id = np.argmax(score.flatten()) sampled.append(char_id) return sampled
def generate(self, enc_hs, start_id, eos_id, max_len=40): sampled = [] sample_id = start_id h = enc_hs[:, -1] self.lstm.set_state(h) while sample_id != eos_id: if len(sampled) > max_len: break x = np.array([int(sample_id)]).reshape((1, 1)) out = self.embed.forward(x) dec_hs = self.lstm.forward(out) c = self.attention.forward(enc_hs, dec_hs) out = np.concatenate((c, dec_hs), axis=2) score = self.affine.forward(out) sample_id = np.argmax(score.flatten()) sampled.append(sample_id) return sampled