def train_pos(args): ###################################################### # # Data preprocessing # ###################################################### datasets = { args.datasetName: #Name of the dataset { 'columns': { 0: 'tokens', 1: 'POS', 2: 'chunk_BIO' }, #CoNLL format for the input data. Column 1 contains tokens, column 3 contains POS information 'label': 'POS', #Which column we like to predict 'evaluate': True, #Should we evaluate on this task? Set true always for single task setups 'commentSymbol': None } #Lines in the input data starting with this string will be skipped. Can be used to skip comments } # :: Path on your computer to the word embeddings. Embeddings by Komninos et al. will be downloaded automatically :: embeddingsPath = args.embeddings #'komninos_english_embeddings.gz' # :: Prepares the dataset to be used with the LSTM-network. Creates and stores cPickle files in the pkl/ folder :: pickleFile = perpareDataset(embeddingsPath, datasets) ###################################################### # # The training of the network starts here # ###################################################### #Load the embeddings and the dataset embeddings, mappings, data = loadDatasetPickle(pickleFile) # Some network hyperparameters params = { 'classifier': ['CRF'], 'LSTM-Size': [100], 'dropout': (0.25, 0.25) } model = BiLSTM(params) model.setMappings(mappings, embeddings) model.setDataset(datasets, data) model.modelSavePath = args.model_save + '/[ModelName]_[Epoch].h5' model.fit(epochs=25) fpath = args.model_save + '/' + args.datasetName + '_1.h5' save_dir, model_init = os.path.split(fpath) print(save_dir) print(model_init) remove_except_last_model(save_dir, model_init)
def run_experiment(dataset_id, dataset_dict, task, embeddings, mappings, data): # set network hyperparameters and mappings/datasets model = BiLSTM(network_params) model.setMappings(mappings, embeddings) model.setDataset(dataset_dict, data) # path to store the trained model and model results experiment_name = f'{dataset_id}_{task.lower()}' model.modelSavePath = models_dir / f'{experiment_name}.h5' model.storeResults(results_dir / f'{experiment_name}.csv') # build and train the model model.buildModel() model.fit( epochs=500) # do not limit training by epochs - use early stopping
def run_experiment(datasets_dict, lang, task, embeddings, mappings, data): # set network hyperparameters and mappings/datasets model = BiLSTM(network_params) model.setMappings(mappings, embeddings) model.setDataset(datasets_dict, data) # define the experiment name lang_prefix = f'{lang.lower()}_' if lang is not None else '' task_suffix = f'_{task.lower()}' if task is not None else '' experiment_name = lang_prefix + 'datasets' + task_suffix # path to store the trained model and model results model.modelSavePath = models_dir / f'{experiment_name}.h5' model.storeResults(results_dir / f'{experiment_name}.csv') # build and train the model model.buildModel() model.fit( epochs=500) # do not limit training by epochs - use early stopping
# TODO Replace customClassifier dengan main task + auxiliary task custom_classifier = {} custom_classifier[target_task] = [('LSTM', 100), 'CRF'] for task in aux_task: custom_classifier[task] = ['CRF'] params = { 'classifier': ['CRF'], 'LSTM-Size': [100], 'dropout': (0.25, 0.25), 'charEmbeddings': 'CNN', 'customClassifier': custom_classifier } model = BiLSTM(params) model.setMappings(mappings, embeddings) model.setDataset(datasets, data) model.storeResults("/".join( [args.root_dir_result, args.directory_name, "performance.out"])) # Path to store performance scores for dev / test model.predictionSavePath = "/".join([ args.root_dir_result, args.directory_name, "predictions", "[ModelName]_[Data].conll" ]) # Path to store predictions model.modelSavePath = "/".join( [args.root_dir_result, args.directory_name, "models/[ModelName].h5"]) # Path to store models model.fit(epochs=args.nb_epoch)
(datasetName, dataColumns), ] # :: Prepares the dataset to be used with the LSTM-network. Creates and stores cPickle files in the pkl/ folder :: pickleFile = perpareDataset(embeddingsPath, datasetFiles) ###################################################### # # The training of the network starts here # ###################################################### #Load the embeddings and the dataset embeddings, word2Idx, datasets = loadDatasetPickle(pickleFile) data = datasets[datasetName] print("Dataset:", datasetName) print(data['mappings'].keys()) print("Label key: ", labelKey) print("Train Sentences:", len(data['trainMatrix'])) print("Dev Sentences:", len(data['devMatrix'])) print("Test Sentences:", len(data['testMatrix'])) model = BiLSTM(params) model.setMappings(embeddings, data['mappings']) model.setTrainDataset(data, labelKey) model.verboseBuild = True model.modelSavePath = "models/%s/%s/[DevScore]_[TestScore]_[Epoch].h5" % ( datasetName, labelKey) #Enable this line to save the model to the disk model.evaluate(50)
# embeddingsPath = 'embeddings.vec' # :: Prepares the dataset to be used with the LSTM-network. Creates and stores cPickle files in the pkl/ folder :: pickleFile = perpareDataset(embeddingsPath, datasets, useExistent=False) ###################################################### # # The training of the network starts here # ###################################################### #Load the embeddings and the dataset embeddings, mappings, data = loadDatasetPickle(pickleFile) # Some network hyperparameters params = {'classifier': ['CRF'], 'LSTM-Size': [100, 100], 'dropout': (0.5, 0.5), 'charEmbeddings':'LSTM', 'optimizer': 'adam', 'featureNames': ['tokens', 'casing']} MODEL = BiLSTM(params) MODEL.setMappings(mappings, embeddings) MODEL.setDataset(datasets, data) MODEL.storeResults('results/Jurica_NER.csv') #Path to store performance scores for dev / test MODEL.modelSavePath = "models/[ModelName]_[DevScore]_[TestScore]_[Epoch].h5" MODEL.fit(epochs=100)
params = { 'classifier': ['CRF'], 'LSTM-Size': [100, 100], 'dropout': (0.25, 0.25), 'charEmbeddings': 'CNN', 'maxCharLength': 50, 'featureNames': ['tokens', 'casing'], 'earlyStopping': 100, 'wordEmbeddings_trainable': True } print('#######################' + args.mod + ' #######################') model = BiLSTM(params) conll_filePath = '/datastore/liu121/nosqldb2/emnlp_ukplab/conll_eval/conll_%s.txt' % ( args.mod + '_' + args.k_shot) model.setMappings(mappings, embeddings, conll_filePath) model.setDataset(datasets, data_train, data_dev, data_test) model.modelSavePath = "/datastore/liu121/nosqldb2/emnlp_ukplab/models/[ModelName]_bbn.h5" eval_result = model.fit(epochs=100) def report(eval_result, filePath): with open(filePath, 'w+') as f: for key in eval_result: info = eval_result[key] f.write('====================' + key + '====================') f.write(info['epoch'] + '\n') f.write(info["per_f1"] + "\n") f.write(info['per_pre'] + '\n') f.write(info['per_recall'] + '\n') f.write(info["micro_f1"] + '\n')