def run(): with open('{}/time.txt'.format(PATH), 'a') as f: f.write('{},\n'.format(time.strftime('%Y%m%d_%H_%M_%S'))) f.close() model = netLSTM() with torch.no_grad(): model = model.cuda() net_enn_train = enn.ENN(model, NE) for epoch in range(config.epoch): for i, data in enumerate(textLoader): print('#' * 30) print("{}: batch{}".format(time.strftime('%Y%m%d_%H_%M_%S'), i)) input_, target = data #input_ = torch.from_numpy(np.stack(list(shrink(input_, 5)), axis=1)) #target = torch.from_numpy(np.stack(list(shrink(target, 5)), axis=1)) with torch.no_grad(): input_, target = map(Variable, (input_.float(), target.float())) target = target[:, -config.predict_len:, :] print(target.shape) target = target.reshape(-1, config.output_dim) input_ = input_.cuda() target = target.cuda() net_enn_train, loss, pred_data = train(net_enn_train, input_, target) # save pred and target while training save_dir = os.path.join(PATH, 'predict_history') if not os.path.exists(save_dir): os.mkdir(save_dir) save_data = {} save_data['pred'] = np.array(pred_data.mean(0)[:, 0]) save_data['target'] = np.array(np.array(target[:, 0])) save_data = pd.DataFrame.from_dict(save_data) save_filename = '{}_{}.csv'.format(epoch, i) save_data.to_csv(os.path.join(save_dir, save_filename)) """ with open('predict_history'+'/pred.txt', 'a') as f: f.write(list_to_csv(np.array(pred_data.mean(0)[:, 0])) + '\n') f.close() with open('predict_history'+'/target.txt', 'a') as f: f.write(list_to_csv(np.array(target[:, 0])) + '\n') f.close() """ with open(PATH + '/time.txt', 'a') as f: f.write( time.strftime('%Y%m%d_%H_%M_%S') + ',' + str(loss) + ',\n') f.close() with torch.no_grad(): params = net_enn_train.get_parameter() filename = PATH + "/parameters_epoch{}".format(epoch) save_var(params, filename) del params
def train(net_enn, input_, target, feature_name=''): dstb_y = lamuda.Lamuda(target, NE, ERROR_PER) train_losses = Record() losses = Record() lamuda_history = Record() std_history = Record() pred_history = Record() initial_parameters = net_enn.initial_parameters initial_pred = net_enn.output(input_) train_losses.update(criterion(initial_pred.mean(0), target).tolist()) losses.update(criterion(initial_pred.mean(0), target).tolist()) std_history.update(dstb_y.std(initial_pred)) pred_history.update(initial_pred) lamuda_history.update(dstb_y.lamuda(initial_pred)) for j in range(T): torch.cuda.empty_cache() params = net_enn.get_parameter() dstb_y.update() time_ = time.strftime('%Y%m%d_%H_%M_%S') delta = enrml.EnRML(pred_history.get_latest(mean=False), params, initial_parameters, lamuda_history.get_latest(mean=False), dstb_y.dstb, ERROR_PER) params_raw = net_enn.update_parameter(delta) torch.cuda.empty_cache() pred = net_enn.output(input_) loss_new = criterion(pred.mean(0), target).tolist() bigger = train_losses.check(loss_new) record_while = 0 while bigger: record_while += 1 lamuda_history.update( lamuda_history.get_latest(mean=False) * GAMMA) if lamuda_history.get_latest(mean=False) > GAMMA**10: lamuda_history.update(lamuda_history.data[0]) print('abandon current iteration') net_enn.set_parameter(params) loss_new = train_losses.get_latest() dstb_y.update() params_raw = params break dstb_y.update() net_enn.set_parameter(params) delta = enrml.EnRML(pred_history.get_latest(mean=False), params, initial_parameters, lamuda_history.get_latest(mean=False), dstb_y.dstb, ERROR_PER) params_raw = net_enn.update_parameter(delta) torch.cuda.empty_cache() pred = net_enn.output(input_) loss_new = criterion(pred.mean(0), target).tolist() print('update losses, new loss:{}'.format(loss_new)) bigger = train_losses.check(loss_new) train_losses.update(loss_new) save_var(params_raw, '{}/{}_{}_params'.format(PATH, time_, feature_name)) print("iteration:{} \t current train losses:{}".format( j, train_losses.get_latest(mean=True))) save_txt( '{}/loss_{}.txt'.format(PATH, feature_name), time.strftime('%Y%m%d_%H_%M_%S') + ',' + str(train_losses.get_latest(mean=True)) + ',\n') pred_history.update(pred) std_history.update(dstb_y.std(pred)) if std_history.bigger(): lamuda_history.update(lamuda_history.get_latest(mean=False)) else: lamuda_tmp = lamuda_history.get_latest(mean=False) / GAMMA if lamuda_tmp < 0.005: lamuda_tmp = 0.005 lamuda_history.update(lamuda_tmp) return net_enn, train_losses.get_latest( mean=True), pred_history.get_latest(mean=False)
def run(): for epoch in range(config.epoch): print(epoch) while config.input_dim + 1 <= len(config.columns): current_feature_name = config.columns[config.input_dim] textLoader = DataLoader(text, batch_size=config.batch_size, shuffle=True, num_workers=config.num_workers, drop_last=config.drop_last) model = netLSTM_withbn() with torch.no_grad(): model = model.cuda() net_enn_train = enn.ENN(model, NE) # If pre_existent epoch found, set net_enn_train parameters with pre_existent epoch record. # Only processed if current epoch count is 0 epoch_list = [ i for i in os.listdir(PATH) if i.startswith( "parameters_{}_epoch_".format(current_feature_name)) ] if len(epoch_list) > 0 and epoch == 0: print("Pre_existent epoch found: {}".format( sorted(epoch_list)[-1])) epoch_pre_existent = pickle.load( open(os.path.join(PATH, sorted(epoch_list)[-1]), 'rb')) net_enn_train.set_parameter(epoch_pre_existent) if epoch > 0: parameter_path = os.path.join( PATH, "parameters_{}_epoch_{}".format(current_feature_name, epoch - 1)) print("Setting checkpoint {}".format(parameter_path)) parameter_checkpoint = pickle.load(open(parameter_path, 'rb')) net_enn_train.set_parameter(parameter_checkpoint) for i, data in enumerate(textLoader): print('#' * 30) print("{}: batch{}".format(time.strftime('%Y%m%d_%H_%M_%S'), i)) # preparing the train data input_, target = data input_ = torch.from_numpy( np.stack(list(shrink(input_, config.shrink_len)), axis=1)) target = torch.from_numpy( np.stack(list(shrink(target, config.shrink_len)), axis=1)) with torch.no_grad(): input_, target = map(Variable, (input_.float(), target.float())) target = target.reshape(-1, config.output_dim) input_ = input_.cuda() target = target.cuda() # train the model net_enn_train, loss, pred_data = train( net_enn_train, input_, target, feature_name=current_feature_name) with torch.no_grad(): params = net_enn_train.get_parameter() filename = PATH + "/parameters_{}_epoch_{}".format( current_feature_name, epoch) save_var(params, filename) del params test(net_enn_train, feature_name=current_feature_name, draw_result=(epoch == config.epoch - 1)) config.input_dim += 1 text.reset_train_dataset() config.input_dim -= 3 text.reset_train_dataset() text.reset_test_dataset()