当前位置:   article > 正文

TransE代码理解_github - zulihit/transe-learn

github - zulihit/transe-learn

TransE理解

论文地址:https://proceedings.neurips.cc/paper_files/paper/2013/hash/1cecc7a77928ca8133fa24680a88d2f9-Abstract.html
参考代码地址:https://github.com/zulihit/TransE

import codecs
import random
import json
import math
import numpy as np
import pandas as pd
import copy
import time

# 数据集地址
file_FB15k = "FB15k/"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
entity2id = {}
relation2id = {}
  • 1
  • 2

数据加载

# 数据加载
def data_loader(file):
    file1 = file + "train.txt"
    file2 = file + "entity2id.txt"
    file3 = file + "relation2id.txt"

    with codecs.open(file2, 'r') as f1, codecs.open(file3, 'r') as f2:  # entity2id和relation2id
        lines1 = f1.readlines()  # readlines:作为列表返回文件中的所有行,其中每一行都是列表对象中的一项
        lines2 = f2.readlines()  # 也就是将每一行拿出来组成一个列表返回
        for line in lines1:
            line = line.strip().split('\t')  # 这一步是拆分出实体和编号    strip():删除头尾的空格     \t:水平制表符相当于tab
            if len(line) != 2:  # 如果这一行不是两个元素,就放弃这一行
                continue
            entity2id[line[0]] = int(line[1])  # 转换成字典{实体:id}

        for line in lines2:  # 同理,制作关系和编号的字典
            line = line.strip().split('\t')
            if len(line) != 2:
                continue
            relation2id[line[0]] = int(line[1])

    entity_set = set()  # 用set去重
    relation_set = set()
    triple_list = []

    with codecs.open(file1, 'r') as f:  # 文件读尽量用codecs.open方法,一般不会出现编码的问题
        content = f.readlines()  # 读取训练集
        for line in content:
            triple = line.strip().split("\t")
            if len(triple) != 3:
                continue

            h_ = entity2id[triple[0]]  # 找到训练集中三元组的对应编号
            t_ = entity2id[triple[1]]
            r_ = relation2id[triple[2]]

            triple_list.append([h_, t_, r_])  # 储存三元组的编号

            entity_set.add(h_)  # 储存头实体的编号
            entity_set.add(t_)  # 储存尾实体的编号

            relation_set.add(r_)  # 储存关系的编号

    return entity_set, relation_set, triple_list  # 返回实体的编号集合,关系的编号集合,三元组的编号集合
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
entity_set, relation_set, triple_list = data_loader(file_FB15k)
print("entity_set长度(里面是这样的entity_set: {0, 1, 2, 3,...14950}):",len(entity_set))
print("relation_set长度(里面是这样的relation_set: {0, 1, 2, 3,...1344}):",len(relation_set))
print("triple_list:",triple_list[2]) #这里需要注意的是[头实体,尾实体,关系]
  • 1
  • 2
  • 3
  • 4
entity_set长度(里面是这样的entity_set: {0, 1, 2, 3,...14950}): 14951
relation_set长度(里面是这样的relation_set: {0, 1, 2, 3,...}): 1345
triple_list: [7374, 13062, 648]
  • 1
  • 2
  • 3
print("Complete load. entity : %d , relation : %d , triple : %d" % (
        len(entity_set), len(relation_set), len(triple_list)))
  • 1
  • 2
Complete load. entity : 14951 , relation : 1345 , triple : 483142
  • 1

L1和L2

def distanceL2(h, r, t):
    # 为方便求梯度,去掉sqrt(sqrt是计算平方根的函数)
    return np.sum(np.square(h + r - t))


def distanceL1(h, r, t):
    return np.sum(np.fabs(h + r - t))  # fabs()方法返回数字的绝对值
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

关系向量&实体向量(嵌入)初始化

# 关系向量&实体向量(嵌入)初始化
def emb_initialize(entity_set,relation_set,embedding_dim):
    relation_dict = {}
    entity_dict = {}

    for relation in relation_set:
        # 初始化关系
        # np.random.uniform从一个均匀分布[low,high)中随机采样,注意定义域是左闭右开,即包含low,不包含high
        r_emb_temp = np.random.uniform(-6 / math.sqrt(embedding_dim),  # math.sqrt正平方根
                                        6 / math.sqrt(embedding_dim),
                                        embedding_dim)
        
        # 伪代码中关系向量初始化的办法(L2范数归一化):关系向量除以它的L2范数,每个r_emb_temp代表一个关系的向量
        # 关于L2范数归一化:https://www.pianshen.com/article/9664381455/
        relation_dict[relation] = r_emb_temp / np.linalg.norm(r_emb_temp, ord=2)  # np.linalg.norm求范数,ord=2代表二范数
    
    
    for entity in entity_set:
        # 初始化实体
        e_emb_temp = np.random.uniform(-6 / math.sqrt(embedding_dim),
                                        6 / math.sqrt(embedding_dim),
                                        embedding_dim)
        entity_dict[entity] = e_emb_temp / np.linalg.norm(e_emb_temp, ord=2)

    relation = relation_dict  # 初始化后的关系  {关系编号:L2范数归一化后的向量}
    entity = entity_dict  # 初始化后的实体   {实体编号:L2范数归一化后的向量}
    print("关系向量  relation[0]:\n",relation[0] ) # 返回一个关系字典
    print("实体向量  entity[0]:\n",entity[0]) 
    return relation,entity



