def run(checkpoint=None, dir=CHECKPOINT_DIR): gc.collect() batch_size = 1 device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # torch.autograd.set_detect_anomaly(True) clean_train_loader = DataLoader(clean_train_data, batch_size=batch_size, shuffle=True, num_workers=2, pin_memory=True) clean_test_loader = DataLoader(clean_test_data, batch_size=batch_size, shuffle=False, num_workers=2, pin_memory=True) if checkpoint: model = load_checkpoint(checkpoint, dir=dir).to(device) else: model = RNN(hidden_size=80, output_size=5, n_layers=2, batch_size=batch_size, bidirectional=False).to(device) optimizer = torch.optim.Adam(model.parameters(), lr=0.001) loss = torch.nn.CrossEntropyLoss() losses = train(clean_train_loader, clean_test_loader, 500, model, optimizer, loss, device, checkpoint_dir=CHECKPOINT_DIR) return model, losses
import numpy as np import matplotlib.pyplot as plt from torch.autograd import Variable from dataset import dataset, dataset2 from network import RNN import os model_dir = '/home/lixiaoyu/project/airQuality/Analysis-of-Air-Quality-and-Outpatient-Quantity/ckpt/' TIME_STEP = 120 INPUT_SIZE = 7 HIDDEN_SIZE = 32 LR = 0.01 EPOCH = 1000 rnn = RNN(INPUT_SIZE=INPUT_SIZE, HIDDEN_SIZE=HIDDEN_SIZE) optimizer = torch.optim.Adam(rnn.parameters(), lr=LR) loss_func = nn.MSELoss() LoadModel = False def train(train_loader, num_e): torch.manual_seed(1) if LoadModel: checkpoint = torch.load(model_dir + '{}.ckpt'.format(num_e)) rnn.load_state_dict(checkpoint['state_dict']) print('Loading model~~~~~~~~~~', num_e) for e in range(EPOCH): print('epoch>>>>>>> ', e) rnn.train()
opts = options.parse() #data loader data_loader = data.dataloader(opts) train_loader = util.create_dataset(data_loader.train_data, data_loader.letteridx, data_loader.labelidx, opts) test_loader = util.create_dataset(data_loader.test_data, data_loader.letteridx, data_loader.labelidx, opts) from network import RNN from train import trainer from test import tester '''RNN model''' RNN = RNN(opts, data_loader.letteridx).to(device) if opts.print_model: print(RNN) '''Optimizers''' import torch.optim as optim RNN_optim = optim.Adam(RNN.parameters(), lr=opts.lr, betas=(opts.beta1, opts.beta2)) '''Criterion''' criterion = nn.NLLLoss() '''run training''' trainer(opts, RNN, RNN_optim, criterion, train_loader) '''test''' tester(opts, RNN, test_loader)
test_labels = onehot_sequence(th.from_numpy(test_labels), 10, cuda) test_set = TensorDataset(test_data, test_labels) test_loader = DataLoader(test_set, args.batch_size) cnn_path = args.pretrained_cnn_path if args.pretrained_cnn else None model = RNN(args.n_units, 10, cnn_path, cuda) if args.gpu > -1: model.cuda() if args.criterion == 'regression_loss': from criterions import regression_loss criterion = regression_loss(args.entropy_scale) else: criterion = getattr(__import__('criterions'), args.criterion)() if args.gpu > -1: criterion.cuda() optimizer = Adam(model.parameters(), lr=1e-3) vis = visdom.Visdom() tb_path = args.tensorboard_path if args.tensorboard_log: tb_path += '/%s' % args.tensorboard_log TensorboardVisualizer.configure(tb_path) loss_list = [] ratio_list = [] if not args.disable_tensorboard: loss_tb = TensorboardVisualizer('loss' + args.tensorboard_postfix) ratio_tb = TensorboardVisualizer('training accuracy' + args.tensorboard_postfix) validation_tb = TensorboardVisualizer('validation accuracy' + args.tensorboard_postfix) if not args.disable_visdom: loss_vis = VisdomVisualizer(vis, {'title': 'loss'}) ratio_vis = VisdomVisualizer(vis, {'title': 'training accuracy'}) validation_vis = VisdomVisualizer(vis, {'title': 'validation accuracy'})