def __init__(self, models): super(MDSM, self).__init__(models) self.model = models["model"] self.sigma0 = config.get("loss", "sigma0") self.sigma_begin = config.get("loss", "sigma_begin") self.sigma_end = config.get("loss", "sigma_end") self.dist = config.get("loss", "dist")
def __init__(self, models): super(BiMDSM, self).__init__(models) self.sigma0 = config.get("loss", "sigma0") self.sigma_begin = config.get("loss", "sigma_begin") self.sigma_end = config.get("loss", "sigma_end") self.dist = config.get("loss", "dist") self.inner_loss_div_sigmas = config.get("loss", "inner_loss_div_sigmas", default=True)
def get(self): api = QQAPIClient(config.get('qq_apiid'), config.get('qq_appkey'), redirect_uri=config.get('qq_callback_url')) code = self.request.query_arguments.get('code')[0] print code access_token = api.request_access_token(code) api.set_access_token(access_token['access_token'], access_token['expires_in']) user_info = api.get.user__get_user_info() openid = api.get_openid() now = datetime.utcnow() user = User.objects.filter(third_info__third_type='qq', third_info__openid=openid).first() if not user: user = User(create_on=now, modify_on=now, last_login=now) else: user.modify_on = now user.last_login = now user.username = openid user.nick_name = user_info.get('nickname', '') user.email = '*****@*****.**' % openid gender = 'm' if user_info.get('gender') == u'男' else 'f' user.gender = gender user.avatar = user_info.get('figureurl_qq_2', '') user.third_info = {'third_type': 'qq', 'info': dict(user_info), 'openid': openid} user.save() # set user cookie self.set_secure_cookie(config.get('uname'), openid) self.write(json.dumps(make_success_response(user_info)))
def __init__(self): super(BaseRunner, self).__init__() self.batch_size = config.get("training", "batch_size") self.n_its = config.get("training", "n_its") self.n_ckpts = config.get("training", "n_ckpts", default=10) self.log_invl = config.get("interval", "log", default=100) self.name = config.name self.device = config.device self.workspace_root = config.workspace_root self.roots = {} # set path self.model_root = os.path.join(self.workspace_root, "models") if not os.path.exists(self.model_root): os.makedirs(self.model_root) self.ckpt_path = os.path.join(self.model_root, "{}.ckpt.pth".format(self.name)) self.model_path = os.path.join(self.model_root, "{}.pth".format(self.name)) # set dataset self.data_factory = None self.dataset = None self.tr = None self.val = None self.te = None self.labelled_tr = None self.labelled_te = None self.init_dataset() # set model self.models = {} self.init_model() self.model = self.models.get("model") self.q = self.models.get("q") # set criterion self.criterion = None self.init_criterion() # states of training self.it = None self.best_val_loss = None # set evaluator self.evaluator_root = os.path.join(self.workspace_root, "evaluation") self.evaluator_family = config.get("evaluator", "family", default="BaseEvaluator") self.evaluator = eval(self.evaluator_family)(self, self.evaluator_root)
def __init__(self): super(EBMResNet, self).__init__() self.channels = config.get("model", "channels") self.feature_net = config.get("model", "feature_net") self.scalar_net = config.get("model", "scalar_net") hw = self.v_shape[-1] in_channels = self.v_shape[0] self.af = nn.ELU() self.v2f = nn.DataParallel( eval(self.feature_net)(in_channels, self.channels)) self.f2e = eval(self.scalar_net)( in_features=(hw // 8)**2 * 8 * self.channels, features=(hw // 8)**2 * 4 * self.channels)
def __init__(self): super(GRBM, self).__init__() self.fix_std = config.get("model", "fix_std", default=False) if self.fix_std: std = config.get("model", "std") self.log_std = torch.ones(self.v_dim).to(self.device) * np.log(std) else: self.log_std = nn.Parameter(torch.zeros(self.v_dim), requires_grad=True) self.b_h = nn.Parameter(torch.zeros(self.h_dim), requires_grad=True) self.b_v = nn.Parameter(torch.zeros(self.v_dim), requires_grad=True) self.W = nn.Parameter(torch.zeros(self.v_dim, self.h_dim), requires_grad=True) self.init_parameters() self.log_partition_ = None
def init_dataset(self): self.dataset = config.get("data", "dataset") self.data_factory = eval(self.dataset)() if config.get("data", "use_val", default=False): self.tr = self.data_factory.get_train_data() self.val = self.data_factory.get_val_data() else: self.tr = self.data_factory.get_train_val_data() self.val = None self.te = self.data_factory.get_test_data() if self.data_factory.allow_labelled(): self.labelled_tr = self.data_factory.get_train_val_data( labelled=True) self.labelled_te = self.data_factory.get_test_data(labelled=True)
def _init_optim_bilevel(criterion): optimizer = config.get("optim", "optimizer") scheduler = config.get("optim", "scheduler") lr = config.get("optim", "lr") lr_q = config.get("optim", "lr_q", default=lr) weight_decay = config.get("optim", "weight_decay", default=0.) model, q = criterion.models["model"], criterion.models["q"] # init optimizers if optimizer == "Adam": betas = tuple(config.get("optim", "betas", default=(0.9, 0.95))) criterion.opts["model"] = optim.Adam(model.parameters(), lr=lr, weight_decay=weight_decay, betas=betas) criterion.opts["q"] = optim.Adam(q.parameters(), lr=lr_q, weight_decay=weight_decay, betas=betas) elif optimizer == "RMSProp": criterion.opts["model"] = optim.RMSprop(model.parameters(), lr=lr, weight_decay=weight_decay) criterion.opts["q"] = optim.RMSprop(q.parameters(), lr=lr_q, weight_decay=weight_decay) elif optimizer == "SGD": momentum = config.get("optim", "momentum", default=0.01) criterion.opts["model"] = optim.SGD(model.parameters(), lr=lr, momentum=momentum, weight_decay=weight_decay) criterion.opts["q"] = optim.SGD(q.parameters(), lr=lr_q, momentum=momentum, weight_decay=weight_decay) else: raise NotImplementedError # init schedulers n_its = config.get("training", "n_its") if scheduler == "step": criterion.schs["model"] = optim.lr_scheduler.StepLR( criterion.opts["model"], n_its // 6) criterion.schs["q"] = optim.lr_scheduler.StepLR( criterion.opts["q"], n_its // 6) elif scheduler == "cosine": criterion.schs["model"] = optim.lr_scheduler.CosineAnnealingLR( criterion.opts["model"], n_its, eta_min=1e-6) criterion.schs["q"] = optim.lr_scheduler.CosineAnnealingLR( criterion.opts["q"], n_its, eta_min=1e-6) elif scheduler == "const": criterion.schs["model"] = optim.lr_scheduler.StepLR( criterion.opts["model"], n_its) criterion.schs["q"] = optim.lr_scheduler.StepLR( criterion.opts["q"], n_its) else: raise NotImplementedError
def log_likelihood(self, it, **kwargs): ais = config.get("others", "ais", default=False) if ais: self.model.update_log_partition() ll = evaluate(self.te, self.runner, lambda v: self.model.log_likelihood(v, ais=ais), batch_size=min(10000, len(self.te))) logging.info("[log_likelihood] [it: {}] [log_likelihood: {}]".format(it, ll)) writer.add_scalar("log_likelihood", ll, global_step=it)
def build_sign(*args): """ build a self signed cookie for verify login state """ skey = config.get('skey') tm = datetime.now().strftime('%Y%m%d') signed_args = list(args) signed_args.append(tm) signed_args.append(skey) s = '/'.join(signed_args).lower() sign = md5.md5(s).hexdigest() return sign
def main(): args = init() runner = BaseRunner() if args.mode == "train": try: runner.fit() except: logging.error("training is failed") logging.error(traceback.format_exc()) elif args.mode == "valid": try: valid_sample(runner) except: logging.error("validation is failed") logging.error(traceback.format_exc()) elif args.mode == "test": test_root = os.path.join(config.workspace_root, "test") if not os.path.exists(test_root): os.makedirs(test_root) try: runner.load_model() runner.eval() mean_loss = runner.criterion.evaluate(runner.te, runner).item() np.save(os.path.join(test_root, "test_mean_loss.npy"), mean_loss) for fn, flag in config.get("evaluate_options").items(): if flag: eval("runner.evaluator.{}".format(fn))( name="{}.png".format(fn), it=0) except: np.save(os.path.join(test_root, "test_mean_loss.npy"), float("nan")) logging.error("testing is failed") logging.error(traceback.format_exc()) elif args.mode == "test_ckpt": # test a specific ckpt ckpt = config.get("others", "tested_ckpt") test_root = os.path.join(config.workspace_root, "test", ckpt) sample_from_ckpt(runner, ckpt, 50000, os.path.join(test_root, "samples")) else: raise NotImplementedError return 0
def _update(v, runner, criterion): opt_model, opt_q = criterion.opts["model"], criterion.opts["q"] sch_model, sch_q = criterion.schs["model"], criterion.schs["q"] # update q runner.model.requires_grad_(False) runner.q.requires_grad_(True) for i in range(config.get("update", "n_inner_loops")): opt_q.zero_grad() criterion.mid_vals["inner_loss"] = inner_loss = criterion.inner_loss(v).mean() inner_loss.backward() # backward if config.get("update", "gradient_clip", default=False): clip_grad_norm_(runner.q.parameters(), 0.5) opt_q.step() # step sch_q.step() # update model runner.model.requires_grad_(True) runner.q.requires_grad_(True) backup_q_state_dict = runner.q.state_dict() with higher.innerloop_ctx(runner.q, opt_q) as (fq, diffopt_q): criterion.q = fq for i in range(config.get("update", "n_unroll")): inner_loss = criterion.inner_loss(v).mean() diffopt_q.step(inner_loss) criterion.loss_val = loss = criterion.loss(v).mean() opt_model.zero_grad() runner.model.requires_grad_(True) runner.q.requires_grad_(False) loss.backward() if config.get("update", "gradient_clip", default=False): clip_grad_norm_(runner.model.parameters(), 0.5) opt_model.step() sch_model.step() runner.q.load_state_dict(backup_q_state_dict) criterion.q = runner.q criterion.mid_vals["grad_model"] = grad_norm(runner.model) criterion.mid_vals["grad_q"] = grad_norm(runner.q)
def __init__(self): super(CelebA, self).__init__() self.binarized = config.get("data", "binarized", default=False) self.gauss_noise = config.get("data", "gauss_noise", default=False) self.noise_std = config.get("data", "noise_std", default=0.01) self.flattened = config.get("data", "flattened", default=False) if self.binarized: assert not self.gauss_noise self.data_path = os.path.join("workspace", "datasets", "celeba") # Get datasets im_transformer = transforms.Compose([ transforms.CenterCrop(140), transforms.Resize(32), transforms.RandomHorizontalFlip(p=0.5), transforms.ToTensor() ]) self.train = datasets.CelebA(self.data_path, split="train", target_type=[], transform=im_transformer, download=True) self.val = datasets.CelebA(self.data_path, split="valid", target_type=[], transform=im_transformer, download=True) self.test = datasets.CelebA(self.data_path, split="test", target_type=[], transform=im_transformer, download=True) self.train = UnlabeledDataset(self.train) self.test = UnlabeledDataset(self.test) self.val = UnlabeledDataset(self.val)
def init(): parser = argparse.ArgumentParser(description=globals()['__doc__']) parser.add_argument('--config', type=str, required=True, help='Path to the config file') parser.add_argument('--workspace', type=str, required=True, help='Path to the workspace') parser.add_argument('--mode', type=str, default='train', help='Train, valid or test the model (or others)') args = parser.parse_args() # set config config_ = load_yaml(args.config) config_["device"] = torch.device( 'cuda') if torch.cuda.is_available() else torch.device('cpu') config_["workspace_root"] = args.workspace config.set_config(config_) # set writer summary_root = os.path.join(config.workspace_root, "summary") if not os.path.exists(summary_root): os.makedirs(summary_root) writer.set_path(summary_root) # set seed seed = config.get("others", "seed", default=1234) torch.manual_seed(seed) np.random.seed(seed) random.seed(seed) if torch.cuda.is_available(): torch.cuda.manual_seed_all(seed) # set logger log_root = os.path.join(config.workspace_root, "logs") if not os.path.exists(log_root): os.makedirs(log_root) log_path = os.path.join(log_root, "{}.log".format(args.mode)) set_logger(log_path) logging.info("running @ {}".format(socket.gethostname())) logging.info(config) return args
def sample(self, name, **kwargs): fname = self._prepare("sample", name, "png") if config.get("others", "record_gibbs", default=False): name, ext = os.path.splitext(fname) samples, v0 = self.model.sample(50, ret_v0=True, record_gibbs=True, v0=self.sample_v0(50)) v0 = self.data_factory.unpreprocess(v0) grid_v0 = make_grid(v0, 5) for gibbs_step, v in samples: v = self.data_factory.unpreprocess(v) grid_v = make_grid(v, 5) grid = make_grid([grid_v0, grid_v]) save_image(grid, "{}_gibbs_{}{}".format(name, gibbs_step, ext)) else: super(RBMEvaluator, self).sample(name, **kwargs)
def make_app(): """ make an app instance to start the server """ settings = {'static_path': join(dirname(__file__), 'static'), 'template_path': join(dirname(__file__), 'templates')} cookie_secret = config.get('cookie_secret') app = tornado.web.Application([(r"/qq_redirect/", QQLoginHandler), (r"/login/", DirectLoginHandler), (r"/logout/", LogoutHandler), (r"/", IndexHandler)], cookie_secret=cookie_secret, **settings) return app
def post(self): """ POST会检查给定的用户名和密码是否正确,如果不正确会返回错误 """ username = self.request.body_arguments.get('username')[0] password = self.request.body_arguments.get('password')[0] msg = u'用户名或密码错误' try: user = User.objects.get(username=username) if user.check_password(password): self.set_secure_cookie(config.get('uname'), username) self.write(json.dumps(make_success_response({'uname': username}))) else: self.render('login.html', user=user) except: self.render('login.html')
def __init__(self): super(Cifar10, self).__init__() self.binarized = config.get("data", "binarized", default=False) self.gauss_noise = config.get("data", "gauss_noise", default=True) self.noise_std = config.get("data", "noise_std", default=0.01) self.flattened = config.get("data", "flattened", default=True) if self.binarized: assert not self.gauss_noise self.data_path = os.path.join("workspace", "datasets", "cifar10") # Get datasets im_transformer = transforms.Compose([transforms.ToTensor()]) self.train_val = datasets.CIFAR10(self.data_path, train=True, transform=im_transformer, download=True) self.train = Subset(self.train_val, list(range(40000))) self.val = Subset(self.train_val, list(range(40000, 50000))) self.test = datasets.CIFAR10(self.data_path, train=False, transform=im_transformer, download=True)
def build_sbuss(user, request): """ build the entire cookie info """ user_info = { 'id' : user['id'], 'username' : user['username'], 'level' : user['level'], } rand_str = config.get('rand_str') user_ip = request.environ.get('REMOTE_ADDR') or request.remote_addr user_agent = request.get_header('User-Agent', '') user_info['rand'] = ''.join(random.sample(rand_str, 8)) user_info['sign'] = build_sign(user_info['id'], str(user_info['level']), user_ip, user_agent, user_info['rand']) return encode_cookie(user_info)
def getLogger(package_name): """ init logger instance """ logger = getattr(_LOCALS, package_name, None) if logger is not None: return logger log_file = config.get('log_file') logger = logging.getLogger(package_name) hdlr = logging.FileHandler(log_file) formatter = '[%(asctime)s] Level:%(levelname)s Message:%(message)s', '%Y-%m-%d %a %H:%M:%S' formatter = logging.Formatter(*formatter) hdlr.setFormatter(formatter) logger.addHandler(hdlr) logger.setLevel(logging.DEBUG) setattr(_LOCALS, package_name, logger) return logger
def evaluate_during_training(self): mode = self.training self.eval() for fn, flag in config.get("evaluate_options").items(): times = 0 if flag is True: times = 20 elif isinstance(flag, int): times = flag if times > 0: invl = self.n_its // times if self.it % invl == invl - 1: eval("self.evaluator.{}".format(fn))(name="it_{}".format( self.it), it=self.it) self.train(mode)
def sample(self, name, **kwargs): fname = self._prepare("sample", name, "png") sample_method = config.get("others", "sample_method", default="normal") if sample_method == "normal": samples = self.model.sample(100, random=False) samples = self.data_factory.unpreprocess(samples) grid = make_grid(samples, 10) save_image(grid, fname) elif sample_method == "via_q": idxes = np.random.randint(0, len(self.tr), size=100) v = torch.stack(list(map(lambda idx: self.tr[idx], idxes)), dim=0).to(config.device) feature, _ = self.q.moments(v) samples = self.model.csample_v(feature, random=False) samples = self.data_factory.unpreprocess(samples) grid = make_grid(samples, 10) save_image(grid, fname)
def sample2dir(self, path, n_samples): sample_method = config.get("others", "sample_method", default="normal") idx = 0 for batch_size in amortize(n_samples, self.runner.batch_size): if sample_method == "normal": samples = self.model.sample(batch_size, random=False) samples = self.data_factory.unpreprocess(samples) elif sample_method == "via_q": idxes = np.random.randint(0, len(self.tr), size=batch_size) v = torch.stack(list(map(lambda idx: self.tr[idx], idxes)), dim=0).to(config.device) feature, _ = self.q.moments(v) samples = self.model.csample_v(feature, random=False) samples = self.data_factory.unpreprocess(samples) else: raise NotImplementedError for sample in samples: save_image(sample, os.path.join(path, "%d.png" % idx)) idx += 1
def __init__(self): super(FreyFace, self).__init__() self.binarized = config.get("data", "binarized", default=False) self.gauss_noise = config.get("data", "gauss_noise", default=True) self.noise_std = config.get("data", "noise_std", default=0.01) self.preprocess = config.get("data", "preprocess", default=None) self.flattened = config.get("data", "flattened", default=True) if self.binarized: assert not self.gauss_noise assert self.preprocess is None assert self.preprocess == "standardize" or self.preprocess == "subtract_mean" or self.preprocess is None self.data_root = os.path.join("workspace", "datasets", "freyface") if not os.path.exists(self.data_root): os.makedirs(self.data_root) self.data_path = os.path.join(self.data_root, "frey_rawface.mat") if not os.path.exists(self.data_path): request.urlretrieve( "https://cs.nyu.edu/~roweis/data/frey_rawface.mat", self.data_path) data = sio.loadmat(self.data_path)['ff'].transpose().astype( np.float32) / 255. data = torch.tensor(data).view(-1, 1, 28, 20) train_data = data[:1400] val_data = data[1400:1700] test_data = data[1700:] # Get datasets self.train = QuickDataset(train_data) self.val = QuickDataset(val_data) self.test = QuickDataset(test_data) self.uniform = QuickDataset(torch.rand_like(train_data)) # Calculate the train mean and std if self.preprocess == "standardize": standardize_mode = config.get("data", "standardize_mode", default="pixel") if standardize_mode == "pixel": self.train_mean = 0.6049 self.train_std = 0.1763 elif standardize_mode == "vector": self.train_mean = train_data.mean(dim=0) self.train_std = train_data.std(dim=0, unbiased=False) + 1e-3 else: raise NotImplementedError
def _init_optim_nce(criterion): optimizer = config.get("optim", "optimizer") scheduler = config.get("optim", "scheduler") lr = config.get("optim", "lr") weight_decay = config.get("optim", "weight_decay", default=0.) params = [] for name, model in criterion.models.items(): params += model.parameters() params += [criterion.c] # nce parameter # init optimizers if optimizer == "Adam": betas = tuple(config.get("optim", "betas", default=(0.9, 0.95))) criterion.opts["all"] = optim.Adam(params, lr=lr, weight_decay=weight_decay, betas=betas) elif optimizer == "RMSProp": criterion.opts["all"] = optim.RMSprop(params, lr=lr, weight_decay=weight_decay) elif optimizer == "SGD": momentum = config.get("optim", "momentum", default=0.01) criterion.opts["all"] = optim.SGD(params, lr=lr, momentum=momentum, weight_decay=weight_decay) else: raise NotImplementedError # init schedulers n_its = config.get("training", "n_its") if scheduler == "step": criterion.schs["all"] = optim.lr_scheduler.StepLR( criterion.opts["all"], n_its // 6) elif scheduler == "cosine": criterion.schs["all"] = optim.lr_scheduler.CosineAnnealingLR( criterion.opts["all"], n_its, eta_min=1e-6) elif scheduler == "const": criterion.schs["all"] = optim.lr_scheduler.StepLR( criterion.opts["all"], n_its) else: raise NotImplementedError
def __init__(self): super(Mnist, self).__init__() self.binarized = config.get("data", "binarized", default=False) self.gauss_noise = config.get("data", "gauss_noise", default=True) self.noise_std = config.get("data", "noise_std", default=0.01) self.preprocess = config.get("data", "preprocess", default=None) self.flattened = config.get("data", "flattened", default=True) self.padding = config.get("data", "padding", default=False) if self.binarized: assert not self.gauss_noise assert self.preprocess is None assert self.preprocess == "standardize" or self.preprocess == "subtract_mean" or self.preprocess is None self.data_path = os.path.join("workspace", "datasets", "mnist") # Get datasets im_transformer = transforms.Compose([transforms.ToTensor()]) self.train_val = datasets.MNIST(self.data_path, train=True, transform=im_transformer, download=True) self.train = Subset(self.train_val, list(range(50000))) self.val = Subset(self.train_val, list(range(50000, 60000))) self.test = datasets.MNIST(self.data_path, train=False, transform=im_transformer, download=True) # digits = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) # self.train = my_filter(self.train, digits) # self.val = my_filter(self.val, digits) # self.test = my_filter(self.test, digits) self.train_mean = 0.1309 self.train_std = 0.3085
def get(self): api = QQAPIClient(config.get("qq_apiid"), config.get("qq_appkey"), redirect_uri=config.get("qq_callback_url")) auth_url = api.get_authorization_url("authorize") self.render("index.html", auth_url=auth_url)
import json from utils.config import config file_name = config.get("data", "sample_content_file") with open(file_name, 'r') as file: i = 0 for line in file: patent = json.loads(line) print patent['id'] i+=1 print i
def make_app(): """ make an app instance to start the server """ settings = {'static_path': join(dirname(__file__), 'static'), 'template_path': join(dirname(__file__), 'templates')} cookie_secret = config.get('cookie_secret') app = tornado.web.Application([(r"/qq_redirect/", QQLoginHandler), (r"/login/", DirectLoginHandler), (r"/logout/", LogoutHandler), (r"/", IndexHandler)], cookie_secret=cookie_secret, **settings) return app if __name__ == "__main__": # 1. generate app instance app = make_app() # 2. Make Tornado app listen on port 8080 serve_port = int(config.get('server_port')) app.listen(serve_port) print "Listening at %s" % serve_port # 3. Start IOLoop tornado.ioloop.IOLoop.current().start()
def __init__(self, models): super(IWAE, self).__init__(models) self.model = models["model"] self.q = models["q"] self.k = config.get("loss", "k")
def __init__(self, models): super(BiDSM, self).__init__(models) self.noise_std = config.get("loss", "noise_std")
def __init__(self, models): super(BiSSM, self).__init__(models) self.noise_type = config.get("loss", "noise_type", default='radermacher')
def is_older(date): volatility_days = datetime.datetime.now() - datetime.timedelta(days=config.get('volatility')) return volatility_days.timestamp() >= date
def __init__(self, models): self.model = models["model"] self.nu = config.get("loss", "nu", default=1.) self.c = nn.Parameter(torch.scalar_tensor(0., device=config.device)) super(NCE, self).__init__(models)
def init_criterion(self): self.criterion = eval(config.get("criterion", "family"))(self.models)
def __init__(self, models): super(CD, self).__init__(models) self.model = models["model"] self.k = config.get("loss", "k", default=1)
import json from utils.config import config file_name = config.get("data", "sample_content_file") with open(file_name, 'r') as file: i = 0 for line in file: patent = json.loads(line) print patent['id'] i += 1 print i
def __init__(self, models): super(BiSM, self).__init__(models) self.model = models["model"] self.q = models["q"] self.inner_loss_type = config.get("loss", "inner_loss_type") self.k = config.get("loss", "k", default=20) # for iwae inner loss
def post(self): """ 处理退出登录相关的事宜 """ self.clear_cookie(config.get('uname')) self.render('login.html', msg='')
def __init__(self, models): super(DSM, self).__init__(models) self.model = models["model"] self.noise_std = config.get("loss", "noise_std")