def main(): start_time = time.time() init_out_dir() if args.clear_checkpoint: clear_checkpoint() last_step = get_last_checkpoint_step() if last_step >= 0: my_log('\nCheckpoint found: {}\n'.format(last_step)) else: clear_log() print_args() if args.net == 'made': net = MADE(**vars(args)) elif args.net == 'pixelcnn': net = PixelCNN(**vars(args)) elif args.net == 'bernoulli': net = BernoulliMixture(**vars(args)) else: raise ValueError('Unknown net: {}'.format(args.net)) net.to(args.device) my_log('{}\n'.format(net)) params = list(net.parameters()) params = list(filter(lambda p: p.requires_grad, params)) nparams = int(sum([np.prod(p.shape) for p in params])) my_log('Total number of trainable parameters: {}'.format(nparams)) named_params = list(net.named_parameters()) if args.optimizer == 'sgd': optimizer = torch.optim.SGD(params, lr=args.lr) elif args.optimizer == 'sgdm': optimizer = torch.optim.SGD(params, lr=args.lr, momentum=0.9) elif args.optimizer == 'rmsprop': optimizer = torch.optim.RMSprop(params, lr=args.lr, alpha=0.99) elif args.optimizer == 'adam': optimizer = torch.optim.Adam(params, lr=args.lr, betas=(0.9, 0.999)) elif args.optimizer == 'adam0.5': optimizer = torch.optim.Adam(params, lr=args.lr, betas=(0.5, 0.999)) else: raise ValueError('Unknown optimizer: {}'.format(args.optimizer)) if args.lr_schedule: # 0.92**80 ~ 1e-3 scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau( optimizer, factor=0.92, patience=100, threshold=1e-4, min_lr=1e-6) if last_step >= 0: state = torch.load('{}_save/{}.state'.format(args.out_filename, last_step)) ignore_param(state['net'], net) net.load_state_dict(state['net']) if state.get('optimizer'): optimizer.load_state_dict(state['optimizer']) if args.lr_schedule and state.get('scheduler'): scheduler.load_state_dict(state['scheduler']) init_time = time.time() - start_time my_log('init_time = {:.3f}'.format(init_time)) my_log('Training...') sample_time = 0 train_time = 0 start_time = time.time() for step in range(last_step + 1, args.max_step + 1): optimizer.zero_grad() sample_start_time = time.time() with torch.no_grad(): sample, x_hat = net.sample(args.batch_size) assert not sample.requires_grad assert not x_hat.requires_grad sample_time += time.time() - sample_start_time train_start_time = time.time() log_prob = net.log_prob(sample) # 0.998**9000 ~ 1e-8 beta = args.beta * (1 - args.beta_anneal**step) with torch.no_grad(): energy = ising.energy(sample, args.ham, args.lattice, args.boundary) loss = log_prob + beta * energy assert not energy.requires_grad assert not loss.requires_grad loss_reinforce = torch.mean((loss - loss.mean()) * log_prob) loss_reinforce.backward() if args.clip_grad: nn.utils.clip_grad_norm_(params, args.clip_grad) optimizer.step() if args.lr_schedule: scheduler.step(loss.mean()) train_time += time.time() - train_start_time if args.print_step and step % args.print_step == 0: free_energy_mean = loss.mean() / args.beta / args.L**2 free_energy_std = loss.std() / args.beta / args.L**2 entropy_mean = -log_prob.mean() / args.L**2 energy_mean = energy.mean() / args.L**2 mag = sample.mean(dim=0) mag_mean = mag.mean() mag_sqr_mean = (mag**2).mean() if step > 0: sample_time /= args.print_step train_time /= args.print_step used_time = time.time() - start_time my_log( 'step = {}, F = {:.8g}, F_std = {:.8g}, S = {:.8g}, E = {:.8g}, M = {:.8g}, Q = {:.8g}, lr = {:.3g}, beta = {:.8g}, sample_time = {:.3f}, train_time = {:.3f}, used_time = {:.3f}' .format( step, free_energy_mean.item(), free_energy_std.item(), entropy_mean.item(), energy_mean.item(), mag_mean.item(), mag_sqr_mean.item(), optimizer.param_groups[0]['lr'], beta, sample_time, train_time, used_time, )) sample_time = 0 train_time = 0 if args.save_sample: state = { 'sample': sample, 'x_hat': x_hat, 'log_prob': log_prob, 'energy': energy, 'loss': loss, } torch.save(state, '{}_save/{}.sample'.format( args.out_filename, step)) if (args.out_filename and args.save_step and step % args.save_step == 0): state = { 'net': net.state_dict(), 'optimizer': optimizer.state_dict(), } if args.lr_schedule: state['scheduler'] = scheduler.state_dict() torch.save(state, '{}_save/{}.state'.format( args.out_filename, step)) if (args.out_filename and args.visual_step and step % args.visual_step == 0): torchvision.utils.save_image( sample, '{}_img/{}.png'.format(args.out_filename, step), nrow=int(sqrt(sample.shape[0])), padding=0, normalize=True) if args.print_sample: x_hat_np = x_hat.view(x_hat.shape[0], -1).cpu().numpy() x_hat_std = np.std(x_hat_np, axis=0).reshape([args.L] * 2) x_hat_cov = np.cov(x_hat_np.T) x_hat_cov_diag = np.diag(x_hat_cov) x_hat_corr = x_hat_cov / ( sqrt(x_hat_cov_diag[:, None] * x_hat_cov_diag[None, :]) + args.epsilon) x_hat_corr = np.tril(x_hat_corr, -1) x_hat_corr = np.max(np.abs(x_hat_corr), axis=1) x_hat_corr = x_hat_corr.reshape([args.L] * 2) energy_np = energy.cpu().numpy() energy_count = np.stack( np.unique(energy_np, return_counts=True)).T my_log( '\nsample\n{}\nx_hat\n{}\nlog_prob\n{}\nenergy\n{}\nloss\n{}\nx_hat_std\n{}\nx_hat_corr\n{}\nenergy_count\n{}\n' .format( sample[:args.print_sample, 0], x_hat[:args.print_sample, 0], log_prob[:args.print_sample], energy[:args.print_sample], loss[:args.print_sample], x_hat_std, x_hat_corr, energy_count, )) if args.print_grad: my_log('grad max_abs min_abs mean std') for name, param in named_params: if param.grad is not None: grad = param.grad grad_abs = torch.abs(grad) my_log('{} {:.3g} {:.3g} {:.3g} {:.3g}'.format( name, torch.max(grad_abs).item(), torch.min(grad_abs).item(), torch.mean(grad).item(), torch.std(grad).item(), )) else: my_log('{} None'.format(name)) my_log('')
data_train = data_train[:, init_scope] data_pos = data_pos[:, init_scope] data_neg = data_neg[:, init_scope] xtr = torch.from_numpy(data_train).float().cuda() xte = torch.from_numpy(data_pos).float().cuda() xod = torch.from_numpy(data_neg).float().cuda() # construct model and ship to GPU hidden_list = list(map(int, args.hiddens.split(','))) model = MADE(xtr.size(1), hidden_list, xtr.size(1) * 2, num_masks=args.num_masks) print("number of model parameters:", sum([np.prod(p.size()) for p in model.parameters()])) model.cuda() # set up the optimizer opt = torch.optim.Adam(model.parameters(), args.learning_rate, weight_decay=args.weight_decay) scheduler = torch.optim.lr_scheduler.StepLR(opt, step_size=45, gamma=0.1) # list to store loss loss_tr = [] loss_te = [] loss_od = [] # start the training for epoch in range(args.epoch): scheduler.step(epoch)
# load the dataset print("loading binarized mnist from", args.data_path) mnist = np.load(args.data_path) xtr, xte = mnist['train_data'], mnist['valid_data'] xtr = torch.from_numpy(xtr).cuda() xte = torch.from_numpy(xte).cuda() # construct model and ship to GPU hidden_list = list(map(int, args.hiddens.split(','))) model = MADE(xtr.size(1), hidden_list, xtr.size(1), num_masks=args.num_masks) print("number of model parameters:", sum([np.prod(p.size()) for p in model.parameters()])) model.cuda() # set up the optimizer opt = torch.optim.Adam(model.parameters(), 1e-3, weight_decay=1e-4) scheduler = torch.optim.lr_scheduler.StepLR(opt, step_size=45, gamma=0.1) # start the training for epoch in range(100): print("epoch %d" % (epoch, )) scheduler.step(epoch) run_epoch( 'test', upto=5) # run only a few batches for approximate test accuracy run_epoch('train')
if __name__ == "__main__": # load the dataset from some path mnist = np.load("binarized_mnist.npz") x_train, x_test = mnist["train_data"], mnist["valid_data"] x_train = torch.as_tensor(x_train).cuda() x_test = torch.as_tensor(x_test).cuda() hidden_list = [500] resample_every = 20 model = MADE(x_train.size(1), hidden_list, x_train.size(1)) print( "number of model parameters: {np.sum([np.prod(p.size()) for p in model.parameters()])}" ) model.cuda() opt = torch.optim.Adam(model.parameters(), lr=1e-3, weight_decay=1e-4) scheduler = torch.optim.lr_scheduler.StepLR(opt, step_size=50, gamma=0.1) # The training for epoch in range(100): print(f"Epoch {epoch}") scheduler.step() # get an estimate of the test loss run_one_epoch("test", upto=5) run_one_epoch("train") print("Final test eval:") run_one_epoch("test")
scope_list = np.arange(n_RV) scope_temp = np.delete(scope_list, np.where(scope_list % 34 == 17)) init_scope = list(np.delete(scope_temp, np.where(scope_temp % 34 == 33))) # modify data to remove 0 (imag) columns data_train = data_train[:, init_scope] data_pos = data_pos[:, init_scope] data_neg = data_neg[:, init_scope] xtr = torch.from_numpy(data_train).float().cuda() xte = torch.from_numpy(data_pos).float().cuda() xod = torch.from_numpy(data_neg).float().cuda() # construct model and ship to GPU hidden_list = list(map(int, args.hiddens.split(','))) model = MADE(xtr.size(1), hidden_list, xtr.size(1) * 2, num_masks=args.num_masks) print("number of model parameters:", sum([np.prod(p.size()) for p in model.parameters()])) model.cuda() # set up the optimizer opt = torch.optim.Adam(model.parameters(), args.learning_rate, weight_decay=args.weight_decay) scheduler = torch.optim.lr_scheduler.StepLR(opt, step_size=45, gamma=0.1) # list to store loss loss_tr = [] loss_te = [] loss_od = [] # start the training for epoch in range(args.epoch): scheduler.step(epoch) loss_tr.append(run_epoch('train')) loss_te.append(run_epoch('test')) # run validation, which is pos
def BuckyBall(): start_time = time.time() init_out_dir() print_args() if args.ham == 'buckey': ham = buckyball_2(args.beta) # elif args.ham == 'sk': # ham = SKModel(args.n, args.beta, args.device, seed=args.seed) # elif args.ham == 'full': # ham = FullModel() # elif args.ham == 'buckey': # ham = buckyball_2(args.beta) else: raise ValueError('Unknown ham: {}'.format(args.ham)) #ham.J.requires_grad = False net = MADE(**vars(args)) net.to(args.device) my_log('{}\n'.format(net)) params = list(net.parameters()) params = list(filter(lambda p: p.requires_grad, params)) nparams = int(sum([np.prod(p.shape) for p in params])) my_log('Total number of trainable parameters: {}'.format(nparams)) if args.optimizer == 'sgd': optimizer = torch.optim.SGD(params, lr=args.lr) elif args.optimizer == 'sgdm': optimizer = torch.optim.SGD(params, lr=args.lr, momentum=0.9) elif args.optimizer == 'rmsprop': optimizer = torch.optim.RMSprop(params, lr=args.lr, alpha=0.99) elif args.optimizer == 'adam': optimizer = torch.optim.Adam(params, lr=args.lr, betas=(0.9, 0.999)) elif args.optimizer == 'adam0.5': optimizer = torch.optim.Adam(params, lr=args.lr, betas=(0.5, 0.999)) else: raise ValueError('Unknown optimizer: {}'.format(args.optimizer)) init_time = time.time() - start_time my_log('init_time = {:.3f}'.format(init_time)) my_log('Training...') sample_time = 0 train_time = 0 start_time = time.time() if args.beta_anneal_to < args.beta: args.beta_anneal_to = args.beta beta = args.beta while beta <= args.beta_anneal_to: for step in range(args.max_step): optimizer.zero_grad() sample_start_time = time.time() with torch.no_grad(): sample, x_hat = net.sample(args.batch_size) assert not sample.requires_grad assert not x_hat.requires_grad sample_time += time.time() - sample_start_time train_start_time = time.time() log_prob = net.log_prob(sample) with torch.no_grad(): energy = ham.energy(sample) loss = log_prob + beta * energy assert not energy.requires_grad assert not loss.requires_grad loss_reinforce = torch.mean((loss - loss.mean()) * log_prob) loss_reinforce.backward() if args.clip_grad > 0: # nn.utils.clip_grad_norm_(params, args.clip_grad) parameters = list(filter(lambda p: p.grad is not None, params)) max_norm = float(args.clip_grad) norm_type = 2 total_norm = 0 for p in parameters: param_norm = p.grad.data.norm(norm_type) total_norm += param_norm.item()**norm_type total_norm = total_norm**(1 / norm_type) clip_coef = max_norm / (total_norm + args.epsilon) for p in parameters: p.grad.data.mul_(clip_coef) optimizer.step() train_time += time.time() - train_start_time if args.print_step and step % args.print_step == 0: free_energy_mean = loss.mean() / beta / args.n free_energy_std = loss.std() / beta / args.n entropy_mean = -log_prob.mean() / args.n energy_mean = energy.mean() / args.n mag = sample.mean(dim=0) mag_mean = mag.mean() if step > 0: sample_time /= args.print_step train_time /= args.print_step used_time = time.time() - start_time my_log( 'beta = {:.3g}, # {}, F = {:.8g}, F_std = {:.8g}, S = {:.5g}, E = {:.5g}, M = {:.5g}, sample_time = {:.3f}, train_time = {:.3f}, used_time = {:.3f}' .format( beta, step, free_energy_mean.item(), free_energy_std.item(), entropy_mean.item(), energy_mean.item(), mag_mean.item(), sample_time, train_time, used_time, )) sample_time = 0 train_time = 0 with open(args.fname, 'a', newline='\n') as f: f.write('{} {} {:.3g} {:.8g} {:.8g} {:.8g} {:.8g}\n'.format( args.n, args.seed, beta, free_energy_mean.item(), free_energy_std.item(), energy_mean.item(), entropy_mean.item(), )) if args.ham == 'hop': ensure_dir(args.out_filename + '_sample/') np.savetxt('{}_sample/sample{:.2f}.txt'.format( args.out_filename, beta), sample.cpu().numpy(), delimiter=' ', fmt='%d') np.savetxt('{}_sample/log_prob{:.2f}.txt'.format( args.out_filename, beta), log_prob.cpu().detach().numpy(), delimiter=' ', fmt='%.5f') beta += args.beta_inc
def train(train_data, test_data, image_shape): """ Trains MADE model on binary image dataset. Arguments: train_data: A (n_train, H, W, 1) uint8 numpy array of binary images with values in {0, 1} test_data: An (n_test, H, W, 1) uint8 numpy array of binary images with values in {0, 1} image_shape: (H, W), height and width of the image Returns: - a (# of training iterations,) numpy array of train_losses evaluated every minibatch - a (# of epochs + 1,) numpy array of test_losses evaluated once at initialization and after each epoch - a numpy array of size (100, H, W, 1) of samples with values in {0, 1} """ use_cuda = True device = torch.device('cuda') if use_cuda else None train_data = torch.from_numpy( train_data.reshape( (train_data.shape[0], train_data.shape[1] * train_data.shape[2]))).float().to(device) test_data = torch.from_numpy( test_data.reshape( (test_data.shape[0], test_data.shape[1] * test_data.shape[2]))).float().to(device) def nll_loss(batch, output): return F.binary_cross_entropy(torch.sigmoid(output), batch) H, W = image_shape input_dim = H * W made = MADE(input_dim) epochs = 10 lr = 0.005 batch_size = 32 train_loader = torch.utils.data.DataLoader(train_data, batch_size=batch_size, shuffle=True) optimizer = torch.optim.Adam(made.parameters(), lr=lr) init_test_loss = nll_loss(test_data, made(test_data)) train_losses = [] test_losses = [init_test_loss.item()] # Training for epoch in range(epochs): for batch in train_loader: optimizer.zero_grad() output = made(batch) loss = nll_loss(batch, output) loss.backward() optimizer.step() train_losses.append(loss.item()) test_loss = nll_loss(test_data, made(test_data)) test_losses.append(test_loss.item()) print(f'{epoch + 1}/{epochs} epochs') # Generate samples made.eval() samples = torch.zeros(size=(100, H * W)).to(device) with torch.no_grad(): for i in range(H * W): logits = made(samples) probas = torch.sigmoid(logits) pixel_i_samples = torch.bernoulli(probas[:, i]) samples[:, i] = pixel_i_samples return np.array(train_losses), np.array(test_losses), samples.reshape( (100, H, W, 1)).detach().cpu().numpy()
def run(split, upto=None): torch.set_grad_enabled(split=='train') model.train() if split == 'train' else model.eval() nsamples = 1 if split == 'train' else xte N, D = x.size() B = 128 n_steps = N // B if upto is None else min(N//B, upto) losses = [] for step in range(n_steps): xb = Variable(x[step * B: step * B + B]) xbhat = torch.zeros_like(xb) for s in range(nsamples): if step % args.resample_every == 0 or split == 'test': model.update_masks() xbhat += model(xb) xbhat /= nsamples loss = F.binary_cross_entropy_with_logits(xbhat, xb, size_average=False) / B lossf = loss.data.item() losses.append(lossf) if split == 'train': opt.zero_grad() loss.backward() opt.step() print("%s epoch avg loss: %f" %(split, np.mean(losses))) if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('-d', '--data-path', required=True, type=str, help="Path to binarized_mnist.npz") parser.add_argument('-q', '--hiddens', type=str, default='500', help="Comma separated sizes for hidden layers, e.g. 500, or 500,500") parser.add_argument('-n', '--num-masks', type=int, default=1, help="Number of orderings for order/connection-agnostic training") parser.add_argument('-r', '--resample-every', type=int, default=20, help="For efficiency we can choose to resample orders/masks only once every this many steps") parser.add_argument('-s', '--samples', type=int, default=1, help="How many samples of connectivity/masks to average logits over during inference") args = parser.parse_args() np.random_seed(42) torch.manual_seed(42) torch.cuda.manual_seed_all(42) print("loading binarized mnist from", args.data_path) mnist = np.load(args.data_path) xtr, xte = mnist['train_data'], mnist['valid_data'] xtr = torch.from_numpy(xtr).cuda() xte = torch.from_numpy(xte).cuda() # construct model and ship to GPU hidden_list = list(map(int, args.hiddens.split(','))) model = MADE(xtr.size(1), hidden_list, xtr.size(1), num_masks=args.num_masks) print("number of model parameters:",sum([np.prod(p.size()) for p in model.parameters()])) model.cuda() # set up the optimizer opt = torch.optim.Adam(model.parameters(), 1e-3, weight_decay=1e-4) scheduler = torch.optim.lr_scheduler.StepLR(opt, step_size=45, gamma=0.1) # start the training for epoch in range(100): print("epoch %d" % (epoch, )) scheduler.step(epoch) run_epoch('test', upto=5) # run only a few batches for approximate test accuracy run_epoch('train') print("optimization done. full test set eval:") run_epoch('test')