def r_lstm(seq_len, num_hidden, C): import my_layer as ml T = seq_len cs = [S.Variable('c')] hs = [S.Variable('h')] preds = [] datas = [S.Variable('data%d' % i) for i in range(T)] param = LSTMParam(x2g_weight=S.Variable("x2g_weight"), x2g_bias=S.Variable("x2g_bias"), h2g_weight=S.Variable("h2g_weight"), h2g_bias=S.Variable("h2g_bias"), Y_weight=S.Variable("Y_weight"), Y_bias=S.Variable("Y_bias")) for t in range(T): pred, c, h = r_lstm_step(datas[t], num_hidden, C, c=cs[-1], h=hs[-1], param=param) pred = S.LogisticRegressionOutput(data=pred, name='logis%d' % t) if t != 0: useless = S.Custom(prev_data=preds[-1], this_data=pred, name='regloss', op_type='regloss') preds.append(pred) cs.append(c) hs.append(h) return S.Group(preds)
def unroll_lstm(seq_len, num_hidden, C, H, W): T = seq_len cs = [S.Variable('c')] hs = [S.Variable('h')] preds = [] datas = [S.Variable('data%d' % i) for i in range(T)] param = LSTMParam(i2h_weight=S.Variable("i2h_weight"), i2h_bias=S.Variable("i2h_bias"), h2h_weight=S.Variable("h2h_weight"), h2h_bias=S.Variable("h2h_bias"), Y_weight=S.Variable("Y_weight"), Y_bias=S.Variable("Y_bias")) for t in range(T): pred, c, h = LSTM(datas[t], num_hidden, C, H, W, c=cs[-1], h=hs[-1], param=param) pred = S.LogisticRegressionOutput(data=pred, name='logis%d' % t) preds.append(pred) cs.append(c) hs.append(h) return S.Group(preds)
def train(self): return S.Group(self.last_states + [self.train_loss])
def eval(self): return S.Group(self.last_states + [self.eval_loss])