caffe源码理解之inner_product_layer

本文详细解析了Caffe框架中的inner_product_layer,包括成员变量和成员函数,重点介绍了Reshape、LayerSetUp、Forward_cpu和Backward_cpu四个关键函数。在Forward_cpu中,实现了y=wx+b的计算,而Backward_cpu则用于更新权重和偏置,讲解了如何进行反向传播计算。Caffe的全连接层与激活函数分开处理,提供了更大的灵活性。
摘要由CSDN通过智能技术生成

介绍

inner_product_layer也即全连接层,如下示意图,每个输出都连接到所有的输入。
图一

正文

成员变量

首先介绍一下几个成员变量

protected:
  int M_;//样本数量
  int K_;//单个输入特征长度
  int N_;//输出神经元数量
  bool bias_term_;//是否添加偏置,上图中的(+1)。
  Blob<Dtype> bias_multiplier_;//偏置的乘子

成员函数

其中的构造等成员函数基本上继承父类的,不详述,主要介绍LayerSetUp,Forward_cpu,Backward_cpu,Reshape这四个成员函数。

Reshape

template <typename Dtype>
void InnerProductLayer<Dtype>::Reshape(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top) {
  // Figure out the dimensions
  const int axis = bottom[0]->CanonicalAxisIndex(
      this->layer_param_.inner_product_param().axis());
  const int new_K = bottom[0]->count(axis);
  /**这里解释一下,blob的CanonicalAxisIndex是为了标准化维度索引的输入,将一些非法维度输入转化为合法输入。
  *blob的count(int)是统计从某个维度开始,到结尾的总个数。这里第一个维度表示的是样本个数,也即是M_,与全连接层是独立的,其后面的是表示输入特征的个数。
  */
  CHECK_EQ(K_, new_K)
      << "Input size incompatible with inner product parameters.";
  // The first "axis" dimensions are independent inner products; the total
  // number of these is M_, the product over these dimensions.
  M_ = bottom[0]->count(0, axis);
  // The top shape will be the bottom shape with the flattened axes dropped,
  // and replaced by a single axis wit
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值