| 8 | |
| 9 | template <typename Dtype> |
| 10 | void InnerProductLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom, |
| 11 | const vector<Blob<Dtype>*>& top) { |
| 12 | const int num_output = this->layer_param_.inner_product_param().num_output(); |
| 13 | bias_term_ = this->layer_param_.inner_product_param().bias_term(); |
| 14 | transpose_ = this->layer_param_.inner_product_param().transpose(); |
| 15 | N_ = num_output; |
| 16 | const int axis = bottom[0]->CanonicalAxisIndex( |
| 17 | this->layer_param_.inner_product_param().axis()); |
| 18 | // Dimensions starting from "axis" are "flattened" into a single |
| 19 | // length K_ vector. For example, if bottom[0]'s shape is (N, C, H, W), |
| 20 | // and axis == 1, N inner products with dimension CHW are performed. |
| 21 | K_ = bottom[0]->count(axis); |
| 22 | // Check if we need to set up the weights |
| 23 | if (this->blobs_.size() > 0) { |
| 24 | LOG(INFO) << "Skipping parameter initialization"; |
| 25 | } else { |
| 26 | if (bias_term_) { |
| 27 | this->blobs_.resize(2); |
| 28 | } else { |
| 29 | this->blobs_.resize(1); |
| 30 | } |
| 31 | // Initialize the weights |
| 32 | vector<int> weight_shape(2); |
| 33 | if (transpose_) { |
| 34 | weight_shape[0] = K_; |
| 35 | weight_shape[1] = N_; |
| 36 | } else { |
| 37 | weight_shape[0] = N_; |
| 38 | weight_shape[1] = K_; |
| 39 | } |
| 40 | this->blobs_[0].reset(new Blob<Dtype>(weight_shape)); |
| 41 | // fill the weights |
| 42 | shared_ptr<Filler<Dtype> > weight_filler(GetFiller<Dtype>( |
| 43 | this->layer_param_.inner_product_param().weight_filler())); |
| 44 | weight_filler->Fill(this->blobs_[0].get()); |
| 45 | // If necessary, intiialize and fill the bias term |
| 46 | if (bias_term_) { |
| 47 | vector<int> bias_shape(1, N_); |
| 48 | this->blobs_[1].reset(new Blob<Dtype>(bias_shape)); |
| 49 | shared_ptr<Filler<Dtype> > bias_filler(GetFiller<Dtype>( |
| 50 | this->layer_param_.inner_product_param().bias_filler())); |
| 51 | bias_filler->Fill(this->blobs_[1].get()); |
| 52 | } |
| 53 | } // parameter initialization |
| 54 | this->param_propagate_down_.resize(this->blobs_.size(), true); |
| 55 | } |
| 56 | |
| 57 | template <typename Dtype> |
| 58 | void InnerProductLayer<Dtype>::Reshape(const vector<Blob<Dtype>*>& bottom, |