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 print('First suite of neural network tests passed.') # # # # # # # # # # # # # # # # # # # # # # # # # Test predicate attributes # # # # # # # # # # # # # # # # # # # # # # # # kb.store( 'lives_in(tom, seattle)' ) ## TODO seems weird to have to add this fact then stick 'formerly' on it # on the next line. kb.edge_attr('tom', 'lives_in', 'seattle', {'formerly': 1.0}) kb.seed(555) kb.build_kg_model(cuda=False, embedding_size=50, node_attributes=['owns_a_raincoat', 'doesnt_own_raincoat'], pred_attributes=['formerly'], attr_loss_to_graph_loss=0.9, pred_loss_to_graph_loss=5.0) kb.train_kg_model(steps=8001, batch_size=2, neg_to_pos=4) # # # # # # # # # # # # # # # # # # # # # # # # # People from Seattle should be more likely to # own an umbrella (attribute prediction test) # # # # # # # # # # # # # # # # # # # # # # # # x = kb._kg_model.run_embedding(kb.get_embedding('other1'), 'owns_a_raincoat') y = kb._kg_model.run_embedding(kb.get_embedding('other1'),
import context from zincbase import KB kb = KB() kb.store('a(b,c)') kb.attr('b', {'is_letter': 1.0}) assert kb.node('b') == {'is_letter': 1.0} kb.edge_attr('b', 'a', 'c', {'both_alpha': 1.0}) assert kb.edge('b', 'a', 'c') == {'both_alpha': 1.0} assert kb.to_triples() == [('b', 'a', 'c')] triples = kb.to_triples(data=True) assert triples == [('b', 'a', 'c', {'is_letter': 1.0}, {'both_alpha': 1.0}, {}, False)] kb.attr('c', {'is_letter': 0.9}) triples = kb.to_triples(data=True) assert triples == [('b', 'a', 'c', {'is_letter': 1.0}, {'both_alpha': 1.0}, {'is_letter': 0.9}, False)] neg_rule_idx = kb.store('~a(b,c)') triples = kb.to_triples(data=True) assert triples == [('b', 'a', 'c', {'is_letter': 1.0}, {'both_alpha': 1.0}, {'is_letter': 0.9}, True)] kb.delete_rule(neg_rule_idx) triples = kb.to_triples(data=True) assert triples == [('b', 'a', 'c', {'is_letter': 1.0}, {'both_alpha': 1.0}, {'is_letter': 0.9}, False)] kb.edge_attr('b', 'a', 'c', {'truthiness':-1}) triples = kb.to_triples(data=True) assert triples == [('b', 'a', 'c', {'is_letter': 1.0}, {'both_alpha': 1.0, 'truthiness': -1}, {'is_letter': 0.9}, True)] kb.delete_edge_attr('b', 'a', 'c', ['truthiness']) triples = kb.to_triples(data=True) assert triples == [('b', 'a', 'c', {'is_letter': 1.0}, {'both_alpha': 1.0}, {'is_letter': 0.9}, False)] print('All graph tests passed.')