RELNOTES: This allows the use of '.' in variables (e.g. "hparams.parse('a.b=1.0'...
authorA. Unique TensorFlower <gardener@tensorflow.org>
Fri, 11 May 2018 18:04:33 +0000 (11:04 -0700)
committerTensorFlower Gardener <gardener@tensorflow.org>
Fri, 11 May 2018 18:10:59 +0000 (11:10 -0700)
NOTE: the use of '.' in variable names is now allowed, but it is not recommended.
PiperOrigin-RevId: 196278660

tensorflow/contrib/training/python/training/hparam.py
tensorflow/contrib/training/python/training/hparam_test.py

index f0418f0..3beb7bf 100644 (file)
@@ -34,7 +34,7 @@ from tensorflow.python.util import deprecation
 # where <rhs> is either a single token or [] enclosed list of tokens.
 # For example:  "var[1] = a" or "x = [1,2,3]"
 PARAM_RE = re.compile(r"""
-  (?P<name>[a-zA-Z][\w]*)      # variable name: "var" or "x"
+  (?P<name>[a-zA-Z][\w\.]*)      # variable name: "var" or "x"
   (\[\s*(?P<index>\d+)\s*\])?  # (optional) index: "1" or None
   \s*=\s*
   ((?P<val>[^,\[]*)            # single value: "a" or None
@@ -200,6 +200,13 @@ def parse_values(values, type_map):
   If a hyperparameter name in both an index assignment and scalar assignment,
   a ValueError is raised.  (e.g. 'a=[1,2,3],a[0] = 1').
 
+  The hyperparameter name may contain '.' symbols, which will result in an
+  attribute name that is only accessible through the getattr and setattr
+  functions.  (And must be first explicit added through add_hparam.)
+
+  WARNING: Use of '.' in your variable names is allowed, but is not well
+  supported and not recommended.
+
   The `value` in `name=value` must follows the syntax according to the
   type of the parameter:
 
index 11fd15b..660c97f 100644 (file)
@@ -118,6 +118,21 @@ class HParamsTest(test.TestCase):
     self.assertEqual('2.3"', hparams2.c_c)
     self.assertEqual('/a=b/c/d', hparams2.d)
 
+  def testWithPeriodInVariableName(self):
+    hparams = hparam.HParams()
+    hparams.add_hparam(name='a.b', value=0.0)
+    hparams.parse('a.b=1.0')
+    self.assertEqual(1.0, getattr(hparams, 'a.b'))
+    hparams.add_hparam(name='c.d', value=0.0)
+    with self.assertRaisesRegexp(ValueError, 'Could not parse'):
+      hparams.parse('c.d=abc')
+    hparams.add_hparam(name='e.f', value='')
+    hparams.parse('e.f=abc')
+    self.assertEqual('abc', getattr(hparams, 'e.f'))
+    hparams.add_hparam(name='d..', value=0.0)
+    hparams.parse('d..=10.0')
+    self.assertEqual(10.0, getattr(hparams, 'd..'))
+
   def testSetFromMap(self):
     hparams = hparam.HParams(a=1, b=2.0, c='tanh')
     hparams.override_from_dict({'a': -2, 'c': 'identity'})