コード例 #1
0
# transr.set_parameters(parameters)
transr.ent_embeddings = transe.ent_embeddings
trainer = Trainer(model=model_r,
                  data_loader=train_dataloader,
                  train_times=1000,
                  alpha=0.1,
                  use_gpu=False)
trainer.run()
transr.save_checkpoint('./checkpoint/transr.ckpt')

epoch = trainer.epoch
loss = trainer.loss
line = plt.plot(epoch, loss, label=u'TransR')
plt.xlabel(u'epoch')
plt.ylabel(u'loss')
plt.show()
plt.savefig("./embedding/TransR/loss_epoch.png")

entity = trainer.model.model.ent_embeddings.weight.data
relationship = trainer.model.model.rel_embeddings.weight.data

entity_np = np.array(entity)
np.savetxt('./embedding/TransR/entity_result.txt', entity_np)

relationship_np = np.array(relationship)
np.savetxt('./embedding/TransR/relationship_result.txt', relationship_np)

# test the model
transr.load_checkpoint('./checkpoint/transr.ckpt')
tester = Tester(model=transr, data_loader=test_dataloader, use_gpu=False)
tester.run_link_prediction(type_constrain=False)
                  use_gpu=False)
trainer.run()
transr.save_checkpoint('./checkpoint/fault_dataset_transr.ckpt')

epoch = trainer.epoch
loss = trainer.loss
line = plt.plot(epoch, loss, label=u'TransR')
plt.xlabel(u'epoch')
plt.ylabel(u'loss')
plt.show()
plt.savefig("./embedding/TransR/loss_epoch.png")

entity = trainer.model.model.ent_embeddings.weight.data
relationship = trainer.model.model.rel_embeddings.weight.data
transfer_matrix = trainer.model.model.transfer_matrix.weight.data

entity_np = np.array(entity)
np.savetxt('./embedding/TransR/fault_dataset_entity_result.txt', entity_np)

relationship_np = np.array(relationship)
np.savetxt('./embedding/TransR/fault_dataset_relationship_result.txt',
           relationship_np)

transfer_matrix_np = np.array(transfer_matrix)
np.savetxt('./embedding/TransR/fault_dataset_transfer_matrix_result.txt',
           transfer_matrix_np)

# test the model
transr.load_checkpoint('./checkpoint/fault_dataset_transr.ckpt')
tester = Tester(model=transr, data_loader=test_dataloader, use_gpu=False)
tester.run_link_prediction(type_constrain=False)