euclidean loss

个人感觉相当于L2范式开平方,也相当于针对两个向量的欧氏距离开平方

说的更直白点就是两个向量对应位置相减得到每个位置的差,然后把每个位置的差开平方再相加

前向传播cpp代码:


template <typename Dtype>
void EuclideanLossLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
    const vector<Blob<Dtype>*>& top) {
  int count = bottom[0]->count();
  caffe_sub(
      count,
      bottom[0]->cpu_data(),
      bottom[1]->cpu_data(),
      diff_.mutable_cpu_data());
  Dtype dot = caffe_cpu_dot(count, diff_.cpu_data(), diff_.cpu_data());
  Dtype loss = dot / bottom[0]->num() / Dtype(2);
  top[0]->mutable_cpu_data()[0] = loss;
}

注意:caffe_cpu_dot做的是点积,点积对应点相乘后还要把所有这些乘积结果相加,不只是做乘积

将bottom0和bottom1按照对应位置相减赋值给diff_这个blob,对这个blob中所有数字开平方相加起来就是loss

猜你喜欢

转载自www.cnblogs.com/ymjyqsx/p/9221180.html