【问题标题】:How to loop over all the elements of a Blob variable in Caffe?如何遍历 Caffe 中 Blob 变量的所有元素?
【发布时间】:2016-08-02 10:46:54
【问题描述】:

在 Caffe 中处理新的损失层,

我在 diff_.cpu_data() 中有一些值,让我们将其中的每个元素命名为 Di

现在,我想为每个 Di 计算这个函数:

并将结果分配给层的bottom[0]->mutable_cpu_diff()中的相应元素。

如您所见,对于第二项,不需要循环输入和输出变量(分别为 diff_.cpu_databottom[0]->mutable_cpu_diff()),而在第一项,我需要访问输入变量中每个元素的值,那么我当然需要将函数的结果分配给输出变量的相应元素,如果它们是二维数组,显然我可以做点什么像这样:

但如您所知,这些变量是 4-D 数组,我不清楚该怎么做。

我应该使用 Offset() 函数或类似的东西来循环这些类似于this 的变量的所有元素吗?

有人可以向我解释一下或推荐给我有用的参考吗?

谢谢,

【问题讨论】:

    标签: machine-learning computer-vision neural-network caffe conv-neural-network


    【解决方案1】:

    首先,您应该将第二项的结果 (1/N^2 \sum_i D_i) 存储到局部变量中。正如你already know,这个总和可以使用caffe_cpu_dot来计算。

    所以你的代码可能看起来像:

    vector<Dtype> mult_data( diff_.count(), Dtype(1) );
    const Dtype* Di = diff_.cpu_data();
    Dtype const_sum = caffe_cpu_dot( diff_.count(), &mult_data[0], Di );
    Dtype N = diff_.count();
    const_sum /= N*N; // divide by N^2
    

    现在您可以遍历所有项目(假设 bottom[0]-&gt;count()==diff_.count()):

    Dtype* bottom_diff = bottom[0]->mutable_cpu_diff();
    for (int i=0; i<diff_.count(); i++) {
      bottom_diff[i] = 2*Di[i]/N - const_sum;
    }
    

    或者,如果您想要更多类似“blas”的东西:

    caffe_copy(diff_.count(), Di, bottom_diff);  // copy Di to bottom_diff
    caffe_scal(diff_.count(), Dtype(2.0/N), bottom_diff);  // bottom_diff is now (2/N)*Di
    caffe_add_scalar(diff_.count(), -const_sum, bottom_diff);  // subtract the second term from all the elements.
    

    【讨论】:

    • 完美的解决方案,非常感谢。让我在 caffe_scal(diff_.count(), 2.0/N, bottom_diff); 之前提一下。最好定义 Dtype alpha = 2.0/N;然后将其称为: caffe_scal(diff_.count(), alpha, bottom_diff);以防止任何类型不匹配。再次感谢您。
    • 只是一个小问题。当我使用 Dtype const_sum = caffe_cpu_dot( diff_.count(), &mult_data[0], Di );在 Backward_cpu 中效果很好,但是当我将其转换为 gpu 模式时: Dtype const_sum; caffe_gpu_dot(diff_.count(), &mult_data[0], Di, &const_sum);在 Backward_gpu 中,我得到了 CUBLAS_STATUS_MAPPING_ERROR。你知道为什么吗?
    • @Ali 转GPU的时候,是不是把所有的代码都放在了不同的.cu文件里?
    • 是的。我愿意。我应该转换 const Dtype* Di = diff_.cpu_data();进入 const Dtype* Di = diff_.gpu_data(); ?我不这么认为。
    猜你喜欢
    • 1970-01-01
    • 2015-07-24
    • 2010-10-24
    • 2015-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-24
    相关资源
    最近更新 更多