首页 > 其他分享 >AmpliGraph1.4 使用记录

AmpliGraph1.4 使用记录

时间:2023-03-10 10:25:08浏览次数:56  
标签:10 set 记录 filter AmpliGraph1.4 使用 model loss ampligraph

参考官方文档:https://docs.ampligraph.org/en/1.4.0/index.html

由于笔者的电脑装最新的 Ampligrah 2.0 时总会报错,所以装的老版本 1.4。

安装:

conda、CUDA 和 CUDnn 的安装略。

参考 https://docs.ampligraph.org/en/1.4.0/install.html

conda create --name ampligraph python=3.7
conda activate ampligraph
conda install tensorflow-gpu==1.5
pip install ampligraph

运行 example

python 运行 https://docs.ampligraph.org/en/1.4.0/examples.html 中的 Train and evaluate an embedding model 的 example:

import numpy as np
from ampligraph.datasets import load_wn18
from ampligraph.latent_features import ComplEx
from ampligraph.evaluation import evaluate_performance, mrr_score, hits_at_n_score

def main():

    # load Wordnet18 dataset:
    X = load_wn18()

    # Initialize a ComplEx neural embedding model with pairwise loss function:
    # The model will be trained for 300 epochs.
    model = ComplEx(batches_count=10, seed=0, epochs=20, k=150, eta=10,
                    # Use adam optimizer with learning rate 1e-3
                    optimizer='adam', optimizer_params={'lr':1e-3},
                    # Use pairwise loss with margin 0.5
                    loss='pairwise', loss_params={'margin':0.5},
                    # Use L2 regularizer with regularizer weight 1e-5
                    regularizer='LP', regularizer_params={'p':2, 'lambda':1e-5}, 
                    # Enable stdout messages (set to false if you don't want to display)
                    verbose=True)

    # For evaluation, we can use a filter which would be used to filter out 
    # positives statements created by the corruption procedure.
    # Here we define the filter set by concatenating all the positives
    filter = np.concatenate((X['train'], X['valid'], X['test']))
    
    # Fit the model on training and validation set
    model.fit(X['train'], 
              early_stopping = True,
              early_stopping_params = \
                      {
                          'x_valid': X['valid'],       # validation set
                          'criteria':'hits10',         # Uses hits10 criteria for early stopping
                          'burn_in': 100,              # early stopping kicks in after 100 epochs
                          'check_interval':20,         # validates every 20th epoch
                          'stop_interval':5,           # stops if 5 successive validation checks are bad.
                          'x_filter': filter,          # Use filter for filtering out positives 
                          'corruption_entities':'all', # corrupt using all entities
                          'corrupt_side':'s+o'         # corrupt subject and object (but not at once)
                      }
              )

    

    # Run the evaluation procedure on the test set (with filtering). 
    # To disable filtering: filter_triples=None
    # Usually, we corrupt subject and object sides separately and compute ranks
    ranks = evaluate_performance(X['test'], 
                                 model=model, 
                                 filter_triples=filter,
                                 use_default_protocol=True, # corrupt subj and obj separately while evaluating
                                 verbose=True)

    # compute and print metrics:
    mrr = mrr_score(ranks)
    hits_10 = hits_at_n_score(ranks, n=10)
    print("MRR: %f, Hits@10: %f" % (mrr, hits_10))
    # Output: MRR: 0.886406, Hits@10: 0.935000

if __name__ == "__main__":
    main()

笔者的电脑 TensorFlow 会报错:

ImportError: cannot import name ‘trace‘ from ‘tensorflow.python.profiler

百度一下,发现 https://blog.csdn.net/xiaoqiaoliushuiCC/article/details/123337109

得知是 tensorflow-estimator 与 tensorflow 的版本不匹配。

在 ampligraph 虚拟环境下运行:

conda install tensorflow-estimator=1.15

即解决问题。

笔者笔记本的独显为 NVIDIA GeForce RTX 2060 ,一个 epoch 的训练大概耗时1s。

最后训练出来的指标为:

MRR: 0.878728, Hits@10: 0.932500

loss 可视化:

代填

标签:10,set,记录,filter,AmpliGraph1.4,使用,model,loss,ampligraph
From: https://www.cnblogs.com/coldchair/p/17202469.html

相关文章