b = [b == i for i in range(16)] b = torch.stack(b, dim=1).float() preds = model(b.view(-1, 16, 4, 4)) preds = torch.argsort(preds.cpu(), dim=1, descending=True) dead_s = games.move_batch(preds) if dead_s: result.append(count) break count += 1 return np.mean(result) if __name__ == '__main__': from time import time from network import ConvNet device = 'cuda' if torch.cuda.is_available() else 'cpu' print(f'Using {device}') names = [ ] for name in names: print(name) m = ConvNet(**{'channels': 64, 'blocks': 3}) m.load_state_dict(torch.load('models/{}.pt'.format(name), map_location=device)) m.to(device) t = time() print(eval_nn(m, name, number=5000, device=device, verbose=True)) t = time() - t print('{0:.3f} seconds'.format(t)) print('-'*10)
acc = acc / NBATCHES print("\r epoch=%d" % i, "loss=%4.4f" % loss, "accuracy=%0.4f" % acc) ### Store current model torch.save(model.state_dict(), modelDir + "/%d_%0.2f.model" % (i, acc)) ### Break on good performance if loss < 0.001 and 0.999 < acc: break except KeyboardInterrupt: print("\n\nTraining stopped prematurely") ### Calculate accuracy print("\nTesting CNN on cpu..") model = model.to("cpu") model.eval() correct = 0 for nB in range(0, NTEST, batchSize): y = model(dataTest[nB:nB+batchSize]) _, predicted = torch.max(y.data, 1) correct += (predicted == labelsTest[nB:nB+batchSize]).sum().item() print("Accuracy : %0.2f" % (correct / NTEST)) data, iClasses, classToLabel = dataloader.loadAndPrepAllImages(nFiles=NFILES, imsize=IMAGE_SIZE, classes=classes) data = torch.FloatTensor(data) correct = [0] * NCLASSES table = np.zeros((NCLASSES, NCLASSES)) #[[0] * NCLASSES] * NCLASSES
def main(args): if args.name: args.name += '_' logname = f'{args.name}{args.t_tuple[0]}_{args.t_tuple[1]}_soft{args.soft}' \ f'c{args.channels}b{args.blocks}_p{args.patience}_' \ f'bs{args.batch_size}lr{args.lr}d{args.decay}_s{args.seed}' print(logname) random.seed(args.seed) np.random.seed(args.seed) torch.manual_seed(args.seed) device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') print('Using {}'.format(device)) torch.backends.cudnn.benchmark = True train_set = OneHotConvGameDataset(args.path, args.t_tuple[0], args.t_tuple[1], device, soft=args.soft) train_dat = DataLoader(train_set, batch_size=args.batch_size, shuffle=True) m = ConvNet(channels=args.channels, blocks=args.blocks) if args.pretrained: m.load_state_dict(torch.load('models/{}.pt'.format(args.pretrained), map_location=device)) print('Loaded ' + args.pretrained) logname = 'pre_'+logname m.to(device) loss_fn = nn.KLDivLoss(reduction='batchmean') optimizer = torch.optim.Adam(m.parameters(), lr=args.lr, weight_decay=args.decay) t_loss = [] min_move = [] best = 0.0 timer = 0 if args.patience == 0: stop = args.epochs else: stop = args.patience data_len = len(train_dat) for epoch in range(args.epochs): print('-' * 10) print('Epoch: {}'.format(epoch)) timer += 1 m.train() running_loss = 0 for x, y in tqdm(train_dat): optimizer.zero_grad() pred = m(x) loss = loss_fn(pred, y) running_loss += loss.data.item() loss.backward() optimizer.step() running_loss /= data_len if epoch == 2 and running_loss > 210/1000: stop = 0 print('Train mLoss: {:.3f}'.format(1e3 * running_loss)) t_loss.append(running_loss) m.eval() time1 = time() ave_min_move = eval_nn_min(m, number=10, repeats=40, device=device) time_str = ', took {:.0f} seconds'.format(time()-time1) min_move.append(ave_min_move) if ave_min_move >= best: tqdm.write(str(ave_min_move) + ' ** Best' + time_str) best = ave_min_move timer = 0 torch.save(m.state_dict(), 'models/' + logname + '_best.pt') else: tqdm.write(str(ave_min_move) + time_str) if timer >= stop: print('Ran out of patience') print(f'Best score: {best}') # torch.save(m.state_dict(), 'models/'+logname+f'_e{epoch}.pt') break else: print(f'{stop - timer} epochs remaining') np.savez('logs/'+logname, t_loss=t_loss, min_move=min_move, params=args)
raise ValueError('Seed is {}'.format(seed)) random.seed(seed) np.random.seed(seed) torch.manual_seed(seed) name = str(seed).zfill(5) start_time = time() use_network = False if not use_network: selfplay_fixed(name, number=10, times=4, verbose=args.verbose) else: torch.backends.cudnn.benchmark = True m_name = 'models/20200207/best_s7_jit.pth' if not os.path.exists(m_name): full_name = '20200207/0_1000_soft3.0c64b3_p10_bs2048lr0.1d0.0_s7_best' print('Tracing: {}'.format(full_name)) model = ConvNet(channels=64, blocks=3) model.load_state_dict(torch.load('models/{}.pt'.format(full_name))) model.to('cuda') model.eval() model = torch.jit.trace(model, torch.randn(10, 16, 4, 4, dtype=torch.float32, device='cuda')) torch.jit.save(model, m_name) print('Jit model saved') else: print(m_name) model = torch.jit.load(m_name) selfplay(name, model, number=10, times=4, verbose=args.verbose) total_time = time() - start_time print(f'Took {total_time/60:.1f} minutes')