Useful Functions For Tensorflow

Keshav Aggarwal
Coinmonks
3 min readAug 14, 2018

--

Tensorflow

Tensorflow is a great library for deep learning and has a lot of functionality to offer. Tensorflow provides many features which makes it a lot easier to develop Deep Neural Netwoks.

In this post we are going to see some useful predefined and user defined methods which might help you while working with tensorflow.

In Built Mehtods

1. tf.zeros_like

This function takes a tensor as an input and returns a tensor with zeros as value and same shape and type as input tensor.
tensor = tf.constant([[1, 2, 3], [4, 5, 6]])
tf.zeros_like(tensor) # [[0, 0, 0], [0, 0, 0]]
This function can be helpful in situation like creating a black image from a given input image.
* You can use tf.zeros if you want to give the shape explicitly.
* If you want to initialize 1 instead of 0 you can use tf.ones_like.

2. tf.pad

Adds specified padding around a tensor with a constant value resulting in increased dimension of the tensor.
t = tf.constant([[1, 2, 3], [4, 5, 6]])
paddings = tf.constant([[1, 1,], [2, 2]])
# 'constant_values' is 0.
# rank of 't' is 2.
tf.pad(t, paddings, "CONSTANT") # [[0, 0, 0, 0, 0, 0, 0],
# [0, 0, 1, 2, 3, 0, 0],
# [0, 0, 4, 5, 6, 0, 0],
# [0, 0, 0, 0, 0, 0, 0]]
It can be used to add border around an image.

3. tf.enable_eager_execution

This helps you in running the tensorflow codes as you execute them. Using eager execution you don’t need to build graph and run it in a session. You can read more about how to use eager execution here.
*Eager execution needs to be first statement after importing tensorflow.

User Defined Functions

Here are some functions that I use in my code. These functions certainly makes many things easier.

1. Visualize weights of a Convolution Layer

If you want to visualize how the convolution filters look like then you can use below function.

2. Getting the weights of a Convolution layer

To get the values of weights you can use below function just by passing the name of the layer.

3. Get the output of a Convolution layer

If you want to see how the filters are getting activated or how does the output of a layer looks like.

These are the few functions that I thought might be useful while using tensorflow. If you use some functions which you think might be useful please put them in comment or mail me.

Get Best Software Deals Directly In Your Inbox

--

--

Keshav Aggarwal
Coinmonks

Autonomous Vehicle Engineer. #FutureAI #LearningAI