def main(args): if not path.exists(args.save_dir): makedirs(args.save_dir) print("Create {}".format(args.save_dir)) if args.mode == "train": train_loader = DataLoader("train", args.batch_size, args.pad_size, args.crop_size) valid_loader = DataLoader("valid", args.batch_size*2) solver = Solver(args, train_loader, valid_loader) solver.train()
def test(config, model_dir, test_dir): data_loader = DataLoader(test_dir, mode='test', tokenize_func=lmmrl_tokenizer, encode_func=lmmrl_encoder) batch_loader = BatchLoader(data_loader, batch_size=config.batch_size, timesteps=config.timesteps, mode='test') cfg_proto = tf.ConfigProto(intra_op_parallelism_threads=0, inter_op_parallelism_threads=0) cfg_proto.gpu_options.allow_growth = True # Load word frequency information with open(os.path.join(test_dir, 'word_freq.txt'), encoding='utf-8') as f: freq = f.read().split() config['freq'] = freq config.save_dir = model_dir model = Model(config) with tf.Session(config=cfg_proto, graph=model.graph) as sess: # Restore model/Initialize weights initializer = tf.random_uniform_initializer(-0.05, 0.05) with tf.variable_scope("model", reuse=None, initializer=initializer): _ = restore_model(sess, model, model_dir) print("Model restored from %s" % model_dir) # Finalize graph to prevent memory leakage sess.graph.finalize() # Prepare loader batch_loader.reset_pointers() # Start from an empty RNN state init_states = sess.run(model.initial_states) states = init_states acc_loss = np.zeros(batch_loader.batch_size) end_epoch = False b = 1 while not end_epoch: x, y, end_epoch = batch_loader.next_batch() if end_epoch: break loss = model.forward(sess, x, y, states, mode='val') # accumulate evaluation metric here acc_loss += loss print("Batch = %d, Average loss = %.4f" % (b, np.mean(loss))) b += 1 final_metric = np.exp(np.mean(acc_loss) / (b - 1)) print("(Averaged) Evaluation metric = %.4f" % final_metric)
def main(): batch_size = 10 # generating our data train_inputs, train_targets = generate_data(1000, 2) test_inputs, test_targets = generate_data(1000, 2) # creating our loaders for training and test sets train_loader = DataLoader(train_inputs, train_targets, batch_size) test_loader = DataLoader(test_inputs, test_targets, batch_size) # defining our layers layers = [ Linear(input_dim=train_inputs[0].shape[0], output_dim=25), Relu(), Linear(input_dim=25, output_dim=25), Relu(), Linear(input_dim=25, output_dim=2), Tanh() ] # creating our model model = Sequential(layers) # init our optimizer optimizer = SGD(model.get_params(), lr=0.01) # init our trainer trainer = Trainer(model=model, optimizer=optimizer, epochs=500, loss=LossMSE(), train_loader=train_loader, test_loader=test_loader) # starting the training session trainer.train() return 0
user_feature = torch.tensor(user_feature, dtype=torch.float32) item_feature = torch.tensor(item_feature, dtype=torch.float32) if opt["cuda"]: user_feature = user_feature.cuda() item_feature = item_feature.cuda() UV = UV.cuda() VU = VU.cuda() adj = adj.cuda() fake_adj = fake_adj.cuda() corruption_UV = corruption_UV.cuda() corruption_VU = corruption_VU.cuda() print("Loading data from {} with batch size {}...".format(opt['data_dir'], opt['batch_size'])) train_batch = DataLoader(opt['data_dir'] + 'train.txt', opt['batch_size'], opt, user_real_dict, user_fake_dict, item_real_dict, item_fake_dict, evaluation=False) dev_batch = DataLoader(opt['data_dir'] + 'test.txt', opt["batch_size"], opt, user_real_dict, user_fake_dict, item_real_dict, item_fake_dict, evaluation=True) # model if not opt['load']: trainer = DGITrainer(opt) else: # load pretrained model model_file = opt['model_file'] print("Loading model from {}".format(model_file)) model_opt = torch_utils.load_config(model_file) model_opt['optim'] = opt['optim'] trainer = DGITrainer(opt) trainer.load(model_file)
# opt['word_vocab_size'] = len(char2id)+2 # opt['pos_size'] = len(id2pos)+2 vocab_file = opt['vocab_dir'] + '/vocab.pkl' vocab = Vocab(vocab_file, load=True) opt['word_vocab_size'] = vocab.size emb_file = opt['vocab_dir'] + '/embedding.npy' emb_matrix = np.load(emb_file) assert emb_matrix.shape[0] == vocab.size # assert emb_matrix.shape[1] == opt['word_emb_dim'] word2id = vocab.word2id # load data 加载数据 print("Loading data from {} with batch size {}...".format( opt['data_dir'], opt['batch_size'])) train_batch = DataLoader(bert_tokenizer, train_data, predicate2id, subj_type2id, obj_type2id, opt['batch_size']) model_id = opt['id'] if len(opt['id']) > 1 else '0' + opt['id'] model_save_dir = opt['save_dir'] + '/' + model_id opt['model_save_dir'] = model_save_dir helper.ensure_dir(model_save_dir, verbose=True) # save config helper.save_config(opt, model_save_dir + '/config.json', verbose=True) file_logger = helper.FileLogger( model_save_dir + '/' + opt['log'], header="# epoch\ttrain_loss\dev_p\tdev_r\tdev_f1") # print model info helper.print_config(opt) print(opt['num_class']) # model 初始化模型
opt['word_vocab_size'] = len(char2id) + 2 opt['pos_size'] = len(id2pos) + 2 vocab_file = opt['vocab_dir'] + '/vocab.pkl' vocab = Vocab(vocab_file, load=True) opt['word_vocab_size'] = vocab.size emb_file = opt['vocab_dir'] + '/embedding.npy' emb_matrix = np.load(emb_file) assert emb_matrix.shape[0] == vocab.size assert emb_matrix.shape[1] == opt['word_emb_dim'] word2id = vocab.word2id # load data print("Loading data from {} with batch size {}...".format( opt['data_dir'], opt['batch_size'])) train_batch = DataLoader(train_data, predicate2id, char2id, word2id, pos2id, subj_type2id, obj_type2id, opt['batch_size']) model_id = opt['id'] if len(opt['id']) > 1 else '0' + opt['id'] model_save_dir = opt['save_dir'] + '/' + model_id opt['model_save_dir'] = model_save_dir helper.ensure_dir(model_save_dir, verbose=True) # save config helper.save_config(opt, model_save_dir + '/config.json', verbose=True) file_logger = helper.FileLogger( model_save_dir + '/' + opt['log'], header="# epoch\ttrain_loss\dev_p\tdev_r\tdev_f1") # print model info helper.print_config(opt) print(opt['num_class']) # model
# set CPU/GPU device for execution if args.gpu_ids is not None: if args.gpu_ids[0] >= 0: device = torch.device("cuda", args.gpu_ids[0]) else: device = torch.device("cpu") else: device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") # ---------------------------------------------------------------------------- # Importing Dataset Loader # ---------------------------------------------------------------------------- if args.overfit is True: train_loader = DataLoader(os.path.join(args.data_dir, "train"), num_workers=args.cpu_workers, batch_size=args.batch_size, limit=256, shuffle=True) test_loader = DataLoader(os.path.join(args.data_dir, "val"), num_workers=args.cpu_workers, batch_size=args.batch_size, limit=256, shuffle=False) else: train_loader = DataLoader(os.path.join(args.data_dir, "train"), num_workers=args.cpu_workers, batch_size=args.batch_size, shuffle=True) test_loader = DataLoader(os.path.join(args.data_dir, "val"), num_workers=args.cpu_workers, batch_size=args.batch_size,
""" Read YAML file and do some additional processing. """ config = {} with open(config_file, 'r') as file: config = yaml.load(file, Loader=yaml.SafeLoader) return config if __name__ == "__main__": os.environ['TF_CPP_MIN_LOG_LEVEL'] = "2" args, unknown = _parse_args() config = parse_config_file(args.config_file) loader = DataLoader(config) circle = ganCIRCLE(config, args.key, args.secret) circle.g_lr_hr.save('sr_generator_model') epochs = config["epochs"] iterations = tf.constant((loader.epoch_size // (loader.batch_size)) - 2) tf.print('Each epoch will have %s iterations' % (iterations.numpy())) pb = Progbar(tf.cast(iterations * epochs, tf.float32), width=10, verbose=1) for epoch in tf.range(epochs): g_total_loss_list = [] d_total_loss_list = []
def train(data_dir, save_dir, best_dir, config): """Prepare the data and begin training.""" # Create variables batch_size = config.batch_size timesteps = config.timesteps num_epochs = config.epochs # Load the text and vocabulary data_loader = DataLoader( data_dir, mode='train', tokenize_func=lmmrl_tokenizer, encode_func=lmmrl_encoder, word_markers=config.include_word_markers, max_word_length=config.max_word_length ) # Prepare batches for training and validation train_batch_loader = BatchLoader(data_loader, batch_size=batch_size, timesteps=timesteps, mode='train') val_batch_loader = BatchLoader(data_loader, batch_size=batch_size, timesteps=timesteps, mode='val') # update vocabulary sizes config.word_vocab_size = len(data_loader.vocabs['words']) config.char_vocab_size = len(data_loader.vocabs['chars']) # Run on GPU by default cfg_proto = tf.ConfigProto(intra_op_parallelism_threads=0, inter_op_parallelism_threads=0) cfg_proto.gpu_options.allow_growth = True ########################################################################## # Load word frequency information ########################################################################## with open(os.path.join(data_dir, 'word_freq.txt'), encoding='utf-8') as f: freq = f.read().split() config['freq'] = freq ########################################################################## # Create model config.save_dir = save_dir model = Model(config) with tf.Session(config=cfg_proto, graph=model.graph) as sess: # Restore model/Initialize weights initializer = tf.random_uniform_initializer(-0.05, 0.05) with tf.variable_scope("model", reuse=None, initializer=initializer): steps_done = restore_model(sess, model, save_dir) logger.info("Loaded %d completed steps", steps_done) # Find starting epoch start_epoch = model.epoch_cntr.eval() # Start epoch-based training lr = config.initial_learning_rate # Finalize graph to prevent memory leakage sess.graph.finalize() last_val_ppl = 10000 for epoch in range(start_epoch, num_epochs): logger.info("Epoch %d / %d", epoch+1, num_epochs) # train run_epoch(sess, model, train_batch_loader, 'train', save_dir=save_dir, lr=lr) # fine-tune after every epoch sess.run(model.update_unknown) model.fine_tune(sess) # validate val_ppl = run_epoch(sess, model, val_batch_loader, 'val', best_dir=best_dir) # update learning rate conditionally if val_ppl >= last_val_ppl: lr *= config.lr_decay logger.info("Decaying learning rate to %.4f", lr) last_val_ppl = val_ppl # increment epoch sess.run([model.incr_epoch])