relation,entity=emb_initialize(entity_set,relation_set,50)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
关系向量  relation[0]:
 [-0.06124931  0.05381147 -0.0581685   0.00965697  0.02497375  0.21745009
  0.00957725 -0.11250099  0.0888398   0.00656037  0.16423882  0.09413429
  0.22683678 -0.22823347  0.03280322  0.03559384 -0.04643496  0.25829363
 -0.16438011  0.16169445  0.04193395  0.01949435  0.00759052  0.16294735
 -0.15454095  0.22251406 -0.16183664 -0.0116275  -0.22146209 -0.15569882
 -0.26301086  0.0739365   0.15301796  0.02804698  0.09017532 -0.19006649
  0.15857319 -0.20143701  0.0257449   0.22238414 -0.14402593  0.07735178
 -0.16285992  0.11581199 -0.04704164 -0.0047954  -0.02833891  0.1342455
  0.21679655  0.2492022 ]
实体向量  entity[0]:
 [-0.04303367  0.02916706 -0.02495438 -0.04141798  0.1203754  -0.01869183
 -0.16110016  0.10667474  0.20629609  0.09499036 -0.17413892  0.22841423
  0.14314854 -0.06291098  0.23317245 -0.07646504 -0.09352154  0.22610284
  0.03655919 -0.22014374  0.22399264 -0.17550837 -0.19925698  0.21728113
  0.18104154  0.13808154 -0.11072903 -0.1916764  -0.01511032  0.08146982
  0.22693272  0.09733503  0.09761     0.18111636  0.00903148 -0.20821903
 -0.17265452  0.00196428 -0.11702929  0.0885079   0.16693078  0.14552579
 -0.08605191 -0.06132584 -0.20229443  0.04791079  0.02205383  0.12145553
 -0.02652395  0.1460217 ]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

由此可以看出,这里是把实体(关系)用一个50维的嵌入向量表示,一个思路就是嵌入维度越大,所能表示的特征越多。

负样本

def Corrupt(entity, triple):  # 随机替换头实体和尾实体制作负样本
    corrupted_triple = copy.deepcopy(triple)  # deepcopy:将被复制对象完全再复制一遍作为独立的新个体单独存在
    seed = random.random()
    if seed > 0.5:
        # 替换head
        head = triple[0]
        rand_head = head
        while (rand_head == head):
            rand_head = random.randint(0, len(entity) - 1)  # 从实体集合随机选择一个实体作为头实体替换
        corrupted_triple[0] = rand_head

    else:
        # 替换tail
        tail = triple[1]
        rand_tail = tail
        while (rand_tail == tail):
            rand_tail = random.randint(0, len(entity) - 1)  # 从实体集合随机选择一个实体作为尾实体替换
        corrupted_triple[1] = rand_tail
    return corrupted_triple

print("三元组:",triple_list[3])
print("负三元组(随机替换头尾实体):",Corrupt(entity,triple_list[3])) #这里需要注意的是[头实体,尾实体,关系]


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
三元组: [11436, 7445, 143]
负三元组(随机替换头尾实体): [11436, 10061, 143]
  • 1
  • 2

更新实体向量(嵌入)

# 损失函数
def hinge_loss(dist_correct, dist_corrupt):  # 损失函数
    return max(0, dist_correct - dist_corrupt + 1) #这里设置margin为1,正确三元组的L1(L2)范数-错误三元组的L1(L2)范数
  • 1
  • 2
  • 3
