| 143 | |
| 144 | template <typename Dtype> |
| 145 | void SGDSolver<Dtype>::Regularize(int param_id) { |
| 146 | const vector<Blob<Dtype>*>& net_params = this->net_->learnable_params(); |
| 147 | const vector<float>& net_params_weight_decay = |
| 148 | this->net_->params_weight_decay(); |
| 149 | Dtype weight_decay = this->param_.weight_decay(); |
| 150 | string regularization_type = this->param_.regularization_type(); |
| 151 | Dtype local_decay = weight_decay * net_params_weight_decay[param_id]; |
| 152 | switch (Caffe::mode()) { |
| 153 | case Caffe::CPU: { |
| 154 | if (local_decay) { |
| 155 | if (regularization_type == "L2") { |
| 156 | // add weight decay |
| 157 | caffe_axpy(net_params[param_id]->count(), |
| 158 | local_decay, |
| 159 | net_params[param_id]->cpu_data(), |
| 160 | net_params[param_id]->mutable_cpu_diff()); |
| 161 | } else if (regularization_type == "L1") { |
| 162 | caffe_cpu_sign(net_params[param_id]->count(), |
| 163 | net_params[param_id]->cpu_data(), |
| 164 | temp_[param_id]->mutable_cpu_data()); |
| 165 | caffe_axpy(net_params[param_id]->count(), |
| 166 | local_decay, |
| 167 | temp_[param_id]->cpu_data(), |
| 168 | net_params[param_id]->mutable_cpu_diff()); |
| 169 | } else { |
| 170 | LOG(FATAL) << "Unknown regularization type: " << regularization_type; |
| 171 | } |
| 172 | } |
| 173 | break; |
| 174 | } |
| 175 | case Caffe::GPU: { |
| 176 | #ifndef CPU_ONLY |
| 177 | if (local_decay) { |
| 178 | if (regularization_type == "L2") { |
| 179 | // add weight decay |
| 180 | caffe_gpu_axpy(net_params[param_id]->count(), |
| 181 | local_decay, |
| 182 | net_params[param_id]->gpu_data(), |
| 183 | net_params[param_id]->mutable_gpu_diff()); |
| 184 | } else if (regularization_type == "L1") { |
| 185 | caffe_gpu_sign(net_params[param_id]->count(), |
| 186 | net_params[param_id]->gpu_data(), |
| 187 | temp_[param_id]->mutable_gpu_data()); |
| 188 | caffe_gpu_axpy(net_params[param_id]->count(), |
| 189 | local_decay, |
| 190 | temp_[param_id]->gpu_data(), |
| 191 | net_params[param_id]->mutable_gpu_diff()); |
| 192 | } else { |
| 193 | LOG(FATAL) << "Unknown regularization type: " << regularization_type; |
| 194 | } |
| 195 | } |
| 196 | #else |
| 197 | NO_GPU; |
| 198 | #endif |
| 199 | break; |
| 200 | } |
| 201 | default: |
| 202 | LOG(FATAL) << "Unknown caffe mode: " << Caffe::mode(); |
nothing calls this directly
no test coverage detected