Tensorflow for Complete Begineers: Getting Started with Tensors

Tensorflow for Complete Begineers: Getting Started with Tensors

Tensorflow for Complete Begineers: Getting Started with Tensors. An understanding of “tensors” and their operations can help complete beginners start from scratch. Tensorflow is a buzz word nowadays in this exciting world of Artificial Intelligence (AI), especially as Deep Learning continues to rapidly accelerate progress in AI.

An understanding of “tensors” and their operations can help complete beginners start from scratch.

Tensorflow is a buzz word nowadays in this exciting world of Artificial Intelligence (AI), especially as Deep Learning continues to rapidly accelerate progress in AI. But for someone just starting with Tensorflow, the experience can be scary and daunting, as the terminologies and usage of the beautiful library can be confusing for complete beginners. When I first started learning Tensorflow, I faced similar challenges, and hope to simplify some of the intricacies through this article. This article requires a basic understanding of Python to get a clearer picture of Tensorflow.

How does Tensorflow work?

The basic (and simplest) way to describe Tensorflow is that it is a cool library in Python (and probably other programming languages as well) which allows us to create computational graphs to develop neural network models. The basic element which comprises Tensorflow objects is a Tensor, and all computations which are performed occur in these Tensors. So literally (in my words), these Tensors flow in an orderly manner when you develop any neural network model, and give rise to the final outputs when evaluted. So yes, learning what tensors really are and how we can use them is the first step to getting started with Tensorflow.

So what are Tensors?

We are all familiar with data types in programming, right? 1, 2, 3 _etc. are integers, numbers with decimal points (1.5,3.141,5.2555 etc.)_ are floating point values and another common one is strings (such as “Hello. How are you today?”). When we have a collection of multiple elements together, we generally term that as a list_ (e.g. [1,4,7])**._**

A collection of lists can be used to develop a matrix/matrices like this one:

[[1,4,7], [5,8,12], [1,66,88]]

Now, let’s come to Tensors- they are basically higher dimensional representations/generalisations of conventional matrices and vectors. It could even be a 1-D matrix, a 2-D matrix, a 4-D matrix, or an n-dimensional matrix! So we can have multi-dimensional arrays beyond a single list in a Tensor. That’s it- that is what a tensor really is in the most basic sense. Just like we can assign variables values in conventional programming, we could do the same in Tensorflow. Just that there is a specialised way to do it. Similar, just like you could perform multiple operations on conventional variables (or constants) in programming, we can do that with Tensors. We can slice tensors and select a portion of its elements, have various data types for tensors (integers, floating point, strings etc.) and much more.

Getting started with using Tensorflow in Python

The very first step is to install the beautiful library! _pip _is all you need here. Note that if you’re using Google Colab (which is very similar to Jupyter notebooks in Python), you don’t even need to install this library as it is already installed and available ready for use. Thank you, Google!

#Install the tensorflow module (not required in Google Colab, but is needed in your local PC)

!pip install tensorflow

Tensorflow, at present, has an older version of type 1.x, while the latest releases use 2.x. So let’s select the current tensorflow version.

#Here, we would go with the latest (2.x) release of tensorflow by selecting the version

#Note: By default, Colab tends to use the 2.x version
%tensorflow_version 2.x

Let’s import the tensorflow module using the golden keyword _import! _And yes, you could check what version of the library you’re using by simply printing its version attribute. As we are using Colab for this article, the message “requirement already satisfied” is shown in the output.

#import the tensorflow module

import tensorflow as tf
#Display the tensorflow version we are using
print(tf.version)

Requirement already satisfied: tensorflow in /usr/local/lib/python3.6/dist-packages (2.3.0)

_<module ‘tensorflow.api.v2.version’ from ‘/usr/local/lib/python3.6/dist-packages/tensorflow/api/v2/version/__init_.py’>

Now, let’s get started with creating Tensors. We would create some for int, float and string here. Notice the specialised way of creating tensors using _tf.Variable _attribute. This means that we are creating a Tensor which would be a variable nature, so we can change/modify its values by performing specialised operations, just like we would do in conventional programming with variables.

#The science (and art) of creating tensors

scalar_val = tf.Variable(123,tf.int16)
floating_val = tf.Variable(123.456,tf.float32)
string_val = tf.Variable(“hello everyone. Nice to learn tensorflow!”,tf.string)

And now, it’s very easy to print out the values of these Tensors!

#Let us display the values (print) these tensors

print(scalar_val)
print(floating_val)
print(string_val)

_<tf.Variable ‘Variable:0’ shape=() dtype=int32, numpy=123>_

_<tf.Variable ‘Variable:0’ shape=() dtype=float32, numpy=123.456> <tf.Variable ‘Variable:0’ shape=() dtype=string, numpy=b’hello everyone. Nice to learn tensorflow!’>_

We can determine the shape and rank of the Tensors, like you might have heard in mathematics modules at school. If not, shape simply refers to the number of elements which are contained along each dimension of the tensor. While rank is the deepest level of nesting within the Tensor. Going into some code might make it more clear.

#The idea behind shape and rank of tensors

#Shape: Describes the dimension of the tensor (total elements contained along each dimension)
scalar_val_shap = tf.shape(scalar_val)
print(scalar_val_shap)
floating_val_shap = tf.shape(floating_val)
print(floating_val)

tf.Tensor([], shape=(0,), dtype=int32)

_<tf.Variable ‘Variable:0’ shape=() dtype=float32, numpy=123.456>_

So we get a shape of 0 right? It just means that these are scalar values rather than lists or nested lists.

#Now, if we use e.g. lists/nested lists instead of just a “single” scalar value

list_tensor1 = tf.Variable([1,3,5,6],tf.int16)
print(list_tensor1)
print(tf.shape(list_tensor1))
list_tensor2 = tf.Variable([[1,2,3],[4,5,6]],tf.int16)
print(list_tensor2)
print(tf.shape(list_tensor2))
#how about the rank? It describes the level of nesting within the tensor in simple words.
print(tf.rank(list_tensor1))
print(tf.rank(list_tensor2))

You can see that the list_tensor1 _has 4 elements, so that’s what gives it a shape of (4,). Note that (4,) is nothing but (4,1), just showing 4 elements within a single list. Next, for _list_tensor2, observe a shape of (2,3) signifying that we have a nested list- with 2 lists, each containing 3 elements.

artificial-intelligence deep-learning python machine-learning tensorflow

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Artificial Intelligence, Machine Learning, Deep Learning 

Artificial Intelligence (AI) will and is currently taking over an important role in our lives — not necessarily through intelligent robots.

AI(Artificial Intelligence): The Business Benefits of Machine Learning

Enroll now at CETPA, the best Institute in India for Artificial Intelligence Online Training Course and Certification for students & working professionals & avail 50% instant discount.

Artificial Neural Network | Deep Learning with TensorFlow and Artificial Intelligence

Artificial Neural Network | Deep Learning with Tensorflow and Artificial Intelligence | I have talked about Artificial neural networks and its implementation in TensorFlow using google colab. You will learn: What is an Artificial Neural Network? Building your neural network using Tensorflow.

Artificial Intelligence vs. Machine Learning vs. Deep Learning

Artificial Intelligence vs. Machine Learning vs. Deep Learning. We are going to discuss we difference between Artificial Intelligence, Machine Learning, and Deep Learning

Artificial Intelligence vs. Machine Learning vs. Deep Learning

Simple explanations of Artificial Intelligence, Machine Learning, and Deep Learning and how they’re all different