An introduction to one of the the most basic structures used in machine learning: a tensor.

🧵👇

Tensors are the data structure used by machine learning systems, and getting to know them is an essential skill you should build early on.

A tensor is a container for numerical data. It is the way we store the information that we'll use within our system.

(2 / 16)
Three primary attributes define a tensor:

▫️ Its rank
▫️ Its shape
▫️ Its data type

(3 / 16)
The rank of a tensor refers to the tensor's number of axes.

Examples:

▫️ The rank of a matrix is 2 because it has two axes.
▫️ The rank of a vector is 1 because it has a single axis.

(4 / 16)
The shape of a tensor describes the number of dimensions along each axis.

Example:

▫️ A square matrix may have (3, 3) dimensions.
▫️ A tensor of rank 3 may have (2, 5, 7) dimensions.

(5 / 16)
The data type of a tensor refers to the type of data contained in it.

For example, when thinking about Python 🐍's numpy library, here are some of the supported data types:

▫️ float32
▫️ float64
▫️ uint8
▫️ int32
▫️ int64

(6 / 16)
In the previous tweets I used the terms "vector" and "matrix," to referr to tensors with a specific rank (1 and 2 respectively.)

We can also use these mathematical concepts when describing tensors.

(7 / 16)
A scalar —or a 0D tensor— has rank 0 and contains a single number. These are also called "0-dimensional tensors."

The attached image shows how to construct a 0D tensor using numpy. Notice its shape and its rank (.ndim attribute.)

(8 / 16)
A vector —or a 1D tensor— has rank 1 and represents an array of numbers.

The attached image shows a vector with shape (4, ). Notice how its rank (.ndim attribute) is 1.

(9 / 16)
A matrix —or a 2D tensor— has rank 2 and represents an array of vectors. The two axes of a matrix are usually referred to as "rows" and "columns."

The attached image shows a matrix with shape (3, 4).

(10 / 16)
You can obtain higher-dimensional tensors (3D, 4D, etc.) by packing lower-dimensional tensors in an array.

For example, packing a 2D tensor in an array gives us a 3D tensor. Packing this one in another array gives us a 4D tensor, and so on.

(11 / 16)
Here are some common tensor representations:

▫️ Vectors: 1D - (features)
▫️ Sequences: 2D - (timesteps, features)
▫️ Images: 3D - (height, width, channels)
▫️ Videos: 4D - (frames, height, width, channels)

(12 / 16)
Commonly, machine learning algorithms deal with a subset of data at a time (called "batches.")

When using a batch of data, the tensor's first axis is reserved for the size of the batch (number of samples.)

(13 / 16)
For example, if your handling 2D tensors (matrices), a batch of them will have a total of 3 dimensions:

▫️ (samples, rows, columns)

Notice how the first axis is the number of matrices that we have in our batch.

(14 / 16)
Following the same logic, a batch of images can be represented as a 4D tensor:

▫️ (samples, height, width, channels)

And a batch of videos as a 5D tensor:

▫️ (samples, frames, height, width, channels)

(15 / 16)
If all of this makes sense, you are on your way! If something doesn't click, reply with your question, and I'll try to answer.

Either way, make sure to follow me for more machine learning content! 2021 is going to be great!

(16 / 16)

More from Santiago

You gotta think about this one carefully!

Imagine you go to the doctor and get tested for a rare disease (only 1 in 10,000 people get it.)

The test is 99% effective in detecting both sick and healthy people.

Your test comes back positive.

Are you really sick? Explain below 👇

The most complete answer from every reply so far is from Dr. Lena. Thanks for taking the time and going through


You can get the answer using Bayes' theorem, but let's try to come up with it in a different —maybe more intuitive— way.

👇


Here is what we know:

- Out of 10,000 people, 1 is sick
- Out of 100 sick people, 99 test positive
- Out of 100 healthy people, 99 test negative

Assuming 1 million people take the test (including you):

- 100 of them are sick
- 999,900 of them are healthy

👇

Let's now test both groups, starting with the 100 people sick:

▫️ 99 of them will be diagnosed (correctly) as sick (99%)

▫️ 1 of them is going to be diagnosed (incorrectly) as healthy (1%)

👇

More from Machine learning

Starting a new project using #Angular? Here is a list of all the stuff i use to launch my projects the fastest i can.

A THREAD 👇

Have you heard about Monorepo? I created one with all my Angular (and Nest) projects using
https://t.co/aY5llDtXg8.

I can share A LOT of code with it. Ex: Everytime i start a new project, i just need to import an Auth lib, that i created, and all Auth related stuff is set up.

Everyone in the Angular community knows about https://t.co/kDnunQZnxE. It's not the most beautiful component library out there, but it's good and easy to work with.

There's a bunch of state management solutions for Angular, but https://t.co/RJwpn74Qev is by far my favorite.

There's a lot of boilerplate, but you can solve this with the built-in schematics and/or with your own schematics

Are you not using custom schematics yet? Take a look at this:

https://t.co/iLrIaHVafm
https://t.co/3382Tn2k7C

You can automate all the boilerplate with hundreds of files associates with creating a new feature.
This is a Twitter series on #FoundationsOfML.

❓ Today, I want to start discussing the different types of Machine Learning flavors we can find.

This is a very high-level overview. In later threads, we'll dive deeper into each paradigm... 👇🧵

Last time we talked about how Machine Learning works.

Basically, it's about having some source of experience E for solving a given task T, that allows us to find a program P which is (hopefully) optimal w.r.t. some metric


According to the nature of that experience, we can define different formulations, or flavors, of the learning process.

A useful distinction is whether we have an explicit goal or desired output, which gives rise to the definitions of 1️⃣ Supervised and 2️⃣ Unsupervised Learning 👇

1️⃣ Supervised Learning

In this formulation, the experience E is a collection of input/output pairs, and the task T is defined as a function that produces the right output for any given input.

👉 The underlying assumption is that there is some correlation (or, in general, a computable relation) between the structure of an input and its corresponding output and that it is possible to infer that function or mapping from a sufficiently large number of examples.

You May Also Like