| 8 | |
| 9 | template <typename Dtype> |
| 10 | void EmbedLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom, |
| 11 | const vector<Blob<Dtype>*>& top) { |
| 12 | N_ = this->layer_param_.embed_param().num_output(); |
| 13 | CHECK_GT(N_, 0) << "EmbedLayer num_output must be positive."; |
| 14 | K_ = this->layer_param_.embed_param().input_dim(); |
| 15 | CHECK_GT(K_, 0) << "EmbedLayer input_dim must be positive."; |
| 16 | bias_term_ = this->layer_param_.embed_param().bias_term(); |
| 17 | // Check if we need to set up the weights |
| 18 | if (this->blobs_.size() > 0) { |
| 19 | LOG(INFO) << "Skipping parameter initialization"; |
| 20 | } else { |
| 21 | if (bias_term_) { |
| 22 | this->blobs_.resize(2); |
| 23 | } else { |
| 24 | this->blobs_.resize(1); |
| 25 | } |
| 26 | // Initialize the weights -- |
| 27 | // transposed from InnerProductLayer for spatial locality. |
| 28 | vector<int> weight_shape(2); |
| 29 | weight_shape[0] = K_; |
| 30 | weight_shape[1] = N_; |
| 31 | this->blobs_[0].reset(new Blob<Dtype>(weight_shape)); |
| 32 | // fill the weights |
| 33 | shared_ptr<Filler<Dtype> > weight_filler(GetFiller<Dtype>( |
| 34 | this->layer_param_.embed_param().weight_filler())); |
| 35 | weight_filler->Fill(this->blobs_[0].get()); |
| 36 | // If necessary, initialize and fill the bias term |
| 37 | if (bias_term_) { |
| 38 | vector<int> bias_shape(1, N_); |
| 39 | this->blobs_[1].reset(new Blob<Dtype>(bias_shape)); |
| 40 | shared_ptr<Filler<Dtype> > bias_filler(GetFiller<Dtype>( |
| 41 | this->layer_param_.embed_param().bias_filler())); |
| 42 | bias_filler->Fill(this->blobs_[1].get()); |
| 43 | } |
| 44 | } // parameter initialization |
| 45 | this->param_propagate_down_.resize(this->blobs_.size(), true); |
| 46 | } |
| 47 | |
| 48 | template <typename Dtype> |
| 49 | void EmbedLayer<Dtype>::Reshape(const vector<Blob<Dtype>*>& bottom, |