【问题标题】:Implimentation of WARP loss in Keras在 Keras 中实现 WARP 丢失
【发布时间】:2018-02-28 04:28:53
【问题描述】:

我正在尝试使用 Keras API 实现扭曲损失(成对排名函数的类型)。我有点卡住了这怎么能成功。

warp loss的定义取自lightFM doc.:

对于给定的(用户,正面物品对),从所有剩余物品中随机抽取一个负面物品。计算两个项目的预测;如果负项的预测超过正项的预测加上一个边距,则执行梯度更新以将正项排名较高,将负项排名较低。如果不存在排名违规,则继续对负项进行抽样,直到发现违规为止。

例如在semantic embeddings of #hashtags 中使用了Warp 函数,这是一篇由facebook AI 研究发表的论文。在本文中,他们尝试预测短文本中最具代表性的主题标签。其中'user' 被认为是短文本,'positive item' 是短文本的标签,negative items 是从“标签查找”中统一采样的一些随机标签。

我正在遵循另一个三元组损失的实现来创建经线:github

我的理解是,对于每个数据点,我将有 3 个输入。嵌入示例('semi' 伪代码):

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,), dtype='int32', 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') 计算余弦,但这假设尺寸相同。所以我不确定如何使用merge 模式 cos 来处理多个反例,所以我正在尝试创建自己的warp_loss

任何见解,实施类似的例子,cmets 都是有用的。

【问题讨论】:

  • 你能帮我理解rankpositive itemnegative itemuser是什么吗?我完全不知道这些是什么意思。
  • @DanielMöller 我已经用论文中的一个例子更新了这个问题。希望现在更清楚。
  • 您最终使用@MpizosDimitris 做了什么? :)

标签: python keras


【解决方案1】:

首先,我认为不可能在批量训练范式中实施 WARP。因此你不能在 Keras 中实现 WARP。这是因为 WARP 本质上是顺序的,所以它不能处理分批的数据,就像 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 个精度而不是平均精度。

【讨论】:

  • 你的意思是“如果你做了完全随机的批次,你可以成功”?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多