Fix TensorFlow tf.get_variable() TypeError: must be str, not list – TensorFlow Tutorial

By | July 22, 2020

TensorFlow tf.get_variable() can create a new or return an existing tensor. We can learn how to use it in this tutorial:

Understand tf.get_variable(): A Beginner Guide

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.

For example:

w = tf.get_variable(shape, name, dtype = tf.float32)

You will get TypeError: must be str, not list.

fix tensorflow tf.get_variable() 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.

Leave a Reply

Your email address will not be published. Required fields are marked *