def update_embeddings(entity,relation, Tbatch,learning_rate):
    # 这里深拷贝了整个字典,会造成训练速度较慢,但是是易于理解的,增加训练速度的版本是transE_speed
    copy_entity = copy.deepcopy(entity)
    copy_relation = copy.deepcopy(relation)
    loss=0
    n=0 #用于提前退出,因为这个只是用于展示
    for triple, corrupted_triple in Tbatch:
        # 取copy里的vector累积更新
        h_correct_update = copy_entity[triple[0]]  # 取正确三元组的头实体的(初始化)向量
        t_correct_update = copy_entity[triple[1]]  # 取正确三元组的尾实体的(初始化)向量
        relation_update = copy_relation[triple[2]]  # 取正确三元组的关系的(初始化)向量
        
        h_corrupt_update = copy_entity[corrupted_triple[0]]  # 取错误三元组的头实体的(初始化)向量
        t_corrupt_update = copy_entity[corrupted_triple[1]]  # 取错误三元组的尾实体的(初始化)向量

        # 取原始的vector计算梯度
        h_correct = entity[triple[0]]  # 正确三元组的头实体向量
        t_correct = entity[triple[1]]  # 正确三元组的尾实体向量
        relation_temp = relation[triple[2]]  # 正确三元组的关系向量
        h_corrupt = entity[corrupted_triple[0]]  # 错误三元组的头实体向量
        t_corrupt = entity[corrupted_triple[1]]  # 错误三元组的尾实体向量
        print("正确三元组:",triple[0],triple[2],triple[1])
        print("错误三元组:",corrupted_triple[0],triple[2],corrupted_triple[1])
        if True:  # 选择l1还是l2范数计算距离这是一个超参数,假设这里选择L1范数
            dist_correct = distanceL1(h_correct, relation_temp, t_correct)  # 计算正确三元组的L1范数
            dist_corrupt = distanceL1(h_corrupt, relation_temp, t_corrupt)  # 计算错误三元组的L1范数
            print("正确三元组的L1范数:",dist_correct)
            print("错误三元组的L1范数:",dist_corrupt)
        #else:
        #    dist_correct = distanceL2(h_correct, relation, t_correct)  # 计算正确三元组的L2范数
        #    dist_corrupt = distanceL2(h_corrupt, relation, t_corrupt)  # 计算错误三元组的L2范数

        err = hinge_loss(dist_correct, dist_corrupt)  # 计算三元组的距离函数,也是原文中的距离函数,目标是使正确三元组和错误三元组区分开
        print("三元组的距离: ",err)
        if err > 0:  # 误差只可能是大于0和等于0,等于0时不能求导
            loss += err
            print("loss:",loss)
            # 关于L1范数的求导方法:参考了刘知远组实现中的实现,是先对L2范数求导,逐元素判断正负,为正赋值为1,负则为-1。
            grad_pos = 2 * (h_correct + relation_temp - t_correct)  # 正确三元组对L2范数求导的结果
            grad_neg = 2 * (h_corrupt + relation_temp - t_corrupt)  # 错误三元组对L2范数求导的结果
            print("正确三元组的梯度:\n",grad_pos)
            print("错误三元组的梯度:\n",grad_neg)
            if True:
                for i in range(len(grad_pos)):
                    if (grad_pos[i] > 0):
                        grad_pos[i] = 1
                    else:
                        grad_pos[i] = -1

                for i in range(len(grad_neg)):
                    if (grad_neg[i] > 0):
                        grad_neg[i] = 1
                    else:
                        grad_neg[i] = -1
            '''
            update_embeddings函数中,要对correct triplet和corrupted triplet都进行更新。
            虽然写作$(h,l,t)$和$(h',l,t')$,但两个三元组只有一个entity不同(前面说了,不同时替换头尾实体),
            所以在每步更新时重叠实体需要更新两次(和更新relation一样)。
            例如正确的三元组是(1,2,3),错误的是(1,2,4),那么1和2都需要更新两次
            '''
            # 更新正确三元组的向量,更新的过程也就是梯度下降的过程
            # (h+r-t)head系数为正,减梯度;tail系数为负,加梯度
            # 基于正确三元组更新向量
            h_correct_update -= learning_rate * grad_pos
            t_correct_update -= (-1) * learning_rate * grad_pos
            relation_update -= learning_rate * grad_pos  # 正确三元组的时候relation前面的符号是正号
            print("更新正确三元组向量(头实体):\n",h_correct_update)
            print("更新正确三元组向量(尾实体):\n",t_correct_update)
            print("更新正确三元组向量(关系):\n",relation_update)
            # corrupt项整体为负,因此符号与correct相反
            # 基于错误三元组更新向量
            if triple[0] == corrupted_triple[0]:  # 若替换的是尾实体,则头实体更新两次
                h_correct_update -= (-1) * learning_rate * grad_neg
                t_corrupt_update -= learning_rate * grad_neg
                relation_update -= (-1) * learning_rate * grad_neg  # 错误三元组的时候relation前面的符号是负号
            elif triple[1] == corrupted_triple[1]:  # 若替换的是头实体,则尾实体更新两次
                h_corrupt_update -= (-1) * learning_rate * grad_neg
                t_correct_update -= learning_rate * grad_neg
                relation_update -= (-1) * learning_rate * grad_neg  # 错误三元组的时候relation前面的符号是负号
            #print("更新错误三元组向量(头实体):\n",h_correct_update)
        print("==================================")
        n=n+1
        if n==5: #只循环5次,用于展示
            break
    # batch norm,更新完一个epoch重新归一化一下
    for i in copy_entity.keys():  # copy_entity.keys代表实体的编号
        copy_entity[i] /= np.linalg.norm(copy_entity[i])  # l1范数归一化,这里好像跟原文不符,原文每个epoch前貌似用的l2范数归一化,而且只针对实体
    for i in copy_relation.keys():
        copy_relation[i] /= np.linalg.norm(copy_relation[i])  # l1范数归一化
    # 达到批量更新的目的
    entity = copy_entity
    relation = copy_relation
    return loss
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
# 测试一下
nbatches = 400  # 根据batchsize计算的,看有多少个batch
batch_size = len(triple_list) // nbatches  # 根据三元组数量来判断batch_size
print("batch size: ", batch_size)
Sbatch = random.sample(triple_list, batch_size)  # 取一个batch的三元组样本
Tbatch = []  # 负样本
# 每个triple选3个负样例
for triple in Sbatch:  # 创造负样本
    corrupted_triple = Corrupt(entity,triple)
    Tbatch.append((triple, corrupted_triple))  # 包含正样本和负样本的列表
