试图理解caffe中的自定义损失层

用户4911648

我见过可以像这样在 caffe 中定义自定义损失层,例如 EuclideanLoss:

import caffe
import numpy as np


    class EuclideanLossLayer(caffe.Layer):
        """
        Compute the Euclidean Loss in the same manner as the C++ 
EuclideanLossLayer
        to demonstrate the class interface for developing layers in Python.
        """

        def setup(self, bottom, top):
            # check input pair
            if len(bottom) != 2:
                raise Exception("Need two inputs to compute distance.")

        def reshape(self, bottom, top):
            # check input dimensions match
            if bottom[0].count != bottom[1].count:
                raise Exception("Inputs must have the same dimension.")
            # difference is shape of inputs
            self.diff = np.zeros_like(bottom[0].data, dtype=np.float32)
            # loss output is scalar
            top[0].reshape(1)

        def forward(self, bottom, top):
            self.diff[...] = bottom[0].data - bottom[1].data
            top[0].data[...] = np.sum(self.diff**2) / bottom[0].num / 2.

        def backward(self, top, propagate_down, bottom):
            for i in range(2):
                if not propagate_down[i]:
                    continue
                if i == 0:
                    sign = 1
                else:
                    sign = -1
                bottom[i].diff[...] = sign * self.diff / bottom[i].num

但是,我有一些关于该代码的问题:

如果我想自定义此层并更改此行中的损失计算:

top[0].data[...] = np.sum(self.diff**2) / bottom[0].num / 2.

让我们说:

channelsAxis = bottom[0].data.shape[1]
self.diff[...] = np.sum(bottom[0].data, axis=channelAxis) - np.sum(bottom[1].data, axis=channelAxis)
top[0].data[...] = np.sum(self.diff**2) / bottom[0].num / 2.

我该如何更改向后功能?对于 EuclideanLoss,它是:

bottom[i].diff[...] = sign * self.diff / bottom[i].num

它必须如何查找我描述的损失?

标志有什么用?

沙伊

尽管将您所追求损失作为一个"Python"来实现可能是一项非常有教育意义的练习,但您可以使用现有获得相同的损失您只需要"Reduction"在调用常规"EuclideanLoss"之前为每个 blob添加一个层:

layer {
  type: "Reduction"
  name: "rx1"
  bottom: "x1"
  top: "rx1"
  reduction_param { axis: 1 operation: SUM }
} 
layer {
  type: "Reduction"
  name: "rx2"
  bottom: "x2"
  top: "rx2"
  reduction_param { axis: 1 operation: SUM }
} 
layer {
  type: "EuclideanLoss"
  name: "loss"
  bottom: "rx1"
  bottom: "rx2"
  top: "loss"
}

更新:
根据您的评论,如果您只想对频道维度求和并保持所有其他维度不变,您可以使用固定的 1x1 转换(如您所建议的):

layer {
  type: "Convolution"
  name: "rx1"
  bottom: "x1"
  top: "rx1"
  param { lr_mult: 0 decay_mult: 0 } # make this layer *fixed*
  convolution_param {
    num_output: 1
    kernel_size: 1
    bias_term: 0  # no need for bias
    weight_filler: { type: "constant" value: 1 } # sum
  }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在python中构建自定义Caffe层

来自分类Dev

编译时清除添加到tensorflow 2中的自定义层的损失

来自分类Dev

千层面/ theano的自定义损失功能

来自分类Dev

Keras 中自定义损失的输出

来自分类Dev

React中的自定义API层

来自分类Dev

自定义keras损失

来自分类Dev

自定义损失函数Caffe(Spearman系数),同时进行修正(回归)

来自分类Dev

如何在Tensorflow中编写自定义损失函数?

来自分类Dev

在Pytorch中自定义距离损失功能?

来自分类Dev

Keras,Python中的自定义损失函数?

来自分类Dev

Keras模型-在自定义损失函数中获取输入

来自分类Dev

在CNN的Keras自定义损失函数中处理数据

来自分类Dev

基于输入数据的Keras中的自定义损失函数

来自分类Dev

张量流中具有循环的自定义损失

来自分类Dev

在自定义损失函数中迭代张量

来自分类Dev

张量流中的自定义损失函数

来自分类Dev

keras 中的加权 mse 自定义损失函数 - 自定义权重

来自分类Dev

PyTorch中具有自定义后向功能的损失-简单的MSE示例中的爆炸损失

来自分类Dev

如何将自定义bn层添加到caffe

来自分类Dev

如何在Keras中创建自定义卷积层

来自分类Dev

在 tensorflow 中创建自定义层时出错

来自分类Dev

在 Keras 中实现自定义层(RStudio 接口)

来自分类Dev

Keras上的自定义损失功能

来自分类Dev

在Keras建立自定义损失

来自分类Dev

带和不带自定义循环的Keras中的回归自定义损失返回值

来自分类Dev

从自定义uitableviewcell删除自定义子层

来自分类Dev

从自定义uitableviewcell删除自定义子层

来自分类Dev

Keras中的自定义损失函数应该返回该批次的单个损失值,还是返回该训练批次中每个样本的大量损失?

来自分类Dev

将我的自定义损失函数添加到火炬中

Related 相关文章

  1. 1

    在python中构建自定义Caffe层

  2. 2

    编译时清除添加到tensorflow 2中的自定义层的损失

  3. 3

    千层面/ theano的自定义损失功能

  4. 4

    Keras 中自定义损失的输出

  5. 5

    React中的自定义API层

  6. 6

    自定义keras损失

  7. 7

    自定义损失函数Caffe(Spearman系数),同时进行修正(回归)

  8. 8

    如何在Tensorflow中编写自定义损失函数?

  9. 9

    在Pytorch中自定义距离损失功能?

  10. 10

    Keras,Python中的自定义损失函数?

  11. 11

    Keras模型-在自定义损失函数中获取输入

  12. 12

    在CNN的Keras自定义损失函数中处理数据

  13. 13

    基于输入数据的Keras中的自定义损失函数

  14. 14

    张量流中具有循环的自定义损失

  15. 15

    在自定义损失函数中迭代张量

  16. 16

    张量流中的自定义损失函数

  17. 17

    keras 中的加权 mse 自定义损失函数 - 自定义权重

  18. 18

    PyTorch中具有自定义后向功能的损失-简单的MSE示例中的爆炸损失

  19. 19

    如何将自定义bn层添加到caffe

  20. 20

    如何在Keras中创建自定义卷积层

  21. 21

    在 tensorflow 中创建自定义层时出错

  22. 22

    在 Keras 中实现自定义层(RStudio 接口)

  23. 23

    Keras上的自定义损失功能

  24. 24

    在Keras建立自定义损失

  25. 25

    带和不带自定义循环的Keras中的回归自定义损失返回值

  26. 26

    从自定义uitableviewcell删除自定义子层

  27. 27

    从自定义uitableviewcell删除自定义子层

  28. 28

    Keras中的自定义损失函数应该返回该批次的单个损失值,还是返回该训练批次中每个样本的大量损失?

  29. 29

    将我的自定义损失函数添加到火炬中

热门标签

归档