Ejemplo n.º 1
0
def blackboard_load():
    global local_config
    logger = logging.getLogger('application')
    logger.setLevel(level=logging.DEBUG)
    logger.error('Loading blackboard...')
    """Deliver all of the information we have about a blackboard given an id."""
    try:
        board_id = request.form.get('id')
        global collection_location

        condition = "id='{}'".format(board_id)
        rows = fetch_table_entries(collection_location, 'blackboards',
                                   condition)

        query = json.loads(rows[0][3])
        construction_graph = json.loads(rows[0][4])

        # Contact Neo4j to get the large graph of this backboard
        database = KnowledgeGraph(local_config['clientHost'])
        graph = database.getNodesByLabel(board_id)

        return jsonify({'graph': graph,\
            'query': query,\
            'constructionGraph': construction_graph})

    except Exception as ex:
        print(ex)
        raise InvalidUsage("Unspecified exception {0}".format(ex), 410)
    except:
        raise InvalidUsage('Failed to load blackboard.', 410)
Ejemplo n.º 2
0
    def answer(self):
        '''
        Answer the question.

        Returns the answer struct, something along the lines of:
        https://docs.google.com/document/d/1O6_sVSdSjgMmXacyI44JJfEVQLATagal9ydWLBgi-vE
        '''

        # get all subgraphs relevant to the question from the knowledge graph
        database = KnowledgeGraph()
        subgraphs = database.query(
            self)  # list of lists of nodes with 'id' and 'bound'
        G = database.getGraphByLabel(self.id)
        del database

        # compute scores with NAGA, export to json
        pr = ProtocopRank(G)
        score_struct, subgraphs = pr.report_scores_dict(
            subgraphs)  # returned subgraphs are sorted by rank

        out_struct = []
        for substruct, subgraph in zip(score_struct, subgraphs):
            graph = UniversalGraph(nodes=substruct['nodes'],
                                   edges=substruct['edges'])
            graph.merge_multiedges()
            graph.to_answer_walk(subgraph)

            out_struct += [
                {'nodes':graph.nodes,\
                'edges':graph.edges,\
                'score':substruct['score']},
                ]
        score_struct = out_struct

        for i in range(len(score_struct)):
            # score_struct[i]['edges'] = UniversalGraph.mergeMultiEdges(score_struct[i]['edges'])
            score_struct[i]['info'] = {
                'name': AnswerSet.constructName(score_struct[i])
            }

        max_results = 1000
        if len(score_struct) > max_results:
            return score_struct[:max_results]
        else:
            return score_struct
Ejemplo n.º 3
0
    mr = np.mean(all_ranks)
    mrr = np.mean(1.0 / all_ranks)
    print("hits@1={:4f}, hits@10={:.4f}, MR={:.4f}, MRR={:.4f}".format(hits1, hits10, mr, mrr))


# settings
DATA_DIR = '../data/FB15k'
EMBEDDING_DIM = 50
LEARNING_RATE = 0.01
TRAIN_BATCH_SIZE = 4096
TEST_BATCH_SIZE = 32
SCORE_METHOD = 'L1'  # 'L1' or 'L2'
NORMALIZE = True  # set True to normalize embedding
EPOCHS = 20

kg = KnowledgeGraph(DATA_DIR)
model = TransE(kg.num_entities, kg.num_relations, dimension=EMBEDDING_DIM, normalize=NORMALIZE,
               score_method=SCORE_METHOD)
train_ds = tf.data.Dataset.from_generator(kg.train_data_generator, output_types=(tf.int64), output_shapes=(5,))
train_ds = train_ds.batch(TRAIN_BATCH_SIZE)
valid_ds = tf.data.Dataset.from_generator(kg.valid_data_generator, output_types=(tf.int64)).batch(TEST_BATCH_SIZE)
test_ds = tf.data.Dataset.from_generator(kg.test_data_generator, output_types=(tf.int64)).batch(TEST_BATCH_SIZE)
#valid_ds = tf.data.Dataset.from_tensor_slices(kg.valid_triples.values).batch(TEST_BATCH_SIZE)
#test_ds = tf.data.Dataset.from_tensor_slices(kg.test_triples.values).batch(TEST_BATCH_SIZE)

for e in range(1, EPOCHS+1):
    total_loss = 0.0
    for batch_data in train_ds:
        loss = model.train_step(batch_data)
        total_loss += loss.numpy()
    print("Epoch {}: loss={:.6f}".format(e, total_loss))
Ejemplo n.º 4
0
                        default=1.0)
    parser.add_argument(
        '--negative_sampling',
        dest='negative_sampling',
        type=str,
        help='choose unit or bern to generate negative examples',
        default='bern')
    parser.add_argument(
        '--score_func',
        dest='score_func',
        type=str,
        default='l1',
        help='choose l1 or l2 to calculate distance of vectors')
    args = parser.parse_args()
    print(args)
    KG = KnowledgeGraph(data_dir=args.data_dir,
                        negative_sampling=args.negative_sampling)
    model = TransE(num_entity=KG.num_entity,
                   num_relation=KG.num_relation,
                   learning_rate=args.learning_rate,
                   batch_size=args.batch_size,
                   num_epochs=args.num_epochs,
                   margin=args.margin,
                   dimension=args.dimension,
                   score_func=args.score_func)
    model.compile()
    tp, tn = KG.get_training_data()
    train_model(model, tp, tn)
    model.save_embeddings()
    test_model(model, KG.get_test_data())
Ejemplo n.º 5
0
"""
    This module acts as interface to both local and remote data available to the application.
"""
import pickle
import re
import os
import traceback
from Settings import domainsToRelationsMapping, KG_DUMP_PATH, KB_DUMP_PATH
from KnowledgeGraph import KnowledgeGraph
import KnowledgeBaseServer as KBS

knowledgeGraph = KnowledgeGraph(domainsToRelationsMapping)


##################################################################################################
#
#                                       Knowledge Graph API
#
##################################################################################################
def dump_knowledge_graph():
    """
    Save the Knowledge Graph structure on a file.
    """
    pickle.dump(knowledgeGraph, open(KG_DUMP_PATH, "wb"))


def initialize_knowledge_graph():
    """
    Initialize the knowledge Graph using the dump, if available, or: if the KBS dump is available, 
    use it; otherwise, use the online KBS.
    """