def predict(t_net, t_dat, train_size, batch_size, T, 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
def train_iteration(model, loss_func, X, y_history, y_target): model.enc_opt.zero_grad() model.dec_opt.zero_grad() input_weighted, input_encoded = model.encoder(numpy_to_tvar(X)) y_pred = model.decoder(input_encoded, numpy_to_tvar(y_history)) y_true = numpy_to_tvar(y_target) # print("loss_func y_pred:",y_pred.shape,type(y_pred)) # print("loss_func y_true:",y_true.shape,type(y_true)) loss = torch.sqrt(loss_func(y_pred, y_true)) loss.backward() encoder_params = [p for p in model.encoder.parameters() if p.requires_grad] decoder_params = [p for p in model.decoder.parameters() if p.requires_grad] torch.nn.utils.clip_grad_norm_(encoder_params, args.clip) torch.nn.utils.clip_grad_norm_(decoder_params, args.clip) model.enc_opt.step() model.dec_opt.step() model.enc_sch.step() model.dec_sch.step() return loss.item()
def predict(encoder, decoder, t_dat, batch_size: int, T: int) -> Tuple[np.ndarray, List[float]]: y_pred = np.zeros((t_dat.feats.shape[0] - T + 1, t_dat.targs.shape[1])) run_times = [] 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): idx = range(b_idx, b_idx + T - 1) X[b_i, :, :] = t_dat.feats[idx, :] y_history[b_i, :] = t_dat.targs[idx] t_val_start = process_time() _, input_encoded = encoder(numpy_to_tvar(X)) y_pred[y_slc] = decoder(input_encoded, numpy_to_tvar(y_history)).cpu().data.numpy() t_val_end = process_time() run_times.append(t_val_start - t_val_end) return y_pred, run_times
def predict(encoder, decoder, t_dat, batch_size, T): y_pred = np.zeros((t_dat.feats.shape[0] - T + 1, t_dat.targs.shape[1])) print("feats: ", t_dat.feats.shape) print("targs: ", t_dat.targs.shape) print("y_pred:", y_pred.shape, len(y_pred)) print("batch_size:", batch_size) print("T:", T) 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] print(batch_idx) b_len = len(batch_idx) print("b_len:", b_len) X = np.zeros((b_len, T - 1, t_dat.feats.shape[1])) print("X:", X.shape) y_history = np.zeros((b_len, T - 1, t_dat.targs.shape[1])) print("y_history:", y_history.shape) for b_i, b_idx in enumerate(batch_idx): # print("b_i: ",b_i,"b_idx:",b_idx) idx = range(b_idx, b_idx + T - 1) # print("feats:",t_dat.feats[idx, :]) # print("targs:",t_dat.targs[idx]) X[b_i, :, :] = t_dat.feats[idx, :] y_history[b_i, :] = t_dat.targs[idx] y_history = numpy_to_tvar(y_history) print("y_history:", y_history.shape) _, input_encoded = encoder(numpy_to_tvar(X)) y_pred[y_slc] = decoder(input_encoded, y_history).cpu().data.numpy() print("y_pred:", y_pred.shape) return y_pred
def train_iteration(t_net, loss_func, 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()
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
def train_iteration(t_net: TCHA, loss_func: typing.Callable, X, y_history, y_target, speed): X = numpy_to_tvar(X) input_weighted, input_encoded = t_net.encoder(X) _,y_pred = t_net.decoder(input_encoded, numpy_to_tvar(y_history), numpy_to_tvar(speed)) y_true = numpy_to_tvar(y_target) loss = loss_func(y_pred, y_true) # loss = loss_func(numpy_to_tvar(y_pred[1]), y_true) t_net.enc_opt.zero_grad() t_net.dec_opt.zero_grad() loss.backward() t_net.enc_opt.step() t_net.dec_opt.step() return loss.item()
def predict(encoder, decoder, t_dat, batch_size: int, T: int) -> np.ndarray: y_pred = np.zeros((t_dat.feats.shape[0] - T + 1, t_dat.targs.shape[1])) 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): idx = range(b_idx, b_idx + T - 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 = encoder(numpy_to_tvar(X)) y_pred[y_slc] = decoder(input_encoded, y_history).cpu().data.numpy() return y_pred
def predict(t_net: TCHA, t_dat: TrainData, train_size: int, batch_size: int, T: int, interval: int, on_train=False): # summary_temporal = np.zeros(T) # summary_spatial = np.zeros((T, t_dat.feats.shape[1])) summary_temporal, summary_spatial = [], [] embeded_weights = np.zeros((T, t_dat.feats.shape[1])) count = 0 out_size = t_dat.targs.shape[1] if on_train: y_pred = np.zeros((train_size - T - interval, out_size)) y_tar = np.zeros((train_size - T - interval, out_size)) else: y_pred = np.zeros((t_dat.feats.shape[0] - train_size, out_size)) y_tar = np.zeros((t_dat.feats.shape[0] - train_size, out_size)) p = 0 for y_i in range(0, len(y_pred) - interval, batch_size): y_slc = slice(y_i, y_i + batch_size) # batch_idx:批次开始、结束的下标 batch_idx = range(len(y_pred))[y_slc] b_len = len(batch_idx) X = np.zeros((b_len, T, t_dat.feats.shape[1])) y_history = np.zeros((b_len, T, t_dat.targs.shape[1])) speed = np.zeros((b_len, T, t_dat.speeds.shape[1])) for b_i, b_idx in enumerate(batch_idx): # b_i:每批次下标号,需清0 # b_idx:开始下标下标号码,不清0 if on_train: idx = range(b_idx, b_idx + T) else: idx = range(b_idx + train_size - T - interval, b_idx + train_size - interval) X[b_i, :, :] = t_dat.feats[idx, :] y_history[b_i, :] = t_dat.targs[idx] speed[b_i, :] = t_dat.speeds[idx] y_tar[p] = t_dat.targs[idx[-1] + interval] p += 1 y_history = numpy_to_tvar(y_history) speed = numpy_to_tvar(speed) spatial_weights, input_encoded = t_net.encoder(numpy_to_tvar(X)) temporal_weights, pred = t_net.decoder(input_encoded, y_history, speed) y_pred[y_slc] = pred.cpu().data.numpy() # following comment: weights to be drawn spatial_weights = spatial_weights.cpu().detach().numpy() temporal_weights = temporal_weights.cpu().detach().numpy() count += spatial_weights.shape[0] # summary_spatial += spatial_weights.mean(axis=0) # summary_temporal += temporal_weights.mean(axis=0) for cnt in range(temporal_weights.shape[0]): # 每一批次中各路段平均 summary_spatial.append(spatial_weights.mean(axis=1)[cnt]) # 每一批次中各时段平均 summary_temporal.append(temporal_weights[cnt]) ss = np.array(summary_spatial) st = np.array(summary_temporal) # summary_temporal /= count # summary_spatial /= count # print(summary_spatial) # for i in range(summary_spatial.shape[1]): # for j in range(T): # embeded_weights[j][i] = summary_spatial[j][i] + summary_temporal[T - 1 - j] # pdSS, pdST = pd.DataFrame(ss), pd.DataFrame(st) # pdSS.to_excel('spatial weights.xls') # pdST.to_excel('temporal weights.xls') return (ss, st), y_pred, y_tar
def train(net, train_data, t_cfg, n_epochs=10, save_plots=False): iter_per_epoch = int(np.ceil(t_cfg.train_size * 1. / t_cfg.batch_size)) iter_losses = np.zeros(n_epochs * iter_per_epoch) epoch_losses = np.zeros(n_epochs) logging.info( f"Iterations per epoch: {t_cfg.train_size * 1. / t_cfg.batch_size:3.3f} ~ {iter_per_epoch:d}." ) n_iter = 0 # print("train_data.feats: ",train_data.feats.shape) # print("train_data.targs: ",train_data.targs.shape) for e_i in range(n_epochs): perm_idx = np.random.permutation( t_cfg.train_size - t_cfg.T ) # train_size - T is done so that when we take the next T samples for training we don't get an index error for t_i in range(0, t_cfg.train_size, t_cfg.batch_size): batch_idx = perm_idx[t_i:(t_i + t_cfg.batch_size)] # print("batch_idx: ",len(batch_idx)) feats, y_history, y_target = prep_train_data( batch_idx, t_cfg, train_data) loss = train_iteration(net, t_cfg.loss_func, feats, y_history, y_target) iter_losses[e_i * iter_per_epoch + t_i // t_cfg.batch_size] = loss # if (j / t_cfg.batch_size) % 50 == 0: # self.logger.info("Epoch %d, Batch %d: loss = %3.3f.", i, j / t_cfg.batch_size, loss) n_iter += 1 adjust_learning_rate(net, n_iter) epoch_losses[e_i] = np.mean(iter_losses[range( e_i * iter_per_epoch, (e_i + 1) * iter_per_epoch)]) if e_i % 10 == 0 or e_i == n_epochs - 1: y_test_pred = predict(net, train_data, t_cfg.train_size, t_cfg.batch_size, t_cfg.T, on_train=False) # print("y_test_pred:",y_test_pred.shape,type(y_test_pred)) # print("targs:",train_data.targs[t_cfg.train_size:].shape,type(train_data.targs[t_cfg.train_size:])) mse = (t_cfg.loss_func( numpy_to_tvar(y_test_pred), numpy_to_tvar( train_data.targs[t_cfg.train_size:]))).cpu().data.numpy() # print("mse:",mse) # print("y_test_pred:",y_test_pred.shape,type(y_test_pred)) # print("targs:",train_data.targs[t_cfg.train_size:].shape,type(train_data.targs[t_cfg.train_size:])) # TODO: make this MSE and make it work for multiple inputs val_loss = y_test_pred - train_data.targs[t_cfg.train_size:] logging.info( f"Epoch {e_i:d}, train loss: {epoch_losses[e_i]:3.3f}, val loss: {np.mean(np.abs(val_loss))}, mse loss: {mse}" ) # y_train_pred = predict(net, train_data, # t_cfg.train_size, t_cfg.batch_size, t_cfg.T, # on_train=True) # plt.figure() # plt.plot(range(1, 1 + len(train_data.targs)), train_data.targs, # label="True") # plt.plot(range(t_cfg.T, len(y_train_pred) + t_cfg.T), y_train_pred, # label='Predicted - Train') # plt.plot(range(t_cfg.T + len(y_train_pred), len(train_data.targs) + 1), y_test_pred, # label='Predicted - Test') # plt.legend(loc='upper left') # utils.save_or_show_plot(f"pred_{e_i}.png", save_plots) return iter_losses, epoch_losses
def train(model, train_data, t_cfg, n_epochs=10, save_plots=False): iter_per_epoch = int(np.ceil(t_cfg.train_size * 1. / t_cfg.batch_size)) iter_losses = np.zeros(n_epochs * iter_per_epoch) epoch_losses = np.zeros(n_epochs) logging.info( f"Iterations per epoch: {t_cfg.train_size * 1. / t_cfg.batch_size:3.3f} ~ {iter_per_epoch:d}." ) n_iter = 0 stored_loss = 100000000 for e_i in range(n_epochs): perm_idx = np.random.permutation( t_cfg.train_size - t_cfg.T ) # train_size - T is done so that when we take the next T samples for training we don't get an index error for t_i in range(0, t_cfg.train_size, t_cfg.batch_size): batch_idx = perm_idx[t_i:(t_i + t_cfg.batch_size)] # print("batch_idx: ",len(batch_idx)) feats, y_history, y_target = prep_train_data( batch_idx, t_cfg, train_data) loss = train_iteration(model, t_cfg.loss_func, feats, y_history, y_target) iter_losses[e_i * iter_per_epoch + t_i // t_cfg.batch_size] = loss # if (j / t_cfg.batch_size) % 50 == 0: # self.logger.info("Epoch %d, Batch %d: loss = %3.3f.", i, j / t_cfg.batch_size, loss) n_iter += 1 # adjust_learning_rate(model, n_iter) epoch_losses[e_i] = np.mean(iter_losses[range( e_i * iter_per_epoch, (e_i + 1) * iter_per_epoch)]) # if e_i % 500 ==0: # model = model._replace(enc_sch = optim.lr_scheduler.CosineAnnealingLR(model.enc_opt, args.epochs, eta_min = args.min_lr)) # model = model._replace(dec_sch = optim.lr_scheduler.CosineAnnealingLR(model.dec_opt, args.epochs, eta_min = args.min_lr)) if e_i % 10 == 0 or e_i == n_epochs - 1: y_test_pred = predict(model, train_data, t_cfg.train_size, t_cfg.batch_size, t_cfg.T, on_train=False) rmse = (torch.sqrt( t_cfg.loss_func( numpy_to_tvar(y_test_pred), numpy_to_tvar(train_data.targs[t_cfg.train_size:]))) ).cpu().data.numpy() rmsle = (rmsleloss( numpy_to_tvar(y_test_pred), numpy_to_tvar( train_data.targs[t_cfg.train_size:]))).cpu().data.numpy() val_loss = y_test_pred - train_data.targs[t_cfg.train_size:] logging.info( f"Epoch {e_i:d}, train loss: {epoch_losses[e_i]:3.3f}, val loss: {np.mean(np.abs(val_loss))}, rmse loss: {rmse}, rmsle loss: {rmsle}" ) if rmsle < stored_loss: stored_loss = rmsle torch.save(model.encoder.state_dict(), os.path.join("results", save_name, "encoder.pt")) torch.save(model.decoder.state_dict(), os.path.join("results", save_name, "decoder.pt")) logging.info("Saving model") return iter_losses, epoch_losses