selfloss=update_embeddings(entity,relation,Tbatch,0.01)  # 更新向量
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
batch size:  1207
正确三元组: 3559 520 13032
错误三元组: 11237 520 13032
正确三元组的L1范数: 9.160141048180064
错误三元组的L1范数: 9.834198022289906
三元组的距离:  0.3259430258901581
loss: 0.3259430258901581
正确三元组的梯度:
 [-0.37901602  0.35999408  0.25189135 -0.48776021 -0.34353277  0.13417585
  0.12309854  0.55943398 -0.40032831 -0.6257741  -0.82395044 -0.64565296
 -0.13032958 -0.42676876  0.06002297 -0.11228136 -0.39317566  0.08805352
  0.13574403  0.37024427  0.39853769 -0.83249792  0.34527112  0.31937902
  0.03344285  0.68976952 -0.41013865  0.21129952 -0.24349778  0.83943312
  0.01960911 -0.72258477  0.17864673  0.82505802 -0.21363834  0.3877319
 -0.18607471 -0.38540827  1.21557675  0.88342087 -0.25167498  0.54851887
  0.01164001 -0.69430878  0.30424088 -0.04292894  0.12082423  0.10352803
  0.04737885 -0.00299312]
错误三元组的梯度:
 [-1.21288997  0.0426023   0.07324305 -0.45636903 -0.11113273  0.17646589
  0.61759574  0.83078712  0.2223841  -0.64670274 -1.24342105 -0.76046952
 -0.21324672 -0.13913355 -0.19443816  0.31559439 -0.55318034 -0.28676876
 -0.1359051  -0.21112282  0.04126087 -0.75024583 -0.26448353  0.63174885
  0.36455667  0.119203    0.06355581  0.17073111 -0.08695611  0.35151922
 -0.84224382 -0.43388996  0.07136001  0.53999022 -0.45768278  0.51899856
 -0.48669738 -0.24570011  1.10199662  0.45988928 -0.54091843  0.40703835
  0.03968196 -0.22040216 -0.46371491 -0.01390717  0.61497178  0.22873204
 -0.36592812  0.32693831]
更新正确三元组向量(头实体):
 [ 0.2297027   0.06617272 -0.02236433  0.03971393 -0.0183397   0.11760258
 -0.14633861 -0.06802978 -0.06738895 -0.14294786 -0.00737368 -0.16590576
  0.14661019 -0.12888502  0.20840432 -0.22658516  0.19490931 -0.07120115
  0.03070406  0.19483447  0.12137166 -0.23538472  0.12832474 -0.1298464
 -0.17738364  0.10871538 -0.03781839  0.11963854 -0.11801649  0.19612825
  0.13535835 -0.10416392 -0.19844118  0.23489312  0.09573639  0.12432242
  0.06108367 -0.13226323  0.1536877   0.11574505  0.01557258  0.17376242
  0.07849308 -0.09953866  0.17191859 -0.20786826  0.0094432  -0.07974221
  0.13303983 -0.0930276 ]
更新正确三元组向量(尾实体):
 [ 0.21671441  0.01611362  0.00714604  0.04116654  0.19156749  0.05765291
 -0.18083127 -0.15081217 -0.1026865   0.07931343  0.14264843  0.20150768
  0.0923356   0.12387304  0.18558506 -0.17221731  0.15485868 -0.18112329
 -0.07792114 -0.05432951 -0.16490448  0.18528051  0.04996853 -0.05508658
 -0.03808002 -0.19579378 -0.00197225 -0.12294335  0.12599166 -0.20157153
 -0.0945553   0.09187188 -0.06727104 -0.16573768 -0.04166313 -0.03553562
 -0.11778666 -0.13913391 -0.20343586 -0.17693545  0.1756986   0.04243341
  0.21554448  0.18772311 -0.17024577 -0.03645544  0.21043763 -0.15602006
  0.05864182 -0.07796291]
