For any queries you can reach us at / WhatsApp us: +919158876092

TensorFlow Variables

TensorFlow Variables

To create variables in TensorFlow we use tensorflow.get_variable()


tf.get_variable(name = "", values, dtype, initializer)
  • `name = ""`: Name of the variable
  • `values`: Dimension of the tensor
  • `dtype`: Type of data. Optional
  • `initializer`: How to initialize the tensor. Optional

If initializer is specified, there is no need to include the `values` as the shape of `initializer` is used.

import tensorflow as tf  
# Create a Variable  
var = tf.get_variable("var", [1, 2])  
#following initializes the variable with a initial/default value  
var_init_1 = tf.get_variable("var_init_1", [1, 2], dtype=tf.int32,  initializer=tf.zeros_initializer)  
#Initializes the first value of the tensor equals to tensor_const  
tensor_const = tf.constant([[10, 20],[30, 40]])  
var_init_2 = tf.get_variable("var_init_2", dtype=tf.int32,  initializer=tensor_const)  

The output will be <tf.Variable 'var:0' shape=(1, 2) dtype=float32_ref> <tf.Variable 'var_init_1:0' shape=(1, 2) dtype=int32_ref> <tf.Variable 'var_init_2:0' shape=(2, 2) dtype=int32_ref>