Пример #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)
    def __init__(self, KL = None, KM = None):

        #student KL (knowledge level)
        if (KL == None):
            self._KL = DecayKnowledgeLevel()
        else:
            self._KL = KL

        #student KM (knowledge model)
        if (KM == None):
            self._KM = PointKnowledgeModel()
        else:
            self._KM = KM

        #Knowledge graph
        self._KG = KnowledgeGraph()

        #Knowledge level of DB
        self._knowledgeLevel = {}

        #Knowledge graph of DB
        #This variable is for every student
        self._knowledgeGraph = {}

        #Temporary DB
        self.tmpDB = DummyDB()

        #An estimator which is used for calculate estimated KL based on the performance
        self._estimator = KnowledgeEstimator()

        #Bind a KM to the KL as its observer
        self._KL.addObserver(self._KM)

        #Bind a KM to the KG as its observer
        self._KG.addObserver(self._KM)
Пример #3
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
Пример #4
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))
class KnowledgeManager(object):
    """
    This is the class that manages students' performance and update his/her
    estimated knowledge level
    """

    #Init
    def __init__(self, KL = None, KM = None):

        #student KL (knowledge level)
        if (KL == None):
            self._KL = DecayKnowledgeLevel()
        else:
            self._KL = KL

        #student KM (knowledge model)
        if (KM == None):
            self._KM = PointKnowledgeModel()
        else:
            self._KM = KM

        #Knowledge graph
        self._KG = KnowledgeGraph()

        #Knowledge level of DB
        self._knowledgeLevel = {}

        #Knowledge graph of DB
        #This variable is for every student
        self._knowledgeGraph = {}

        #Temporary DB
        self.tmpDB = DummyDB()

        #An estimator which is used for calculate estimated KL based on the performance
        self._estimator = KnowledgeEstimator()

        #Bind a KM to the KL as its observer
        self._KL.addObserver(self._KM)

        #Bind a KM to the KG as its observer
        self._KG.addObserver(self._KM)

    @classmethod
    def initWithKL(cls, KL = None):
        if (isinstance(KL, BaseKnowledgeLevel)):
            return cls(KL, None)
        else:
            raise TypeError("Input is not an acceptable type of KL(knowledge level) object!")

    @classmethod
    def initWithKM(cls, KM = None):
        if (isinstance(KM, BaseKnowledgeModel)):
            return cls(None, KM)
        else:
            raise TypeError("Input is not an acceptable type of KM(knowledge Model) object!")

    @classmethod
    def initWithKLAndKM(cls, KL = None, KM = None):
        if (isinstance(KL, BaseKnowledgeLevel)):
            if (isinstance(KM, BaseKnowledgeModel)):
                return cls(KL, KM)
            else:
                raise TypeError("Input is not an acceptable type of KM(knowledge Model) object!")
        else:
            raise TypeError("Input is not an acceptable type of KL(knowledge level) object!")

    #Initialize the parameters about KL for a certain student
    def initKnowledgeLevel(self, studentID = None):
        if (studentID == None):
            raise ValueError("Input student Id is not available!")
        
        #Create a knowledge level table for the student
        studentKL = self._knowledgeLevel.get(studentID)
        self._KL.setKnowledge(studentKL)

        #Initialize the estimation in KM
        self._KM.setKnowledge(studentKL)

    #Initialize the parameters about KG
    def initKnowledgeGraph(self):
        self._KG.setDependency(self._knowledgeGraph)
        
        self._KG.updateConcept() #Because KG involves constant values only, its content needs to be update once only
        
    #Pass the ALEKS score
    def setALEKS(self, aleks = None):
        if (aleks == None):
            raise ValueError("Input ALEKS score is not available!")

        self._KL.setALEKS(aleks)
    
    #Save the student's performance and update his/her estimated knowledge level
    def savePerformance(self, studentID = None, conceptID = None, hints = None, prompts = None, summary = None, lcc = None):
        if (studentID == None):
            raise ValueError("Input student Id is not available!")
        
        if (conceptID == None):
            raise ValueError("Input concept Id is not available!")
    
        if (hints == None or hints < 0):
            raise ValueError("Hints value is invalid! (0, 1, 2, ..., n)")

        if (prompts == None or prompts < 0):
            raise ValueError("Prompts value is invalid! (0, 1, 2, ..., n)")

        if (summary != True and summary != False):
            raise ValueError("Prompts value is invalid! (True or False)")

        if (lcc == None or lcc < 0):
            raise ValueError("LCC score value is invalid! (0~1)")

        #Caculate the observed estimation about knowledge level based on the student's performance
        obsEst = self._estimator(hints, prompts, summary, lcc)

        #Update the estimation
        self._KL.updateKnowledge(conceptID, obsEst)

        #Save updated estimation back to the knowledge level table
        #(The table need to be saved back to the DB later)
        self._knowledgeLevel[studentID] = self._KL.getKnowledge()

    def getKnowledgeLevel(self, conceptID = None):
        if (conceptID == None):
            raise ValueError("Input concept id is not available!")

        return self._KM.getKnowledgeLevel(conceptID)

    def loadKnowledgeDB(self):
        #TODO: Replace this temorary DB by real DB
        self._knowledgeLevel = self.tmpDB._knowledge

    def saveKnowledgeDB(self):
        #TODO: Replace this temorary DB by real DB
        self.tmpDB._knowledge = self._knowledgeLevel

    def loadGraphDB(self):
        #TODO: Replace this temorary DB by real DB
        self._knowledgeGraph = self.tmpDB._dependency

    def saveGraphDB(self):
        #TODO: Replace this temorary DB by real DB
        self.tmpDB._dependency = self._knowledgeGraph
Пример #6
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())
Пример #7
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.
    """