How to square weights in inner product layer?

242 views Asked by At

I started with Caffe and ran it well. I need to square weights in inner product layer. The Forward_cpu function expresses the weight, but I don't know how to square it.

forward_cpu function is defined as follow:

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);
  }
}

Note that I use caffe_sqr, but caffe_sqr<Dtype>(weight.count(), weights, new_weights); returns an error. When I make my new layer, the warning is:

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

and after training my model, error is:

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

There are 1 answers

7
Shai On BEST ANSWER

Note that weight is defined as a pointer to Dtype, pointers do not have count() method.

You need the count() of the weight blob:

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