def main(): """Run main training and evaluation script.""" arguments = docopt.docopt(__doc__, version='Kmeans 1.0') out = arguments['--out'] if arguments['classify']: X = load_data(arguments['<test_path>']) model = joblib.load(arguments['--model']) pred_test = predict(model, X) with open(out) as f: writer = csv.writer(f) for i, c in enumerate(pred_test): writer.write([i, c]) print(' * [INFO] Wrote classification results to', out) return (X, Y), (X_test, Y_test) = load_data(arguments['<train_path>'], arguments['<test_path>']) data_train = {'X': X, 'Y': Y} data_test = {'X': X_test, 'Y': Y_test} assert X.shape[1] == X_test.shape[1], 'Dim of data incompatible' if arguments['--grid']: best_run, best_accuracy = (), 0 for kernel in grid: for C in grid[kernel]: results = run(data_train, data_test, kernel, C, confusion=arguments['--confusion']) if results['validation'] > best_accuracy: best_model = results['model'] best_accuracy = results['validation'] best_run = { 'kernel': kernel, 'C': C, 'train_accuracy': results['train'] } print(' * [INFO] Best run:', best_accuracy) print(' * [INFO] More information:', best_run) joblib.dump(best_model, out) print(' * [INFO] Saved model to', out) else: results = run(data_train, data_test, C=float(arguments['--C']), kernel=arguments['--kernel'], confusion=arguments['--confusion']) joblib.dump(results['model'], out) print(' * [INFO] Saved model to', out)
def main(args): # data, word2ids = util.load_train_data(train_file, word2vec_file) data, word2ids, embed_arr = util.load_data() feature_dict = util.build_feature_dict(args, data) model = init_model(words_dict=word2ids, feature_dict=feature_dict, args=args) model.quick_load_embed(embed_arr) data_loader = make_dataset(data, model) start_epoch = 0 # TRAIN/VALID LOOP logger.info('-' * 100) logger.info('Train now! Output loss every %d batch...' % args.display_iter) stats = {'timer': util.Timer(), 'epoch': 0, 'best_valid': 0} for epoch in range(start_epoch, args.num_epochs): stats['epoch'] = epoch train(args, data_loader, model, stats) result = evaluate(model, data_loader, global_stats=stats) if result[args.valid_metric] > stats['best_valid']: logger.info('Best valid: %s = %.2f (epoch %d, %d updates)' % (args.valid_metric, result[args.valid_metric], stats['epoch'], model.updates)) model.save(args.model_file) stats['best_valid'] = result[args.valid_metric]
TIME = 1590585091 # plain TIME = 1590586392 # prior EPISODE = 90000 # TEST_MODEL = f"../results/model/{TIME}/best_model.zip" TEST_MODEL = f"../results/model/{TIME}/rl_model_{EPISODE}_steps.zip" CFG_FILE = f"./.results/{TIME}.json" if __name__ == "__main__": cfg = parse() cfg_log = ConfigLog(cfg) # test & train if cfg.test: cfg_log.load(CFG_FILE) # load data df_train, df_test, df_rate = load_data(cfg) rl_returns = [] naked_returns = [] covered_returns = [] delta_returns = [] env = DummyVecEnv([lambda: HedgeEnv(df_test, df_rate, cfg)]) T = env.get_attr('T')[0] model = DDPG(MlpPolicy, env, verbose=1) model.load(TEST_MODEL) delta = DeltaHedge() for i in range(cfg.test_times): # rl env.set_attr("b_rl", True) obs = env.reset() # every time, create a new transaction naked_returns.append(naked(env)) covered_returns.append(covered(env))
def fetch_data(filename): return load_data(filename)
def main(args): # -------------------------------------------------------------------------- # DATA logger.info('-' * 100) logger.info('Load data files') train_exs = util.load_data(args, args.train_file) logger.info('Num train examples = %d' % len(train_exs)) dev_exs = util.load_data(args, args.dev_file) logger.info('Num dev examples = %d' % len(dev_exs)) #test_exs = util.load_data(args, args.test_file) #logger.info('Num dev examples = %d' % len(test_exs)) # -------------------------------------------------------------------------- # MODEL logger.info('-' * 100) start_epoch = 0 if args.checkpoint and os.path.isfile(args.model_file + '.checkpoint'): # Just resume training, no modifications. logger.info('Found a checkpoint...') checkpoint_file = args.model_file + '.checkpoint' model, start_epoch = DocReader.load_checkpoint(checkpoint_file, args) else: # Training starts fresh. But the model state is either pretrained or # newly (randomly) initialized. if args.pretrained: logger.info('Using pretrained model...') model = DocReader.load(args.pretrained, args) if args.expand_dictionary: logger.info('Expanding dictionary for new data...') # Add words in training + dev examples words = util.load_words(args, train_exs + dev_exs) added_words = model.expand_dictionary(words) # Load pretrained embeddings for added words if args.words_embedding_file: model.load_embeddings(added_words, args.words_embedding_file) logger.info('Expanding char dictionary for new data...') # Add words in training + dev examples else: logger.info('Training model from scratch...') model = util.init_from_scratch(args, train_exs, dev_exs) # Set up partial tuning of embeddings if args.tune_partial > 0: logger.info('-' * 100) logger.info('Counting %d most frequent question words' % args.tune_partial) top_words = util.top_words(args, train_exs, model.word_dict) for word in top_words[:5]: logger.info(word) logger.info('...') for word in top_words[-6:-1]: logger.info(word) model.tune_embeddings([w[0] for w in top_words]) # Set up optimizer model.init_optimizer() # Use the GPU? if args.cuda: model.cuda() # Use multiple GPUs? if args.parallel: model.parallelize() # -------------------------------------------------------------------------- # DATA ITERATORS # Three datasets: train and dev. If we sort by length it's faster. logger.info('-' * 100) logger.info('Make data loaders') train_dataset = data.ReaderDataset(train_exs, model) if args.sort_by_len: train_sampler = data.SortedBatchSampler(train_dataset.lengths(), args.batch_size, shuffle=True) else: train_sampler = torch.utils.data.sampler.RandomSampler(train_dataset) train_loader = torch.utils.data.DataLoader( train_dataset, batch_size=args.batch_size, sampler=train_sampler, num_workers=args.data_workers, collate_fn=vector.batchify, # pin_memory= args.cuda, ) dev_dataset = data.ReaderDataset(dev_exs, model) if args.sort_by_len: dev_sampler = data.SortedBatchSampler(dev_dataset.lengths(), args.dev_batch_size, shuffle=False) else: dev_sampler = torch.utils.data.sampler.SequentialSampler(dev_dataset) dev_loader = torch.utils.data.DataLoader( dev_dataset, batch_size=args.dev_batch_size, sampler=dev_sampler, num_workers=args.data_workers, collate_fn=vector.batchify, # pin_memory=args.cuda, ) # ------------------------------------------------------------------------- # PRINT CONFIG logger.info('-' * 100) logger.info('CONFIG:\n%s' % json.dumps(vars(args), indent=4, sort_keys=True)) # -------------------------------------------------------------------------- # -------------------------------------------------------------------------- # TRAIN/VALID LOOP logger.info('-' * 100) logger.info('Starting training...') stats = { 'timer': tool.Timer(), 'epoch': 0, 'best_f1_score': 0, 'best_em_score': 0 } train_saver = tool.DataSaver(args.model_name, "train") dev_saver = tool.DataSaver(args.model_name, "dev") for epoch in range(start_epoch, args.num_epochs): stats['epoch'] = epoch # Train train(args, train_loader, model, stats, train_saver) # Validate unofficial (train) validate_official(args, train_loader, model, stats, train_saver) # Validate unofficial (dev) result = validate_official(args, dev_loader, model, stats, dev_saver) # Save best valid if args.valid_metric is None or args.valid_metric == 'no': model.save(args.model_file) # {'exact_match': exact_match.avg,"f1_score":f1_score_avg.avg} if result['exact_match'] > stats['best_em_score']: stats['best_em_score'] = result['exact_match'] if result['f1_score'] > stats['best_f1_score']: stats['best_f1_score'] = result['f1_score'] logger.info( 'Best f1_score = %.2f Best em_score: = %.2f (epoch %d, %d updates)' % (stats['best_f1_score'], stats['best_em_score'], stats['epoch'], model.updates)) model.save(args.model_file) # save trained data train_saver.save(args.model_dir) dev_saver.save(args.model_dir) return model
differences = [ np.sum(np.abs(x - x_pred)) for x, x_pred in zip(X_test, reconstructed_data) ] correct = float(len([diff for diff in differences if diff < threshold])) accuracy = correct / X_test.shape[0] print(' * [INFO] Reconstruction accuracy: %f (avg: %f)' % (accuracy, np.mean(differences))) if __name__ == '__main__': arguments = docopt.docopt(__doc__) data_path = arguments['<data>'] checkpoint_id = arguments['<checkpoint_id>'] out = arguments['<out>'] latent = int(arguments['--latent']) is_training = arguments['train'] (X, Y) = load_data(data_path) checkpoint_path = CKPT_FORMAT.format(id=checkpoint_id or ID_) if is_training: model = train(X, latent) os.makedirs(os.path.dirname(checkpoint_path)) joblib.dump(model, checkpoint_path) print(' * [INFO] Saved model to', checkpoint_path) else: print(' * [INFO] Using model', checkpoint_path) model = joblib.load(checkpoint_path) encoded_train = model.transform(X) scipy.io.savemat(out, {'X': encoded_train, 'Y': Y}) print(' * [INFO] Saved featurized data to', out)
import tflearn from tflearn.layers.core import input_data, dropout, fully_connected from tflearn.layers.conv import conv_3d, max_pool_3d from tflearn.layers.estimator import regression from math import ceil from utils.util import load_data arguments = docopt.docopt(__doc__, version='3d cnn 1.0') train_path = arguments['<train>'] test_path = arguments['<test>'] epochs = int(arguments['--epochs']) # Data loading and preprocessing (X, Y), (X_test, Y_test) = load_data(train_path, test_path, conv=True) def one_hot(v): return np.eye(2)[v.astype(int)].reshape((-1, 2)) Y = np.ravel(Y.T) Y_test = np.ravel(Y_test.T) Y_oh = one_hot(Y) Y_test_oh = one_hot(Y_test) # Convolutional network building with tf.device('/gpu:%s' % arguments['--gpu-id']): network = input_data(shape=[None, 30, 30, 30, 1])
if __name__ == '__main__': arguments = docopt.docopt(__doc__) data_path = arguments['<data>'] checkpoint = arguments['<checkpoint_id>'] out = arguments['<out>'] sparsity_weight = float(arguments['--sp-weight']) sparsity_level = float(arguments['--sp-level']) gpu_id = arguments['--gpu-id'] sparsity = arguments['--sparsity'] latent = int(arguments['--latent']) epochs = int(arguments['--epochs']) conv = bool(arguments['--conv']) is_training = arguments['train'] (X, Y) = load_data(train=data_path, conv=conv) models = train(X, gpu_id, sparsity, latent, sparsity_weight=sparsity_weight, sparsity_level=sparsity_level, epochs=epochs, conv=conv, checkpoint=checkpoint, is_training=is_training) if not is_training: encoded_train = np.array(models['encoding_model'].predict(X)) scipy.io.savemat(out, {'X': encoded_train, 'Y': Y}) print('Saved to', out)
from inference.optimizer import load_weights from utils.util import (load_data, get_stability_classes, convert_to_array, plot_cutoff) from inference.optimizer import metric_object from inference.inference import KLdivergence from utils.preprocess import preprocess_data import time if __name__ == "__main__": filename = "data/Electrical Grid Stability.csv" egrid_data = load_data(filename) stable_class, unstable_class = get_stability_classes(egrid_data) stable_grid = convert_to_array(stable_class) _, stable_val, stable_test = preprocess_data(stable_grid) unstable_grid = convert_to_array(unstable_class) _, _, unstable_test = preprocess_data(unstable_grid) LOSS = metric_object original_dim = 13 model = load_weights(original_dim) model_output, _ = KLdivergence(model, LOSS, stable_val) stable_output, _ = KLdivergence(model, LOSS, stable_test) unstable_output, _ = KLdivergence(model, LOSS, unstable_test) plot_cutoff(model_output, stable_output, unstable_output)
print("Start running") data_dir = "datasets" for key in ["p1", "p2", "p3"]: for enum, _set in enumerate([ [(f"{data_dir}/{key}/train-random-erdos-5000-40-50", f"{data_dir}/{key}/test-random-erdos-500-40-50", f"{data_dir}/{key}/test-random-erdos-500-51-60")], ]): for index, (_train, _test1, _test2) in enumerate(_set): print(f"Start for dataset {_train}-{_test1}-{_test2}") _train_graphs, (_, _, _n_node_labels) = load_data( dataset=f"{file_path}/{data_path}/{_train}.txt", degree_as_node_label=False) _test_graphs, _ = load_data( dataset=f"{file_path}/{data_path}/{_test1}.txt", degree_as_node_label=False) _test_graphs2, _ = load_data( dataset=f"{file_path}/{data_path}/{_test2}.txt", degree_as_node_label=False) for _net_class in [ "acgnn", "gin", "acrgnn", # "acrgnn-single"
def main(): # Training settings # Note: Hyper-parameters need to be tuned in order to obtain results reported in the paper. parser = argparse.ArgumentParser( description= 'Implementation of COMPACT GRAPH ARCHITECTURE FOR SPEECH EMOTION RECOGNITION paper' ) parser.add_argument('--dataset', type=str, default="IEMOCAP", help='name of dataset (default: IEMOCAP)') parser.add_argument('--device', type=int, default=0, help='which gpu to use if any (default: 0)') parser.add_argument('--batch_size', type=int, default=128, help='input batch size for training (default: 32)') parser.add_argument( '--iters_per_epoch', type=int, default=50, help='number of iterations per each epoch (default: 90)') parser.add_argument('--epochs', type=int, default=1000, help='number of epochs to train (default: 1000)') parser.add_argument('--lr', type=float, default=0.0005, help='learning rate (default: 0.01)') parser.add_argument( '--seed', type=int, default=0, help='random seed for splitting the dataset into 10 (default: 0)') parser.add_argument( '--fold_idx', type=int, default=5, help='the index of fold in 10-fold validation. Should be less then 10.' ) parser.add_argument( '--num_layers', type=int, default=2, help='number of layers INCLUDING the input one (default: 5)') parser.add_argument('--hidden_dim', type=int, default=64, help='number of hidden units (default: 64)') parser.add_argument('--final_dropout', type=float, default=0.5, help='final layer dropout (default: 0.5)') parser.add_argument( '--graph_pooling_type', type=str, default="sum", choices=["sum", "average"], help= 'Pooling over nodes in a graph to get graph embeddig: sum or average') parser.add_argument('--graph_type', type=str, default="line", choices=["line", "cycle"], help='Graph construction options') parser.add_argument('--Normalize', type=bool, default=True, choices=[True, False], help='Normalizing data') parser.add_argument('--patience', type=int, default=10, help='Normalizing data') parser.add_argument('--beta1', default=0.9, type=float, help='beta1 for adam') parser.add_argument('--beta2', default=0.999, type=float, help='beta2 for adam') parser.add_argument('--weight-decay', '--wd', default=1e-4, type=float, metavar='W', help='weight decay (default: 1e-4)') args = parser.parse_args() #set up seeds and gpu device torch.manual_seed(0) np.random.seed(0) device = torch.device( "cuda:" + str(args.device)) if torch.cuda.is_available() else torch.device("cpu") if torch.cuda.is_available(): torch.cuda.manual_seed_all(0) ##load data graphs, num_classes = load_data(args.dataset, args.Normalize) ##10-fold cross validation. Conduct an experiment on the fold specified by args.fold_idx. train_graphs, test_graphs = separate_data(graphs, args.seed, args.fold_idx) A = nx.to_numpy_matrix(train_graphs[0][0].g) if (args.graph_type == 'cycle'): A[0, -1] = 1 A[-1, 0] = 1 A = torch.Tensor(A).to(device) model = Graph_CNN_ortega(args.num_layers, train_graphs[0][0].node_features.shape[1], args.hidden_dim, num_classes, args.final_dropout, args.graph_pooling_type, device, A).to(device) Num_Param = sum(p.numel() for p in model.parameters() if p.requires_grad) b = 0 for p in model.parameters(): if p.requires_grad: a = p.numel() b += a print("Number of Trainable Parameters= %d" % (Num_Param)) acc_train_sum = 0 acc_test_sum = 0 for i in range(args.fold_idx): train_data = train_graphs[i] test_data = test_graphs[i] # optimizer = RAdam(model.parameters(), lr=args.lr, betas=(args.beta1, args.beta2), # weight_decay=args.weight_decay) optimizer = optim.Adam(model.parameters(), lr=args.lr) # optimizer = AdamW(model.parameters(), lr=args.lr, betas=(args.beta1, args.beta2), # weight_decay=args.weight_decay) scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=50, gamma=0.5) early_stopping = EarlyStopping(patience=args.patience, verbose=True) for epoch in range(1, args.epochs + 1): scheduler.step() avg_loss = train(args, model, device, train_data, optimizer, epoch, A) if (epoch > 1): #### Validation check with torch.no_grad(): val_out = pass_data_iteratively(model, test_data) val_labels = torch.LongTensor( [graph.label for graph in test_data]).to(device) val_loss = criterion(val_out, val_labels) val_loss = np.average(val_loss.detach().cpu().numpy()) #### Check early stopping early_stopping(val_loss, model) if early_stopping.early_stop: print("Early stopping") break if ((epoch > 300) and (epoch % 20 == 0)) or (epoch % 10 == 0): acc_train, acc_test, _, _ = test(args, model, device, train_data, test_data, num_classes) model.load_state_dict(torch.load('checkpoint.pt')) acc_train, acc_test, output, label = test(args, model, device, train_data, test_data, num_classes) acc_train_sum += acc_train acc_test_sum += acc_test model = Graph_CNN_ortega(args.num_layers, train_graphs[0][0].node_features.shape[1], args.hidden_dim, num_classes, args.final_dropout, args.graph_pooling_type, device, A).to(device) print('Average train acc: %f, Average test acc: %f' % (acc_train_sum / args.fold_idx, acc_test_sum / args.fold_idx))