Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zero initialiser for biases using get_variable in tensorflow

A code I'm modifying is using tf.get_variable for weight variables, and tf.Variable for bias initialisation. After some searching, it seems that get_variable should always be favoured due to its portability in regards to sharing. So I tried to change the bias variable to get_variable but can't seem to get it to work.

Original: tf.Variable(tf.zeros([128]), trainable=True, name="b1")

My attempt: tf.get_variable(name="b1", shape=[128], initializer=tf.zeros_initializer(shape=[128]))

I get an error saying that the shape should not be specified for constants. But removing the shape then throws an error for no arguments.

I'm very new to tf so I'm probably misunderstanding something fundamental here. Thanks for the help in advance :)

like image 844
Keir Simmons Avatar asked Jan 24 '17 06:01

Keir Simmons


People also ask

What does tf Get_variable do?

The function tf. get_variable() returns the existing variable with the same name if it exists, and creates the variable with the specified shape and initializer if it does not exist.

How do you initialize a parameter in TensorFlow?

To initialize a new variable from the value of another variable use the other variable's initialized_value() property. You can use the initialized value directly as the initial value for the new variable, or you can use it as any other tensor to compute a value for the new variable.

Is tf variable trainable?

To make this easier, the variable constructor supports a trainable=<bool> parameter. tf. GradientTape watches trainable variables by default: with tf.

How do you initialize a TensorFlow variable in a matrix?

First, remember that you can use the TensorFlow eye functionality to easily create a square identity matrix. We create a 5x5 identity matrix with a data type of float32 and assign it to the Python variable identity matrix. So we used tf. eye, give it a size of 5, and the data type is float32.


1 Answers

Following should work: tf.get_variable(name="b1", shape=[128], initializer=tf.zeros_initializer())

like image 81
user1454804 Avatar answered Oct 05 '22 23:10

user1454804