caffe撸码:euclidean.cpp

namespace caffe {

template <typename Dtype>
void EuclideanLossNormLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
    const vector<Blob<Dtype>*>& top) {
  int count = bottom[0]->count();//count: count其实等于num*channels*height*width,也就是整个Blob元素的数量,在sacnn中num=1,channels=1, height和width分别为输出特征图的宽和高。

  caffe_gpu_sub(
      count,
      bottom[0]->gpu_data(),//网络的输出值
      bottom[1]->gpu_data(),//标签值
      diff_.mutable_gpu_data());//存储bottom[0]-bottom[1]
  Dtype dot;
// LOG(INFO) << "estedmated count" << bottom[0]->cpu_data()[0];
 // LOG(INFO) << "groundtruth count" << bottom[1]->cpu_data()[0];
 caffe_gpu_set(count, Dtype(1), diffdiv_.mutable_gpu_data());//注释1 
 caffe_gpu_axpy(count, Dtype(1), bottom[1]->gpu_data(), diffdiv_.mutable_gpu_data());//注释2 Y[i]+1 标签值+1,即(bottom[1]->gpu_data())+1
   
 caffe_gpu_div(count, diff_.gpu_data(), diffdiv_.gpu_data(), diff_.mutable_gpu_data());
//注释3 diff_.gpu_data=diff_.gpu_data()/diffdiv_.gpu_data(),其中diff_.gpu_data存储bottom[0]-bottom[1],diffdiv_.gpu_data()存储标签值+1
 caffe_gpu_dot(count, diff_.gpu_data(), diff_.gpu_data(), &dot);//计算两个输入向量的点乘
 //dot = diff_.asum_data();   
 Dtype loss = dot / bottom[0]->num() / Dtype(2);
  top[0]->mutable_cpu_data()[0] = loss;
}

template <typename Dtype>
void EuclideanLossNormLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
    const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {
  for (int i = 0; i < 2; ++i) {
    if (propagate_down[i]) {
      const Dtype sign = (i == 0) ? 1 : -1;
      const Dtype alpha = sign * top[0]->cpu_diff()[0] / bottom[i]->num();
      caffe_gpu_axpby(
          bottom[i]->count(),              // count
          alpha,                              // alpha
          diff_.gpu_data(),                   // a
          Dtype(0),                           // beta
          bottom[i]->mutable_gpu_diff());  // b
          caffe_gpu_div(bottom[i]->count(), bottom[i]->gpu_data(), diffdiv_.gpu_data(), bottom[i]->mutable_gpu_data());
    }
  }
}

INSTANTIATE_LAYER_GPU_FUNCS(EuclideanLossNormLayer);

}  // namespace caffe
/**
注释:
1.caffe_gpu_set函数:
template <typename Dtype>
void caffe_gpu_set(const int N, const Dtype alpha, Dtype* Y) {
  if (alpha == 0) {
    CUDA_CHECK(cudaMemset(Y, 0, sizeof(Dtype) * N));  // NOLINT(caffe/alt_fn)
    return;
  }
  // NOLINT_NEXT_LINE(whitespace/operators)
  set_kernel<Dtype><<<CAFFE_GET_BLOCKS(N), CAFFE_CUDA_NUM_THREADS>>>(
      N, alpha, Y);
}

template <typename Dtype>
__global__ void set_kernel(const int n, const Dtype alpha, Dtype* y) {
  CUDA_KERNEL_LOOP(index, n) {
    y[index] = alpha;
  }
}
功能:
用常数alpha对Y进行初始化:
Y[i]=1;也就是diffdiv_.mutable_gpu_data()的值
2.
template <>
void caffe_axpy<float>(const int N, const float alpha, const float* X,
    float* Y) { cblas_saxpy(N, alpha, X, 1, Y, 1); }
功能:
Y=alpha*X+Y  此处alpha=1,Y[i]=1
3.
void caffe_gpu_div<double>(const int N, const double* a,
    const double* b, double* y) {
  // NOLINT_NEXT_LINE(whitespace/operators)
  div_kernel<double><<<CAFFE_GET_BLOCKS(N), CAFFE_CUDA_NUM_THREADS>>>(
      N, a, b, y);
功能:
y[i]=a[i]\b[i]
4.
template <>
void caffe_cpu_axpby<float>(const int N, const float alpha, const float* X,
                            const float beta, float* Y) {
  cblas_saxpby(N, alpha, X, 1, beta, Y, 1);
}

功能:Y= alpha*X+beta*Y

**/

持续撸码↖(^ω^)↗

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值