def trainAll(params, trainOpt, trainInput, trainTarget, trainInputWeights, validInput, validTarget, validInputWeights, initWeights=None): model = nn.load(params['modelFilename']) if initWeights is not None: model.loadWeights(initWeights) trainer = nn.Trainer(name=params['name'], model=model, trainOpt=trainOpt, outputFolder=params['outputFolder']) # Combine train & valid set allInput, allTarget, allInputWeights = combineInputs( trainInput, trainTarget, trainInputWeights, validInput, validTarget, validInputWeights) trainer.train(trainInput=allInput, trainTarget=allTarget, trainInputWeights=allInputWeights) return model, trainer
def trainValid(params, trainOpt, trainInput, trainTarget, trainInputWeights, validInput, validTarget, validInputWeights, initWeights=None): model = nn.load(params['modelFilename']) if initWeights is not None: model.loadWeights(initWeights) trainer = nn.Trainer( name=params['name']+\ ('-v' if params['validDataFilename'] is not None else ''), model=model, trainOpt=trainOpt, outputFolder=params['outputFolder'] ) # Validation training trainer.train(trainInput=trainInput, trainTarget=trainTarget, trainInputWeights=trainInputWeights, validInput=validInput, validTarget=validTarget, validInputWeights=validInputWeights) return model, trainer
trainTarget = trainData[1] trainInput, trainTarget = vt.shuffleData(trainInput, trainTarget, np.random.RandomState(2)) with open(configFilename) as f: trainOpt = yaml.load(f) for i in range(0, 10): trainInput_, trainTarget_, testInput_, testTarget_ = \ vt.splitData(trainInput, trainTarget, 0.1, i) trainOpt['heldOutRatio'] = 0.1 trainOpt['xvalidNo'] = 0 trainOpt['needValid'] = True model = nn.load(modelFilename) trainer = nn.Trainer(name=name + ('-%d-v' % i), model=model, trainOpt=trainOpt, outputFolder=outputFolder) trainer.train(trainInput_, trainTarget_) # Train again with all data, without validation trainOpt['needValid'] = False trainOpt['numEpoch'] = trainer.stoppedEpoch + 1 trainer = nn.Trainer(name=name + ('-%d' % i), model=model, trainOpt=trainOpt, outputFolder=outputFolder) trainer.train(trainInput_, trainTarget_) testOutput = nn.test(model, testInput_) testRate, correct, total = nn.calcRate(model, testOutput, testTarget_) with open(os.path.join(trainer.outputFolder, 'result.txt'), 'w+') as f: f.write('Test rate: %f' % testRate)
def main() -> None: parser = argparse.ArgumentParser( description='Train and test using the Deepmind GNN framework.') parser.add_argument('--file', '-f', nargs='+', help='Individual graph files to process', default=[]) parser.add_argument('--dir', '--directory', '-d', nargs='+', help='Individual project directories to process', default=[]) parser.add_argument( '--project', '-p', nargs='+', help= 'Directories containing a list of directories (equivalent to --dir PROJECT/*)', default=[]) parser.add_argument('--report-count', type=int, help='Count of items to report. Default 10', default=10) parser.add_argument('--load-model', help='File to load model from') parser.add_argument('--load-mappings', help='File to load mappings from') parser.add_argument('--train-split-ratio', type=float, help='Train/test split ratio (default=0.8)', default=0.8) parser.add_argument('--step-size', type=float, help='Step size (default=1e-3)', default=1e-3) parser.add_argument( '--random-seed', type=int, help='Random seed to use. Default=100, negative number = random', default=100) parser.add_argument('--niter', type=int, help='Number of iterations to run the GNN for.', default=10) parser.add_argument( '--ignore-edge-type', nargs='+', type=int, help='Whether to completely ignore the given edge type', default=[]) parser.add_argument('--no-collapse-any-unk', help='Don\'t collapse the $any$ and UNK tokens.', default=False, action='store_true') parser.add_argument('--batch-size', type=int, help='File batch size for training and testing', default=16) parser.add_argument( '--iteration-ensemble', help='Whether to run the "iteration ensemble" experiment.', default=False, action='store_true') parser.add_argument('--top-k-accuracy', nargs='+', type=int, help='How many accuracies to report. Default 1.', default=[1, 2, 3, 4, 5]) parser.add_argument('--node-latent-size', type=int, help='Latent state vector size for nodes', default=32) parser.add_argument( '--node-hidden-size', type=int, help='Hidden state vector size for nodes (in 1-layer net)', default=64) parser.add_argument('--edge-latent-size', type=int, help='Latent state vector size for edges', default=32) parser.add_argument( '--edge-hidden-size', type=int, help='Hidden state vector size for edges (in 1-layer net)', default=64) parser.add_argument( '--train-without-unk', help='Whether to remove all UNK from the training data.', default=True, action='store_true') parser.add_argument( '--load-train-test', help='Use train-test data from a file. Calculate split otherwise.', default=False, action='store_true') parser.add_argument('--train-loss-after-ep', help='FOR BO USE ONLY.', default=None, type=int) ast_type_unk_group = parser.add_mutually_exclusive_group() ast_type_unk_group.add_argument( '--ast-nonunk-percent', type=float, help='The percentage of AST types to explicitly encode (i.e. not UNK)') ast_type_unk_group.add_argument( '--ast-nonunk-count', type=int, help='The number of AST types to explicitly encode (i.e. not UNK)') edge_type_unk_group = parser.add_mutually_exclusive_group() edge_type_unk_group.add_argument( '--edge-nonunk-percent', type=float, help='The percentage of edge types to explicitly encode (i.e. not UNK)' ) edge_type_unk_group.add_argument( '--edge-nonunk-count', type=int, help='The number of edge types to explicitly encode (i.e. not UNK)') label_unk_group = parser.add_mutually_exclusive_group() label_unk_group.add_argument( '--label-nonunk-percent', type=float, help='The percentage of labels to explicitly encode (i.e. not UNK)') label_unk_group.add_argument( '--label-nonunk-count', type=int, default=20, help='The number of labels to explicitly encode (i.e. not UNK)') args = parser.parse_args() utils.log('Args: {}'.format(args)) utils.set_random_seed(args.random_seed) if not args.load_train_test: project_names, project_graph_jsons = collect_graph_jsons( args.file, args.dir, args.project) train_idxs, test_idxs = get_train_test_index_split( len(project_names), args.train_split_ratio) def get_split(idxs): names = [project_names[i] for i in idxs] graph_jsons = [project_graph_jsons[i] for i in idxs] return names, graph_jsons train_names, train_graph_jsons = get_split(train_idxs) test_names, test_graph_jsons = get_split(test_idxs) index_maps = load_index_maps(args, utils.flatten(train_graph_jsons)) # save_index_maps(index_maps) describe_index_maps(index_maps) utils.save_data('train_test', train_names=train_names, train_graph_jsons=train_graph_jsons, test_names=test_names, test_graph_jsons=test_graph_jsons, index_maps=index_maps) else: [ train_names, train_graph_jsons, test_names, test_graph_jsons, index_maps ] = utils.load_train_test_data('train_test_full') train_graph_tuple, train_labels = graph_construction.graphs_json_to_graph_tuple_and_labels( utils.flatten(train_graph_jsons), index_maps, ignore_edge_types=args.ignore_edge_type, ) if args.train_without_unk: for labels in train_labels: for (k, v) in list(labels.items()): if v == 0: del labels[k] train_graphs = gn_utils.split_np(train_graph_tuple) test_graph_tuple, test_labels = graph_construction.graphs_json_to_graph_tuple_and_labels( utils.flatten(test_graph_jsons), index_maps, ignore_edge_types=args.ignore_edge_type, ) test_graphs = gn_utils.split_np(test_graph_tuple) describe_dataset('train', train_names, train_graphs, train_labels) describe_dataset('test', test_names, test_graphs, test_labels) print_top_k_accuracies(train_labels, train_labels, args.top_k_accuracy, 'Train') print_top_k_accuracies(train_labels, test_labels, args.top_k_accuracy, 'Test') label_name_map = utils.invert_bijective_dict(index_maps.label_index_map) report_label_distribution('train', train_labels, label_name_map, args.report_count) report_label_distribution('test', test_labels, label_name_map, args.report_count) report_params = nn.ReportParameters(args.report_count, label_name_map) trainer = nn.Trainer(train_graphs, train_labels, test_graphs, test_labels, index_maps.label_index_map, niter=args.niter, iteration_ensemble=args.iteration_ensemble, batch_size=args.batch_size, top_k_report=args.top_k_accuracy, node_latent_size=args.node_latent_size, node_hidden_size=args.node_hidden_size, edge_latent_size=args.edge_latent_size, edge_hidden_size=args.edge_hidden_size, train_loss_after_epno=args.train_loss_after_ep) trainer.train(stepsize=args.step_size, load_model=args.load_model, report_params=report_params)
trainInput = trainData[0] trainTarget = trainData[1] if params['validDataFilename'] is not None: validData = np.load(params['validDataFilename']) validInput = validData[0] validTarget = validData[1] else: validInput = None validTarget = None model = nn.load(params['modelFilename']) trainer = nn.Trainer( name=params['name']+\ ('-v' if params['validDataFilename'] is not None else ''), model=model, trainOpt=trainOpt, outputFolder=params['outputFolder'] ) trainer.train(trainInput, trainTarget, validInput, validTarget) if params['testDataFilename'] is not None: if params['imageqa']: imageqa_test.testAll( trainer.name, model, params['dataFolder'], params['outputFolder']) else: testData = np.load(params['testDataFilename']) testInput = testData[0] testTarget = testData[1] model.loadWeights(np.load(trainer.modelFilename))