TensorFlow tf.get_variable() can create a new or return an existing tensor. We can learn how to use it in this tutorial:
However, you may encounter TypeError: must be str, not list when using it. In this tutorial, we will introduce you how to fix this type error.
Why does this type error occur?
The main reason is the parameters are wrong.
tf.get_variable() is defined as:
tf.get_variable( name, shape=None, dtype=None, initializer=None, regularizer=None, trainable=True, collections=None, caching_device=None, partitioner=None, validate_shape=True, use_resource=None, custom_getter=None, constraint=None )
The first parameter is the name, which is a python string. the sencond is the shape, it can be a python list or tuple.
w = tf.get_variable(shape, name, dtype = tf.float32)
You will get TypeError: must be str, not list.
How to fix this type error.
Make the first parameter is a string.
You can fix the code above as following:
w = tf.get_variable(name, shape, dtype = tf.float32)
Then you will find this error is fixed.