如何对内积层的权重求平方?

How to square weights in inner product layer?

我开始用 Caffe 和 运行 它很好。我需要在 inner product layer 中对权重进行平方。 Forward_cpu函数表示weight,但是不知道怎么平方

forward_cpu函数定义如下:

template <typename Dtype>
void InnerProductLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
    const vector<Blob<Dtype>*>& top) {
  const Dtype* bottom_data = bottom[0]->cpu_data();
  Dtype* top_data = top[0]->mutable_cpu_data();
  const Dtype* weight = this->blobs_[0]->cpu_data();
  Dtype* sqr_weight;
  caffe_sqr<Dtype>(this->blobs_[0]->count(), weight, sqr_weight);
  caffe_cpu_gemm<Dtype>(CblasNoTrans, transpose_ ? CblasNoTrans : CblasTrans,
      M_, N_, K_, (Dtype)1.,
      bottom_data, weight, (Dtype)0., top_data);
  if (bias_term_) {
    caffe_cpu_gemm<Dtype>(CblasNoTrans, CblasNoTrans, M_, N_, 1, (Dtype)1.,
        bias_multiplier_.cpu_data(),
        this->blobs_[1]->cpu_data(), (Dtype)1., top_data);
  }
}

注意我用的是caffe_sqr,但是caffe_sqr<Dtype>(weight.count(), weights, new_weights);returns出错了。当我制作新图层时,警告是:

warning: ‘sqr_weight’ is used uninitialized in this function [-Wuninitialized]
 caffe_sqr<Dtype>(this->blobs_[0]->count(), weight, sqr_weight);

训练我的模型后,错误是:

F1229 20:00:38.622575  5272 mkl_alternate.hpp:34] Check failed: y 
Check failure stack trace: 
@     0x7f4f97e675cd  google::LogMessage::Fail()
@     0x7f4f97e69433  google::LogMessage::SendToLog()
@     0x7f4f97e6715b  google::LogMessage::Flush()
@     0x7f4f97e69e1e  google::LogMessageFatal::~LogMessageFatal()
@     0x7f4f98338760  vSqr<>()
@     0x7f4f982eb45a  caffe::PositiveInnerProductLayer<>::Forward_cpu()
@     0x7f4f9830b0d3  caffe::Net<>::ForwardFromTo()
@     0x7f4f9830b347  caffe::Net<>::ForwardPrefilled()
@     0x7f4f981e075f  caffe::Solver<>::Test()
@     0x7f4f981e119e  caffe::Solver<>::TestAll()
@     0x7f4f981e12eb  caffe::Solver<>::Step()
@     0x7f4f981e1f85  caffe::Solver<>::Solve()
@           0x40aafb  train()
@           0x406f48  main
@     0x7f4f970f6830  __libc_start_main
@           0x407609  _start
@              (nil)  (unknown)

注意weight被定义为指向Dtype的指针,指针没有count()方法。

您需要权重 blob 的 count()

this->blobs_[0]->count()