如何在内积层中加权?

时间:2016-12-26 14:19:10

标签: deep-learning conv-neural-network caffe inner-product

我从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);会返回错误。当我创建新图层时,警告是:

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)

1 个答案:

答案 0 :(得分:1)

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

您需要权重blob的count()

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