更新正确三元组向量(关系):
 [-0.1724963   0.09993794  0.12545604 -0.21242749  0.06814081 -0.02286175
 -0.00294339  0.16693461 -0.20546171 -0.06062576 -0.23195311  0.07458696
 -0.08943938  0.06937367 -0.02280778  0.02822718 -0.20663845 -0.09589538
 -0.07075319 -0.09404184 -0.11700729  0.03441627  0.06427935  0.20444933
  0.12602504  0.0103756  -0.13922319 -0.16693213  0.15225926 -0.00798322
 -0.25010909 -0.13525658  0.19049351 -0.01810178 -0.21421869  0.0040079
 -0.24190768 -0.16957481  0.22066482  0.11902993  0.06428854  0.11293043
  0.11287141 -0.02989262 -0.22004393  0.17994835  0.23140655 -0.05451384
 -0.08070859  0.04356814]
==================================
正确三元组: 12489 591 3764
错误三元组: 3432 591 3764
正确三元组的L1范数: 10.072297497304145
错误三元组的L1范数: 8.895223316435523
三元组的距离:  2.1770741808686225
loss: 2.5030172067587806
正确三元组的梯度:
 [-0.20675598 -0.83125079  0.40518409 -0.27664591 -0.23839436 -0.04849558
  0.10270012  0.03573358 -0.28901653 -0.54806713  0.52595571  0.19228698
 -0.28891565 -0.34848291  0.60373705 -0.38225062  0.18428005 -0.09909495
  0.48397984  0.33237687  0.39685326  0.26258934  0.20324456 -0.03127339
 -0.08698592 -1.06777599 -0.28804742  0.9433875   0.85814169 -0.38462137
  0.52426569 -1.07268463  0.06561395 -0.1351267   0.85691044  0.80275474
  0.32775847  0.8533333  -0.60398975 -0.62201439  0.3411744   0.08062966
  0.43420765 -0.52652602 -0.06032774 -0.01874879 -0.38629008 -0.37470968
 -0.3143244   0.79667938]
错误三元组的梯度:
 [ 0.57900001 -0.69060333 -0.27781468  0.29042609 -0.41242099 -0.33514097
 -0.49171434 -0.438654    0.08553278 -0.34020408 -0.03004907  0.23082889
  0.44902808 -0.5231981   0.70284469 -0.44376323  0.00236497  0.1680438
  0.03400182  0.96799707 -0.11136612  0.12276734 -0.14667088 -0.05740552
 -0.53705433 -0.39941882 -0.06872949  0.39747502  0.91435588 -0.12735192
  0.01999781 -0.39984922  0.11006548 -0.59624287  0.52324563  0.17826109
  0.54646694  0.85043908 -0.26478771 -0.60848199  0.55856062  0.24023241
  0.27502472  0.38001486  0.38453466  0.05923525 -0.23232777  0.3212445
  0.46356412  0.40164359]
更新正确三元组向量(头实体):
 [-0.12991586 -0.16440062  0.16125645 -0.06963659  0.00588732 -0.06145422
  0.15302673  0.14754389 -0.17820692 -0.17379509  0.15935649  0.06423061
 -0.19406064  0.09327949 -0.13462955  0.08170153 -0.02968334 -0.2178587
  0.01878297 -0.08934636  0.05930044  0.16612617  0.18147629 -0.05369676
  0.02258476 -0.09889161 -0.04378755  0.21309609 -0.0055314   0.10504572
  0.02104224 -0.10539113  0.00688008  0.19415205  0.09957171  0.14861912
 -0.0955123  -0.02709809  0.08867102 -0.20362468 -0.08499755  0.00741165
  0.18733285 -0.22504347 -0.22273776 -0.18459085 -0.13170815 -0.21553465
 -0.16820369  0.17852849]
更新正确三元组向量(尾实体):
 [ 0.0050869   0.08520743 -0.0782068   0.02720928  0.01639467  0.1189452
  0.12811563  0.0370657   0.01536301  0.11157175 -0.125298    0.24521036
 -0.02412924  0.11570441 -0.17468522  0.08811775 -0.10033296 -0.19931802
 -0.14839989 -0.00535774 -0.16605157  0.0905454   0.20020356  0.00116181
  0.11966507  0.23274371  0.19135325 -0.10567917 -0.18644574  0.09486631
 -0.19564498  0.21459862 -0.17647544  0.15895447 -0.16585566 -0.08354602
 -0.09783547 -0.17778807  0.14655853  0.03630079 -0.09895671 -0.09075587
  0.07575139  0.17568436 -0.01532985 -0.23376763  0.10097763  0.17765354
  0.20537207  0.02594474]
