wooway777
wooway777

Reputation: 93

TypeError: __init__() got an unexpected keyword argument 'shape'

I am new to Tensorflow and I met an error while trying to run some sample codes.

import tensorflow as tf

g1 = tf.Graph()
with g1.as_default():
    v = tf.get_variable("v", initializer=tf.zeros_initializer(shape=[1]))

Running the code above gives the error: TypeError: __init__() got an unexpected keyword argument 'shape'.

The comment below says that tf.zeros_initializer does not accept 'shape' argument according to the documentation. I tried

v = tf.get_variable("v", initializer=tf.zeros_initializer())

and it says ValueError: Shape of a new variable (v) must be fully defined, but instead was .

So, what kind of argument/expression should I use to define the shape without causing a type error?

I cannot find how to solve it online. Please help. Thank you

Upvotes: 1

Views: 15413

Answers (2)

Omar Cusma Fait
Omar Cusma Fait

Reputation: 321

In version 2.4.1 I use tf.constant() instead

Upvotes: 1

wooway777
wooway777

Reputation: 93

It appears that the book I'm using is a bit out of date. The following code appears to work so far.

v = tf.get_variable("v", shape=[1], initializer=tf.zeros_initializer)

I will check if it actually works with more code added later.

Upvotes: 4

Related Questions