def run_CNN_model(seg_train, seg_test, embedding_path, embed_dim, cnn_model, write_predictions=False, write_No_rel=False, initial_predictions=None, final_predictions=None): """ Call CNN models :param seg_test: test segments :param cnn_model: choose the model :param seg_train: train segments :type write_Predictions: write entities and predictions to file :param initial_predictions: folder to save the initial relation predictions :param final_predictions: folder to save the final relation predictions :param write_No_rel: Write the no-relation predictions back to files :return: None """ if cnn_model == 'segment': model = Model(data_object=seg_train, data_object_test=seg_test, segment=True, test=True, write_Predictions=write_predictions) embedding = Embeddings(embedding_path, model, embedding_dim=embed_dim) seg_cnn = Segment_CNN(model, embedding, initial_predictions=initial_predictions, final_predictions=final_predictions, write_No_rel=write_No_rel) elif cnn_model == 'sentence': model = Model(data_object=seg_train, data_object_test=seg_test, test=True, write_Predictions=write_predictions) embedding = Embeddings(embedding_path, model, embedding_dim=embed_dim) single_sent_cnn = Sentence_CNN(model, embedding, initial_predictions=initial_predictions, final_predictions=final_predictions, write_No_rel=write_No_rel) else: # multilabel sentence-CNN does not have the option to write the predictions back model = Model(data_object=seg_train, data_object_test=seg_test, test=True, multilabel=True) embedding = Embeddings(embedding_path, model, embedding_dim=embed_dim) multi_sent_cnn = Sentence_CNN(model, embedding)
preceding_segs='../data/test_segments/preceding_seg', concept1_segs='../data/test_segments/concept1_seg', middle_segs='../data/test_segments/middle_seg', concept2_segs='../data/test_segments/concept2_seg', succeeding_segs='../data/test_segments/succeeding_seg', track='../data/test_segments/track').data_object connection_train = Set_Connection(CSV=True, sentences='../data/segments/sentence_train', labels='../data/segments/labels_train', preceding_segs='../data/segments/preceding_seg', concept1_segs='../data/segments/concept1_seg', middle_segs='../data/segments/middle_seg', concept2_segs='../data/segments/concept2_seg', succeeding_segs='../data/segments/succeeding_seg', track='../data/segments/track').data_object model = Model(data_object=connection_train, data_object_test=connection_test, segment=True, test=True, write_Predictions=True) embedding=Embeddings(embedding_path, model, embedding_dim=int(sys.argv[2])) if sys.argv[4] is not None and sys.argv[5] is not None: seg_cnn = Segment_CNN(model, embedding, final_predictions=sys.argv[5], No_Rel=sys.argv[4]) else: seg_cnn = Segment_CNN(model, embedding) else: connection_test = Set_Connection(CSV=True, sentence_only=True, sentences='../data/test_segments/sentence_train', labels='../data/test_segments/labels_train', track='../data/test_segments/track' ).data_object connection_train = Set_Connection(CSV=True, sentence_only=True, sentences='../data/segments/sentence_train', labels='../data/segments/labels_train', track='../data/segments/track' ).data_object
import sys sys.path.append('../') from RelEx_NN.model import Model from RelEx_NN.embeddings import Embeddings from RelEx_NN.cnn import Sentence_CNN from segment import Set_Connection # Use pre-trained word embeddings embedding_path = "../../word_embeddings/glove.6B.200d.txt" # path to sentence and label CSV files data = Set_Connection(CSV=True, sentence_only=True, sentences='data/sentence_train', labels='data/labels_train').data_object model = Model(data, False) embedding = Embeddings(embedding_path, model) # train CNN model sent_cnn = Sentence_CNN(model, embedding, True)
import sys sys.path.append('../') from RelEx_NN.model import Model from RelEx_NN.embeddings import Embeddings from RelEx_NN.cnn import Sentence_CNN from segment import Set_Connection # Use pre-trained word embeddings embedding_path = "../../word_embeddings/glove.6B.200d.txt" # path to sentence and label CSV files data = Set_Connection(CSV=True, sentence_only=True, sentences='data/sentence_train', labels='data/labels_train').data_object model = Model(data, False, False, True) embedding = Embeddings(embedding_path, model) # train CNN model sent_cnn = Sentence_CNN(model, embedding, True, filters=300, drop_out=0.1, filter_conv=3, optimizer='adam')
import sys sys.path.append('../') from RelEx_NN.model import Model from RelEx_NN.embeddings import Embeddings from RelEx_NN.cnn import Segment_CNN from segment import Set_Connection # Use pre-trained word embeddings embedding_path = "../../word_embeddings/glove.6B.200d.txt" # path to sentence and label CSV files data = Set_Connection( CSV=True, sentences='data/sentence_train', labels='data/labels_train', preceding_segs='data/segments/preceding_seg', concept1_segs='data/segments/concept1_seg', middle_segs='data/segments/middle_seg', concept2_segs='data/segments/concept2_seg', succeeding_segs='data/segments/succeeding_seg').data_object # data = Set_Connection(CSV=True, sentence_only = False, sentences='../data/sentence_train', labels='../data/labels_train',preceding_segs='../data/segments/preceding_seg', concept1_segs='../data/segments/concept1_seg',middle_segs='../data/segments/middle_seg',concept2_segs='../data/segments/concept2_seg', succeeding_segs='../data/segments/succeeding_seg' ).data_object model = Model(data) embedding = Embeddings(embedding_path, model) # train CNN model seg_cnn = Segment_CNN(model, embedding, True)
embedding_path = "../word_embeddings/glove.6B.300d.txt" else: if int(sys.argv[2]) == 200: embedding_path = "../word_embeddings/patent_w2v.txt" if sys.argv[3] == 'segment': data = Set_Connection(CSV=True, sentences='../data/segments/sentence_train', labels='../data/segments/labels_train', preceding_segs='../data/segments/preceding_seg', concept1_segs='../data/segments/concept1_seg', middle_segs='../data/segments/middle_seg', concept2_segs='../data/segments/concept2_seg', succeeding_segs='../data/segments/succeeding_seg', track='../data/segments/track').data_object model = Model(data, segment=True) # model = Model(data, segment=True, write_Predictions=True) embedding = Embeddings(embedding_path, model, embedding_dim=int(sys.argv[2])) # if sys.argv[4] and sys.argv[5]: # seg_cnn = Segment_CNN(model, embedding, True, final_predictions=sys.argv[5], No_Rel=sys.argv[4]) # else: seg_cnn = Segment_CNN(model, embedding, True) else: data = Set_Connection(CSV=True, sentence_only=True, sentences='../data/segments/sentence_train', labels='../data/segments/labels_train', track='../data/segments/track').data_object