October 2019
Intermediate to advanced
366 pages
12h 4m
English
A variable is a mutable tensor that can be trained using an optimizer. For example, they can be the free variables that constitute the weights and biases of a neural network.
We will now create two variables, one uniformly initialized, and one initialized with constant values:
import tensorflow as tfimport numpy as np# variable initialized randomlyvar = tf.get_variable("first_variable", shape=[1,3], dtype=tf.float32)# variable initialized with constant valuesinit_val = np.array([4,5])var2 = tf.get_variable("second_variable", shape=[1,2], dtype=tf.int32, initializer=tf.constant_initializer(init_val))# create the sessionsess = tf.Session()# initialize all the variablessess.run(tf.global_variables_initializer())print(sess.run(var)) ...Read now
Unlock full access