Introduction to TensorFlow Fundamentals

Introduction to TensorFlow Fundamentals

This is the first lab in the Machine Learning with TensorFlow series. The primary focus of this lab is to help you understand the fundamental components of TensorFlow.

Learning Objectives:

  • Creating scalars, vectors, matrices, and tensors
  • Inspecting different attributes of a tensor.
  • Performing basic operations on tensors.
  • Learn to index and slice tensors.
  • Reshaping and manipulating tensors.

To get the most out of this series:

  • you should know how to write code, ideally have some experience programming in Python.
  • have a decent understanding of variables, linear equations, matrices, calculus, and statistics.
  • don't just stick to the examples and code I've provided, play around and break things - that's the best way to learn.

Code/Environment Setup

In order to try out the code snippets or complete to-do tasks mentioned in each section, you can use Google Colaboratory notebook. Colab is basically Google's implementation of Jupyter Notebooks where you can run your code.

You can learn more about working with Colabs by following this notebook.

Introduction to scalars, vectors and matrices


Before we start talking about TensorFlow, let's try to understand what a Tensor is.

A tensor is basically an N-dimensional array with a uniform data type (called dtype). You can think of tensors as similar to NumPy arrays(np.array).

Note: Tensors are immutable. You can never update the content of a tensor but you can always create new ones.

Some of the basic tensors include:

  • Scalars - tensors with rank-0 or no axes/dimension
  • Vectors - tensors with rank-1 or 1 axis
  • Matrix - tensors with rank-2 or 2 axes

And then you can have as many axes as you want.

Here's a diagram to help you visualize this:

Let's start off by creating some basic tensors.

Import the libraries

The first step is to import the tensorflow & numpy library, so go ahead and add the code snippet below in the cell and run it by pressing shift + Enter on your keyboard: 

import numpy as np
import tensorflow as tf

Check the version of tensorflow using:


You should have some version of TensorFlow 2.x. If not you can use ! pip install tensorflow==2.4 to install.

Here's what this should look in the colab notebook:

With the tools imported for us to use, let's create some tensors!