Caffe源码解析—核函数
生活随笔
收集整理的這篇文章主要介紹了
Caffe源码解析—核函数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
forward_cpu_gemm
forward_cpu_bias
backward_cpu_bias
weight_cpu_gemm
backward_cpu_gemm
其余函數參考鏈接
forward_cpu_gemm
output = weights * input
weight: N x K? ?input: K x M? ?output: N x M
template <typename Dtype> void BaseConvolutionLayer<Dtype>::forward_cpu_gemm(const Dtype* input,const Dtype* weights, Dtype* output, bool skip_im2col) {const Dtype* col_buff = input;if (!is_1x1_) {if (!skip_im2col) {conv_im2col_cpu(input, col_buffer_.mutable_cpu_data());}col_buff = col_buffer_.cpu_data();}for (int g = 0; g < group_; ++g) {caffe_cpu_gemm<Dtype>(CblasNoTrans, CblasNoTrans, conv_out_channels_ /group_, conv_out_spatial_dim_, kernel_dim_ / group_,(Dtype)1., weights + weight_offset_ * g, col_buff + col_offset_ * g,(Dtype)0., output + output_offset_ * g);} }forward_cpu_bias
output = bias * bias_multiplier_
bias:N x 1
bias_multiplier_:1 x M的單位向量
template <typename Dtype> void BaseConvolutionLayer<Dtype>::forward_cpu_bias(Dtype* output,const Dtype* bias) {caffe_cpu_gemm<Dtype>(CblasNoTrans, CblasNoTrans, num_output_,height_out_ * width_out_, 1, (Dtype)1., bias, bias_multiplier_.cpu_data(),(Dtype)1., output); }backward_cpu_bias
template <typename Dtype> void BaseConvolutionLayer<Dtype>::backward_cpu_bias(Dtype* bias,const Dtype* input) {caffe_cpu_gemv<Dtype>(CblasNoTrans, num_output_, height_out_ * width_out_, 1.,input, bias_multiplier_.cpu_data(), 1., bias); } 計算關于bias的導數weight_cpu_gemm
template <typename Dtype> void BaseConvolutionLayer<Dtype>::weight_cpu_gemm(const Dtype* input,const Dtype* output, Dtype* weights) {const Dtype* col_buff = input;if (!is_1x1_) {conv_im2col_cpu(input, col_buffer_.mutable_cpu_data());col_buff = col_buffer_.cpu_data();}for (int g = 0; g < group_; ++g) {caffe_cpu_gemm<Dtype>(CblasNoTrans, CblasTrans, conv_out_channels_ / group_,kernel_dim_ / group_, conv_out_spatial_dim_,(Dtype)1., output + output_offset_ * g, col_buff + col_offset_ * g,(Dtype)1., weights + weight_offset_ * g);} }//計算關于weight的導數用于更新。backward_cpu_gemm
template <typename Dtype> void BaseConvolutionLayer<Dtype>::backward_cpu_gemm(const Dtype* output,const Dtype* weights, Dtype* input) {Dtype* col_buff = col_buffer_.mutable_cpu_data();if (is_1x1_) {col_buff = input;}for (int g = 0; g < group_; ++g) {caffe_cpu_gemm<Dtype>(CblasTrans, CblasNoTrans, kernel_dim_ / group_,conv_out_spatial_dim_, conv_out_channels_ / group_,(Dtype)1., weights + weight_offset_ * g, output + output_offset_ * g,(Dtype)0., col_buff + col_offset_ * g);}if (!is_1x1_) {conv_col2im_cpu(col_buff, input);}計算關于bottom data的導數以便傳給下一層其余函數參考鏈接
總結
以上是生活随笔為你收集整理的Caffe源码解析—核函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: InsightFace及其mxnet、t
- 下一篇: 高效神经网络的设计