Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_fit_predict_TransE_early_stopping_with_filter():
X = load_wn18()
model = TransE(batches_count=1, seed=555, epochs=7, k=50, loss='pairwise', loss_params={'margin': 5},
verbose=True, optimizer='adagrad', optimizer_params={'lr': 0.1})
X_filter = np.concatenate((X['train'], X['valid'], X['test']))
model.fit(X['train'], True, {'x_valid': X['valid'][::100],
'criteria': 'mrr',
'x_filter': X_filter,
'stop_interval': 2,
'burn_in': 1,
'check_interval': 2})
y = model.predict(X['test'][:1])
print(y)
def test_evaluate_performance_default_protocol_with_filter():
wn18 = load_wn18()
X_filter = np.concatenate((wn18['train'], wn18['valid'], wn18['test']))
model = TransE(batches_count=10, seed=0, epochs=1,
k=50, eta=10, verbose=True,
embedding_model_params={'normalize_ent_emb': False, 'norm': 1},
loss='self_adversarial', loss_params={'margin': 1, 'alpha': 0.5},
optimizer='adam',
optimizer_params={'lr': 0.0005})
model.fit(wn18['train'])
from ampligraph.evaluation import evaluate_performance
ranks_sep = []
from ampligraph.evaluation import hits_at_n_score, mrr_score, mr_score
ranks = evaluate_performance(wn18['test'][::100], model, X_filter, verbose=True, corrupt_side='o',
def test_large_graph_mode():
set_entity_threshold(10)
X = load_wn18()
model = ComplEx(batches_count=100, seed=555, epochs=1, k=50, loss='multiclass_nll', loss_params={'margin': 5},
verbose=True, optimizer='sgd', optimizer_params={'lr': 0.001})
model.fit(X['train'])
X_filter = np.concatenate((X['train'], X['valid'], X['test']), axis=0)
ranks_all = evaluate_performance(X['test'][::1000], model, X_filter, verbose=True, corrupt_side='s+o',
use_default_protocol=True)
y = model.predict(X['test'][:1])
print(y)
reset_entity_threshold()
def test_fit_predict_wn18_TransE():
X = load_wn18()
model = TransE(batches_count=1, seed=555, epochs=5, k=100, loss='pairwise',
loss_params={'margin': 5},
verbose=True, optimizer='adagrad',
optimizer_params={'lr': 0.1})
model.fit(X['train'])
y = model.predict(X['test'][:1])
print(y)
def test_evaluate_performance_so_side_corruptions_with_filter():
X = load_wn18()
model = ComplEx(batches_count=10, seed=0, epochs=5, k=200, eta=10, loss='nll',
regularizer=None, optimizer='adam', optimizer_params={'lr': 0.01}, verbose=True)
model.fit(X['train'])
ranks = evaluate_performance(X['test'][::20], model=model, verbose=True,
use_default_protocol=False, corrupt_side='s+o')
mrr = mrr_score(ranks)
hits_10 = hits_at_n_score(ranks, n=10)
print("ranks: %s" % ranks)
print("MRR: %f" % mrr)
print("Hits@10: %f" % hits_10)
assert(mrr is not np.Inf)
def test_evaluate_performance_nll_complex():
X = load_wn18()
model = ComplEx(batches_count=10, seed=0, epochs=10, k=150, optimizer_params={'lr': 0.1}, eta=10, loss='nll',
optimizer='adagrad', verbose=True)
model.fit(np.concatenate((X['train'], X['valid'])))
filter_triples = np.concatenate((X['train'], X['valid'], X['test']))
ranks = evaluate_performance(X['test'][:200], model=model, filter_triples=filter_triples, verbose=True)
mrr = mrr_score(ranks)
hits_10 = hits_at_n_score(ranks, n=10)
print("ranks: %s" % ranks)
print("MRR: %f" % mrr)
print("Hits@10: %f" % hits_10)
def test_evaluate_RandomBaseline():
model = RandomBaseline(seed=0)
X = load_wn18()
model.fit(X["train"])
ranks = evaluate_performance(X["test"],
model=model,
use_default_protocol=False,
corrupt_side='s+o',
verbose=False)
hits10 = hits_at_n_score(ranks, n=10)
hits1 = hits_at_n_score(ranks, n=1)
assert hits10 < 0.01 and hits1 == 0.0
def test_evaluate_performance_TransE():
X = load_wn18()
model = TransE(batches_count=10, seed=0, epochs=100, k=100, eta=5, optimizer_params={'lr': 0.1},
loss='pairwise', loss_params={'margin': 5}, optimizer='adagrad')
model.fit(np.concatenate((X['train'], X['valid'])))
filter_triples = np.concatenate((X['train'], X['valid'], X['test']))
ranks = evaluate_performance(X['test'][:200], model=model, filter_triples=filter_triples, verbose=True)
# ranks = evaluate_performance(X['test'][:200], model=model)
mrr = mrr_score(ranks)
hits_10 = hits_at_n_score(ranks, n=10)
print("ranks: %s" % ranks)
print("MRR: %f" % mrr)
print("Hits@10: %f" % hits_10)
def test_fit_predict_wn18_ComplEx():
X = load_wn18()
model = ComplEx(batches_count=1, seed=555, epochs=5, k=100,
loss='pairwise', loss_params={'margin': 1}, regularizer='LP',
regularizer_params={'lambda': 0.1, 'p': 2},
optimizer='adagrad', optimizer_params={'lr': 0.1})
model.fit(X['train'])
y = model.predict(X['test'][:1])
print(y)
def test_load_wn18():
wn18 = load_wn18()
assert len(wn18['train']) == 141442
assert len(wn18['valid']) == 5000
assert len(wn18['test']) == 5000
ent_train = np.union1d(np.unique(wn18["train"][:, 0]), np.unique(wn18["train"][:, 2]))
ent_valid = np.union1d(np.unique(wn18["valid"][:, 0]), np.unique(wn18["valid"][:, 2]))
ent_test = np.union1d(np.unique(wn18["test"][:, 0]), np.unique(wn18["test"][:, 2]))
distinct_ent = np.union1d(np.union1d(ent_train, ent_valid), ent_test)
distinct_rel = np.union1d(np.union1d(np.unique(wn18["train"][:, 1]), np.unique(wn18["train"][:, 1])),
np.unique(wn18["train"][:, 1]))
assert len(distinct_ent) == 40943
assert len(distinct_rel) == 18