How to create and initialize variables in TensorFlow?
In TensorFlow, we can create variables by calling tf.Variable, the first parameter sets the variable's initial value and second optional parameter defines the name of variable. TensorFlow have global_variables_initializer() functions that create operations which initialize variables.
## # TensorFlow program with example of create and initialize variables. from __future__ import absolute_import from __future__ import division from __future__ import print_function import tensorflow as tf tensorX = tf.constant([1.5, 2.5, 3.5]) variableA = tf.Variable(tensorX, name="variableA") variableB = tf.Variable([10, 20.5, 30.5], name="variableB") variableC = tf.Variable(["US", "UK"], name="variableC") variableD = tf.Variable([variableA, variableB], name="variableD") sess = tf.Session() sess.run(tf.global_variables_initializer()) print(sess.run(variableA)) print(sess.run(variableB)) print(sess.run(variableC)) print(sess.run(variableD))
Sample output of above program.
C:\tf\examples\chapter6>python example1.py
[1.5 2.5 3.5]
[10. 20.5 30.5]
[b'US' b'UK']
[[ 1.5 2.5 3.5]
[10. 20.5 30.5]]
C:\tf\examples\chapter6>
[1.5 2.5 3.5]
[10. 20.5 30.5]
[b'US' b'UK']
[[ 1.5 2.5 3.5]
[10. 20.5 30.5]]
C:\tf\examples\chapter6>
2018-07-28T12:11:40+05:30
2018-07-28T12:11:40+05:30
Amit Arora
Amit Arora
Python Programming Tutorial
Python
Practical Solution