Пример #1
0
from zincbase import KB

kb = KB()
kb.seed(555)

kb.from_csv('./assets/countries_s1_train.csv', delimiter='\t')

# specifying both a ~ and a truthiness < 0 is probably unnecessary.
kb.store('~locatedin(canada, africa)', {'truthiness': -1.})

kb.build_kg_model(cuda=False,
                  embedding_size=100,
                  pred_attributes=['truthiness'])

kb.train_kg_model(steps=1000, batch_size=4, neg_ratio=0.01)

canada_in_africa = kb.estimate_triple_prob('canada', 'locatedin', 'africa')
canada_in_asia = kb.estimate_triple_prob('canada', 'locatedin', 'asia')
canada_in_america = kb.estimate_triple_prob('canada', 'locatedin',
                                            'northern_america')
assert 2 * canada_in_africa < canada_in_asia
assert canada_in_america > 2 * canada_in_asia

clafrica_truthiness = kb.estimate_triple_prob_with_attrs(
    'canada', 'locatedin', 'africa', 'truthiness')
clasia_truthiness = kb.estimate_triple_prob_with_attrs('canada', 'locatedin',
                                                       'asia', 'truthiness')

assert clafrica_truthiness < clasia_truthiness

print('All truthiness tests passed.')
Пример #2
0
# prediction test)
# # # # # # # # # # # # # # # # # # # # # # # #
bay_prob = kb.estimate_triple_prob('tom', 'lives_in', 'bay_area')
sea_prob = kb.estimate_triple_prob('tom', 'lives_in', 'seattle')
assert bay_prob > sea_prob

bay_prob = kb.estimate_triple_prob('shamala', 'lives_in', 'bay_area')
sea_prob = kb.estimate_triple_prob('shamala', 'lives_in', 'seattle')
assert bay_prob > 2 * sea_prob

sea_prob = kb.estimate_triple_prob('other1', 'lives_in', 'seattle')
bay_prob = kb.estimate_triple_prob('other1', 'lives_in', 'bay_area')
assert sea_prob > 2 * bay_prob
sea_prob = kb.estimate_triple_prob('other2', 'lives_in', 'seattle')
bay_prob = kb.estimate_triple_prob('other2', 'lives_in', 'bay_area')
assert sea_prob > 2 * bay_prob

x = kb.estimate_triple_prob_with_attrs('tom', 'lives_in', 'bay_area',
                                       'formerly')
y = kb.estimate_triple_prob_with_attrs('john', 'lives_in', 'bay_area',
                                       'formerly')
z = kb.estimate_triple_prob_with_attrs('mary', 'lives_in', 'bay_area',
                                       'formerly')
assert x > y
assert x > z

x = kb.estimate_triple_prob_with_attrs('tom', 'knows', 'john', 'formerly')
y = kb.estimate_triple_prob('tom', 'knows', 'john')
assert y > x

print('Neural network tests passed.')