更新正确三元组向量(关系):
 [ 0.06162476 -0.13601735 -0.0668712  -0.01147708 -0.07868983  0.18615163
 -0.00356104 -0.1226114   0.07906167  0.04133327 -0.05167663  0.24712324
  0.05547357 -0.12181653  0.23181285 -0.15470908 -0.00850959 -0.00100679
  0.04480705  0.22017705 -0.05692538  0.0257139   0.09034955  0.06922188
  0.08358735 -0.17225267  0.12111709  0.12291848  0.2181565  -0.1724901
  0.01544562 -0.18635256 -0.18054854 -0.07276093  0.13302785  0.13921223
  0.13155607  0.24597667 -0.21410737 -0.04108172  0.12662804 -0.08785269
  0.07552237  0.16746481  0.20724404 -0.02855117  0.06954074  0.23583335
  0.24641356  0.21575595]
==================================
正确三元组: 5971 242 1937
错误三元组: 5971 242 12379
正确三元组的L1范数: 10.838409158700102
错误三元组的L1范数: 10.324248809616375
三元组的距离:  1.5141603490837277
loss: 4.017177555842508
正确三元组的梯度:
 [-0.2583954   0.75117153 -0.70019031  0.37722074 -0.22390412 -0.37128223
 -0.37664392 -0.01448126 -0.36556971  0.67554285  0.36036681  0.58679804
 -0.56599424  0.79970803  0.75649744  0.77286221 -0.26326082  0.14947996
 -0.67023404  0.02380669 -0.76563816  0.7129515   0.94371575  0.15692248
 -0.05732602  0.04239009 -0.1545963  -0.17958389  0.50802604 -0.48672663
 -0.55946516 -0.60719591 -0.48154064 -0.06867678  0.07234063  0.15862991
 -0.35610727 -0.00660994  0.26682611  0.14069412 -0.46288159  0.37630276
 -0.9899526   0.45630982 -1.08016621  0.20687556 -0.2692745  -0.53531404
 -0.43873147 -1.07163607]
错误三元组的梯度:
 [-0.73952717  0.90640584 -0.59234038  0.13835401 -0.02106316 -0.23818818
 -0.20512517  0.41712066 -1.08411331  0.53046497  0.46595905 -0.04045682
 -0.51185347  0.89064319  0.97088536  0.09680112 -0.213252    0.43408414
 -0.47290427 -0.2345759   0.02207074  0.43654382  0.07963893 -0.64194125
 -0.36320662 -0.08258446 -0.7346493   0.04089253 -0.08650893 -0.21946392
 -0.97173921 -0.02592454 -0.2536815   0.14912094  0.63431087  0.63769143
 -0.58913114  0.5826085   0.78669324 -0.33504835 -0.10750919  0.73398359
 -0.32363269  0.28295547 -0.98100014  0.24760741 -0.36813614 -0.25353888
 -0.30583286 -0.1667329 ]
更新正确三元组向量(头实体):
 [-0.08934616 -0.0456189  -0.04346564  0.06966536 -0.03918103 -0.11917562
 -0.19253371  0.05937545 -0.22708118  0.0902964   0.23607142 -0.0409323
  0.05726446  0.10340646  0.23561154  0.0353324   0.080485    0.16431726
 -0.177941   -0.14737261 -0.16632585  0.04386661  0.09845879 -0.17417872
 -0.24173488  0.0009411  -0.14134846 -0.21125681  0.1364854  -0.21538004
 -0.19120584  0.08871361  0.04766718 -0.00025555 -0.02630896  0.04873789
 -0.09395941  0.20110266  0.17984689  0.08842095  0.06703177  0.21846395
 -0.18744731  0.20221225 -0.15945802 -0.12049582  0.06079068 -0.07816313
  0.00929007 -0.0691089 ]
更新正确三元组向量(尾实体):
 [-0.12878045 -0.13286137  0.1605667   0.05689098  0.18539835  0.19010479
 -0.08320368  0.05279641 -0.18501312 -0.09568796  0.11891911 -0.19168768
  0.17982601 -0.04070374  0.03453008 -0.10578747 -0.00246913  0.18461503
  0.19290356 -0.17485211  0.17971106 -0.11025855 -0.20289303 -0.18443148
  0.01375137 -0.10042602 -0.21482027 -0.05401662 -0.12962625 -0.04619295
 -0.07648262  0.17444274  0.20365959  0.1535966   0.19770323  0.00588395
  0.08088917  0.19095797  0.09442052 -0.08938368  0.16257912  0.0060258
  0.15515716  0.12779967  0.15341584 -0.07033769  0.00725963  0.14077621
 -0.02993194  0.19856707]
更新正确三元组向量(关系):
 [-0.13863199  0.25834329 -0.11606282  0.14583599  0.14262733  0.15363929
 -0.04899193  0.01618034 -0.1107168   0.12178707  0.03303109  0.11264364
 -0.13043557  0.22574382  0.14716726  0.21531124 -0.18458455  0.06503775
  0.06572753 -0.04557615 -0.00678217  0.17235059  0.14050606  0.03820848
  0.25682324 -0.11017207 -0.12076996  0.09744824 -0.04209863 -0.04417623
 -0.13500936 -0.18786882 -0.05477791  0.14951375  0.2301825   0.00646102
  0.02679495  0.01655034  0.01798669 -0.13745758 -0.10589344 -0.05428676
 -0.12237182  0.12374234 -0.19720924  0.12359591 -0.1581683  -0.01871768
 -0.22858774 -0.23814207]
