make gradient checker's kink use feature absolute value
authorJonathan L Long <jonlong@cs.berkeley.edu>
Fri, 25 Apr 2014 23:38:48 +0000 (16:38 -0700)
committerJonathan L Long <jonlong@cs.berkeley.edu>
Fri, 25 Apr 2014 23:51:50 +0000 (16:51 -0700)
In theory, layer functions could be nonsmooth anywhere; in all cases in
use so far, they are nonsmooth at either zero or +1 and -1. In the
future, it might be necessary to generalize the kink mechanism beyond
this stopgap measure.

src/caffe/test/test_gradient_check_util.hpp

index da54a96..bcf0397 100644 (file)
@@ -22,6 +22,9 @@ namespace caffe {
 template <typename Dtype>
 class GradientChecker {
  public:
+  // kink and kink_range specify an ignored nonsmooth region of the form
+  // kink - kink_range <= |feature value| <= kink + kink_range,
+  // which accounts for all nonsmoothness in use by caffe
   GradientChecker(const Dtype stepsize, const Dtype threshold,
       const unsigned int seed = 1701, const Dtype kink = 0.,
       const Dtype kink_range = -1)
@@ -151,7 +154,8 @@ void GradientChecker<Dtype>::CheckGradientSingle(Layer<Dtype>* layer,
       Dtype feature = current_blob->cpu_data()[feat_id];
       // LOG(ERROR) << "debug: " << current_blob->cpu_data()[feat_id] << " "
       //     << current_blob->cpu_diff()[feat_id];
-      if (kink_ - kink_range_ > feature || feature > kink_ + kink_range_) {
+      if (kink_ - kink_range_ > fabs(feature)
+          || fabs(feature) > kink_ + kink_range_) {
         // We check relative accuracy, but for too small values, we threshold
         // the scale factor by 1.
         Dtype scale = max(