def eval_model(args): """Evaluate model on training data.""" cfg, lbl = util.get_label_cfg_by_args(args) uid = cfg['uniqueid'] print('We are playing with %s' % uid) outdir = 'models/%s/gate_expert' % uid outname = 'gate_expert_model.pt' if KLLOSS: outname = 'gate_expert_kldiv_model.pt' if args.warm: outname = outname.replace('.pt', '_warm.pt') mdl_path = os.path.join(outdir, outname) gate_expert = GateExpertNet(mdl_path, args.argmax) eval_fun = gate_expert.get_y data = npload(cfg['file_path'], uid) datax = data[cfg['x_name']] datay = data[cfg['y_name']] evaly = eval_fun(datax) print(np.histogram(evaly[:, 48])) fig, ax = pld.get3dAxis() ax.scatter(datax[:, 0], datax[:, 1], evaly[:, 48]) loss = l1loss(evaly, datay) err_norm = np.mean(loss, axis=1) fig, ax = plt.subplots() ax.hist(err_norm) plt.show()
def eval_final_label(args): """Evaluation of labels on the training set.""" cfg, lbl = util.get_label_cfg_by_args(args) uid = cfg['uniqueid'] print('We are playing with %s' % uid) outdir = 'models/%s/gate_expert' % uid outname = 'gate_expert_model.pt' if KLLOSS: outname = 'gate_expert_kldiv_model.pt' if args.warm: outname = outname.replace('.pt', '_warm.pt') mdl_path = os.path.join(outdir, outname) gate_expert = GateExpertNet(mdl_path, False) eval_fun = gate_expert.get_p_y data = npload(cfg['file_path'], uid) datax = data[cfg['x_name']] p, v = eval_fun(datax) label = np.argmax(p, axis=1) if args.draw: fig, ax = plt.subplots() n_expert = np.amax(label) + 1 for i in range(n_expert): mask = label == i ax.scatter(datax[mask, 0], datax[mask, 1]) plt.show() label_name = 'data/pen/gate_expert_label.npy' if KLLOSS: label_name = label_name.replace('_label', '_kldiv_label') if args.warm: label_name = label_name.replace('.npy', '_warm.npy') np.save(label_name, label)
def eval_model_on_valid(args): """Evaluate model on the validation set.""" cfg, lbl = util.get_label_cfg_by_args(args) uid = cfg['uniqueid'] print('We are playing with %s' % uid) outdir = 'models/%s/gate_expert' % uid outname = 'gate_expert_model.pt' if KLLOSS: outname = 'gate_expert_kldiv_model.pt' if args.warm: outname = outname.replace('.pt', '_warm.pt') mdl_path = os.path.join(outdir, outname) gate_expert = GateExpertNet(mdl_path, False) eval_fun = gate_expert.get_y valid_set = np.load(cfg['valid_path']) valid_x = valid_set[cfg['x_name']] valid_y = valid_set[cfg['y_name']] predy = eval_fun(valid_x) # dump output into some file valid_name = 'data/%s/gate_expert_valid_data.npz' % uid if KLLOSS: valid_name = valid_name.replace('_valid', '_kldiv_valid') if args.warm: valid_name = valid_name.replace('.npz', '_warm.npz') np.savez(valid_name, x=valid_x, y=predy)
def main(): # pen, car, drone specifies which dataset to use # the next five clustering approach specifies which data to load args = getArgs('pen', 'car', 'drone', 'kmean', 'pcakmean', 'speuclid', 'spdydx', 'spvio', 'neighbor10', 'umap', 'pca') cfg, lbl_name = get_label_cfg_by_args(args) if args.umap: # I am done using this draw_umap_label(cfg, lbl_name) elif args.pca: draw_pca_label(cfg, lbl_name) else: check_cluster_label(cfg, lbl_name, args.neighbor)
def eval_model_on_valid(args): """Evaluate model on the validation set.""" cfg, lbl = util.get_label_cfg_by_args(args) uid = cfg['uniqueid'] print('We are playing with %s' % uid) outdir='models/%s/mom' % uid outname='mom_model.pt' eval_fun = momLoader(os.path.join(outdir, outname), args.prob, False) valid_set = np.load(cfg['valid_path']) valid_x = valid_set[cfg['x_name']] valid_y = valid_set[cfg['y_name']] predy = eval_fun(valid_x) # dump output into some file np.savez('data/%s/mom_valid_data.npz' % uid, x=valid_x, y=predy)
def main(): # num shows dataset size # drawpca draws pca results for datasets # umap draws scatter plot of umap projection # penscatter draws scatter plot on pendulum dataset, we draw scatter plot of x0 args = util.get_args('num', 'drawpca', 'umap', 'penscatter') cfg, lbl = util.get_label_cfg_by_args(args) if args.num: show_dataset_size(cfg) if args.drawpca: drawPCA(cfg) if args.umap: drawUMap(cfg) if args.penscatter: drawPenScatter(cfg)
def eval_final_label(args): """Evaluation of labels on the training set.""" cfg, lbl = util.get_label_cfg_by_args(args) uid = cfg['uniqueid'] print('We are playing with %s' % uid) outdir = 'models/%s/moe' % uid outname = 'moe_model.pt' mdl_path = os.path.join(outdir, outname) eval_fun = get_moe_loader(mdl_path, True) data = npload(cfg['file_path'], uid) datax = data[cfg['x_name']] p, v = eval_fun(datax) label = np.argmax(p, axis=1) np.save('data/pen/moe_label.npy', label)
def train_model_warm(args): """Train that model warmly by loading some pre-trained models. We say warm we mean we pretrain gate or experts. Not sure which I should use instead. Maybe I really need a function to modify weights of GaoNet to consider xScale and yScale """ cfg, lbl_name = util.get_label_cfg_by_args(args) uid = cfg['uniqueid'] result = util.get_clus_reg_by_dir('models/%s/%s' % (uid, lbl_name)) cls, regs = result[5] # we know this is bad but could possibly work cls_data = torch.load(cls) print(cls_data.keys()) cls_net = cls_data['model'] xmean, xstd = cls_data['xScale'] print('xmean', xmean, 'xstd', xstd) # cls_net.extendXYScale((xmean, xstd)) expert = Experts([[2, 60, 75]] * 5) run_the_training(args, cls_net, expert)
def eval_model(args): """Evaluate a model.""" cfg, lbl = util.get_label_cfg_by_args(args) eval_rst = run_the_training(args) y = eval_rst['y'] if args.prob: x = eval_rst['x'] p, predy = eval_rst['predy'] print(p.shape, predy.shape) argmax_idx = np.argmax(p, axis=1) n_model = np.amax(argmax_idx) + 1 fig, ax = plt.subplots() for i in range(n_model): mask = argmax_idx == i ax.scatter(*x[mask].T) plt.show() else: predy = eval_rst['predy'] n_data = y.shape[0] # get error error_y = y - predy error_norm = np.linalg.norm(error_y, axis=1) # get histogram fig, ax = plt.subplots() ax.hist(error_norm, bins='auto') ax.set_xlabel('Prediction Error') ax.set_ylabel('Count') diff_angle = y[:, 48] - predy[:, 48] fig, ax = plt.subplots() ax.hist(diff_angle, bins='auto') ax.set_xlabel(r'$\theta_f$ Error') ax.set_ylabel('Count') # show difference error_order = np.argsort(error_norm) for i in range(n_data // 10): fig, ax = plt.subplots() for j in range(10): idx = -1 - (i * 10 + j) ax.plot(y[idx], color='C%d' % j) ax.plot(predy[idx], color='C%d' % j, ls='--') plt.show()
def run_the_training(args): """Run the MoE training without using any clustering information but let it find it on its own.""" cfg, lbl = util.get_label_cfg_by_args(args) uid = cfg['uniqueid'] print('We are playing with %s' % uid) data = npload(cfg['file_path'], uid) data_feed = {'x': data[cfg['x_name']], 'y': data[cfg['y_name']]} dimx = data_feed['x'].shape[1] dimy = data_feed['y'].shape[1] n_model = args.k # create the network net = MoMNet([dimx, 100, n_model], [[dimx, int(np.ceil(300.0 / n_model)), dimy] for _ in range(n_model)]) net.argmax = False config = genTrainConfig(outdir='models/%s/mom' % uid, outname='mom_model.pt', overwrite=False) if args.eval: mdl_path = os.path.join(config['outdir'], config['outname']) eval_fun = momLoader(mdl_path, withclus=args.prob, argmax=False) predy = eval_fun(data_feed['x']) return {'x': data_feed['x'], 'y': data_feed['y'], 'predy': predy} trainOne(config, data_feed, net=net)
def main(): # pen, car, drone still means which problem we want to look into # pcakmean specifies the clustering approach we intend to use # error means we calculate evaluation error on the validation set # constr means we evaluate constraint violation # eval just evaluates data on validation set and save into a npz file for rollout validation # snn means we evaluate SNN network # roll means look into rollout results and extract useful information. It turns out they all fail, somehow args = util.get_args('debug', 'error', 'constr', 'eval', 'snn', 'roll') global DEBUG if args.debug: DEBUG = True cfg, lbl_name = util.get_label_cfg_by_args(args) if args.error: eval_valid_error(cfg, lbl_name, args) if args.constr: eval_valid_constr_vio(cfg, lbl_name, args) if args.eval: eval_on_valid(cfg, lbl_name, args) if args.roll: check_rollout_results(cfg, lbl_name, args)
def main(): # pen, car, drone still means which problem we want to look into # pcakmean specifies the clustering approach we intend to use # clas means we train classifier # debug enables debug mode # miss trains models that are missing, but why? args = util.get_args('debug', 'clas', 'miss') global DEBUG if args.debug: DEBUG = True cfg, lbl_name = util.get_label_cfg_by_args(args) if args.pen: def archi_fun(ratio): return gen_archi_fun([2, 300, 75], ratio, 20) clas = [2, 100, -1] elif args.car: def archi_fun(ratio): return gen_archi_fun([4, 200, 200, 149], ratio, 20) clas = [4, 500, -1] elif args.drone: def archi_fun(ratio): return gen_archi_fun([7, 1000, 1000, 317], ratio, 20) clas = [7, 1000, -1] elif args.dtwo: def archi_fun(ratio): return gen_archi_fun([11, 2000, 2000, 317], ratio, 30) clas = [11, 1000, -1] elif args.done: def archi_fun(ratio): return gen_archi_fun([7, 1000, 1000, 317], ratio, 30) clas = [7, 1000, -1] else: print('You have to choose one dataset') raise SystemExit if args.clas: train_model_classifier(cfg, lbl_name, clas) elif args.miss: train_missing_models(cfg, lbl_name, archi_fun) else: train_model_by_data(cfg, lbl_name, archi_fun)
def run_the_training(args, clus=None, expert=None): """Run the MoE training without using any clustering information but let it find it on its own.""" # load data cfg, lbl = util.get_label_cfg_by_args(args) uid = cfg['uniqueid'] print('We are playing with %s' % uid) data = npload(cfg['file_path'], uid) data_feed = {'x': data[cfg['x_name']], 'y': data[cfg['y_name']]} dimx = data_feed['x'].shape[1] dimy = data_feed['y'].shape[1] # create gate and expert if clus is None: n_model = 5 clus = GaoNet([dimx, 100, n_model]) expert = Experts([[dimx, 60, dimy]] * n_model) # cuda it clus.cuda() expert.cuda() # set data loader xname, yname = 'x', 'y' factory = KeyFactory(data_feed, xname, yname, scalex=True, scaley=True) factory.shuffle(None) draw_clus_region(clus, data_feed['x'], factory) # create two sets trainsize = 0.8 trainSet = SubFactory(factory, 0.0, trainsize) testSet = SubFactory(factory, trainsize, 1.0) batch_size = 32 test_batch_size = -1 trainLder = DataLoader(trainSet, batch_size=batch_size, shuffle=False) testLder = DataLoader(testSet, batch_size=test_batch_size, shuffle=False) # set up file output outname = 'gate_expert_model.pt' outdir = 'models/pen/gate_expert' if KLLOSS: outname = 'gate_expert_kldiv_model.pt' if args.warm: outname = outname.replace('.pt', '_warm.pt') # set optimizer lr = 1e-3 opt_G = torch.optim.Adam(clus.parameters(), lr=lr) opt_E = torch.optim.Adam(expert.parameters(), lr=lr) # set other training stuff n_epoch = 500 back_check_epoch = 8 best_test_loss = np.inf best_test_loss_expert = np.inf best_test_epoch = 0 def get_mean_error(g_y, exp_y, feedy): """Calculate two loss""" error_traj = torch.mean((exp_y - feedy.expand_as(exp_y))**2, dim=2).t() g = f.softmax(g_y) log_g = f.log_softmax(g_y) posterior = g * torch.exp( -0.5 * error_traj) # b by r probability, not scaled to 1 traj_prob = torch.mean(-torch.log(torch.sum(posterior, dim=1))) if KLLOSS: posterior_scale = Variable( (posterior / torch.sum(posterior, dim=1, keepdim=True) ).data) # do not use gradient of it div_error = f.kl_div(log_g, posterior_scale) return traj_prob, div_error else: Og = torch.sum(exp_y * g.t().unsqueeze(2), dim=0) traj_error = f.smooth_l1_loss(Og, feedy) return traj_prob, traj_error # start training for epoch in range(n_epoch): sum_train_loss = 0 sum_train_loss_prob = 0 for idx, batch_data in enumerate(trainLder): feedy = Variable(batch_data[yname], requires_grad=False).cuda() feedx = Variable(batch_data[xname], requires_grad=False).cuda() # train experts opt_E.zero_grad() opt_G.zero_grad() exp_y = expert(feedx) g_y = clus(feedx) g = f.softmax(g_y) # this is prior log_g = f.log_softmax(g_y) error_traj = torch.mean((exp_y - feedy.expand_as(exp_y))**2, dim=2).t() posterior = g * torch.exp( -0.5 * error_traj) # b by r probability, not scaled to 1 posterior_scale = Variable( (posterior / torch.sum(posterior, dim=1, keepdim=True) ).data) # do not use gradient of it lossi = torch.mean(-torch.log(torch.sum(posterior, dim=1))) lossi.backward(retain_graph=True) sum_train_loss_prob += lossi.cpu().data.numpy() * feedx.size()[0] opt_E.step() # update h by regression error all_pred = exp_y if KLLOSS: error = f.kl_div(log_g, posterior_scale) else: Og_before = all_pred * g.t().unsqueeze(2) Og = torch.sum(Og_before, dim=0) error = f.smooth_l1_loss(Og, feedy) sum_train_loss += error.cpu().data.numpy() * feedx.size()[0] error.backward() opt_G.step() # val = clus.printWeights(3) mean_train_loss = sum_train_loss / trainLder.getNumData() mean_train_loss_prob = sum_train_loss_prob / trainLder.getNumData() # evaluate on test data sum_test_loss_gate = 0 sum_test_loss_expert = 0 n_test_data = testLder.getNumData() for idx, batch_data in enumerate(testLder): feedy = Variable(batch_data[yname], volatile=True).cuda() feedx = Variable(batch_data[xname], volatile=True).cuda() exp_y = expert(feedx) g_y = clus(feedx) traj_prob, div_error = get_mean_error(g_y, exp_y, feedy) sum_test_loss_gate += div_error.cpu().data.numpy() * feedx.size( )[0] sum_test_loss_expert += traj_prob.cpu().data.numpy() * feedx.size( )[0] mean_test_loss_gate = sum_test_loss_gate / n_test_data mean_test_loss_expert = sum_test_loss_expert / n_test_data print('epoch %d gate loss %f expert loss %f test gate loss %f expert loss %f' \ % (epoch, mean_train_loss, mean_train_loss_prob, mean_test_loss_gate, mean_test_loss_expert)) if mean_test_loss_gate < best_test_loss: best_test_loss = mean_test_loss_gate best_test_epoch = epoch if mean_test_loss_expert < best_test_loss_expert: best_test_loss_expert = mean_test_loss_expert best_test_epoch = epoch if epoch > best_test_epoch + back_check_epoch: break print('Save model now') # draw region for classifier draw_clus_region(clus, data_feed['x'], factory) clus.cpu() expert.cpu() model = { 'gate': clus, 'expert': expert, 'xScale': [trainLder.xmean, trainLder.xstd], 'yScale': [trainLder.ymean, trainLder.ystd] } if not os.path.exists(outdir): os.mkdir(outdir) torch.save(model, os.path.join(outdir, outname))
def main(): args = util.get_args('show') cfg, lbl_name = util.get_label_cfg_by_args(args) show_picky_states(cfg, lbl_name, args)
def main(): args = util.get_args() cfg, lbl = util.get_label_cfg_by_args(args) show_picky_examples(cfg)