加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 在Keras中实施WARP损失

发布时间:2020-12-20 13:19:49 所属栏目:Python 来源:网络整理
导读:我正在尝试使用Keras API实现warp loss(成对排名函数的类型).我有点担心这可以成功. 翘曲损失的定义取自lightFM doc: For a given (user,positive item pair),sample a negative item at random from all the remaining items. Compute predictions for bot
我正在尝试使用Keras API实现warp loss(成对排名函数的类型).我有点担心这可以成功.

翘曲损失的定义取自lightFM doc:

For a given (user,positive item pair),sample a negative item at random from all the remaining items. Compute predictions for both items; if the negative item’s prediction exceeds that of the positive item plus a margin,perform a gradient update to rank the positive item higher and the negative item lower. If there is no rank violation,continue sampling negative items until a violation is found.

例如,在semantic embeddings of #hashtags,一篇由facebook AI研究发表的论文中使用了Warp函数.在本文中,他们试图预测短文本中最具代表性的主题标签.在“用户”被认为是短文本的情况下,“正项目”是短文本的主题标签,而负项目是从“主题标签查找”统一采样的一些随机主题标签.

我正在追随另一个三重奏损失的印象,以创造经线之一:github

我的理解是,对于每个数据点,我将有3个输入.嵌入示例(‘半’伪代码):

sequence_input = Input(shape=(100,),dtype='int32') # 100 features per data point
positive_example = Input(shape=(1,dtype='int32',name="positive") # the one positive example
negative_examples = Input(shape=(1000,name="random_negative_examples") # 1000 random negative examples.

#map data points to already created embeddings
embedded_seq_input = embedded_layer(sequence_input)
embedded_positive = embedded_layer(positive_example)
embedded_negatives = embedded_layer(negative_examples)

conv1 = Convolution1D(...)(embeddded_seq_input)
               .
               .
               .
z = Dense(vector_size_of_embedding,activation="linear")(convN)

loss = merge([z,embedded_positive,embedded_negatives],mode=warp_loss)
                         .
                         .
                         .

warp_loss在哪里(我假设得到1000个随机消极而不是全部取消而且得分来自cosinus similatiry):

def warp_loss(X):
    # pseudocode
    z,positive,negatives = X
    positive_score = cosinus_similatiry(z,positive)
    counts = 1
    loss = 0
    for negative in negatives:
        score = cosinus_similatiry(z,negative)
        if score > positive_score:
           loss = ((number_of_labels - 1) / counts) * (score + 1 - positive_score
        else:
           counts += 1
    return loss

如何计算扭曲很好地描述:post

我不确定这是否是正确的方法,但我找不到实现warp_loss伪函数的方法.我可以使用merge([x,u],mode =’cos’)计算cosinus,但这假定相同的维度.因此,我不确定如何使用合并模式cos来表示多个负面示例,因此我尝试创建自己的warp_loss.

任何见解,实施类似的例子,评论都是有用的.

解决方法

首先,我认为在批量训练范例中不可能实施WARP.因此,您无法在Keras中实施WARP.这是因为WARP本质上是顺序的,所以它无法处理分批的数据,la Keras.我想如果你完全随机批次,你可以把它拉下来.

通常对于WARP,您包含1的边距,但在文章中您可以将其视为超文本:

if neg_score > pos_score-1: #margin of 1
  loss = log(num_items / counts) #loss weighted by sample count
  loss = max(1,loss) #this looks like same thing you were doing in diff way

这优于它的前身BPR,因为它优化了前k精度而不是平均精度.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读