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 :)
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.
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.
To make this easier, the variable constructor supports a trainable=<bool> parameter. tf. GradientTape watches trainable variables by default: with tf.
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.
Following should work:
tf.get_variable(name="b1", shape=[128], initializer=tf.zeros_initializer())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With