Ejemplo n.º 1
0
def predict(t_net: DaRnnNet, t_dat: TrainData, train_size: int, batch_size: int, T: int, on_train=False):
    out_size = t_dat.targs.shape[1]
    if on_train:
        y_pred = np.zeros((train_size - T + 1, out_size))
    else:
        y_pred = np.zeros((t_dat.feats.shape[0] - train_size, out_size))

    for y_i in range(0, len(y_pred), batch_size):
        y_slc = slice(y_i, y_i + batch_size)
        batch_idx = range(len(y_pred))[y_slc]
        b_len = len(batch_idx)
        X = np.zeros((b_len, T - 1, t_dat.feats.shape[1]))
        y_history = np.zeros((b_len, T - 1, t_dat.targs.shape[1]))

        for b_i, b_idx in enumerate(batch_idx):
            if on_train:
                idx = range(b_idx, b_idx + T - 1)
            else:
                idx = range(b_idx + train_size - T, b_idx + train_size - 1)

            X[b_i, :, :] = t_dat.feats[idx, :]
            y_history[b_i, :] = t_dat.targs[idx]

        y_history = numpy_to_tvar(y_history)
        _, input_encoded = t_net.encoder(numpy_to_tvar(X))
        y_pred[y_slc] = t_net.decoder(input_encoded, y_history).cpu().data.numpy()

    return y_pred
Ejemplo n.º 2
0
def train_iteration(t_net: DaRnnNet, loss_func: typing.Callable, X, y_history, y_target):
    t_net.enc_opt.zero_grad()
    t_net.dec_opt.zero_grad()

    input_weighted, input_encoded = t_net.encoder(numpy_to_tvar(X))
    y_pred = t_net.decoder(input_encoded, numpy_to_tvar(y_history))

    y_true = numpy_to_tvar(y_target)
    loss = loss_func(y_pred, y_true)
    loss.backward()

    t_net.enc_opt.step()
    t_net.dec_opt.step()

    return loss.item()
Ejemplo n.º 3
0
def predict(t_net: DaRnnNet,
            t_dat: TrainData,
            train_size: int,
            batch_size: int,
            T: int,
            on_train=False,
            eval=False):
    out_size = t_dat.targs.shape[1]
    if on_train:
        y_pred = np.zeros((train_size - T + 1, out_size))
    else:
        y_pred = np.zeros((t_dat.feats.shape[0] - train_size, out_size))
        #y_pred = np.zeros((VALI_SIZE, out_size))

    if eval:
        y_pred = np.zeros((VALI_SIZE, out_size))

    for y_i in range(0, len(y_pred), batch_size):

        y_slc = slice(y_i, y_i + batch_size)
        batch_idx = range(len(y_pred))[y_slc]
        b_len = len(batch_idx)
        X = np.zeros((b_len, T - 1, t_dat.feats.shape[1]))
        y_history = np.zeros((b_len, T - 1, t_dat.targs.shape[1]))

        for b_i, b_idx in enumerate(batch_idx):
            if on_train:
                idx = range(b_idx, b_idx + T - 1)
            else:
                # ANDREA --> The validation set is chosen at random
                # b_idx = np.random.randint(0, len(t_dat.feats)-train_size)
                idx = range(b_idx + train_size - T, b_idx + train_size - 1)

            X[b_i, :, :] = t_dat.feats[idx, :]

            ## Leave it zeros
            # y_history[b_i, :] = t_dat.targs[idx]

        y_history = numpy_to_tvar(y_history)
        _, input_encoded = t_net.encoder(numpy_to_tvar(X))
        y_pred[y_slc] = t_net.decoder(input_encoded,
                                      y_history).cpu().data.numpy()

    return y_pred
Ejemplo n.º 4
0
def train_iteration(t_net: DaRnnNet, loss_func: typing.Callable, X, y_history,
                    y_target):
    t_net.enc_opt.zero_grad()
    t_net.dec_opt.zero_grad()

    X = Variable(torch.from_numpy(X).type(torch.FloatTensor).to(device))
    y_history = Variable(
        torch.from_numpy(y_history).type(torch.FloatTensor).to(device))
    y_target = Variable(
        torch.from_numpy(y_target).type(torch.FloatTensor).to(device))

    input_weighted, input_encoded = t_net.encoder(X)
    y_pred = t_net.decoder(input_encoded, y_history)

    y_true = y_target
    loss = loss_func(y_pred, y_true)
    loss.backward()

    t_net.enc_opt.step()
    t_net.dec_opt.step()

    return loss.item()