total_steps += config.summary_steps print ("\r", "val [{}] loss: {:.3f}, Perplexity: {:.3f}, blue score: {:.3f} ".format(total_steps, val_loss, np.exp(val_loss), bleu_score)) # 儲存模型和結果 if total_steps % config.store_steps == 0 or total_steps >= config.num_steps: save_model_t(model, optimizer, config.store_model_path, total_steps, timestr) # save_model(model, optimizer, config.store_model_path, total_steps) with open(f'{config.store_model_path}/output_{timestr}_{total_steps}.txt', 'w') as f: for line in result: print (line, file=f) return train_losses, val_losses, bleu_scores if __name__ == '__main__': config = configurations() print ('config:\n', vars(config)) train_losses, val_losses, bleu_scores = train_process(config) timestr = time.strftime("%Y%m%d-%H-%M-%S") plt.figure() plt.plot(train_losses) plt.xlabel('iters') plt.ylabel('loss') plt.title('train loss') plt.savefig(f'./{config.figure_output_path}/train_loss_{timestr}.png') plt.clf() plt.figure()
resultsFileName = './e1/livello_{}/results_{}_{}_{}_keyboard_bis.txt'.format(level,af,opt,level) finally: line_storage.sort(key = lambda x: x.loss) config.printResults(resultsFileName,line_storage,simulation_tStart,time(),af,opt,args["approximation"]) if args["p"]: print("Plot best experiment loss history: \n{}".format(line_storage[0].toString())) config.plot_history(line_storage[0].history) return 0 if __name__ == '__main__': import sys import argparse parser = argparse.ArgumentParser(description="Esperimento Livello 1") parser.add_argument("activation-function", choices=['sigmoid','tanh','relu'], help="funzioni di attivazione per la rete test") parser.add_argument("optimizator", choices=['sgd', 'adagrad','adam'], help="funzioni di ottimizzazione") parser.add_argument("approximation", choices=["ideal","noref","ref"], help="Approssimazione: ideale, senza riflessioni, con riflessioni", default=0) parser.add_argument("-p", action="store_true", help="Alla fine della simulazione avvia la procedura di plot della loss") args = vars(parser.parse_args()) #print(args) config.configurations() sys.exit(main(args))
from utils import * from dataset import EN2CNDataset from model import * os.environ["CUDA_VISIBLE_DEVICES"] = '0' seed = 1114 np.random.seed(seed) random.seed(seed) torch.cuda.empty_cache() torch.manual_seed(seed) torch.cuda.manual_seed(seed) torch.backends.cudnn.deterministic = True if __name__ == '__main__': print("\nLoading configurations...", flush=True) config = configurations(sys.argv) config.load_model = True print('config:\n', vars(config)) print("\nLoading data...", flush=True) test_dataset = EN2CNDataset(config.data_path, config.seq_len, "testing") test_loader = data.DataLoader(test_dataset, batch_size=1) print("\nBuilding model...", flush=True) model, _ = build_model(config, test_dataset.en_size, test_dataset.cn_size) criterion = nn.CrossEntropyLoss(ignore_index=0) print("\nStart testing...", flush=True) test_loss, bleu_score, result = test_model(model, test_loader, criterion, config.beam_width)