def reset_optimizer(self, lr=0.0002, beta1=0.5, scheduler_gamma=0.3, scheduler_step_size=5, scheduler_type='StepLR'): self.optimizer_G = Adam(list(self.netG_A2B.parameters()) + list(self.netG_B2A.parameters()), lr=lr, betas=(beta1, 0.999)) self.optimizer_D = Adam(list(self.netD_A.parameters()) + list(self.netD_B.parameters()), lr=lr, betas=(beta1, 0.999)) self.optimizers = [] self.optimizers.append(self.optimizer_G) self.optimizers.append(self.optimizer_D) if scheduler_type == 'StepLR': self.schedulers = [ lr_scheduler.StepLR(optimizer, scheduler_step_size, gamma=scheduler_gamma) for optimizer in self.optimizers ] elif scheduler_type == 'linear_decay': self.schedulers = [ lr_scheduler.LambdaLR(optimizer, utils.Linear_decay(scheduler_step_size)) for optimizer in self.optimizers ] else: raise Exception('unknown scheduler type')
def __init__(self, N, lr=0.1, theta=None): self.N = N self.optim = Adam(lr=lr) self.sp = Softplus() self.var = 0.01 * np.random.normal(size=2 * N + 1) self.a = self.sp(self.var[0:N]) self.b = self.sp(self.var[N:2 * N]) self.theta = np.random.gamma(1., 1.) if theta is None else theta self.var[-1] = self.sp.inv(self.theta)
def compile(self, train_batch_size): print("Compiling functions...") train_input = T.tensor4() target_y = T.matrix() target_x = T.matrix() train_output, g_y, g_x = self.get_train_output(train_input, train_batch_size) classification_loss = self.get_NLL_cost(train_output[-1], self.target) tracking_loss = self.get_tracking_cost(g_y, g_x, target_y, target_x) loss = 5 * classification_loss + tracking_loss updates = Adam(loss, self.params, lr=self.learning_rate) # updates = self.get_updates(loss, self.params, self.learning_rate) self.train_func = theano.function( inputs=[train_input, self.target, target_y, target_x], outputs=[train_output[-1], loss], updates=updates, allow_input_downcast=True) h_tm1 = T.matrix() c_tm1 = T.matrix() predict_output, h, c, read, g_x, g_y, delta, sigma_sq = \ self.get_predict_output(self.input, h_tm1, c_tm1) self.predict_func = theano.function( inputs=[self.input, h_tm1, c_tm1], outputs=[predict_output, h, c, read, g_x, g_y, delta, sigma_sq], allow_input_downcast=True) print("Done!")
def compile(self, loss='binary_cross_entropy', optimizer='adam'): if loss == 'binary_cross_entropy': self.loss_function = binary_cross_entropy else: raise ValueError(f"{loss} has not been added yet!") if optimizer == 'adam': self.optimizer = Adam() self.optimizer.init_params(self.layers) else: raise ValueError(f"{loss} has not been added yet!") for idx, layer in enumerate(self.layers): print( f"Layer {str(idx+1)} ; Name: {layer.__class__.__name__} ; Output Shape: {layer.output_shape}" )
def __init__(self): self.__load_dataset() self.optimizer = Adam(cf.learning_rate, cf.beta_1, cf.beta_2, cf.epsilon) self.model = None self.history = None self.loss = None self.accuracy = None return
def __init__(self, dim_input, dim_hidden_layers, dim_output): # dim_hidden_layers in a list with ith element being no. of nodes in hidden layer i self.W = [] self.B = [] self.layers = [] self.X = T.dmatrix() self.Y = T.dmatrix() # reward times action vector for i in range(len(dim_hidden_layers) + 1): w = None lyr = None if i == 0: w = theano.shared( np.array(np.random.rand(dim_input, dim_hidden_layers[0]), dtype=theano.config.floatX)) b = theano.shared( np.zeros((dim_hidden_layers[0], ), dtype=theano.config.floatX)) lyr = self.layer(self.X, w, b) elif i == len(dim_hidden_layers): w = theano.shared( np.array(np.random.rand(dim_hidden_layers[i - 1], dim_output), dtype=theano.config.floatX)) b = theano.shared( np.zeros((dim_output, ), dtype=theano.config.floatX)) lyr = self.softmax_layer(self.layers[i - 1], w, b) # output layer else: w = theano.shared( np.array(np.random.rand(dim_hidden_layers[i - 1], dim_hidden_layers[i]), dtype=theano.config.floatX)) b = theano.shared( np.zeros((dim_hidden_layers[i], ), dtype=theano.config.floatX)) lyr = self.layer(self.layers[i - 1], w, b) self.W.append(w) self.B.append(b) self.layers.append(lyr) #cost equation loss = T.sum(T.log(T.dot(self.layers[-1], -self.Y.T))) #+ L1_reg*L1 + L2_reg*L2 #loss = self.layers[-1] - self.Y #loss = T.sum(T.square(self.layers[-1]-self.Y))#+ L1_reg*L1 + L2_reg*L2 updates = Adam(loss, self.W + self.B) #+ Adam(loss, self.B) #compile theano functions self.backprop = theano.function(inputs=[self.X, self.Y], outputs=loss, updates=updates) self.run_forward_batch = theano.function(inputs=[self.X], outputs=self.layers[-1])
def name_model(): LSTM_SIZE = 300 layer1 = LSTM(len(CHARKEY), LSTM_SIZE, activation=T.tanh) layer2 = Layer(LSTM_SIZE, len(CHARKEY), activation=lambda x:x) params = layer1.params + [layer1.initial_hidden_state] + layer2.params ################# Train ################# train_data = T.ftensor3() n_batch = train_data.shape[0] train_input = T.concatenate([T.zeros([n_batch,1,len(CHARKEY)]),train_data[:,:-1,:]],1) train_output = train_data def _scan_train(last_out, last_state): new_state = layer1.activate(last_out, last_state) layer_out = layer1.postprocess_activation(new_state) layer2_out = layer2.activate(layer_out) new_out = T.nnet.softmax(layer2_out) return new_out, new_state outputs_info = [None, initial_state(layer1, n_batch)] (scan_outputs, scan_states), _ = theano.scan(_scan_train, sequences=[train_input.dimshuffle([1,0,2])], outputs_info=outputs_info) flat_scan_outputs = scan_outputs.dimshuffle([1,0,2]).reshape([-1,len(CHARKEY)]) flat_train_output = train_output.reshape([-1,len(CHARKEY)]) crossentropy = T.nnet.categorical_crossentropy(flat_scan_outputs, flat_train_output) loss = T.sum(crossentropy)/T.cast(n_batch,'float32') adam_updates = Adam(loss, params) train_fn = theano.function([train_data],loss,updates=adam_updates) ################# Eval ################# length = T.iscalar() srng = MRG_RandomStreams(np.random.randint(1, 1024)) def _scan_gen(last_out, last_state): new_state = layer1.activate(last_out, last_state) layer_out = layer1.postprocess_activation(new_state) layer2_out = layer2.activate(layer_out) new_out = T.nnet.softmax(T.shape_padleft(layer2_out)) sample = srng.multinomial(n=1,pvals=new_out)[0,:] sample = T.cast(sample,'float32') return sample, new_state initial_input = np.zeros([len(CHARKEY)], np.float32) outputs_info = [initial_input, layer1.initial_hidden_state] (scan_outputs, scan_states), updates = theano.scan(_scan_gen, n_steps=length, outputs_info=outputs_info) gen_fn = theano.function([length],scan_outputs,updates=updates) return layer1, layer2, train_fn, gen_fn
def experiment(alg='ASNG', eta_x=0.1, eta_theta_factor=0., alpha=1.5, K=5, D=30, maxite=100000, log_file='log.csv', seed=-1): if seed >= 0: np.random.seed(seed) torch.manual_seed(seed) if torch.cuda.is_available(): torch.cuda.manual_seed(seed) nc = (K - 1) * D f = fxc1(K, D, noise=True) categories = K * np.ones(D, dtype=np.int) if alg == 'ASNG': opt_theta = AdaptiveSNG(categories, alpha=alpha, delta_init=nc**-eta_theta_factor) elif alg == 'SNG': opt_theta = SNG(categories, delta_init=nc**-eta_theta_factor) elif alg == 'Adam': opt_theta = Adam(categories, alpha=nc**-eta_theta_factor, beta1=0.9, beta2=0.999) else: print('invalid algorithm!') return optimizer_x = torch.optim.SGD(f.parameters(), lr=eta_x, momentum=0.9, weight_decay=0., nesterov=False) lr_scheduler = torch.optim.lr_scheduler.CosineAnnealingLR( optimizer_x, maxite) print('{}, eta_x={}, eta_theta_factor={} alpha={}'.format( alg, eta_x, eta_theta_factor, alpha)) run(f, opt_theta, optimizer_x, lr_scheduler=lr_scheduler, maxite=maxite, dispspan=100, log_file=log_file)
def agent_init(self, agent_config): """Setup for the agent called when the experiment first starts. Set parameters needed to setup the agent. Assume agent_config dict contains: { network_pickle: string (optional), network_config: dictionary, optimizer_config: dictionary, replay_buffer_size: integer, minibatch_sz: integer, num_replay_updates_per_step: float discount_factor: float, } """ self.replay_buffer = ReplayBuffer(agent_config['replay_buffer_size'], agent_config['minibatch_sz'], agent_config.get("seed")) if "network_pickle" in agent_config: self.network = pickle.load( open(agent_config["network_pickle"], 'rb')) else: self.network = ActionValueNetwork(agent_config['network_config']) self.optimizer = Adam(self.network.layer_sizes, agent_config["optimizer_config"]) self.num_actions = agent_config['network_config']['num_actions'] self.num_replay = agent_config['num_replay_updates_per_step'] self.discount = agent_config['gamma'] self.tau = agent_config['tau'] self.rand_generator = np.random.RandomState(agent_config.get("seed")) self.last_state = None self.last_action = None self.sum_rewards = 0 self.episode_steps = 0
def train(self, x, y, eta, batch=100, eps=1.e-6, max_iter=10000, seed=None, x_test=None, y_test=None, early_stop=True, l2=None): if self.r[0] != x.shape[1]: self.r = [x.shape[1]] + self.r self.npars = self.nparams() print('{} parameters to fit'.format(self.npars[-1])) self.l2 = l2 self.eta = eta self.early_stop = early_stop and x_test is not None self.N = x.shape[0] self.eps = eps self.err = False self.log = {key: [] for key in ['j', 'tj']} if seed: np.random.seed(seed) # allocate all weights self.wght = inirand(self.npars[-1], 1, coef=2) self.grad = np.empty(self.wght.shape) # make weight vies for each layer self.wl, self.gl = [self.weight_views(a) for a in [self.wght, self.grad]] self.set_bias_mask() def eval(check_condition=True): self.log['j'].append(self.evaluate(x, y)) if x_test is not None: self.log['tj'].append(self.evaluate(x_test, y_test)) if check_condition: return self.stop_conditions() eval(False) # mini-batches batches = self.make_batches(x, y, batch) print('{} batches'.format(len(batches))) # adaptive moments self.adam = Adam() cur_iter = 0 while cur_iter < max_iter: if not cur_iter % 100: print('it: {:5d}, J: {:.4f}'.format(cur_iter, self.log['j'][-1])) for xi, yi in batches: self.process(xi, yi) if eval(): break cur_iter += 1 if cur_iter == max_iter: print('train: max number of iterations reached') return not self.err
def reset_optimizer(self, lr=0.0002, beta1=0.5, scheduler_gamma=0.3, scheduler_step_size=5): self.optimizer_G = Adam(list(self.netG_A2B.parameters()) + list(self.netG_B2A.parameters()), lr=lr, betas=(beta1, 0.999)) self.optimizer_D = Adam(list(self.netD_A.parameters()) + list(self.netD_B.parameters()), lr=lr, betas=(beta1, 0.999)) self.optimizers = [] self.optimizers.append(self.optimizer_G) self.optimizers.append(self.optimizer_D) self.schedulers = [ lr_scheduler.StepLR(optimizer, scheduler_step_size, gamma=scheduler_gamma) for optimizer in self.optimizers ]
def get_optimizer(model,opt_name,lr,momentum=0.9,betas=(0.9,0.999)): if opt_name=="vanilla_adam": return torch.optim.Adam(model.parameters(),lr=lr,betas=betas),False elif opt_name=="adam": from adam import Adam return Adam(model.parameters(),lr=lr,betas=betas),True elif opt_name=="adamfast": from .adam import AdamFast return AdamFast(model.parameters(),lr=lr,betas=(0.9,0.999)),True elif opt_name=="sgd_momentum": return torch.optim.SGD(model.parameters(),lr=lr,momentum=momentum),False elif opt_name=="sgd": return torch.optim.SGD(model.parameters(),lr=lr),False elif opt_name=="cgd": from .cgd import CGD return CGD(model.parameters(),lr=lr),True
def agent_init(self, agent_config): self.replay_buffer = ReplayBuffer(agent_config['replay_buffer_size'], agent_config['minibatch_sz'], agent_config.get("seed")) self.network = ActionValueNetwork(agent_config['network_config']) self.optimizer = Adam(self.network.layer_sizes, agent_config["optimizer_config"]) self.num_actions = agent_config['network_config']['num_actions'] self.num_replay = agent_config['num_replay_updates_per_step'] self.discount = agent_config['gamma'] self.tau = agent_config['tau'] self.rand_generator = np.random.RandomState(agent_config.get("seed")) self.last_state = None self.last_action = None self.sum_rewards = 0 self.episode_steps = 0
def cnn_adam(X, Y, val_X, val_Y): """ CNN - ADAM """ model = Model(learning_rate=0.001, batch_size=32, epochs=200, optimizer=Adam()) model.add(Conv2D(2, (3, 3), activation='tanh')) model.add(Maxpool((2, 2), stride=2)) # 16x16 model.add(Dropout(0.5)) model.add(Conv2D(4, (3, 3), activation='tanh')) model.add(Maxpool((2, 2), stride=2)) # 8x8 model.add(Dropout(0.5)) model.add(Flatten()) model.add(Dense(256, 32, activation='tanh')) model.add(Dense(32, 1)) print("Begin Training") model.train(X, Y, val_X, val_Y) model.save_history("experiments/cnn-adam.csv")
def ann_adam_layers(X, Y, val_X, val_Y): """ Extra Layers with sigmoid and Adam """ model = Model(learning_rate=0.001, batch_size=32, epochs=200, optimizer=Adam()) model.add(Dropout(0.5)) model.add(Dense(1024, 512, activation='sigmoid')) model.add(Dropout(0.5)) model.add(Dense(512, 256, activation='sigmoid')) model.add(Dropout(0.5)) model.add(Dense(256, 32, activation='sigmoid')) model.add(Dropout(0.5)) model.add(Dense(32, 1)) print("Begin Training") model.train(X, Y, val_X, val_Y) model.save_history("experiments/ann-adam-1024-512-256-32-1.csv") print( "The CSV file is saved in the experiments folder. You can plot the graph using plot.py" )
def main(args): # Training settings manual_seed = args.seed # print("Random Seed: ", manual_seed) # random.seed(manual_seed) save_dir = args.save_dir torch.manual_seed(manual_seed) use_cuda = not args.no_cuda and torch.cuda.is_available() device = torch.device("cuda" if use_cuda else "cpu") kwargs = {'num_workers': 1, 'pin_memory': True} if use_cuda else {} train_loader = torch.utils.data.DataLoader(datasets.MNIST( '../data', train=True, download=True, transform=transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.1307, ), (0.3081, )) ])), batch_size=args.batch_size, shuffle=True, **kwargs) test_loader = torch.utils.data.DataLoader(datasets.MNIST( '../data', train=False, transform=transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.1307, ), (0.3081, )) ])), batch_size=args.test_batch_size, shuffle=True, **kwargs) model_types = ["fc", "logistic"] if not args.drop_rate else ["fc"] for model_type in model_types: activations = ['relu', 'sigmoid'] if model_type == "fc" else ["None"] for activation in activations: for opt_type in ['adam', 'adagrad', 'rmsprop', 'sgd']: if model_type == 'logistic': model = LOGISTIC().to(device) else: model = FC(activation=activation, drop_rate=args.drop_rate).to(device) weight_decay = 1e-6 if model_type == "fc" else 0. optimizers = { 'adam': Adam(model.parameters(), lr=args.lr, weight_decay=args.weight_decay, betas=args.betas), 'adagrad': optim.Adagrad(model.parameters(), lr=args.lr, weight_decay=args.weight_decay), 'rmsprop': optim.RMSprop(model.parameters(), lr=args.lr, weight_decay=args.weight_decay, alpha=args.betas[1], eps=1e-08, momentum=0), 'sgd': optim.SGD(model.parameters(), lr=args.lr, weight_decay=args.weight_decay, momentum=args.momentum) } optimizer = optimizers[opt_type] train_losses = [] test_losses = [] accuracies = [] test_times = [] for epoch in range(1, args.epochs + 1): train_loss = train(args, model, device, train_loader, optimizer, epoch) t1 = time.time() test_loss, accuracy = test(args, model, device, test_loader) test_time = time.time() - t1 train_losses.append(train_loss) test_losses.append(test_loss) accuracies.append(accuracy) test_times.append(test_time) PATH = os.path.join( save_dir, "{}/{}_{}_{}.pt".format( "drop" if args.drop_rate else "base", activation, model_type, opt_type)) torch.save( { 'args': args, 'opt_type': opt_type, 'train_losses': train_losses, 'test_losses': test_losses, 'accuracies': accuracies, "times": test_times, 'model_state_dict': model.state_dict(), 'optimizer_state_dict': optimizer.state_dict(), }, PATH) print("\nModel saved in \n" + PATH)
batch_size = 3 max_epoch = 1000 text = 'You said good-bye and I said hello.' cbm = CountBasedMethod() word_list = cbm.text_to_word_list(text) word_to_id, id_to_word, corpus = cbm.preprocess(word_list) vocab_size = len(word_to_id) sw = SimpleWord2Vec() contexts, target = sw.create_contexts_target(corpus) contexts = sw.convert_to_one_hot(contexts, vocab_size) target = sw.convert_to_one_hot(target, vocab_size) model = SimpleCBOW(vocab_size, hidden_size) optimiser = Adam() trainer = Trainer(model, optimiser) trainer.fit(contexts, target, max_epoch, batch_size) file_path = '../img/simple_cbow.png' trainer.save_plot_image(file_path) word_vecs = model.word_vecs for word_id, word in id_to_word.items(): print(word, word_vecs[word_id]) # you [ 0.7656109 -0.9978437 1.0643483 1.5883044 0.760913 ] # said [-1.259723 0.21088624 -0.06925534 0.24457063 -1.2771223 ] # good-bye [ 1.097698 -0.89157814 0.80477166 0.11352278 1.1290942 ] # and [-1.0565615 1.3495133 -1.3335469 1.4657804 -1.0490868] # i [ 1.082961 -0.8998216 0.787367 0.11362309 1.1094831 ]
running_mean_mid = None running_var_mid = None running_mean_out = None running_var_out = None break elif load_yn == "y": break else: print("illegal input from keyboard.") eponum = 10 #update_variable #lr = 0.01 #learn rate w_one_adamclass = Adam(w_one.shape) b_one_adamclass = Adam(b_one.shape) w_two_adamclass = Adam(w_two.shape) b_two_adamclass = Adam(b_two.shape) gamma_mid_adamclass = Adam(gamma_mid.shape) beta_mid_adamclass = Adam(beta_mid.shape) gamma_out_adamclass = Adam(gamma_out.shape) beta_out_adamclass = Adam(beta_out.shape) w_conv_adamclass = Adam(w_conv.shape) b_conv_adamclass = Adam(b_conv.shape) for i in range(int(eponum * (trainsize / 100))): #i <- N/B * 100 or so rannumlist = numpy.random.choice(trainsize, 100, replace=False) image = (dlist[rannumlist] / 255) #.T #xlist.shape=[784,100] ylist = anslist[rannumlist]
def train(model, train_loader, test_loader, gen_loader, configs): # model.train() optimizer = Adam(configs.lr, amsgrad=True) # # optimizer, it's better to set up lr for some modules separately so that the whole training become more stable # optimizer = optim.Adamax([ # {'params': model.reader.parameters(), 'lr': 0.2 * configs.lr}, # {'params': model.h_mean.parameters(), 'lr': 0.1 * configs.lr}, # {'params': model.h_var.parameters(), 'lr': 0.1 * configs.lr}, # {'params': model.writer.parameters()}, # {'params': model.vis_dist.parameters()}, # {'params': model.pos_dist.parameters()}, # {'params': model.combine.parameters()}, # {'params': model.describe.parameters()}, # {'params': model.box_vae.parameters(), 'lr': 10 * configs.lr}, # {'params': model.offset_vae.parameters(), 'lr': 10 * configs.lr}, # {'params': model.renderer.parameters()}, # {'params': model.bias_mean.parameters()}, # {'params': model.bias_var.parameters()} load_epoch = 40 # ], lr=configs.lr) ifmask = True # st() x = tf.random.uniform([1, 64, 64, 3]) treex = pickle.load( open( "../PnpNet_tf_eager/data/CLEVR/CLEVR_64_MULTI_LARGE/trees/train/CLEVR_new_000002.tree", "rb")) trees = [treex] # # st() rec_loss, kld_loss, pos_loss, modelout = model( x, trees, "filenames", alpha=0.6, ifmask=ifmask, maskweight=configs.maskweight) # load_epoch =10 # saver = tfe.Saver(model.all_trainable_variables) # saver.restore(osp.join(configs.exp_dir, 'checkpoints_eager', 'model_epoch_{0}'.format(load_epoch))) # print("Weights restored for {} from epoch {}".format(len(model.all_trainable_variables),load_epoch)) # model.cuda() model.load_weights( osp.join(configs.exp_dir, 'checkpoints_eager', 'model_epoch_{0}'.format(load_epoch))) # st() trainer = PNPNetTrainer(model=model, optimizer=optimizer, train_loader=train_loader, val_loader=test_loader, gen_loader=gen_loader, configs=configs) minloss = 1000 for epoch_num in range(load_epoch, configs.epochs + 1): timestamp_start = datetime.datetime.now( pytz.timezone('America/New_York')) # trainer.train_epoch(epoch_num, timestamp_start) if epoch_num % configs.save_interval == 0 and epoch_num > 0: model.save_weights( osp.join(configs.exp_dir, 'checkpoints_eager', 'model_epoch_{0}'.format(epoch_num))) print("Model saved") # if epoch_num % configs.validate_interval == 0 and epoch_num > 0: # minloss = trainer.validate(epoch_num, timestamp_start, minloss) if epoch_num % configs.sample_interval == 0 and epoch_num > 0: trainer.sample(epoch_num, sample_num=4, timestamp_start=timestamp_start) trainer.train_epoch(epoch_num, timestamp_start)
def pre_train(dataloader, test_loader, dict_loader, dataloader_test, mask_labels, total_epochs=50, learning_rate=1e-4, use_gpu=True, seed=123): args = parser.parse_args() pprint(args) num_bits = args.num_bits model = CNN(model_name='alexnet', bit=num_bits, class_num=args.num_class) criterion = custom_loss(num_bits=num_bits) arch = 'cnn_' filename = arch + args.dataset + '_' + str(num_bits) + "bits" checkpoint_filename = os.path.join(args.checkpoint, filename + '.pt') if use_gpu: model = model.cuda() model = torch.nn.DataParallel(model, device_ids=range( torch.cuda.device_count())) criterion = criterion.cuda() torch.cuda.manual_seed(seed) running_loss = 0.0 start_epoch = 0 batch_time = AverageMeter() data_time = AverageMeter() end = time.time() best_prec = -99999 k = 10500 n_samples = 200000 alpha = 0.4 alpha_1 = 0.99 mask_labels = torch.from_numpy(mask_labels).long().cuda() Z_h1 = torch.zeros(n_samples, num_bits).float().cuda() # intermediate values z_h1 = torch.zeros(n_samples, num_bits).float().cuda() # temporal outputs h1 = torch.zeros(n_samples, num_bits).float().cuda() # current outputs Z_h2 = torch.zeros(args.anchor_num, num_bits).float().cuda() # intermediate values z_h2 = torch.zeros(args.anchor_num, num_bits).float().cuda() # temporal outputs h2 = torch.zeros(args.anchor_num, num_bits).float().cuda() # current outputs for epoch in range(start_epoch, total_epochs): model.train(True) rampup_value = rampup(epoch) rampdown_value = rampdown(epoch) learning_rate = rampup_value * rampdown_value * 0.00005 adam_beta1 = rampdown_value * 0.9 + (1.0 - rampdown_value) * 0.5 adam_beta2 = step_rampup(epoch) * 0.99 + (1 - step_rampup(epoch)) * 0.999 if epoch == 0: u_w = 0.0 else: u_w = rampup_value u_w_m = u_w * 5 u_w_m = torch.autograd.Variable(torch.FloatTensor([u_w_m]).cuda(), requires_grad=False) optimizer = Adam(model.parameters(), lr=learning_rate, betas=(adam_beta1, adam_beta2), eps=1e-8, amsgrad=True) anchors_data, anchor_Label = generate_anchor_vectors(dict_loader) for iteration, data in enumerate(dataloader, 0): anchor_index = np.arange(args.anchor_num) np.random.shuffle(anchor_index) anchor_index = anchor_index[:100] anchor_index = torch.from_numpy(anchor_index).long().cuda() anchor_inputs = anchors_data[anchor_index, :, :, :] anchor_labels = anchor_Label[anchor_index, :] inputs, labels, index = data['image'], data['labels'], data[ 'index'] labels = labels.float() mask_flag = Variable(mask_labels[index], requires_grad=False) idx = (mask_flag > 0) if index.shape[0] == args.batch_size: anchor_batch_S, anchor_batch_W = CalcSim( labels[idx, :].cuda(), anchor_labels.cuda()) if inputs.size(3) == 3: inputs = inputs.permute(0, 3, 1, 2) inputs = inputs.type(torch.FloatTensor) zcomp_h1 = z_h1[index.cuda(), :] zcomp_h2 = z_h2[anchor_index, :] labeled_batch_S, labeled_batch_W = CalcSim( labels[idx, :].cuda(), labels[idx, :].cuda()) if use_gpu: inputs = Variable(inputs.cuda(), requires_grad=False) anchor_batch_S = Variable(anchor_batch_S.cuda(), requires_grad=False) anchor_batch_W = Variable(anchor_batch_W.cuda(), requires_grad=False) labeled_batch_S = Variable(labeled_batch_S.cuda(), requires_grad=False) labeled_batch_W = Variable(labeled_batch_W.cuda(), requires_grad=False) # zero the parameter gradients optimizer.zero_grad() y_h1 = model(inputs) y_h2 = model(anchor_inputs) y = F.sigmoid(48 / num_bits * 0.4 * torch.matmul(y_h1, y_h2.permute(1, 0))) loss, l_batch_loss, m_loss = criterion( y, y_h1, y_h2, anchor_batch_S, anchor_batch_W, labeled_batch_S, labeled_batch_W, zcomp_h1, zcomp_h2, mask_flag, u_w_m, epoch, num_bits) h1[index, :] = y_h1.data.clone() h2[anchor_index, :] = y_h2.data.clone() # backward+optimize loss.backward() optimizer.step() running_loss += loss.item() Z_h2 = alpha_1 * Z_h2 + (1. - alpha_1) * h2 z_h2 = Z_h2 * (1. / (1. - alpha_1**(epoch + 1))) print( "Epoch[{}]({}/{}): Time:(data {:.3f}/ batch {:.3f}) Loss_H: {:.4f}/{:.4f}/{:.4f}" .format(epoch, iteration, len(dataloader), data_time.val, batch_time.val, loss.item(), l_batch_loss.item(), m_loss.item())) Z_h1 = alpha * Z_h1 + (1. - alpha) * h1 z_h1 = Z_h1 * (1. / (1. - alpha**(epoch + 1))) if epoch % 1 == 0: MAP = helpers.validate(model, dataloader_test, test_loader) print("Test image map is:{}".format(MAP)) is_best = MAP > best_prec best_prec = max(best_prec, MAP) save_checkpoint( { 'epoch': epoch + 1, 'state_dict': model.state_dict(), 'optimizer': optimizer.state_dict(), }, is_best, prefix=arch, num_bits=num_bits, filename=checkpoint_filename) return model
network.add(layers.Dense(10, activation='softmax')) results_acc = [] result_acc = [] results_loss = [] result_loss = [] test_acc_results = [] test_loss_results = [] nonzero_weights = [] l = [ GRDA(lr=.005, c=.02), SGD(lr=.005, nesterov=False), SGD(lr=.005, nesterov=True), Adagrad(lr=.005), Adam(lr=.005, amsgrad=False), Adam(lr=.005, amsgrad=True) ] allcounts = np.sum([x.size for x in network.get_weights()]) for opt in l: network.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy']) #network.save_weights('initial_weights.h5') #network.load_weights('initial_weights.h5') #initial_weights = network.get_weights() result_acc = [] result_loss = [] test_loss = [] test_acc = []
'filter_num': 30, 'filter_size': 5, 'pad': 0, 'stride': 1, } hidden_size = 256 output_size = 10 network = Net( input_dim=(1, 28, 28), conv_param=conv_param, hidden_size=hidden_size, output_size=output_size, weight_init_std=0.01, ) optimizer = Adam() train_size = x_train.shape[0] batch_size = 64 learning_rate = 0.1 # epochs = 100 train_loss_list = [] train_acc_list = [] test_acc_list = [] epochs = 20 iter_per_epoch = max(train_size / batch_size, 1) max_iter = int(epochs * iter_per_epoch)
def trajopt(wps, writer, init_vel=[0, 0.01], n_pts=40, constraint_weights=[0.01, 1, 0.0001, 0.001, 0.1, 0.1], max_n_opts=1000, lr=1e-6): # define params init_vel = from_numpy(np.array(init_vel)) constraint_weights = from_numpy(np.array(constraint_weights)) wps_init = tensor(wps, requires_grad=True) wp_speeds_init = tensor(np.ones(len(wps) - 2) * 30, requires_grad=True) seg_times_init = tensor(np.ones(len(wps) - 1) * 30, requires_grad=True) # define bounds wps_delta = from_numpy( np.array([ CS['waypoint_tol'], CS['waypoint_tol'], np.deg2rad(CS['angle_tol']) ])) wps_lo, wps_hi = wps_init - wps_delta, wps_init + wps_delta seg_times_lo = from_numpy( np.linalg.norm(wps[1:, :2] - wps[:-1, :2], axis=1) / CS['max_vel']) trajs_lo, trajs_hi = from_numpy(CS['xyp_lims_lo']), from_numpy( CS['xyp_lims_hi']) # define optimizers opts = { 'wps': Adam(wps_init.flatten(), alpha=lr, lo=wps_lo.flatten(), hi=wps_hi.flatten()), 'seg_times': Adam(seg_times_init, alpha=lr, lo=seg_times_lo), 'wp_speeds': Adam(wp_speeds_init, alpha=lr) } costs = [] n_opts = trange(max_n_opts) for n_opt in n_opts: wps = opts['wps'].params.view(wps_init.shape) seg_times = opts['seg_times'].params wp_speeds = opts['wp_speeds'].params # compute trajs trajs, d_trajs = gen_trajs(wps, wp_speeds, init_vel, n_pts) if n_opt == 0: trajs_init, d_trajs_init = get_numpy(trajs), get_numpy(d_trajs) total_time_init = get_numpy(torch.sum(seg_times)) # compute needed values velocities = d_trajs / torch.unsqueeze(torch.unsqueeze(seg_times, 1), 1) speeds = torch.norm(velocities, dim=2) accelerations = (speeds[:, 1:] - speeds[:, :-1]) / torch.unsqueeze( seg_times, 1) * n_pts angles = torch.atan2(d_trajs[:, :, 1], d_trajs[:, :, 0]) betas = torch.asin(torch.tanh(CS['wheelbase'] / 2 * angles / speeds)) # betas = torch.atan2(d_trajs[:,:,1], d_trajs[:,:,0]) steering_angles = torch.atan(2 * torch.tan(betas)) # angles = torch.sin(betas) * 2 / CS['wheelbase'] * speeds velocities_pred = torch.unsqueeze(speeds, 2) * torch.cat([ torch.unsqueeze(torch.cos(angles + betas), 2), torch.unsqueeze(torch.sin(angles + betas), 2) ], 2) # compute loss total_time = torch.sum(seg_times) constraint_costs = torch.stack([ # dynamics torch.norm(velocities_pred - velocities), # steering angle torch.sum( torch.pow( torch.relu(steering_angles - CS['max_steering_angle']), 2)), # acceleration torch.sum(torch.pow(torch.relu(accelerations - CS['max_acc']), 2)), # speed torch.sum(torch.relu(torch.pow(speeds, 2) - CS['max_vel']**2)), # traj bounds torch.sum( torch.pow( torch.relu(trajs - trajs_hi) + torch.relu(-trajs + trajs_lo), 2)) ]) constraint_cost = constraint_costs @ constraint_weights loss = total_time + constraint_cost costs.append( np.concatenate([[get_numpy(total_time)], get_numpy(constraint_costs)])) # Compute grad grad_wps, grad_seg_times, grad_wp_speeds = grad( loss, [wps, seg_times, wp_speeds]) grad_wps[torch.isnan(grad_wps)] = 0 grad_seg_times[torch.isnan(grad_seg_times)] = 0 grad_wp_speeds[torch.isnan(grad_wp_speeds)] = 0 # Step optimizer opts['wps'].collect_grad(grad_wps.flatten()) opts['seg_times'].collect_grad(grad_seg_times) opts['wp_speeds'].collect_grad(grad_wp_speeds) opts['wps'].step() opts['seg_times'].step() opts['wp_speeds'].step() # log progress writer.add_scalar('/costs/loss', loss, n_opt) writer.add_scalar('/costs/total_time', total_time, n_opt) writer.add_scalar('/costs/total_constraints', constraint_cost, n_opt) writer.add_scalar('/costs/dynamics', constraint_costs[0], n_opt) writer.add_scalar('/costs/steering_angle', constraint_costs[1], n_opt) writer.add_scalar('/costs/acceleration', constraint_costs[2], n_opt) writer.add_scalar('/costs/speed', constraint_costs[3], n_opt) writer.add_scalar('/costs/traj_bounds', constraint_costs[4], n_opt) writer.add_scalar('/grads/wps', grad_wps.mean(), n_opt) writer.add_scalar('/grads/seg_times', grad_seg_times.mean(), n_opt) writer.add_scalar('/grads/wp_speeds', grad_wp_speeds.mean(), n_opt) for i in range(len(seg_times)): writer.add_scalar('/seg_times/{}'.format(i), seg_times[i], n_opt) for i in range(len(wp_speeds)): writer.add_scalar('/wp_speeds/{}'.format(i), wp_speeds[i], n_opt) n_opts.set_description( 'Loss {:.3f} | Time {:.3f} | TC {:.3f} | Dynamics {:.3f}'.format( get_numpy(loss), get_numpy(total_time), get_numpy(constraint_cost), get_numpy(constraint_costs[0]))) n_opts.refresh() return { 'wps': get_numpy(opts['wps'].params.view(wps_init.shape)), 'wp_speeds': get_numpy(opts['wp_speeds'].params), 'seg_times': get_numpy(opts['seg_times'].params), 'trajs_init': trajs_init, 'd_trajs_init': d_trajs_init, 'trajs': get_numpy(trajs), 'd_trajs': get_numpy(d_trajs), 'velocities': get_numpy(velocities), 'speeds': get_numpy(speeds), 'accelerations': get_numpy(accelerations), 'steering_angles': get_numpy(steering_angles), 'velocities_pred': get_numpy(velocities_pred), 'betas': get_numpy(betas), 'total_time_init': total_time_init, 'total_time': get_numpy(total_time), 'loss': get_numpy(loss), 'costs': np.array(costs), 'constraint_costs': get_numpy(constraint_costs), 'constraint_cost': get_numpy(constraint_cost) }
class GammaGamma(object): def __init__(self, N, lr=0.1, theta=None): self.N = N self.optim = Adam(lr=lr) self.sp = Softplus() self.var = 0.01 * np.random.normal(size=2 * N + 1) self.a = self.sp(self.var[0:N]) self.b = self.sp(self.var[N:2 * N]) self.theta = np.random.gamma(1., 1.) if theta is None else theta self.var[-1] = self.sp.inv(self.theta) #reparametrization trick def reparam(self, debug=False): a = self.a b = self.b z = 0.2*np.ones(self.N) if debug else \ 1e-15 + np.random.rand(self.N)*(1.-1e-15) #by default, std normal w = np.zeros(self.N) dwda = np.zeros(self.N) #approximation for shape param gradient small = a < 1000 #idea is understandable, math still hazy if np.any(small): a_ = a[small] b_ = b[small] z_ = z[small] # reparam y ~ gamma(a+1, b) y_ = gamma.ppf(z_, a_ + 1, scale=b_**-1) #gamma param -- rate is 1/scale dyda_ = (gamma.ppf(z_, a_ + 1 + 1e-5, scale=b_**-1) - y_) / 1e-5 u_ = 0.3*np.ones(a_.shape) if debug else \ 1e-15 + np.random.rand(np.prod(a_.shape)).reshape(a_.shape)*(1.-1e-15) ua_ = u_**(1. / a_) w[small] = ua_ * y_ dwda[small] = -log(u_) * w[small] / (a_**2) + ua_ * dyda_ large = np.logical_not(small) if np.any(large): #okay a_ = a[large] b_ = b[large] sqa_ = np.sqrt(a_) z_ = 0.3 * np.ones(a_.shape) if debug else np.random.normal( size=a_.shape) w[large] = (a_ + sqa_ * z_) / b_ dwda[large] = (1. + 0.5 * z_ / sqa_) / b_ dwdb = -w / b w[w < 1e-40] = 1e-40 self.w = w self.dwda = dwda self.dwdb = dwdb return w def sample_p(self): return np.random.gamma(self.theta, 1., self.N) def log_p(self, w): theta = self.theta lp = (theta - 1) * log(w) - w - gammaln(theta) return lp.sum(lp.ndim - 1).mean() def log_q(self, w): a = self.a b = self.b lq = a * log(b) + (a - 1) * log(w) - b * w - gammaln(a) return lq.sum(lq.ndim - 1).mean() def sample_q(self, S=1): return np.random.gamma(self.a, scale=self.b**-1, size=(S, self.N)) #scale = 1/rate #details yet to figure out def step(self, dlldw): w = self.w a = self.a b = self.b dwda = self.dwda dwdb = self.dwdb N = self.N theta = self.theta dlpdw = (theta - 1) / (w + eps) - 1. dljdw = dlldw + dlpdw dlqda = log(b) + (a - 1) * dwda / w + log(w) - b * dwda - digamma(a) dlqdb = a / b + (a - 1) * dwdb / w - w - b * dwdb dLda = dljdw * dwda - dlqda dLdb = dljdw * dwdb - dlqdb dLdtheta = -N * digamma(theta) + log(w).sum() grad = np.append( np.concatenate( [dLda * self.sp.jacobian(a), dLdb * self.sp.jacobian(b)]), dLdtheta * self.sp.jacobian(theta)) self.var = self.optim.step(self.var, -grad) self.a = self.sp(self.var[0:N]) self.b = self.sp(self.var[N:2 * N]) self.theta = self.sp(self.var[-1]) def get_hp_name(self): return ['theta'] def get_hp(self): return [self.theta] def print_hp(self): return 'theta %.4f' % self.theta
def compress(args): """Compresses an image, or a batch of images of the same shape in npy format.""" from configs import get_eval_batch_size if args.input_file.endswith('.npy'): # .npy file should contain N images of the same shapes, in the form of an array of shape [N, H, W, 3] X = np.load(args.input_file) else: # Load input image and add batch dimension. from PIL import Image x = np.asarray(Image.open(args.input_file).convert('RGB')) X = x[None, ...] num_images = int(X.shape[0]) img_num_pixels = int(np.prod(X.shape[1:-1])) X = X.astype('float32') X /= 255. eval_batch_size = get_eval_batch_size(img_num_pixels) dataset = tf.data.Dataset.from_tensor_slices(X) dataset = dataset.batch(batch_size=eval_batch_size) # https://www.tensorflow.org/api_docs/python/tf/compat/v1/data/Iterator # Importantly, each sess.run(op) call will consume a new batch, where op is any operation that depends on # x. Therefore if multiple ops need to be evaluated on the same batch of data, they have to be grouped like # sess.run([op1, op2, ...]). # x = dataset.make_one_shot_iterator().get_next() x_next = dataset.make_one_shot_iterator().get_next() x_ph = x = tf.placeholder( 'float32', (None, *X.shape[1:])) # keep a reference around for feed_dict #### BEGIN build compression graph #### # Instantiate model. analysis_transform = AnalysisTransform(args.num_filters) synthesis_transform = SynthesisTransform(args.num_filters) hyper_analysis_transform = HyperAnalysisTransform(args.num_filters) hyper_synthesis_transform = HyperSynthesisTransform(args.num_filters, num_output_filters=2 * args.num_filters) entropy_bottleneck = tfc.EntropyBottleneck() # Initial values for optimization y_init = analysis_transform(x) z_init = hyper_analysis_transform(y_init) y = tf.placeholder('float32', y_init.shape) y_tilde = y + tf.random.uniform(tf.shape(y), -0.5, 0.5) z = tf.placeholder('float32', z_init.shape) # sample z_tilde from q(z_tilde|x) = q(z_tilde|h_a(g_a(x))), and compute the pdf of z_tilde under the flexible prior # p(z_tilde) ("z_likelihoods") z_tilde, z_likelihoods = entropy_bottleneck(z, training=True) z_hat = entropy_bottleneck._quantize( z, 'dequantize') # rounded (with median centering) mu, sigma = tf.split(hyper_synthesis_transform(z_tilde), num_or_size_splits=2, axis=-1) sigma = tf.exp(sigma) # make positive # need to handle images with non-standard sizes during compression; mu/sigma must have the same shape as y y_shape = tf.shape(y_tilde) mu = mu[:, :y_shape[1], :y_shape[2], :] sigma = sigma[:, :y_shape[1], :y_shape[2], :] scale_table = np.exp( np.linspace(np.log(SCALES_MIN), np.log(SCALES_MAX), SCALES_LEVELS)) conditional_bottleneck = tfc.GaussianConditional(sigma, scale_table, mean=mu) # compute the pdf of y_tilde under the conditional prior/entropy model p(y_tilde|z_tilde) # = N(y_tilde|mu, sigma^2) conv U(-0.5, 0.5) y_likelihoods = conditional_bottleneck._likelihood( y_tilde) # p(\tilde y | \tilde z) if conditional_bottleneck.likelihood_bound > 0: likelihood_bound = conditional_bottleneck.likelihood_bound y_likelihoods = math_ops.lower_bound(y_likelihoods, likelihood_bound) y_hat = conditional_bottleneck._quantize( y, 'dequantize') # rounded (with mean centering) x_tilde = synthesis_transform(y_tilde) x_shape = tf.shape(x) x_tilde = x_tilde[:, :x_shape[1], :x_shape[ 2], :] # crop reconstruction to have the same shape as input # Total number of bits divided by number of pixels. # - log p(\tilde y | \tilde z) - log p(\tilde z) - - log q(\tilde z | \tilde y) axes_except_batch = list(range(1, len(x.shape))) # should be [1,2,3] y_bpp = tf.reduce_sum(-tf.log(y_likelihoods), axis=axes_except_batch) / ( np.log(2) * img_num_pixels) z_bpp = tf.reduce_sum(-tf.log(z_likelihoods), axis=axes_except_batch) / ( np.log(2) * img_num_pixels) eval_bpp = y_bpp + z_bpp # shape (N,) train_bpp = tf.reduce_mean(eval_bpp) # Mean squared error across pixels. train_mse = tf.reduce_mean(tf.squared_difference(x, x_tilde)) # Multiply by 255^2 to correct for rescaling. # float_train_mse = train_mse # psnr = - 10 * (tf.log(float_train_mse) / np.log(10)) # float MSE computed on float images train_mse *= 255**2 # The rate-distortion cost. if args.lmbda < 0: args.lmbda = float(args.runname.split('lmbda=')[1].split('-') [0]) # re-use the lmbda as used for training print( 'Defaulting lmbda (mse coefficient) to %g as used in model training.' % args.lmbda) if args.lmbda > 0: rd_loss = args.lmbda * train_mse + train_bpp else: rd_loss = train_bpp rd_gradients = tf.gradients(rd_loss, [y, z]) # Bring both images back to 0..255 range, for evaluation only. x *= 255 x_tilde = tf.clip_by_value(x_tilde, 0, 1) x_tilde = tf.round(x_tilde * 255) mse = tf.reduce_mean(tf.squared_difference(x, x_tilde), axis=axes_except_batch) # shape (N,) psnr = tf.image.psnr(x_tilde, x, 255) # shape (N,) msssim = tf.image.ssim_multiscale(x_tilde, x, 255) # shape (N,) msssim_db = -10 * tf.log(1 - msssim) / np.log(10) # shape (N,) with tf.Session() as sess: # Load the latest model checkpoint, get compression stats save_dir = os.path.join(args.checkpoint_dir, args.runname) latest = tf.train.latest_checkpoint(checkpoint_dir=save_dir) tf.train.Saver().restore(sess, save_path=latest) eval_fields = [ 'mse', 'psnr', 'msssim', 'msssim_db', 'est_bpp', 'est_y_bpp', 'est_z_bpp' ] eval_tensors = [mse, psnr, msssim, msssim_db, eval_bpp, y_bpp, z_bpp] all_results_arrs = {key: [] for key in eval_fields } # append across all batches log_itv = 100 if save_opt_record: log_itv = 10 rd_lr = 0.005 rd_opt_its = 2000 from adam import Adam batch_idx = 0 while True: try: x_val = sess.run(x_next) x_feed_dict = {x_ph: x_val} # 1. Perform R-D optimization conditioned on ground truth x print('----RD Optimization----') y_cur, z_cur = sess.run([y_init, z_init], feed_dict=x_feed_dict) # np arrays adam_optimizer = Adam(lr=rd_lr) opt_record = { 'its': [], 'rd_loss': [], 'rd_loss_after_rounding': [] } for it in range(rd_opt_its): grads, obj, mse_, train_bpp_, psnr_ = sess.run( [rd_gradients, rd_loss, train_mse, train_bpp, psnr], feed_dict={ y: y_cur, z: z_cur, **x_feed_dict }) y_cur, z_cur = adam_optimizer.update([y_cur, z_cur], grads) if it % log_itv == 0 or it + 1 == rd_opt_its: psnr_ = psnr_.mean() if args.verbose: y_hat_, z_hat_ = sess.run([y_hat, z_hat], feed_dict={ y: y_cur, z: z_cur }) bpp_after_rounding, psnr_after_rounding, rd_loss_after_rounding = sess.run( [train_bpp, psnr, rd_loss], feed_dict={ y_tilde: y_hat_, z_tilde: z_hat_, **x_feed_dict }) psnr_after_rounding = psnr_after_rounding.mean() print( 'it=%d, rd_loss=%.4f mse=%.3f bpp=%.4f psnr=%.4f\t after rounding: rd_loss=%.4f, bpp=%.4f psnr=%.4f' % (it, obj, mse_, train_bpp_, psnr_, rd_loss_after_rounding, bpp_after_rounding, psnr_after_rounding)) opt_record['rd_loss_after_rounding'].append( rd_loss_after_rounding) else: print( 'it=%d, rd_loss=%.4f mse=%.3f bpp=%.4f psnr=%.4f' % (it, obj, mse_, train_bpp_, psnr_)) opt_record['its'].append(it) opt_record['rd_loss'].append(obj) print() # this is the latents we end up transmitting y_hat_, z_hat_ = sess.run([y_hat, z_hat], feed_dict={ y: y_cur, z: z_cur }) # If requested, transform the quantized image back and measure performance. eval_arrs = sess.run(eval_tensors, feed_dict={ y_tilde: y_hat_, z_tilde: z_hat_, **x_feed_dict }) for field, arr in zip(eval_fields, eval_arrs): all_results_arrs[field] += arr.tolist() batch_idx += 1 except tf.errors.OutOfRangeError: break for field in eval_fields: all_results_arrs[field] = np.asarray(all_results_arrs[field]) input_file = os.path.basename(args.input_file) results_dict = all_results_arrs trained_script_name = args.runname.split('-')[0] script_name = os.path.splitext(os.path.basename(__file__))[ 0] # current script name, without extension # save RD evaluation results prefix = 'rd' save_file = '%s-%s-input=%s.npz' % (prefix, args.runname, input_file) if script_name != trained_script_name: save_file = '%s-%s-lmbda=%g+%s-input=%s.npz' % ( prefix, script_name, args.lmbda, args.runname, input_file) np.savez(os.path.join(args.results_dir, save_file), **results_dict) if save_opt_record: # save optimization record prefix = 'opt' save_file = '%s-%s-input=%s.npz' % (prefix, args.runname, input_file) if script_name != trained_script_name: save_file = '%s-%s-lmbda=%g+%s-input=%s.npz' % ( prefix, script_name, args.lmbda, args.runname, input_file) np.savez(os.path.join(args.results_dir, save_file), **opt_record) for field in eval_fields: arr = all_results_arrs[field] print('Avg {}: {:0.4f}'.format(field, arr.mean()))
class Agent: def __init__(self): self.name = "expected_sarsa_agent" def agent_init(self, agent_config): """Setup for the agent called when the experiment first starts. Set parameters needed to setup the agent. Assume agent_config dict contains: { network_pickle: string (optional), network_config: dictionary, optimizer_config: dictionary, replay_buffer_size: integer, minibatch_sz: integer, num_replay_updates_per_step: float discount_factor: float, } """ self.replay_buffer = ReplayBuffer(agent_config['replay_buffer_size'], agent_config['minibatch_sz'], agent_config.get("seed")) if "network_pickle" in agent_config: self.network = pickle.load( open(agent_config["network_pickle"], 'rb')) else: self.network = ActionValueNetwork(agent_config['network_config']) self.optimizer = Adam(self.network.layer_sizes, agent_config["optimizer_config"]) self.num_actions = agent_config['network_config']['num_actions'] self.num_replay = agent_config['num_replay_updates_per_step'] self.discount = agent_config['gamma'] self.tau = agent_config['tau'] self.rand_generator = np.random.RandomState(agent_config.get("seed")) self.last_state = None self.last_action = None self.sum_rewards = 0 self.episode_steps = 0 def policy(self, state): """ Args: state (Numpy array): the state. Returns: the action. """ action_values = self.network.get_action_values(state) probs_batch = self.softmax(action_values, self.tau) action = self.rand_generator.choice(self.num_actions, p=probs_batch.squeeze()) return action def agent_start(self, state): """The first method called when the experiment starts, called after the environment starts. Args: state (Numpy array): the state from the environment's evn_start function. Returns: The first action the agent takes. """ self.sum_rewards = 0 self.episode_steps = 0 self.last_state = np.array([state]) self.last_action = self.policy(self.last_state) return self.last_action def agent_step(self, reward, state): """A step taken by the agent. Args: reward (float): the reward received for taking the last action taken state (Numpy array): the state from the environment's step based, where the agent ended up after the last step Returns: The action the agent is taking. """ self.sum_rewards += reward self.episode_steps += 1 state = np.array([state]) action = self.policy(state) self.replay_buffer.append(self.last_state, self.last_action, reward, 0, state) if self.replay_buffer.size() > self.replay_buffer.minibatch_size: current_q = deepcopy(self.network) for _ in range(self.num_replay): experiences = self.replay_buffer.sample() self.optimize_network(experiences, current_q) self.last_state = state self.last_action = action return action def agent_end(self, reward): """Run when the agent terminates. Args: reward (float): the reward the agent received for entering the terminal state. """ self.sum_rewards += reward self.episode_steps += 1 state = np.zeros_like(self.last_state) self.replay_buffer.append(self.last_state, self.last_action, reward, 1, state) if self.replay_buffer.size() > self.replay_buffer.minibatch_size: current_q = deepcopy(self.network) for _ in range(self.num_replay): experiences = self.replay_buffer.sample() self.optimize_network(experiences, current_q) def agent_message(self, message): if message == "get_sum_reward": return self.sum_rewards else: raise Exception("Unrecognized Message!") def softmax(self, action_values, tau=1.0): """ Args: action_values (Numpy array): A 2D array of shape (batch_size, num_actions). The action-values computed by an action-value network. tau (float): The temperature parameter scalar. Returns: A 2D array of shape (batch_size, num_actions). Where each column is a probability distribution over the actions representing the policy. """ preferences = action_values / tau max_preference = np.amax(preferences, 1) reshaped_max_preference = max_preference.reshape((-1, 1)) exp_preferences = np.exp(preferences - reshaped_max_preference) sum_of_exp_preferences = np.sum(exp_preferences, 1) reshaped_sum_of_exp_preferences = sum_of_exp_preferences.reshape( (-1, 1)) action_probs = exp_preferences / reshaped_sum_of_exp_preferences action_probs = action_probs.squeeze() return action_probs def get_td_error(self, states, next_states, actions, rewards, terminals, current_q): """ Args: states (Numpy array): The batch of states with the shape (batch_size, state_dim). next_states (Numpy array): The batch of next states with the shape (batch_size, state_dim). actions (Numpy array): The batch of actions with the shape (batch_size,). rewards (Numpy array): The batch of rewards with the shape (batch_size,). discount (float): The discount factor. terminals (Numpy array): The batch of terminals with the shape (batch_size,). network (ActionValueNetwork): The latest state of the network that is getting replay updates. current_q (ActionValueNetwork): The fixed network used for computing the targets, and particularly, the action-values at the next-states. Returns: The TD errors (Numpy array) for actions taken, of shape (batch_size,) """ q_next_mat = np.apply_along_axis(current_q.get_action_values, 1, next_states).squeeze() probs_mat = self.softmax(q_next_mat, self.tau) v_next_vec = np.einsum("ij,ij->i", probs_mat, q_next_mat) v_next_vec *= (1 - terminals) target_vec = rewards + self.discount * v_next_vec q_mat = np.apply_along_axis(self.network.get_action_values, 1, states).squeeze() batch_indices = np.arange(q_mat.shape[0]) q_vec = np.array([q_mat[i][actions[i]] for i in batch_indices]) delta_vec = target_vec - q_vec return delta_vec def optimize_network(self, experiences, current_q): """ Args: experiences (Numpy array): The batch of experiences including the states, actions, rewards, terminals, and next_states. discount (float): The discount factor. network (ActionValueNetwork): The latest state of the network that is getting replay updates. current_q (ActionValueNetwork): The fixed network used for computing the targets, and particularly, the action-values at the next-states. """ states, actions, rewards, terminals, next_states = map( list, zip(*experiences)) states = np.concatenate(states) next_states = np.concatenate(next_states) rewards = np.array(rewards) terminals = np.array(terminals) batch_size = states.shape[0] delta_vec = self.get_td_error(states, next_states, actions, rewards, terminals, current_q) batch_indices = np.arange(batch_size) delta_mat = np.zeros((batch_size, self.network.num_actions)) delta_mat[batch_indices, actions] = delta_vec td_update = self.network.get_TD_update(states, delta_mat) weights = self.optimizer.update_weights(self.network.get_weights(), td_update) self.network.set_weights(weights)
def main(): setupStartTime = time.time() #load data trainImages, trainLabels, testImages, testLabels = loadMNIST() trainImages = binarize(trainImages) testImages = binarize(testImages) trainImagesByLabel, testImagesByLabel = \ sortMNISTByLabel(trainImages, trainLabels, testImages, testLabels) #parameters numberVisibleUnits = trainImages.shape[-1] numberHiddenUnits = 200 #numberHiddenUnits = int(numberVisibleUnits * 2./3.) temperature, nCDSteps = 1., 1 #sigma = 0.01 sigma = 2. / np.sqrt(numberVisibleUnits + numberHiddenUnits) #gradientWalker = 'sgd' gradientWalker = 'adam' iterations, miniBatchSize = int(6e5), 10 internalRngSeed, externalRngSeed = 1337, 1234 rng = RandomState(seed=externalRngSeed) plotNumber, plotStride = 5, 1 trainingReconstructionErrorOutputStride = 10 trainingOutputStride = iterations // 5 equilibrateFantasyForOutput = 100 #l1Coefficient = 1e-5 l1Coefficient = None l2Coefficient = 1e-4 gamma = 0.1 adversary = fileMidfix = f'{gradientWalker}-{iterations}' parameterFileNameIn, parameterFileNameOut = None, ''.join(('mnistRBM-', fileMidfix, 'step.para')) #parameterFileNameIn, parameterFileNameOut = f'mnistRBM-{gradientWalker}-1000000step.para', f'mnistRBM-{gradientWalker}-{iterations+1000000}step.para' runTraining = True verbose = False mnistReconProbPlotFilePrefix = ''.join(('mnistReconProb-', fileMidfix, 'steps-')) mnistReconPlotFilePrefix = ''.join(('mnistRecon-', fileMidfix, 'steps-')) parameterHistogramFilePrefix = ''.join(('paraHistogram-', fileMidfix, 'steps-')) gradientHistogramFilePrefix = ''.join(('gradHistogram-', fileMidfix, 'steps-')) numReceptiveFields, receptiveFieldFilePrefix = 9, ''.join(('receptiveField-', fileMidfix, 'steps-')) hiddenUnitActivationsSubset = rng.randint(numberHiddenUnits, size=numberHiddenUnits//10) hiddenUnitActivationFilePrefix = ''.join(('hiddenUnitActivation-', fileMidfix, 'steps-')) feFileName = ''.join(('fe-', fileMidfix, 'steps-')) feRatioFileName = ''.join(('feRatio-', fileMidfix, 'steps-')) if gradientWalker == 'sgd': learningRate = 1e-4 elif gradientWalker == 'adam' or gradientWalker == 'adamAdversarial': #learningRate = 1e-4 #learningRate = powerLawGenerator(1e-2, -0.1) learningRate = powerLawGenerator(1e-3, -0.1) adams = dict(zip(['visible', 'hidden', 'weights'], [Adam(stepSize=learningRate) for _ in range(3)])) else: exit(1) #setup RBM visibleProportionOn = np.sum([images.sum(axis=0) for images in trainImagesByLabel], axis=0) / trainImages.shape[0] #visibleProportionOn = None visibleLayer = np.zeros(numberVisibleUnits) hiddenLayer = np.zeros(numberHiddenUnits) if parameterFileNameIn is not None: with open(parameterFileNameIn, 'r') as parameterFile: rbm = RestrictedBoltzmannMachine(visibleLayer, hiddenLayer, temperature=temperature, sigma=sigma, visibleProportionOn=visibleProportionOn, parameterFile=parameterFile, rngSeed=internalRngSeed) else: rbm = RestrictedBoltzmannMachine(visibleLayer, hiddenLayer, temperature=temperature, sigma=sigma, visibleProportionOn=visibleProportionOn, rngSeed=internalRngSeed) if gradientWalker == 'sgd': updateParameters = lambda miniBatch, \ miniFantasyBatch: \ rbm.updateParametersSGD(miniBatch, miniFantasyBatch, learningRate, nCDSteps=nCDSteps, l1Coefficient=l1Coefficient, l2Coefficient=l2Coefficient, verbose=verbose) elif gradientWalker == 'adam': updateParameters = lambda miniBatch, \ miniFantasyBatch: \ rbm.updateParametersAdam(miniBatch, miniFantasyBatch, adams, nCDSteps=nCDSteps, l1Coefficient=l1Coefficient, l2Coefficient=l2Coefficient, verbose=verbose) elif gradientWalker == 'adamAdversarial': updateParameters = lambda miniBatch, \ miniFantasyBatch: \ rbm.updateParametersAdamAdversarial(miniBatch, miniFantasyBatch, adams, gamma, adversary, nCDSteps=nCDSteps, l1Coefficient=l1Coefficient, l2Coefficient=l2Coefficient, verbose=verbose) else: exit(1) #build dict for parameter histogram output weightParameterTypes = {'Visible': rbm.visibleBias, 'Hidden': rbm.hiddenBias, 'Weights': rbm.weights} weightHistogramsByParameterType = {'Visible': [], 'Hidden': [], 'Weights': []} gradientParameterTypes = {'Visible': rbm.visibleStep, 'Hidden': rbm.hiddenStep, 'Weights': rbm.weightStep} gradientHistogramsByParameterType = {'Visible': [], 'Hidden': [], 'Weights': []} historicalRBMs = [] hiddenUnitActivations = [] historicalFEs = [] trainSamplesForFE = getMiniBatchByLabel(trainImagesByLabel, miniBatchSize*10, rng) testSamplesForFE = getMiniBatchByLabel(testImagesByLabel, miniBatchSize*10, rng) setupEndTime = time.time() if runTraining is True: loopStartTime = time.time() #build fantasy batch miniFantasyBatch = np.copy(getMiniBatchByLabel(trainImagesByLabel, miniBatchSize, rng)) for i in range(iterations): miniBatch = getMiniBatchByLabel(trainImagesByLabel, miniBatchSize, rng) miniFantasyBatch = updateParameters(miniBatch, miniFantasyBatch) if (i+1) % trainingReconstructionErrorOutputStride == 0: print(i, rbm.computeReconstructionError(getMiniBatchByLabel(testImagesByLabel, miniBatchSize, rng))) if (i+1) % trainingOutputStride == 0: for parameterType in weightParameterTypes: xs, ys, _ = diagnostics.computeHistogramArray(weightParameterTypes[parameterType].flatten()) weightHistogramsByParameterType[parameterType].append((i, xs, ys)) xs, ys, _ = diagnostics.computeHistogramArray(gradientParameterTypes[parameterType].flatten()) gradientHistogramsByParameterType[parameterType].append((i, xs, ys)) historicalRBMs.append((i, rbm.copy())) hiddenUnitActivations.append((i, rbm.storeHiddenActivationsOnMiniBatch(miniBatch, hiddenUnits=hiddenUnitActivationsSubset))) historicalFEs.append((i, rbm.computeMeanFreeEnergy(trainSamplesForFE), rbm.computeMeanFreeEnergy(testSamplesForFE))) loopEndTime = time.time() if parameterFileNameOut is not None: with open(parameterFileNameOut, 'w') as parameterFile: rbm.dumpParameterFile(parameterFile) outputStartTime = time.time() #plot reconstruction series visibleStarts = getMiniBatchByLabel(testImagesByLabel, 10, rng) plotReconstructionSeries(visibleStarts, rbm, mnistReconProbPlotFilePrefix, mnistReconPlotFilePrefix) #plot fantasy particle series visibleStarts = miniFantasyBatch for i, visible in enumerate(visibleStarts): rbm.visibleLayer = visible for _ in range(equilibrateFantasyForOutput): visibleStarts[i], _ = rbm.gibbsSample(hiddenUnitsStochastic=False) plotReconstructionSeries(visibleStarts, rbm, mnistReconProbPlotFilePrefix+'fantasy', mnistReconPlotFilePrefix+'fantasy') #plot parameter histograms plotParameterHistograms(weightHistogramsByParameterType, gradientHistogramsByParameterType, parameterHistogramFilePrefix, gradientHistogramFilePrefix) #plot receptive fields hiddenUnitIndices = rng.randint(rbm.hiddenBias.shape[0], size=numReceptiveFields) for i, historicalRBM in historicalRBMs: receptiveFieldFileName = ''.join((receptiveFieldFilePrefix, f'{i}.pdf')) plotReceptiveFields(historicalRBM, hiddenUnitIndices, fileName=receptiveFieldFileName) #plot hidden unit activations for i, hiddenUnitActivation in hiddenUnitActivations: hiddenUnitActivationFileName = ''.join((hiddenUnitActivationFilePrefix, f'{i}.pdf')) diagnostics.plotHiddenActivationsOnMiniBatch(hiddenUnitActivation, fileName=hiddenUnitActivationFileName) #plot FE vs time t = [fe[0] for fe in historicalFEs] trainFE = np.array([fe[1] for fe in historicalFEs]) testFE = np.array([fe[2] for fe in historicalFEs]) diagnostics.plotTrainingTestAverageFEVsTime(t, trainFE, testFE, fileName=feFileName) diagnostics.plotTrainingTestAverageFEVsTime(t, trainFE/testFE, None, fileName=feRatioFileName) outputEndTime = time.time() print(f'setup time {setupEndTime-setupStartTime}s') if runTraining is True: print(f'training loop time {loopEndTime-loopStartTime}s') print(f'output time {outputEndTime-outputStartTime}s')
print('Build model...') model = Sequential() model.add(Embedding(max_features, 32)) model.add(LSTM(32, dropout=0.2, recurrent_dropout=0.2)) model.add(Dense(1, activation='sigmoid')) plot_model(model, to_file='model_imdb.png', show_shapes=True) results_acc = [] result_acc = [] results_loss = [] result_loss = [] test_acc_results = [] test_loss_results = [] l = [ Adam(lr=0.001, amsgrad=True), AAdam(lr=0.001, amsgrad=True), Adam(lr=0.001, amsgrad=False), AAdam(lr=0.001, amsgrad=False), Adagrad(), AAdagrad(), SGD(), ASGD() ] #, Adam(lr=0.001, amsgrad = True), AAdam(lr=0.001, amsgrad = True)] for opt in l: model.compile(loss='binary_crossentropy', optimizer=opt, metrics=['accuracy']) #model.save_weights('initial_weights_imdb.h5')
def setup_train(self): # dimensions: (batch, time, 12) chord_types = T.btensor3() # dimensions: (batch, time) chord_roots = T.imatrix() # dimensions: (batch, time) relative_posns = [T.imatrix() for _ in self.encodings] # dimesions: (batch, time, output_data) encoded_melodies = [T.btensor3() for _ in self.encodings] # dimesions: (batch, time) correct_notes = T.imatrix() n_batch, n_time = chord_roots.shape def _build(det_dropout): all_out_probs = [] for encoding, lstmstack, encoded_melody, relative_pos in zip(self.encodings, self.lstmstacks, encoded_melodies, relative_posns): activations = lstmstack.do_preprocess_scan( timestep=T.tile(T.arange(n_time), (n_batch,1)) , relative_position=relative_pos, cur_chord_type=chord_types, cur_chord_root=chord_roots, last_output=T.concatenate([T.tile(encoding.initial_encoded_form(), (n_batch,1,1)), encoded_melody[:,:-1,:] ], 1), deterministic_dropout=det_dropout) out_probs = encoding.decode_to_probs(activations, relative_pos, self.bounds.lowbound, self.bounds.highbound) all_out_probs.append(out_probs) reduced_out_probs = functools.reduce((lambda x,y: x*y), all_out_probs) if self.normalize_artic_only: non_artic_probs = reduced_out_probs[:,:,:2] artic_probs = reduced_out_probs[:,:,2:] non_artic_sum = T.sum(non_artic_probs, 2, keepdims=True) artic_sum = T.sum(artic_probs, 2, keepdims=True) norm_artic_probs = artic_probs*(1-non_artic_sum)/artic_sum norm_out_probs = T.concatenate([non_artic_probs, norm_artic_probs], 2) else: normsum = T.sum(reduced_out_probs, 2, keepdims=True) normsum = T.maximum(normsum, constants.EPSILON) norm_out_probs = reduced_out_probs/normsum return Encoding.compute_loss(norm_out_probs, correct_notes, True) train_loss, train_info = _build(False) updates = Adam(train_loss, self.get_optimize_params(), lr=self.learning_rate_var) eval_loss, eval_info = _build(True) self.loss_info_keys = list(train_info.keys()) self.update_fun = theano.function( inputs=[chord_types, chord_roots, correct_notes] + relative_posns + encoded_melodies, outputs=[train_loss]+list(train_info.values()), updates=updates, allow_input_downcast=True, on_unused_input='ignore', mode=(NanGuardMode(nan_is_error=True, inf_is_error=True, big_is_error=True) if self.nanguard else None)) self.eval_fun = theano.function( inputs=[chord_types, chord_roots, correct_notes] + relative_posns + encoded_melodies, outputs=[eval_loss]+list(eval_info.values()), allow_input_downcast=True, on_unused_input='ignore', mode=(NanGuardMode(nan_is_error=True, inf_is_error=True, big_is_error=True) if self.nanguard else None))
# numpy.random.seed(seed) torch.manual_seed(seed) torch.cuda.manual_seed_all(seed) importlib.reload(model) net = getattr(model, model_name.upper())(input_channel=input_channel, input_size=input_size, output_size=output_size).to(device) if use_cuda: net = DataParallelWithCallback(net) print(f"===> Model:\n{list(net.modules())[0]}") print_param(net) if model_name == 'cnn0' or model_name == 'cnn1': optimizer = Adam(net.parameters(), lr=grid['lr'], l1=grid['l1'], weight_decay=grid['l2'], amsgrad=True) elif model_name == 'vgg19' or model_name == 'cnn2' or model_name == 'cnn3': optimizer = Adam([{ 'params': iter(param for name, param in net.named_parameters() if 'channel_mask' in name), 'l1': grid['l1_channel'] }, { 'params': iter(param for name, param in net.named_parameters() if 'spatial_mask' in name), 'l1': grid['l1_spatial']
loss_func = L.NegativeSampling(args.unit, cs, 20) elif args.out_type == "original": loss_func = SoftmaxCrossEntropyLoss(args.unit, n_vocab) else: raise Exception("Unknown output type: {}".format(args.out_type)) if args.model == "skipgram": model = SkipGram(n_vocab, args.unit, loss_func) elif args.model == "cbow": model = ContinuousBow(n_vocab, args.unit, loss_func) else: raise Exception('Unknown model type:'.format(args.model)) dataset = np.array(dataset, dtype=np.int32) optimizer = Adam() optimizer.setup(model) begin_time = time.time() cur_at = begin_time word_count = 0 skip = (len(dataset) - args.window * 2) // args.batchsize next_count = 100000 for epoch in range(args.epoch): accum_loss = 0 print('epoch: {0}'.format(epoch)) indexes = np.random.permutation(skip) for i in indexes: if word_count >= next_count: now = time.time() duration = now - cur_at