Basics of TensorFlow 1.x part 2

Original article can be found here (source): Artificial Intelligence on Medium

%tensorflow_version 1.ximport tensorflow as tfa = tf.constant(21, tf.int16, name = 'x')b = tf.constant(0.098, tf.float32, name = 'y')c = tf.constant(78.90, name = 'z')print(a, b, c)operation1 = tf.add(b, c)print(operation1)operation2 = tf.sqrt(b)sess = tf.Session()print(sess.run(operation1))print(sess.run(operation2))

Shorthand operations can also be used in place of TensorFlow operations. For example, operation3 = a * b is the shorthand operation for the method tf.multiply(a, b)

Placeholders: Placeholder allows us to create a tensor so that we can assign data to at a later stage or at runtime. It allows us to create our operations and build our computation graph, without needing the data. feed_dict is used to supply values to a placeholder.

The syntax for declaring a placeholder is:

tf.placeholder(dtype, shape = None, name = None)

Where dtype is the datatype,
shape is the dimension of the placeholder,
name is the name of the placeholder.

Shape and Name are optional.

a = tf.placeholder(tf.float32)b = tf.placeholder(tf.float32)print(a, b)operation4 = tf.add(a, b)sess = tf.Session()print(sess.run(operation4, feed_dict = {a : 12.0, b : 10.0}))print(sess.run(operation4, {a : 12.0, b : 10.0}))

Variables: A variable is a tensor object that holds values that can be modified during the runtime. tf.Variable needs an initial value at the time of definition, this factor makes it different from a Placeholder. Variables must be explicitly initialized.

To initialize all the variables:

sess.run(tf.global_variables_initializer())