caffe之deconv

卷积与反卷积的直观感受:
卷积: 这里写图片描述 反卷积:这里写图片描述
特别说明,此处分析的反卷积主要针对 caffe中DeconvolutionLayer类别来说。

反向传播分析,参考博客:
https://grzegorzgwardys.wordpress.com/2016/04/22/8/
http://www.jefkine.com/general/2016/09/05/backpropagation-in-convolutional-neural-networks/
http://www.cnblogs.com/tornadomeet/p/3468450.html
http://blog.youkuaiyun.com/ck1798333105/article/details/52369122
http://www.cnblogs.com/pinard/p/6494810.html

反向传播结论: L层局部梯度 δ =L+1层局部梯度*(卷积)核的转秩
卷积核梯度 ▽W=输入*(卷积)本层局部梯度

卷积及反向传播

要想理解反卷积就要首先了解卷积的反向传播方式,因为他们之间有着千丝万缕的联系。

卷积之反向传播

见代码如下:

/////////////////////////////////////////////////////////////////////////
//                                 正向传播
/////////////////////////////////////////////////////////////////////////
template <typename Dtype>
void ConvolutionLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top) {
  const Dtype* weight = this->blobs_[0]->cpu_data();
  for (int i = 0; i < bottom.size(); ++i) {
    const Dtype* bottom_data = bottom[i]->cpu_data();
    Dtype* top_data = top[i]->mutable_cpu_data();
    for (int n = 0; n < this->num_; ++n) {
      ///forward_cpu_gemm 实现卷积运算
      //  bottom_data*weight=top_data
      this->forward_cpu_gemm(bottom_data + n * this->bottom_dim_, weight,
          top_data + n * this->top_dim_);
      if (this->bias_term_) {
        const Dtype* bias = this->blobs_[1]->cpu_data();
        this->forward_cpu_bias(top_data + n * this->top_dim_, bias);
      }
    }
  }
}
/*****************************************************************
                        反向传播
******************************************************************/
template <typename Dtype>
void ConvolutionLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
      const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {
  const Dtype* weight = this->blobs_[0]->cpu_data();
  Dtype* weight_diff = this->blobs_[0]->mutable_cpu_diff();
  for (int i = 0; i < top.size(); ++i) {
    const Dtype* top_diff = top[i]->cpu_diff();
    const Dtype* bottom_data = bottom[i]->cpu_data();
    Dtype* bottom_diff = bottom[i]->mutable_cpu_diff();
    // Bias gradient, if necessary.
    if (this->bias_term_ && this->param_propagate_down_[1]) {
      Dtype* bias_diff = this->blobs_[1]->mutable_cpu_diff();
      for (int n = 0; n < this->num_; ++n) {
      //  计算▽b,偏移量梯度
        this->backward_cpu_bias(bias_diff, top_diff + n * this->top_dim_);
      }
    }
    if (this->param_propagate_down_[0] || propagate_down[i]) {
      for (int n = 0; n < this->num_; ++n) {
        // gradient w.r.t. weight. Note that we will accumulate diffs.
        if (this->param_propagate_down_[0]) {     
          this->weight_cpu_gemm(bottom_data + n * this->bottom_dim_,        //计算 ▽W ,权重梯度
              top_diff + n * this->top_dim_, weight_diff);
        }
        // gradient w.r.t. bottom data, if necessary.
        if (propagate_down[i]) {
  //// 重点,backward_cpu_gemm 实现矩阵局部梯度域反向计算
  //// top_diff*weight^T(转制)=bottom_diff 实质上还是卷积运算
          this->backward_cpu_gemm(top_diff + n * this->top_dim_, weight,  //计算局部梯度δ
              bottom_diff + n * this->bottom_dim_);
        }
      }
    }
  }
}

这里写图片描述

从convlayer 与 deconvlayer 对比来看,实现的功能是互逆的。
特别是两者在前向运算与后向计算局部梯度 相互对偶(使用同样的函数),而偏移量的计算方式一致。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值