MCPcopy Create free account
hub / github.com/BVLC/caffe / GetObjAndGradient

Method GetObjAndGradient

include/caffe/test/test_gradient_check_util.hpp:233–262  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

231
232template <typename Dtype>
233Dtype GradientChecker<Dtype>::GetObjAndGradient(const Layer<Dtype>& layer,
234 const vector<Blob<Dtype>*>& top, int top_id, int top_data_id) {
235 Dtype loss = 0;
236 if (top_id < 0) {
237 // the loss will be half of the sum of squares of all outputs
238 for (int i = 0; i < top.size(); ++i) {
239 Blob<Dtype>* top_blob = top[i];
240 const Dtype* top_blob_data = top_blob->cpu_data();
241 Dtype* top_blob_diff = top_blob->mutable_cpu_diff();
242 int count = top_blob->count();
243 for (int j = 0; j < count; ++j) {
244 loss += top_blob_data[j] * top_blob_data[j];
245 }
246 // set the diff: simply the data.
247 caffe_copy(top_blob->count(), top_blob_data, top_blob_diff);
248 }
249 loss /= 2.;
250 } else {
251 // the loss will be the top_data_id-th element in the top_id-th blob.
252 for (int i = 0; i < top.size(); ++i) {
253 Blob<Dtype>* top_blob = top[i];
254 Dtype* top_blob_diff = top_blob->mutable_cpu_diff();
255 caffe_set(top_blob->count(), Dtype(0), top_blob_diff);
256 }
257 const Dtype loss_weight = 2;
258 loss = top[top_id]->cpu_data()[top_data_id] * loss_weight;
259 top[top_id]->mutable_cpu_diff()[top_data_id] = loss_weight;
260 }
261 return loss;
262}
263
264} // namespace caffe
265

Callers

nothing calls this directly

Calls 6

caffe_copyFunction · 0.85
caffe_setFunction · 0.85
mutable_cpu_diffMethod · 0.80
countMethod · 0.80
sizeMethod · 0.45
cpu_dataMethod · 0.45

Tested by

no test coverage detected