==================================
正确三元组: 13591 1291 12733
错误三元组: 13053 1291 12733
正确三元组的L1范数: 10.301110197065395
错误三元组的L1范数: 10.956140962774905
三元组的距离:  0.34496923429049
loss: 4.362146790132998
正确三元组的梯度:
 [-0.16795658 -0.12949096  0.86120369 -0.63356445 -0.43294176  0.1614915
  0.72136151  0.09835598 -0.02436859  0.65419805  0.89448053  0.27997895
 -0.38054446  0.00558313 -0.07562275 -0.42429659  0.14469542 -0.23622529
  0.50427403  0.65682846  0.18051775 -0.23145447 -0.30247826 -0.16453314
  0.45272069 -0.25732558  0.35740638 -1.05463174 -0.51151366 -0.51385635
 -0.24108557  0.10228883 -0.70497518  0.5301379  -0.26027818  0.42738776
  0.01525057  0.31995543 -0.57657569  0.27153534  0.54535743  0.87142385
 -0.89743388  0.52178093 -0.39267808  1.03495809  0.10653143  0.09360188
 -0.47958875 -0.69549495]
错误三元组的梯度:
 [-0.67771651  0.12805868  0.72408947 -0.19640587 -0.74853482 -0.34909758
  0.44424789 -0.12377116  0.1842216   0.376691    0.37375837  0.54991684
 -0.03252688 -0.01503632  0.63432943 -0.60377107 -0.15897948 -0.04480671
  0.45616743  0.53679793  0.964548    0.2923565   0.26962191 -0.25070664
  0.13142219 -0.5687559   0.66908898 -1.3728184  -0.31446713 -0.92413308
 -0.21311364 -0.29992207 -0.5525467   1.15602521  0.06037807  0.24726618
 -0.07502866 -0.29430864 -0.34061504  0.46896628 -0.15371691  0.77467971
 -1.09272412  0.19261579 -0.56736933  0.3018252   0.67899331  0.47809996
 -0.43818598 -0.40905736]
更新正确三元组向量(头实体):
 [ 0.03719875 -0.18102125  0.18655618  0.00234794 -0.00593733  0.20994527
  0.2321054  -0.07844085 -0.08978608  0.19390504  0.22651861  0.08342132
  0.04610836  0.02628527 -0.21922707 -0.01643649  0.13454788 -0.01742153
  0.00986113 -0.04198968 -0.22021667 -0.20016039 -0.05358228 -0.17935688
  0.17247146  0.19220583 -0.20978991 -0.01921698  0.08702367 -0.02535365
 -0.12990253 -0.03767222 -0.06934149 -0.17999268 -0.14629433 -0.15465156
 -0.08023339  0.08533757 -0.16837369 -0.17871502  0.21934804  0.17723644
  0.03689182  0.13320018  0.01651606  0.14489723 -0.11404736  0.03530656
 -0.0057997  -0.22038112]
更新正确三元组向量(尾实体):
 [-0.0589535  -0.20761707 -0.07000709  0.24815392  0.17894299  0.10897056
  0.13024331 -0.05340003 -0.08946519 -0.13465577 -0.07007775 -0.05664604
  0.0386865   0.06207172 -0.13455015  0.03412457  0.03162785 -0.10439232
 -0.20029494 -0.12670348 -0.04617016 -0.22046366  0.09273154 -0.0229746
  0.13404191  0.18611716 -0.23198154  0.23538903  0.15334151  0.04696953
  0.09957064 -0.02955164  0.18214594 -0.17534037 -0.23096015 -0.102417
 -0.2346907  -0.1462859   0.06890641 -0.17964636  0.05445803 -0.07502705
  0.2170002  -0.15103151  0.12880815 -0.11985806 -0.11893992  0.0426667
  0.07104367 -0.09966913]
更新正确三元组向量(关系):
 [-0.15013055 -0.0613413   0.14403858 -0.04097624 -0.00159057 -0.05022896
  0.22881867  0.04421881  0.0181366  -0.03146179  0.1206439  -0.03007788
 -0.16769409  0.00857801  0.07686555 -0.13158723 -0.06057233 -0.17508344
  0.01198095  0.21370042  0.23430539 -0.10603051  0.02507469  0.1041157
  0.1579308  -0.10475145  0.12651156 -0.24270986 -0.15943899 -0.154605
  0.13893038  0.029265   -0.07100017  0.23972126 -0.18480491  0.23592844
 -0.17683202 -0.10164575 -0.02100775  0.10483633  0.07778871  0.15344844
 -0.23860857 -0.05334123 -0.05404695  0.22272376  0.01837316  0.02416109
 -0.13295101 -0.19703548]
