使用批量输出的 Keras 损失函数
Keras Loss function using outputs of batch
我正在尝试使用 keras 使用双分支神经网络学习图像和文本的联合嵌入式表示。这是我的模型的样子:
这些是我的训练模型的当前输入和输出:
model = Model([txt_input,img_input], [encoded_txt, encoded_img])
我必须使用双向排名损失,这意味着相应文本和图像的表示应该比任何其他 image/text 更接近彼此 image/text 余量 m。这是整个损失函数,
- s : 相似度函数
- D:训练集
- Yi+ : 给定图像 xi 的一组对应的(正)文本描述(我的实验中只有一个正)
- Yi- :给定图像 xi
的一组非对应(负)描述
- Xi+ : 给定文本描述 yi 的对应(正)图像集(我的实验中只有一个正)
- Xi- :给定文本描述 yi 的非对应(负)图像集
问题是,为了计算这个损失,我不仅要知道当前图像的输出和相应的文本表示,而且我必须计算它们与其他图像的表示的相似度 images/texts。
具体来说,我的问题是:
有没有办法在计算损失时包括整个批次的输出,或者至少包括前 n 个样本的输出?
我看到如何做这样的事情的唯一方法是创建一个具有某种状态的损失函数,该状态保持最后 n 个样本的表示,并使用它们来计算相似性。我认为这不是一个好的解决方案,并且想知道是否有更优雅的方法来实现它。我也在研究 Pytorch 等其他框架,以检查它们是否支持批量损失之类的东西。任何帮助将不胜感激。
谢谢!
PS:我实际上是在尝试重现这篇论文的实验:
L. Wang, Y. Li, and S. Lazebnik, “Learning deep structure-preserving image- text embeddings,” in Proceedings of the IEEE conference on
computer vision and pattern recognition, pp. 5005–5013, 2016.
图片也是从这篇论文中提取出来的。
Concretely, my question is: Is there a way to include the outputs of the entire batch, or at least the previous n samples, when calculating the loss?
我认为您问题的措辞有误或想法有误。比方说,你在训练时设置 batch size = 8,你的损失函数正好得到整个 batch 并且损失是在 batch 上计算的。
检查 keras losses 实现。
class LossFunctionWrapper(Loss):
"""Wraps a loss function in the `Loss` class.
# Arguments
fn: The loss function to wrap, with signature `fn(y_true, y_pred,
**kwargs)`.
reduction: (Optional) Type of loss reduction to apply to loss.
Default value is `SUM_OVER_BATCH_SIZE`.
name: (Optional) name for the loss.
**kwargs: The keyword arguments that are passed on to `fn`.
"""
Default value is SUM_OVER_BATCH_SIZE
.
所以,你可以计算整批的损失。
另外,可以利用triplet loss的概念,在损失函数中生成带有flag的正样本和负样本,方便计算。
最后,这里是论文的一个 tensorflow 实现,可能会有所帮助:https://github.com/lwwang/Two_branch_network
我正在尝试使用 keras 使用双分支神经网络学习图像和文本的联合嵌入式表示。这是我的模型的样子:
这些是我的训练模型的当前输入和输出:
model = Model([txt_input,img_input], [encoded_txt, encoded_img])
我必须使用双向排名损失,这意味着相应文本和图像的表示应该比任何其他 image/text 更接近彼此 image/text 余量 m。这是整个损失函数,
- s : 相似度函数
- D:训练集
- Yi+ : 给定图像 xi 的一组对应的(正)文本描述(我的实验中只有一个正)
- Yi- :给定图像 xi 的一组非对应(负)描述
- Xi+ : 给定文本描述 yi 的对应(正)图像集(我的实验中只有一个正)
- Xi- :给定文本描述 yi 的非对应(负)图像集
问题是,为了计算这个损失,我不仅要知道当前图像的输出和相应的文本表示,而且我必须计算它们与其他图像的表示的相似度 images/texts。
具体来说,我的问题是: 有没有办法在计算损失时包括整个批次的输出,或者至少包括前 n 个样本的输出?
我看到如何做这样的事情的唯一方法是创建一个具有某种状态的损失函数,该状态保持最后 n 个样本的表示,并使用它们来计算相似性。我认为这不是一个好的解决方案,并且想知道是否有更优雅的方法来实现它。我也在研究 Pytorch 等其他框架,以检查它们是否支持批量损失之类的东西。任何帮助将不胜感激。
谢谢!
PS:我实际上是在尝试重现这篇论文的实验:
L. Wang, Y. Li, and S. Lazebnik, “Learning deep structure-preserving image- text embeddings,” in Proceedings of the IEEE conference on computer vision and pattern recognition, pp. 5005–5013, 2016.
图片也是从这篇论文中提取出来的。
Concretely, my question is: Is there a way to include the outputs of the entire batch, or at least the previous n samples, when calculating the loss?
我认为您问题的措辞有误或想法有误。比方说,你在训练时设置 batch size = 8,你的损失函数正好得到整个 batch 并且损失是在 batch 上计算的。
检查 keras losses 实现。
class LossFunctionWrapper(Loss):
"""Wraps a loss function in the `Loss` class.
# Arguments
fn: The loss function to wrap, with signature `fn(y_true, y_pred,
**kwargs)`.
reduction: (Optional) Type of loss reduction to apply to loss.
Default value is `SUM_OVER_BATCH_SIZE`.
name: (Optional) name for the loss.
**kwargs: The keyword arguments that are passed on to `fn`.
"""
Default value is
SUM_OVER_BATCH_SIZE
.
所以,你可以计算整批的损失。
另外,可以利用triplet loss的概念,在损失函数中生成带有flag的正样本和负样本,方便计算。
最后,这里是论文的一个 tensorflow 实现,可能会有所帮助:https://github.com/lwwang/Two_branch_network