# 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
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:
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'})