==================================
正确三元组: 9399 1226 14152
错误三元组: 9399 1226 14262
正确三元组的L1范数: 9.992409340840295
错误三元组的L1范数: 9.942922552785314
三元组的距离:  1.0494867880549812
loss: 5.411633578187979
正确三元组的梯度:
 [ 0.3680397   0.32089285  0.62452467 -0.11492201 -0.09262002 -0.43011459
  0.51431885 -0.30068848 -0.30219077  0.75059382 -0.82481915 -0.51594505
  0.79295137 -0.07095999 -0.3475567  -0.03309919  0.27992241 -1.04000586
 -0.95227105  0.25276741  0.05232533  0.88260994 -0.07266404  0.29301016
 -0.15009508 -0.26230577  0.14339828 -0.37980872  1.19761505  0.07059943
  0.33072642  0.19519713  0.53772988  0.2357335   0.17952196 -0.01149659
  0.46860705 -0.44929227  0.04870082 -0.6611804  -0.47435454  0.55607143
 -0.33234647 -0.67720789 -0.47300547 -0.27851468  0.76242954  0.57561381
  0.01060576 -0.29284736]
错误三元组的梯度:
 [ 0.46310257  0.21061891  1.24805701  0.13339741 -0.00294819 -0.22907679
  0.07070678 -0.78669113  0.00839461  0.00849556 -0.81720606 -0.35734205
  0.28473943  0.14521598 -0.14422891 -0.399389    0.1475983  -1.13260506
 -0.46865487  0.04638166  0.08336715  0.75340647 -0.13428276  0.6772844
 -0.57690078 -0.26076736  0.30900861  0.29524747  0.84295118 -0.43183746
  0.05549771 -0.49581024 -0.14328479  0.09092644  0.24354843 -0.23388881
  0.66421034 -0.91978058 -0.01725474 -0.52507413 -0.46899945  0.55209168
 -0.1651277  -0.68579278  0.23637797 -0.39954224  0.56613526  0.90607213
 -0.2003177  -0.84620808]
更新正确三元组向量(头实体):
 [-0.01831672 -0.18055599  0.20603467  0.22398648 -0.15489488  0.01648978
  0.00368893 -0.0251046  -0.12057599  0.00527216 -0.00037307 -0.13813036
  0.0430363   0.11004196  0.1058476   0.1641077   0.0257299  -0.19869179
 -0.18634728  0.22703158  0.17503868  0.09201114  0.10029876  0.17451867
 -0.20132297  0.06525971  0.1391338   0.13339879  0.2087574  -0.22631443
  0.0393066   0.11737633  0.08829377  0.10877147 -0.01418208 -0.21159191
  0.20547908 -0.19480133 -0.15336722 -0.03435928 -0.06201322  0.1951056
 -0.22891573  0.06897806  0.05911209  0.12833171 -0.04070961  0.057704
  0.11166147 -0.09678127]
更新正确三元组向量(尾实体):
 [-0.05657985 -0.16712552  0.15457465  0.2142093  -0.06569875  0.11085332
 -0.11512669 -0.06287761 -0.01930346 -0.12491337  0.15426069 -0.06612348
 -0.20488343  0.18162021  0.21864213 -0.00233526  0.05588688  0.11340089
  0.168908    0.02132967  0.13220786 -0.21963586  0.18884161  0.19709918
 -0.13572044  0.07838115  0.03376749  0.18850162 -0.15418359 -0.16783863
  0.01981965 -0.1195906  -0.10097688 -0.13422315  0.15743777 -0.22389771
  0.11677102  0.00318792  0.08573639  0.12540468 -0.00666724 -0.2050859
 -0.07770921  0.2196374   0.2013742   0.06186475 -0.17044875 -0.07208446
 -0.07473142 -0.08943234]
更新正确三元组向量(关系):
 [ 0.11575673  0.14387689  0.23080231 -0.03723818  0.07288612 -0.09069376
  0.1083438  -0.15811724 -0.01982286  0.21511138 -0.22777581 -0.15596564
  0.11855595  0.06609826 -0.03098382 -0.15299256  0.14011818 -0.17791025
 -0.09088025 -0.10931821 -0.04666815  0.09965796  0.08221084  0.13908558
  0.02055499 -0.08803145 -0.06366717 -0.10480152  0.20586653  0.06377551
  0.11587626 -0.16936836  0.0495943  -0.15512788  0.23138083  0.01194591
  0.11559546  0.00334312  0.23345403 -0.14082623 -0.15183128 -0.15215579
  0.01503328 -0.15794461 -0.06424062 -0.1757243   0.22147562  0.12801845
 -0.21109001 -0.10907475]
==================================
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/549498
推荐阅读
相关标签
  

闽ICP备14008679号