介绍
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