赞
踩
python代码:
from random import uniform, sample from numpy import * from copy import deepcopy class TransE: def __init__(self, entityList, relationList, tripleList, margin = 1, learingRate = 0.00001, dim = 10, L1 = True): self.margin = margin#避免梯度值为0? self.learingRate = learingRate#学习率 self.dim = dim#向量维度 self.entityList = entityList#一开始,entityList是entity的list;初始化后,变为字典,key是entity,values是其向量(使用narray)。 self.relationList = relationList#理由同上 self.tripleList = tripleList#理由同上 self.loss = 0 self.L1 = L1 def initialize(self): ''' 初始化向量 ''' entityVectorList = { } relationVectorList = { } for entity in self.entityList: n = 0 entityVector = [] while n < self.dim: ram = init(self.dim)#对于每个实体初始化dim个值组成向量 entityVector.append(ram) n += 1 entityVector = norm(entityVector)#归一化 entityVectorList[entity] = entityVector print("entityVector初始化完成,数量是%d"%len(entityVectorList)) for relation in self. relationList: n = 0 relationVector = [] while n < self.dim: ram = init(self.dim)#初始化的范围 relationVector.append(ram) n += 1 relationVector = norm(relationVector)#归一化 relationVectorList[relation] = relationVector print("relationVectorList初始化完成,数量是%d"%len(relationVectorList)) self.entityList = entityVectorList#{'实体名':[初始向量值],'':[],......,'':[]}实体名为KEY,向量值为value self.relationList = relationVectorList#{'关系名':[初始向量值],'':[],......,'':[]} def transE(self, cI = 20): print("训练开始") for cycleIndex in range(cI): Sbatch = self.getSample(150)#[(),(),....()] print(Sbatch) Tbatch = []#150个元组对(原三元组,打碎的三元组)的列表 :{((h,r,t),(h',r,t'))} for sbatch in Sbatch: tripletWithCorruptedTriplet = (sbatch, self.getCorruptedTriplet(sbatch)) if(tripletWithCorruptedTriplet not in Tbatch): Tbatch.append(tripletWithCorruptedTriplet) self.update(Tbatch) if cycleIndex % 100 == 0: print("第%d次循环"%cycleIndex) print(self.loss) #self.writeRelationVector("D:\\transE-master\\transE-master\\relatio
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。