def RotatE(self, head, relation, tail, mode): # RotatE模型实现 pi = 3.14159265358979323846 # head: (1024,1,2000) tail: (1024,1,2000) relation: (1024,256,1000) re_head, im_head = torch.chunk(head, 2, dim=2) # 分块 实数域和复数域 re_tail, im_tail = torch.chunk(tail, 2, dim=2) # Make phases of relations uniformly distributed in [-pi, pi] 关系嵌入的相位在[-pi, pi]之间均匀初始化 # 关系的复数通过欧拉方程实现 cos(θ) + isin(θ) 这样可以限定关系的模长为1 phase_relation = relation/(self.embedding_range.item()/pi) # 一个trick,目的应该是把实体和关系拉齐到同一级别(由于关系这里进行了cos/sin计算) re_relation = torch.cos(phase_relation) # cos(θ) im_relation = torch.sin(phase_relation) # sin(θ) if mode == 'head-batch': re_score = re_relation * re_tail + im_relation * im_tail im_score = re_relation * im_tail - im_relation * re_tail re_score = re_score - re_head im_score = im_score - im_head else: re_score = re_head * re_relation - im_head * im_relation im_score = re_head * im_relation + im_head * re_relation re_score = re_score - re_tail im_score = im_score - im_tail score = torch.stack([re_score, im_score], dim = 0) # score: tensor(2,1024,256,1000) score = score.norm(dim = 0) # 二范数 score: tensor(1024,256,1000) score = self.gamma.item() - score.sum(dim = 2) # score: tensor(1024,256) return score解释
时间: 2024-04-28 20:26:34 浏览: 5
这段代码实现了RotatE模型,用于知识图谱中的关系预测。该模型主要通过将实体和关系表示为复数向量,并将关系的相位初始化为[-pi,pi]之间的均匀分布,限制关系的模长为1。在得到实体、关系的复数向量后,根据不同的模式('head-batch'或'tail-batch')计算得出分数,并将其转换为二范数,最终得到模型的输出。其中,gamma是一个可学习的参数,用于调整分数的大小。
相关问题
def _calc(self, h, t, r): # Calculate rotated complex embeddings re_head, im_head = torch.chunk(h, 2, dim=-1) # 头实体:分块 实数域与复数域 re_tail, im_tail = torch.chunk(t, 2, dim=-1) # 尾实体: re_relation, im_relation = torch.chunk(r, 2, dim=-1) # 关系: re_head = torch.unsqueeze(re_head, dim=-1) im_head = torch.unsqueeze(im_head, dim=-1) re_tail = torch.unsqueeze(re_tail, dim=-1) im_tail = torch.unsqueeze(im_tail, dim=-1) # Perform rotation re_h = re_head * re_relation - im_head * im_relation im_h = re_head * im_relation + im_head * re_relation re_t = re_tail * re_relation + im_tail * im_relation im_t = -re_tail * im_relation + im_tail * re_relation # Concatenate real and imaginary part of embeddings h = torch.cat([re_h, im_h], dim=-1) t = torch.cat([re_t, im_t], dim=-1) return h, t解释
这段代码是一个用于在知识图谱中进行实体关系预测的模型中的一个函数。该函数的作用是将头实体、尾实体和关系的复杂向量表示进行旋转,得到新的向量表示。具体来说,该函数首先将头实体、尾实体和关系的复杂向量表示分为实部和虚部两个部分,然后通过旋转公式计算得到新的实部和虚部,最后将实部和虚部拼接起来得到新的向量表示。其中,旋转公式是re_h = re_head * re_relation - im_head * im_relation和im_h = re_head * im_relation + im_head * re_relation,re_t = re_tail * re_relation + im_tail * im_relation和im_t = -re_tail * im_relation + im_tail * re_relation。
jfinal怎么用到下面sql#sql("getComposeAlarmEventDetail") select a.*, e.rule_name, e.compare_type, e.threshold_value from flow_alarm_base a inner join flow_alarm_event b on a.alarm_event_id = b.id inner join compose_alarm_relation c on b.id = c.common_alarm_id inner join flow_alarm_event d on c.compose_alarm_id = d.id left join alarm_rule e on b.rule_id = e.id where d.id = #para(alarmEventId) #for(o : filter) and #(o.key) = #para(o.value) #end #for(x : advanceFilter) #if("in"==x.value) and #(x.key) #else and #(x.key) #para(x.value) #end #end order by a.create_time desc #end
这段代码是使用jfinal的Sql模板引擎来执行一个带参数的SQL查询语句。具体使用方法如下:
1.在jfinal项目中的Sql模板目录下创建一个.sql文件,比如:getComposeAlarmEventDetail.sql。
2.在getComposeAlarmEventDetail.sql文件中写入SQL查询语句,其中需要用到参数的地方用#para(paramName)来表示,例如:#para(alarmEventId)。
3.在Controller中使用如下代码来执行SQL查询并获取结果:
```
List<Record> result = Db.template("getComposeAlarmEventDetail")
.para("alarmEventId", alarmEventId)
.para("filter", filter)
.para("advanceFilter", advanceFilter)
.find();
```
其中,getComposeAlarmEventDetail是.sql文件的名称,para方法用来传递参数,find方法用来执行SQL查询并返回结果。
需要注意的是,jfinal的Sql模板引擎支持更多的语法,例如:#if、#for等,可以根据具体需要进行使用。