7

When choosing a name for a tensorflow variable, this is ok:

>> tf.Variable(2, name='a')

<tf.Variable 'finegrained_1/decoder/unreadable/a:0' shape=() dtype=int32_ref>

However, this is not:

>> tf.Variable(2, name='a:b')

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
~/mm/appy-finegrained/sign_classification/model_finegrained.py in <module>()
----> 1 tf.Variable(2, name='a:b')

~/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/variables.py in __init__(self, initial_value, trainable, collections, validate_shape, caching_device, name, variable_def, dtype, expected_shape, import_scope, constraint)
    211           dtype=dtype,
    212           expected_shape=expected_shape,
--> 213           constraint=constraint)
    214 
    215   def __repr__(self):

~/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/variables.py in _init_from_args(self, initial_value, trainable, collections, validate_shape, caching_device, name, dtype, expected_shape, constraint)
    287     with ops.control_dependencies(None):
    288       with ops.name_scope(name, "Variable", [] if init_from_fn else
--> 289                           [initial_value]) as name:
    290 
    291         if init_from_fn:

~/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in __enter__(self)
   4930       self._g_manager.__enter__()
   4931       self._name_scope = g.name_scope(self._name)
-> 4932       return self._name_scope.__enter__()
   4933 
   4934   def __exit__(self, type_arg, value_arg, traceback_arg):

~/anaconda3/lib/python3.6/contextlib.py in __enter__(self)
     79     def __enter__(self):
     80         try:
---> 81             return next(self.gen)
     82         except StopIteration:
     83             raise RuntimeError("generator didn't yield") from None

~/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in name_scope(self, name)
   3512         # (viz. '-', '\', '/', and '_').
   3513         if not _VALID_SCOPE_NAME_REGEX.match(name):
-> 3514           raise ValueError("'%s' is not a valid scope name" % name)
   3515       else:
   3516         # Scopes created in the root must match the more restrictive

ValueError: 'a:b' is not a valid scope name

Putting a / in a name is allowed, but it probably breaks some scoping things:

>> tf.Variable(2, name='a/b')

<tf.Variable 'finegrained_1/decoder/unreadable/a/b:0' shape=() dtype=int32_ref>

Are there any defined rules as to what character set is allowed in variable names?

And (with regards to the a/b name) are there any extra guidelines for what shouldn't be used?

Maxim
  • 52,561
  • 27
  • 155
  • 209
Bill Cheatham
  • 11,396
  • 17
  • 69
  • 104
  • You can always play it safe and stick to `a-Z`, `0-9` and `_`. – byxor Mar 12 '18 at 14:40
  • The `:N` suffix identifies a specific _tensor_ in the node whose name you're specifying. That's why you can't have `:` in the name. For all those occasions where you need to specify a tensor name, when no suffix is specified, `:0` is implied. So, for example `get_tensor_by_name('a')` and `get_tensor_by_name('a:0')` both will return the first tensor of the output of node `a`. – GPhilo Mar 12 '18 at 14:42

2 Answers2

8

As for ops naming, see tf.Operation documentation:

NOTE: This constructor validates the name of the Operation (passed as node_def.name). Valid Operation names match the following regular expression:

[A-Za-z0-9.][A-Za-z0-9_.\\-/]*

Each variable is translated into a number of ops, and its name is put into the scope, which naming rules are very similar (see python/framework/ops.py):

_VALID_OP_NAME_REGEX = re.compile("^[A-Za-z0-9.][A-Za-z0-9_.\\-/]*$")
_VALID_SCOPE_NAME_REGEX = re.compile("^[A-Za-z0-9_.\\-/]*$")

Naturally, '/' and ':' are not allowed in the name, since they are used for internal tensorflow purposes:

  • '/' is a separator of nested scopes (the regex allows it, but this means using several scopes in a single declaration);
  • ':' is a separator of tensor name and output index (see this question for details).
Maxim
  • 52,561
  • 27
  • 155
  • 209
0

You can give your ops names with capital letters but you should absolutely not do it. Here's a nice little failure case for you tested in TF 2.8 (yes, the model is nonsense):

input1 = keras.Input(shape=2, name='a')
input2 = keras.Input(shape=2, name='A')
outputs = tf.keras.layers.Average()([input1, input2])

dummy = keras.Model(inputs=[input1, input2], outputs=outputs)
dummy.summary()

dummy.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

dummy.save("dummy.savedmodel")

Summary works fine, compile works fine, fitting the model is probably fine(?) but you can't save it. Because something with absl converts to lowercase and then you can get collisions of names.

Unfortunately, I can't find the rules for Tensorflow naming (if the TF developers can't either then that might explain this weird behaviour). But I would stick to [a-z0-9_]. I tested length up to about 17000 character long names and that seemed to work fine but keep in mind that other tools in your pipeline may have other limits, e.g. a column name in PostgreSQL is usually limited to 63 characters.

grofte
  • 1,839
  • 1
  • 16
  • 15