def _save_emb(self): '''''' final_emb_vec = self.vocab.embvec.eval(session=self.sess) final_wmb_vec = self.vocab.wtvec.eval(session=self.sess) #print final_emb_vec.shape #print final_wmb_vec.shape dicts = self.vocab.id2str plot_figure(dicts, final_emb_vec) return
def train(epochs): for epoch in range(epochs): logger.Print(f"|~~~~~~~~~~~~~~~~~~~~~~~~Training epoch:{epoch}~~~~~~~~~~~~~~~~~~~~~~~~~~~|") model.train() scheduler.step() print(epoch,scheduler.get_lr()[0]) for itr, data in enumerate(train_data,1): rgb_img = data[0] depth_img = data[1] ir_img = data[2] hsv_img = data[3] ycb_img = data[4] labels = data[5] if use_cuda: rgb_img = rgb_img.cuda() depth_img = depth_img.cuda() ir_img = ir_img.cuda() hsv_img = hsv_img.cuda() ycb_img = ycb_img.cuda() labels = labels.cuda() optimizer.zero_grad() outputs = model(hsv_img,ycb_img) score, prob_outputs = torch.max(outputs.data, 1) loss = criterion(outputs,labels) loss.backward() optimizer.step() if itr%200 == 0: message = f'|epoch:{epoch}-iter:{itr}|loss:{loss:.6f}|' logger.Print(message) y_prob_list = [] y_pLabel_list = [] y_label_list = [] for i in range(len(labels)): if prob_outputs[i] > 0.5: y_pLabel_list.append(1) else: y_pLabel_list.append(0) y_prob_list = prob_outputs.data.cpu().numpy() y_label_list = labels.data.cpu().numpy() eval_result = eval_fun(y_prob_list,y_pLabel_list,y_label_list) logger.Print(eval_result) loss_history.append(loss.item()) if epoch%5 == 0: logger.Print(f"|~~~~~~~~~~~~~~~~~~~~~~~~val epoch:{epoch}~~~~~~~~~~~~~~~~~~~~~~~~~~~|") val(epoch, val_data) logger.Print(f"|~~~~~~~~~~~~~~~~~~~~~~~~test epoch:{epoch}~~~~~~~~~~~~~~~~~~~~~~~~~~~|") val(epoch, test_data) pass plot_figure(save_path,loss_history) loss_np = np.array(loss_history) np.save(save_path+f'/loss.npy',loss_np)
def k_fold_cross_validation(net, optimizer, criterion, train_img_list, k, present_time): transform_train, transform_test = get_train_transform( ), get_test_transform() valid_img_num = math.ceil(len(train_img_list) / k) total_best_valid_acc, total_best_valid_loss = 0, 0 for val_idx in range( k): # Which part of training set should be validation set net.load_state_dict( torch.load(f"./weights/{present_time}/init_weight.pth")) train_imgs, valid_imgs = get_cross_valid_img_list( val_idx, valid_img_num, train_img_list) train_loader = get_loader(train_imgs, transform_train) valid_loader = get_loader(valid_imgs, transform_test) train_acc_list, train_loss_list, valid_acc_list, valid_loss_list = list( ), list(), list(), list() best_valid_loss, best_valid_epoch, non_improve_count = 10000, 0, 0 for epoch in range(120): print(f"\n({val_idx})Epoch: {epoch}") train_acc, train_loss = train(net, optimizer, criterion, epoch, train_loader) valid_acc, valid_loss = test(net, criterion, epoch, valid_loader) # 以下的 list 是為了畫圖用 train_acc_list.append(train_acc) train_loss_list.append(train_loss) valid_acc_list.append(valid_acc) valid_loss_list.append(valid_loss) if (valid_loss < best_valid_loss): best_valid_loss = valid_loss best_valid_epoch = epoch non_improve_count = 0 else: non_improve_count += 1 if (non_improve_count >= 10): break total_best_valid_acc += valid_acc_list[best_valid_epoch] total_best_valid_loss += valid_loss_list[best_valid_epoch] plot_figure(train_acc_list, valid_acc_list, train_loss_list, valid_loss_list, val_idx, present_time) print("\n----------") print( f"valid acc: {total_best_valid_acc / k:.2f}, valid loss: {total_best_valid_loss / k:.2f}" )
def main(): args = parseArguments() data, t = DataLoader.load_data(args['dataFile'], args['trainingPoints'], args['validationPoints']) _SVM = SVM(args['B'], args['K'], args['C'], args['gamma'], args['xi'], args['trainingPoints'], args['type']) alpha, b = _SVM.train_SVM(data, t) if args['fig']: utils.plot_figure(_SVM, alpha, data, t, b, args['trainingPoints'], args['type']) precision, recall, f_score, accuracy = utils.compute_metrics( _SVM, alpha, data, t, b, args['trainingPoints'], args['validationPoints']) print(f'{precision=} {recall=} {f_score=} {accuracy=}')
def reg_profit(data_dict=None): """ This baseline can estimate the customer whether responded. And the ground-truth is the (responded_target \cap (profit_target>30)) logistic regression data_dict: """ training_data = data_dict['training_data_profit'] training_gt = data_dict['training_gt_profit'] val_data = data_dict['val_data_profit'] val_gt = data_dict['val_gt_profit'] score_train_list = [] score_val_list = [] for c in np.linspace(1e-6, 1000, 100000): regression_pro = Ridge(alpha=c, max_iter=10000000000, tol=1e-9, solver='auto') regression_pro.fit(training_data, training_gt) score_train = regression_pro.score(training_data, training_gt) score_val = regression_pro.score(val_data, val_gt) score_train_list.append(score_train) score_val_list.append(score_val) print('C=%.3f' % c, 'The train MSE: ', score_train) print('C=%.3f' % c, 'The val MSE: ', score_val) x = np.linspace(1e-6, 1000, 100000) plot_figure(training=score_train_list, validation=score_val_list, stop=1000, start=1e-6, num_point=100000, ylabel='mse', legend=['training', 'validation'], name='./figure/baseline2_reg_profit.png')
def train(epochs): for epoch in range(epochs): logger.Print( f"|~~~~~~~~~~~~~~~~~~~~~~~~Training epoch:{epoch}~~~~~~~~~~~~~~~~~~~~~~~~~~~|" ) model.train() scheduler.step() y_prob_list = [] y_pLabel_list = [] y_label_list = [] total_loss = 0 total_itr = 0 for itr, data in enumerate(train_data, 1): rgb_img = data[0] depth_img = data[1] ir_img = data[2] hsv_img = data[3] ycb_img = data[4] labels = data[5] if use_cuda: rgb_img = rgb_img.cuda() depth_img = depth_img.cuda() ir_img = ir_img.cuda() hsv_img = hsv_img.cuda() ycb_img = ycb_img.cuda() labels = labels.cuda() optimizer.zero_grad() outputs = model(rgb_img, depth_img, ir_img, hsv_img, ycb_img) score, prob_outputs = torch.max(outputs.data, 1) loss = criterion(outputs, labels) loss.backward() optimizer.step() total_loss += loss.item() total_itr = itr if itr > 150: for i in range(len(labels)): if prob_outputs[i] > 0.5: y_pLabel_list.append(1) else: y_pLabel_list.append(0) y_prob_list.extend(prob_outputs.data.cpu().numpy()) y_label_list.extend(labels.data.cpu().numpy()) eval_result, score = eval_fun(y_prob_list, y_pLabel_list, y_label_list) train_score.append(score) logger.Print(eval_result) avg_loss = total_loss / total_itr train_loss.append(avg_loss) message = f'|epoch:{epoch}|loss:{avg_loss:.6f}|' logger.Print(message) logger.Print( f"|~~~~~~~~~~~~~~~~~~~~~~~~val epoch:{epoch}~~~~~~~~~~~~~~~~~~~~~~~~~~~|" ) val(epoch, val_data, 0) if (epoch + 1) % 5 == 0: logger.Print( f"|~~~~~~~~~~~~~~~~~~~~~~~~test epoch:{epoch}~~~~~~~~~~~~~~~~~~~~~~~~~~~|" ) val(epoch, test_data, 1) pass if (epoch + 1) % 50 == 0: plot_curve(save_path, train_score, eval_score, epoch * 5) plot_figure(save_path, train_loss, eval_loss, epoch * 5) for i in range(len(eval_history)): logger.Print(eval_history[i]) train_loss_np = np.array(train_loss) eval_loss_np = np.array(eval_loss) np.save(save_path + '/train_loss_np.npy', train_loss_np) np.save(save_path + '/eval_loss_np.npy', eval_loss_np) train_np = np.array(train_score) eval_np = np.array(eval_score) test_np = np.array(test_score) np.save(save_path + f'/train.npy', train_np) np.save(save_path + f'/eval.npy', eval_np) np.save(save_path + f'/test.npy', test_np)
def audio2npys(input_file, config): # read an audio file and then write a lot of numpy files song_name = input_file.split('/')[-1][:-4] print('!song_name = {}!'.format(song_name)) y, sr = read_via_scipy(input_file) print("dtype={}, sampling rate={}, len_samples={}".format( y.dtype, sr, len(y))) num_ch, mul_win_len = cal_num_channels(config) print('num_ch = {}, mul_win_len={}'.format(num_ch, mul_win_len)) Len = y.shape[0] cnt = 0 st_idx = 0 ed_idx = st_idx + config['audio_samples_frame_size'] nxt_idx = st_idx + config['audio_samples_hop_length'] while st_idx < Len: if ed_idx > Len: ed_idx = Len data = np.zeros(config['audio_samples_frame_size'], dtype='float32') data[:ed_idx - st_idx] = y[st_idx:ed_idx] out_var = np.zeros( (num_ch, config['output_hei'], config['output_wid']), dtype='float32') list_spec = [] list_ceps = [] list_d_spec = [] list_spec_enve = [] channel_anchor = 0 # use this to save thourgh out_var[:,hei,wid] for idx, w_len in enumerate(mul_win_len): # config['is_multi'] is decided by "current for-loop" list_spec.append(get_spectrogram(data, config, w_len)) out_var[channel_anchor] = list_spec[-1] channel_anchor += 1 if config['use_ceps']: list_ceps.append(get_cepstrogram(list_spec[-1], config, w_len)) out_var[channel_anchor] = list_ceps[-1] channel_anchor += 1 if config['use_d_spec']: # mode: all, decay, or attack list_d_spec.append( get_diff_spectrogram(list_spec[-1], mode=config['d_spec_type'])) out_var[channel_anchor] = list_d_spec[-1] channel_anchor += 1 if config['use_spec_enve']: list_spec_enve.append( get_spectral_envelope(list_spec[-1], config)) out_var[channel_anchor] = list_spec_enve[-1] channel_anchor += 1 #print('channel_anchor = ', channel_anchor, num_ch) npy_name = specpath + song_name + '_' + str(cnt).zfill( config['num_digit']) + '.npy' #print('cnt ={}, max={}'.format(cnt, np.max(list_spec[-1]))) np.save(npy_name, out_var) img_name = imgpath + song_name + '_' + str(cnt).zfill( config['num_digit']) + '.png' # plots: 1. spec 2. ceps (all in single file) plot_figure(img_name, list_spec, list_ceps, list_d_spec, list_spec_enve, config) cnt += 1 st_idx = nxt_idx ed_idx = st_idx + config['audio_samples_frame_size'] nxt_idx = st_idx + config['audio_samples_hop_length']
# [b, 1, 28, 28] => [b, 784] =>[b, 10] x = x.view(x.size(0), 28*28) out = net(x) y_onehot = one_hot(y) loss = F.mse_loss(out, y_onehot) optimizer.zero_grad() loss.backward() # w' = w - lr * grad optimizer.step() train_loss.append(loss.item()) if batch_idx % 10 == 0: print(epoch, batch_idx, loss.item()) plot_figure(train_loss) # TEST total_correct = 0 for x,y in test_loader: x = x.view(x.size(0), 28*28) out = net(x) # out: [batch_size, 10] 获取dim=1维度上最大的值对应的索引 pred = out.argmax(dim=1) # 当前batch中正确的个数,继而进行统计 correct = pred.eq(y).sum().float().item() total_correct += correct total_num = len(test_loader.dataset) accuracy = total_correct / total_num
def trainModel(args, model, loss_fn, optim, data_train, data_val=None): def trainEpoch(bool_eval=False): total_loss = 0 total_correct = 0 total_samples = 0 data = data_train if bool_eval: model.eval() data = data_val if args.test and not bool_eval: len_data = 30 else: len_data = len(data) rand_order = torch.randperm(len_data) for i in range(len_data): if args.verbose and (i + 1) % args.report == 0: print("batch {0}/{1}: loss {2:.3f}/ acc {3:.3f}".format( i + 1, len_data, total_loss / total_samples, total_correct / total_samples)) batch, label = data[rand_order[i]] num_sample = len(label) if not bool_eval: model.zero_grad() output = model(batch) loss = loss_fn(output, label) predict = output.argmax(1) total_correct += int(sum(predict == label)) total_loss += loss.data total_samples += num_sample if not bool_eval: loss.backward() optim.step() if bool_eval: model.train() return total_loss / total_samples, total_correct / total_samples print(model) model.train() best_ep, best_acc, best_loss = -1, 0, 0 best_model = None plot_res = [] for ep in range(args.epochs): start = time.time() train_loss, train_acc = trainEpoch() val_loss, val_acc = trainEpoch(bool_eval=True) print("ep {0}: t {1:.3f}/v {2:.3f} (t {3:.3f}/v {4:.3f}) ({5:.2f}s)". format(ep + 1, train_acc, val_acc, train_loss, val_loss, (time.time() - start))) plot_res.append([train_acc, train_loss, val_acc, val_loss]) if val_acc > best_acc: best_acc = val_acc best_loss = val_loss best_ep = ep torch.save( model.state_dict(), args.path_savedir + "{}_{}.model".format(args.model, args.epochs)) print("best model found!") if not args.test: plot_figure( args.path_savedir + "{}_{}".format(args.model, args.epochs), plot_res) print("\nbest epoch: {}\nbest_acc: {}\nbest_loss: {}".format( best_ep, best_acc, best_loss))
def main(): os.makedirs('./model_preds', exist_ok=True) print(args) ntokens = 11 # the size of vocabulary emsize = 32 # embedding dimension nhid = 64 # the dimension of the feedforward network model in nn.TransformerEncoder nlayers = 2 # the number of nn.TransformerEncoderLayer in nn.TransformerEncoder nhead = 4 # the number of heads in the multiheadattention models dropout = 0.5 # the dropout value device = 'cuda' print('ntokens', ntokens, 'emsize', emsize, 'nhid', nhid) print('nlayers', nlayers, 'nhead', nhead, 'dropout', dropout) print() innerstepsize = 1e-2 # stepsize in inner SGD innerepochs = 100 # number of epochs of each inner SGD # ntoken, ninp, nhead, nhid, nlayers, dropout=0.5 model = TransformerModel(ntokens, emsize, nhead, nhid, nlayers, dropout).to(device) # batchsz here means total episode number arc_dataset = ARCVal(root='/home/sid/Desktop/arc/data/', imgsz=args.imgsz) def cond(x): return float(x.split('/')[-1].split('_')[-1][:-4]) all_model_fn = sorted(glob('./model_weights/*.pth'), key=cond)[-3:] # state = torch.load(all_model_fn[0]) # print('averaging weights of') # for fn in all_model_fn[1:]: # print(fn) # for name in state: # state[name] = state[name] + (state[name] - state[name]) # model.load_state_dict(state) # all_model_fn = ['./models/reptile_sz30_epoch_0_step_139_acc_0.299.pth'] for fn in all_model_fn: all_val_acc, all_train_acc = [], [] print('Processing fn', fn) for step, (train_x, train_y, val_x, val_y) in enumerate(arc_dataset): state = torch.load(fn) model.load_state_dict(state) optimizer = torch.optim.AdamW(model.parameters(), lr=innerstepsize) train_x = train_x.to(device).reshape(-1, args.imgsz * args.imgsz) val_x = val_x.to(device).reshape(-1, args.imgsz * args.imgsz) train_y = train_y.to(device) val_y = val_y.to(device) train_losses = [] train_acc = [] model.train() for _ in range(innerepochs): optimizer.zero_grad() outputs = model(train_x).reshape(-1, args.num_class) loss = F.cross_entropy(outputs, train_y) loss.backward() optimizer.step() train_losses.append(loss.item()) acc = (outputs.argmax(1) == train_y).float().mean().item() train_acc.append(acc) all_train_acc.append(np.mean(train_acc)) model.eval() with torch.no_grad(): outputs = F.softmax(model(val_x), dim=1) outputs = outputs.argmax(2).reshape(-1, args.imgsz, args.imgsz) val_acc = (outputs == val_y).float().mean().item() plot_figure(train_x, train_y, val_x, outputs, im_num=step, img_sz=args.imgsz) print('training loss:', np.mean(train_losses), '\ttraining acc:', np.mean(train_acc), '\tvalidation acc:', val_acc) all_val_acc.append(val_acc) print('mean train acc:', np.mean(all_train_acc), 'stddev train acc:', np.std(all_train_acc)) print(f'mean val acc: {np.mean(all_val_acc):.3}', f'stddev val acc: {np.std(all_val_acc):.3}', 'max val acc:', f'{max(all_val_acc):.3}', 'min val acc:', f'{min(all_val_acc):.3}', 'num complete correct:', (np.array(all_val_acc) == 1).sum()) print()
def train(self, env, save_model=False, verbose=False, display=True, save_training_curve=False): if verbose: print('Start training.') rewards = [] recent_rewards = [] for i_episode in range(self.episode): reward = [] time_step = env.reset() cur_state = torch.tensor([utils.get_state(time_step.observation)], device=self.device) while not time_step.last(): action_ID = self.select_action(cur_state, random_choose=True) time_step = env.step(self.action_space[action_ID]) reward.append(time_step.reward) next_state = torch.tensor( [utils.get_state(time_step.observation)], device=self.device) self.memory.push( Transition(state=cur_state, action=torch.tensor([[action_ID]], device=self.device, dtype=torch.long), next_state=next_state, reward=torch.tensor([time_step.reward], device=self.device))) cur_state = next_state self._update_model() reward = np.mean(reward) recent_rewards.append(reward) if verbose: print('Episode {} average reward: {}'.format( i_episode, reward)) if i_episode % self.target_update_episode == 0: self.target_net.load_state_dict(self.policy_net.state_dict()) if i_episode % self.save_episode == 0: self.save_model(self.model_path) if i_episode % self.plot_episode == 0: rewards.append(np.mean(recent_rewards)) recent_rewards = [] if save_training_curve: utils.plot_figure(y=rewards, x=list( range(0, i_episode + 1, self.save_episode)), title='Training Curve', xlabel='Episode', ylabel='Reward', figure_num=0, display=display, save=save_training_curve, filename='DQN_training_curve.png') if verbose: print('End training.')
def cls_responsed(data_dict): """ This baseline can estimate the customer whether responded. And the groundtruth is the (responded_target \cap (profit_target>30)) svm """ training_data = data_dict['training_data'] training_gt = data_dict['training_gt_res'] val_data = data_dict['val_data'] val_gt = data_dict['val_gt_res'] score_train_list = [] score_val_list = [] acc_true_list = [] acc_false_list = [] for c in np.linspace(1e-5, 50, 100): svm = SVC(C=c, tol=0.0000001, max_iter=1000000, class_weight='balanced', kernel='poly') svm.fit(training_data, training_gt) score_train = svm.score(training_data, training_gt) score_val = svm.score(val_data, val_gt) predict = svm.predict(val_data) index_true = np.where((val_gt == 1))[0] index_false = np.where((val_gt == 0))[0] acc_true = np.mean(predict[index_true]) acc_false = np.mean(1 - predict[index_false]) acc_true_list.append(acc_true) acc_false_list.append(acc_false) score_train_list.append(score_train) score_val_list.append(score_val) print('C=%.3f' % c, 'The train mean accuracy for SVM: ', score_train) print('C=%.3f' % c, 'The val mean accuracy for SVM: ', score_val) print('C=%.3f' % c, 'The val true accuracy for SVM: ', acc_true) print('C=%.3f' % c, 'The val true false accuracy for SVM: ', acc_false) print('------------------------------------------------') print('') score_train_list = np.array(score_train_list) score_val_list = np.array(score_val_list) acc_true_list = np.array(acc_true_list) acc_false_list = np.array(acc_false_list) plot_figure(score_train_list, score_val_list, start=1e-5, stop=50, num_point=100, name='./figure/baseline2_svm_poly_2_target.png', ylabel='acc', legend=['training', 'validation']) plot_figure(acc_false_list, acc_true_list, start=1e-5, stop=50, num_point=100, name='./figure/baseline2_svm_poly_2_recall_target.png', ylabel='acc', legend=['false', 'true'])
def main(): weights_dir = './model_weights' os.makedirs('./model_preds', exist_ok=True) print(args) ntokens = 11 # the size of vocabulary emsize = 32 # embedding dimension nhid = 64 # the dimension of the feedforward network model in nn.TransformerEncoder nlayers = 2 # the number of nn.TransformerEncoderLayer in nn.TransformerEncoder nhead = 4 # the number of heads in the multiheadattention models dropout = 0.5 # the dropout value device = torch.device('cuda') model = TransformerModel(ntokens, emsize, nhead, nhid, nlayers, dropout).to(device) def cond(x): return float(x.split('/')[-1].split('_')[-1][:-4]) all_model_fn = sorted(glob(f'./{weights_dir}/*.pth'), key=cond)[-1] print('Using model weights from', all_model_fn) # batchsz here means total episode number arc_dataset = ARCTest( root='/home/sid/Desktop/arc/data/', imgsz=args.imgsz) all_train_acc = [] for step, ((x, y), q) in enumerate(zip(arc_dataset, arc_dataset.query_x_batch)): # print('step:', step) state = torch.load(all_model_fn) model.load_state_dict(state) optimizer = torch.optim.AdamW(model.parameters(), lr=innerstepsize) x, y = x.to(device), y.to(device) x = x.to(device).reshape(-1, args.imgsz*args.imgsz).long() train_losses = [] train_acc = [] model.train() for _ in range(innerepochs): optimizer.zero_grad() outputs = model(x).reshape(-1, args.num_class) loss = F.cross_entropy(outputs, y) loss.backward() optimizer.step() train_losses.append(loss.item()) acc = (outputs.argmax(1) == y).float().mean().item() train_acc.append(acc) print('\ttraining loss:', np.mean(train_losses), '\ttraining acc:', np.mean(train_acc)) all_train_acc.append(np.mean(train_acc)) model.eval() with torch.no_grad(): q = torch.tensor( q.reshape(-1, args.imgsz*args.imgsz)).to(device).long() # print(q.shape) outputs = F.softmax(model(q), dim=1) outputs = outputs.argmax(2).reshape(-1, args.imgsz, args.imgsz) plot_figure(x, y, q, outputs, im_num=step, img_sz=args.imgsz) print('\nmean train acc:', np.mean(all_train_acc), 'stddev train acc:', np.std(all_train_acc))