Python

Tensorflow

TensorFlow is a deep learning module. It’s created by Google and open-source. It has a Python API and can be used with one or more CPUs or GPUs.

It runs on Windows, iOS, Linux, Raspberry Pi, Android and server farms.

There are many other deep learning libraries (Torch, Theano, Cafe, CNTK), but TensorFlow is the most popular.

Related Course:
Complete Guide to TensorFlow for Deep Learning with Python

install tensorflow

The TensorFlow module is available in the PyPi repository and can be installed with pip.

To start, write this line of code:

import tensorflow as tf

That’s all to load the module.

Whats a tensor

Tensors are data.
A tensor is an n-dimensional array

0-d tensor: scalar (number)
1-d tensor: vector
2-d tensor: matrix

tensorflow session

Lets start with a simple program and introducing the concept of sessions. Create a program that adds two numbers (2,6).

Tensorflow has a method tf.add(x,y). The parameters x and y are tensors. The method returns a tensor.

We can visualize our advanced mathematics (2+6) in a data flow graph:

graph

Tensorflow (TF) automatically gives names to nodes, in this case x=2 and y=6.

Code shown below:

import tensorflow as tf

x = tf.add(2, 6)
print(x)

If you run it you’ll see:

Tensor("Add:0", shape=(), dtype=int32)

That’s not the value of x, so how do you get it?

Create a session. Within the session, you can get the variable x.

import tensorflow as tf

a = tf.add(2, 6)

ses = tf.Session()
print(ses.run(a))
ses.close()

This will then give you

>> 8

You can also write this as:

import tensorflow as tf

a = tf.add(2, 6)

with tf.Session() as ses:
print(ses.run(a))

So what does ts.Session() do?

A session object evaluates Tensor objects and Operation objects are executed.

Previous Post Next Post

Cookie policy | Privacy policy | ©

Deep Learning