Useful Tips for Tensorflow Newbies

Tensorflow is one of the most widely used Deep Learning frameworks. In this post, I share some tips I had noted while learning, and it may be useful for you if you start from scratch.

Placeholders

Placeholder is used to assign a latent value during operations of the training phase.

While defining a placeholder with a type of value, the types should be inherited from the package, i.e. Tensorflow.Float32 instead of the standard float data type.

When it is needed to execute a tensorflow operation inline, the approach  below is useful


sess = tf.Session()
sess.run(variable_name)

Example:


x = tf.placeholder(tf.int32)
y = tf.placeholder(tf.int32)

#we define the add operation, notice that it belongs to the method provided by the tensorflow library
added = tf.add(x,y)

#execution
sess.run(added,feed_dict={x:50, y:40})

Constants

These are the most simple variable types you can use in your neural network. Here an example


import numpy as np

mat1 = tf.constant(np.array([[1.0,2.0]]))
mat2 = tf.constant(np.array([[3.0],[4.0]]))

#we now define the operation of multiplying two matrices, in shapes of (1, 2) and (2, 1)
multiplied = tf.matmul(mat1,mat2)

#and finally we execute the define operation in the tensorflow environment
sess.run(multiplied)

Constants, variables, placeholders are similar objects in Tensorflow. For example, you can multiply a placeholder with a variable or a constant without having any error. The main difference is the purpose of use. If you want to add a latent value to an object during the training phase, the placeholder class is the convenient one.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.