Beispiel #1
0
def eval_and_log(evl):
    print('Macro Precision: {}'.format(evl.macro_precision))
    log_metric('Macro Precision', evl.macro_precision)

    print('Macro Recall: {}'.format(evl.macro_recall))
    log_metric('Macro Recall', evl.macro_recall)

    print('Macro F1: {}'.format(evl.macro_f1_score))
    log_metric('Macro F1', evl.macro_f1_score)

    print('Micro Precision: {}'.format(evl.micro_precision))
    log_metric('Micro Precision', evl.micro_precision)

    print('Micro Recall: {}'.format(evl.micro_recall))
    log_metric('Micro Recall', evl.micro_recall)

    print('Micro F1: {}'.format(evl.micro_f1_score))
    log_metric('Micro F1', evl.micro_f1_score)

    print('Pairwise Precision: {}'.format(evl.pairwise_precision))
    log_metric('Pairwise Precision', evl.pairwise_precision)

    print('Pairwise Recall: {}'.format(evl.pairwise_recall))

    print('Pairwise F1: {}'.format(evl.pairwise_f1_score))
    log_metric('Pairwise F1', evl.pairwise_f1_score)

    log_artifact(get_abspath(LOG_PATH))
Beispiel #2
0
def visualize_cluster(cfg):
    file_name = cfg.ex.file_name
    enc_name = cfg.ex.enc
    enc_model = LMS[enc_name]()

    np_n_layer = cfg.model.np_n_layer or enc_model.default_max_layer
    rp_n_layer = cfg.model.rp_n_layer or enc_model.default_max_layer

    print('--- Start: build model ---')
    model = build_model(enc_model=enc_model, file_name=file_name)
    print('--- End: build model ---')

    print('--- Start: read phrases ---')
    entities, relations = model.get_encoded_elems(np_n_layer, rp_n_layer)
    print('--- End: read phrases ---')

    print('--- Start: read clusters ---')
    ent2clusters, rel2clusters = model.read_clusters()
    print('--- End: read clusters ---')

    plt_dir = f'plt_img/{model.cluster_dumped_dir}'
    os.makedirs(get_abspath(plt_dir), exist_ok=True)

    plt_path = f'{plt_dir}/{model.cluster_file_name}'

    n_min_elems = cfg.vis.n_min_elems
    n_max_elems = cfg.vis.n_max_elems
    if n_min_elems:
        plt_path += f'_min{n_min_elems}'
    if n_max_elems:
        plt_path += f'_max{n_max_elems}'
    plt_path += f'.png'

    fig, axes = plt.subplots(1, 2, figsize=(20, 10))
    fig.suptitle('t-SNE of entities and relation clusters')

    print('--- Start: plot noun phrases ---')
    scatter_tsne(entities, ent2clusters, axes[0], n_max_elems, n_min_elems)
    print('--- End: plot noun phrases ---')

    print('--- Start: plot rel phrases ---')
    scatter_tsne(relations, rel2clusters, axes[1], n_max_elems, n_min_elems)
    print('--- End: plot rel phrases ---')

    plt.savefig(get_abspath(plt_path))
Beispiel #3
0
def embed_elements_path(file_prefix, pretrained_name):
    return get_abspath(f'{ELEM_FILE_PATH}/{file_prefix}_{pretrained_name}.pkl')
Beispiel #4
0
 def read_clusters(self, cluster_dumped_path):
     with open(hlp.get_abspath(cluster_dumped_path), 'rb') as f:
         return pickle.load(f)
Beispiel #5
0
 def dump_clusters(self, clusters):
     os.makedirs(hlp.get_abspath(self.cluster_dumped_dir), exist_ok=True)
     with open(hlp.get_abspath(self.cluster_dumped_path), 'wb') as f:
         pickle.dump(clusters, f, protocol=pickle.HIGHEST_PROTOCOL)
Beispiel #6
0
 def __init__(self, model, file_name):
     self.model = model
     self.file_name = file_name
     file_path = hlp.get_abspath(f'{DATA_PATH}/{file_name}')
     self.data = Triples.from_file(file_path)
     self.__init_elements()