What are Tensors | Tensor In-depth Explanation | Tensor in Machine Learning — Transcript
Full transcript
- 0:00Hello guys, welcome to my YouTube
- 0:01channel. This is 100 Days of Machine
- 0:03Learning and today is day 11. So, 10
- 0:07days have passed and we have discussed
- 0:10many things, mostly related to the "why
- 0:12" and "what" parts of machine learning.
- 0:16The "why" and the "what," right? But
- 0:19from now on, from today onwards, we are
- 0:21going to focus on the "how" parts. How
- 0:23does machine learning happen? We are
- 0:25starting practical machine learning
- 0:26from today. And our topic for today is
- 0:29a very important one, because it is by
- 0:31using this that machine learning is
- 0:33able to function. Okay? Today we are
- 0:36going to study about tensors. Okay? I
- 0:38hope you have heard the name at some
- 0:40point. What are tensors? And why are we
- 0:44studying tensors today? I will answer
- 0:46these two questions for you very
- 0:47quickly. First, let me answer why we
- 0:49are studying tensors. First of all.
- 0:51Right? We could have studied something
- 0:53else. But why is the first thing we
- 0:54started in practical machine learning
- 0:56tensors? Let's discuss that. So, a
- 0:59tensor is basically a data structure. I
- 1:03mean, if I explain it to you in the
- 1:05simplest terms, a tensor is nothing but
- 1:07a data structure. A data structure
- 1:09means a way to store data. Now, why are
- 1:11we studying tensors first? The reason
- 1:14is that today, all machine learning
- 1:17systems and all the leading libraries
- 1:19like Scikit-learn and TensorFlow use
- 1:21the tensor as their most basic data
- 1:24structure. Meaning, whatever machine
- 1:27learning problem you are solving, in
- 1:29the machine learning space or the deep
- 1:31learning space, you will have to deal
- 1:33with tensors. So, as a beginner, I
- 1:35honestly skipped this topic. And later
- 1:38I realized that things wouldn't work
- 1:40without studying tensors. So, so that
- 1:42you don't make this mistake, we are
- 1:44starting with tensors. What you learn
- 1:47today will not only help you in machine
- 1:49learning but will also help you in deep
- 1:51learning. Okay? So, it is a very
- 1:53general topic. A very important topic.
- 1:55Let's start with the topic. Okay? So,
- 1:57one more thing I forgot to mention.
- 2:00Tensors are so important that the
- 2:02number one deep learning library today,
- 2:05Google's TensorFlow, is named after
- 2:08tensors. So, trust me, it is a very
- 2:09important concept. Okay? So, yeah,
- 2:11anyways, what is a tensor? Let's start
- 2:13the discussion from there again. A
- 2:14tensor is nothing but a data structure.
- 2:17Data structure means a way to store
- 2:19data. Right? And if we break down
- 2:22tensors into even simpler terms, a
- 2:24tensor is just a container where you
- 2:25store numbers. That's it. A tensor is
- 2:29nothing but a container for numbers.
- 2:31Sometimes you store characters or
- 2:33strings too. But that is very rare.
- 2:35Almost never do you do that. 99.99%of
- 2:38the time, it is going to be a container
- 2:41for numbers. Right? Now guess what? You
- 2:44have used tensors before. I mean, even
- 2:47if you haven't heard the name, you have
- 2:48used them. So if you have ever studied
- 2:51about vectors or matrices in your life,
- 2:53they are actually tensors. So when it
- 2:56is zero-dimensional, you call it a
- 2:58scalar. A single number. When there is
- 3:01a list of numbers, you call it a vector
- 3:03. When there are numbers in 2D, you
- 3:06call it a matrix. Right? Now think, if
- 3:09there are numbers in 3D, what would you
- 3:10call it? Or what would you call it in
- 3:124D? So that is why computer scientists,
- 3:15in fact, this is an area combining
- 3:17physics and math. They decided.
- 3:20Scientists decided to coin a general
- 3:22term. And they use, they call it a
- 3:25tensor. Right? So let's study more in
- 3:27detail about tensors. I will share my
- 3:29screen. So yeah, tensors are basically
- 3:33a container for storing numbers. We
- 3:37have discussed this. Let's talk about
- 3:390D tensors, which are scalars. Right?
- 3:43So whenever you are storing a single
- 3:45number somewhere like this or like this
- 3:48. These are basically called scalars.
- 3:52Right? Or you can call it a 0D tensor.
- 3:58Right? 0D means that the number of
- 4:02dimensions of this particular tensor is
- 4:06zero. There are no dimensions. Right?
- 4:10If I show you practically, what I can
- 4:13do is I can go to Google Colab. If you
- 4:16don't know Google Colab, trust me, it's
- 4:19a very simple tool where you can run
- 4:22Python code online. Right? You don't
- 4:24need to know more than that. I am
- 4:26importing a library. Called NumPy,
- 4:28which we will use a lot in this video,
- 4:30and in machine learning in general,
- 4:31this library is used heavily. If you
- 4:33want to study NumPy, I will put a link
- 4:36to a playlist from my own channel in
- 4:38the description of this video. You can
- 4:40study NumPy from there. NumPy is very
- 4:42simple. All right? Now, if you want to
- 4:45create a scalar using NumPy. Then you
- 4:47don't have to write much. You just need
- 4:48to write one line of code. Let's say we
- 4:50are creating a variable A in which we
- 4:51will store the scalar. This will be
- 4:54equal to np.array and here I put a
- 4:58number and ran it. And as soon as I
- 5:03printed A, which is on your screen,
- 5:06this is actually a scalar or a 0D
- 5:09tensor. All right? How do we know it's
- 5:120D? Whenever you are creating tensors
- 5:14in NumPy. See, one more thing to
- 5:16remember, if you are from a computer
- 5:18science background, then a tensor and
- 5:20an n-dimensional array are the same
- 5:22thing. An array means one-dimensional.
- 5:24A 2D array means an array within an
- 5:26array. A 3D array means an array within
- 5:27an array within an array. All right? So
- 5:29in that way, n-dimensional array is the
- 5:31general term if you come from a math or
- 5:33physics background, it's called a
- 5:35tensor. So the machine learning
- 5:36community has adopted that term. So
- 5:37whenever you see an n-d array, just
- 5:40understand that it is a tensor. All
- 5:42right? So here you have a 0D array.
- 5:44Because there is no array at all. There
- 5:45is just one number. So you call this a
- 5:470D tensor. So whenever you create
- 5:49arrays or tensors in NumPy, you can
- 5:52find their dimension. You have an
- 5:54attribute called ndim. It's clear from
- 5:57the name. Number of dimensions. As soon
- 5:59as you run this, you get zero. From
- 6:02this, you understand that the dimension
- 6:03of this particular tensor is zero. And
- 6:06that is why it is a 0D tensor. All
- 6:08right? So I showed you a small
- 6:10demonstration of this. Let's quickly
- 6:12discuss a 1D tensor. So if you have a
- 6:15list of numbers like this 1, 2, 3, 4,
- 6:20then this is a 1D tensor, and you can
- 6:25also call it a vector, and in
- 6:29programming, you can call it a 1D array
- 6:35or simply an array. All right? Now, for
- 6:39this thing, this tensor, what will its
- 6:42ndim be? It would be one. Meaning, this
- 6:46is a tensor that has one dimension. All
- 6:48right? Now you need to understand one
- 6:51more thing, what is an axis in terms of
- 6:53tensors? So an axis is basically how
- 6:56many dimensions you have. All right?
- 6:58Basically, if you have two dimensions.
- 7:01Let's say your tensor has two
- 7:02dimensions. We will discuss 2D tensors
- 7:04a little later. So this means there are
- 7:06two axes there. Okay? If it is a 3D
- 7:09tensor, then you have three axes. Okay?
- 7:11So dimensions and axes are related.
- 7:15Okay? Whatever the D of the tensor is,
- 7:17whatever the dimension of the tensor is
- 7:19, you will have that many axes. And one
- 7:22more thing, the number of axes is also
- 7:25called rank in some places, and you
- 7:28also call that dimension. Okay? So,
- 7:32there shouldn't be any confusion here.
- 7:34If you go and read in different places,
- 7:35you will see different things. I am
- 7:37just letting you know. Number of axes
- 7:39is equal to rank, equal to the
- 7:40dimension of the tensor. Okay? Let me
- 7:43quickly show you how you can create a
- 7:461D tensor using NumPy. So, I am
- 7:49creating an array. And here, what I am
- 7:52doing is sending a Python list inside
- 7:54the array that contains these values.
- 7:58And now if I print the array, this,
- 8:00which is on your screen, is a 1D tensor
- 8:03. Okay? And if I write arr.ndim, you
- 8:07would see that its n-dimension is one.
- 8:11So, the number of axes inside it is one
- 8:13. Its rank is also one, and it is a 1D
- 8:16tensor. Okay? I guess you have no
- 8:18confusion up to this point. Now I will
- 8:19say something that might confuse you a
- 8:21little. But it is very important to
- 8:22understand. Okay? Listen carefully.
- 8:25This 1D tensor, you also call this a
- 8:32vector. This is also a vector. You can
- 8:35call it a vector. Okay? But if someone
- 8:37asks you what the dimension of this
- 8:39vector is? Then, since there are four
- 8:42numbers here, you would say the
- 8:44dimension of this vector is 4D. Now you
- 8:47will say that I just said that this is
- 8:491D. I said it is a 1D tensor. This is a
- 8:521D tensor. And since it is a 1D tensor,
- 8:55it means it is a vector. But how many
- 8:57dimensions are inside this vector? Four
- 8:59dimensions. Now this might seem a
- 9:01little confusing to you. But I will
- 9:03give you an example in a little while
- 9:04that will make this crystal clear as to
- 9:06what I am trying to say. At this point,
- 9:10just create a distinction in your mind
- 9:12that whenever you create a 1D tensor,
- 9:14it is a vector, and the number of
- 9:16values in that vector determines the
- 9:18dimension of that vector. Meaning, if I
- 9:22give you another example, let's say one
- 9:25and two, this is still a 1D tensor. And
- 9:29it is also a vector. But if someone
- 9:31asks you, how many dimensions does this
- 9:32vector have? Then you would say two,
- 9:34because there are two numbers in it. I
- 9:35hope you guys are following along so
- 9:38far. Okay? If you are following along
- 9:41so far, you must have also noticed one
- 9:43thing: if you keep adding more scalars
- 9:46to a scalar, then it becomes a vector.
- 9:52Meaning, if you added four scalars here
- 9:56, you got a vector. Always remember
- 9:59this. In the case of tensors, as you
- 10:01increase the dimension, how do those
- 10:03dimensions always increase? If you keep
- 10:06adding the previous dimension, you get
- 10:08the next dimension. Meaning, if you
- 10:11want a 1D tensor, basically it is a
- 10:13collection of multiple 0D tensors. If
- 10:17you want a vector, it is basically a
- 10:18collection of scalars. If you want
- 10:21matrices, it is basically a collection
- 10:23of vectors. So, you must always
- 10:24remember this fundamental. Okay? Now
- 10:27let me show you an example of a 2D
- 10:28tensor, which we also call matrices.
- 10:30Okay? So now let's discuss what are 2D
- 10:33tensors or matrices. So, imagine if you
- 10:37have multiple vectors, like. So If you
- 10:47add or collect these vectors, then we
- 10:52call it a matrix. Okay? Something like
- 10:55this. 1 2 3 4 5 6 7 8 9, so basically
- 11:04matrices are nothing but a collection
- 11:07of vectors. Okay? And obviously, its
- 11:11dimension is 2D because there are two
- 11:14axes here. Okay? This one axis is the
- 11:16column axis, and this one axis is the
- 11:18row axis. Okay? So, I hope you are
- 11:20understanding this. Its rank is two,
- 11:23which is equal to the dimension, and
- 11:26there are two axes: rows and columns.
- 11:29Let me quickly show you how this is
- 11:31created. It is very simple. You will
- 11:34use NumPy again. Let's say I am
- 11:36creating a variable called MAT, and MAT
- 11:40is equal to np.array, and here what
- 11:43will I do? I will pass a list of lists
- 11:45in Python, something like this. 1, 2, 3
- 11:47, then a comma; the second item itself
- 11:50is going to be a list 4, 5, 6. And 7, 8
- 11:56, 9, okay. And if we print the MAT, you
- 12:00will get a structure like this; this is
- 12:02a matrix, okay. If you calculate its MM
- 12:06, then as expected, it would be two. 2D
- 12:13Tensor. Okay, now you can easily
- 12:15understand that if you want to create
- 12:17higher dimension tensors from this,
- 12:19what will you do? Basically, if you
- 12:22want to create a 4D tensor. Sorry, if
- 12:25you want to create a 3D tensor, you
- 12:27will have a matrix that looks like this
- 12:30. You will take more matrices like this
- 12:35. This is one matrix. This is the
- 12:38second one. This is the third one. So,
- 12:42basically, suppose if all these
- 12:44matrices have 3 by 3 numbers and you
- 12:47are taking four such matrices, then you
- 12:51will get a 4 by 3 by 3 tensor, which
- 12:54you will call a 3D tensor, and this
- 12:57time there are three axes here. Okay?
- 13:01So you have a column axis. You have a
- 13:04row axis and you have a depth axis.
- 13:08Okay? This is a 3D tensor. Okay? Or you
- 13:11can actually see it in terms of a
- 13:13cuboid. Right? Something like this.
- 13:19Right? Now, if someone asks you what a
- 13:224D tensor would look like? What would a
- 13:244D tensor be like? So a 4D tensor is
- 13:26basically a collection of 3D tensors.
- 13:28If this is a 3D tensor, then a 4D
- 13:30tensor will look like this. Basically,
- 13:36it will be a vector of 3D tensors. See
- 13:38like this. Now, sorry for my drawing.
- 13:42But these lines have 3D tensors placed
- 13:45in this way. These are what you call a
- 13:484D tensor. Right? Now, if someone asks
- 13:51you what a 5D tensor would look like?
- 13:53So a 5D tensor will be a matrix of 3D
- 13:55tensors. Basically, more cuboids will
- 13:59come underneath it like this. Like,
- 14:02this is a 5D tensor. Right? And that is
- 14:08how this entire thing works. This is a
- 14:115D tensor. I mean, if you focus only on
- 14:13this much. If you focus only on this
- 14:16much, then this is a 4D tensor. But if
- 14:22you focus on the whole thing. Focus on
- 14:24this whole thing, this is a 5D tensor.
- 14:26Okay? And that is how this entire thing
- 14:29is stacked on top of each other. Now,
- 14:30if you make a collection of 5D, it will
- 14:33become 6D. If you make a collection of
- 14:356D, it will become 7D. That is the
- 14:36whole idea. So where you were
- 14:38restricted to thinking only about
- 14:40scalars, vectors, and matrices, now you
- 14:42are completely free. Because you have a
- 14:45general concept that we call this thing
- 14:47a tensor, and in terms of that tensor,
- 14:49you can think or do things in any
- 14:51number of dimensions. Okay? Now, if you
- 14:54talk about machine learning, you mostly
- 14:56revolve between zero to 5D. You won't
- 14:59really find tensors with higher
- 15:00dimensions than that. Okay? So that’s
- 15:02why, in today’s video, I will show
- 15:04you one example of each of the five
- 15:06tensors. Okay? To be honest, there’s
- 15:09no need to show an example of a scalar.
- 15:11So I will show you all examples from 1D
- 15:13up to 5D. Practical examples, the kind
- 15:15of examples you might encounter later
- 15:16on. Okay? But before we go there, I
- 15:18just want to tell you one more thing.
- 15:20This is the last concept. Although we
- 15:22have discussed it a little bit already.
- 15:23What do we call rank, axis, and shape?
- 15:25Okay? Let’s discuss it quickly. Rank
- 15:28is just that. The number of dimensions
- 15:30your tensor has is what is called its
- 15:32rank. It is also called the number of
- 15:35axes. Number of axes is equal to rank,
- 15:40is equal to the number of dimensions.
- 15:45These three things are the same, guys.
- 15:46Okay? You must always remember this.
- 15:48Let me explain it to you in terms of a
- 15:51matrix. Suppose you have this matrix.
- 15:53It is a 3x3 matrix. So, obviously, it
- 15:56has two axes. One row axis and one
- 15:59column axis. One more thing comes up
- 16:02here. Then what is shape? Shape is how
- 16:07many items you have in any particular
- 16:10axis. For example, if you talk about
- 16:13the row axis, how many maximum items
- 16:15can you store in the row axis? Three.
- 16:18Right? It’s 3x3, so you can only
- 16:20store three. So the shape of this
- 16:23matrix is 3x3, but you can also have a
- 16:25matrix like this. This is also a matrix
- 16:30. If you calculate its ndim or its rank
- 16:34, it will still be two. Because it
- 16:38still has a column axis and a row axis
- 16:40here. But this time, if you calculate
- 16:43the shape, it will be 2x3. Why am I
- 16:46saying that? Because in the row, in the
- 16:48row axis, how many maximum items can
- 16:50you store? Two. And how many can you
- 16:52store in the column axis? Three. Okay?
- 16:55Let’s take one more example. This is
- 17:004x2. Okay? There is one more thing,
- 17:03size. Size of a tensor. So size means
- 17:06how many items are in that tensor. So
- 17:08simply multiply its shape, all the
- 17:10numbers. Multiply these. 2x3 is six.
- 17:14Meaning there are six items inside it.
- 17:16If you multiply 3 here, you get nine.
- 17:18Meaning there are nine items here. Here
- 17:20, multiply 4 by 2, you get eight.
- 17:21Meaning there are eight items. Okay? So
- 17:23, whenever you want to find out how
- 17:25many items are in a particular tensor,
- 17:27which we call size, it is very simple
- 17:28to calculate. You just find its shape
- 17:30and multiply all the numbers. Okay?
- 17:32This is just not true for scalars. For
- 17:34a scalar, the size is always equal to
- 17:37one. It's simple. If it is a scalar,
- 17:39its size will be one. For tensors above
- 17:41that, you simply have to multiply their
- 17:43shape. If you have a vector, a 1D
- 17:45tensor, it will look something like
- 17:48this. So in this case, your shape will
- 17:52be three. Okay? Because here, in one
- 17:55single place, there are three items.
- 17:57Okay? So here, three is your number of
- 18:00items. Okay? I hope you are
- 18:02understanding. It is very simple. Once
- 18:04you grasp the concepts. Okay? So rank,
- 18:06axis, and shape, these are the three
- 18:08things we discussed. In fact, we also
- 18:10discussed size. Now, the most important
- 18:13part of this video: we will see
- 18:14practical examples of tensors from 1D
- 18:17to 5D that you will encounter later in
- 18:19your life while doing machine learning
- 18:21and deep learning projects. We are
- 18:23going to do that next. Let me now give
- 18:26you two practical examples of a 1D
- 18:27tensor that you will see everywhere in
- 18:29machine learning in the future. Okay?
- 18:32And this is where you will understand
- 18:35why I said a little while ago that a
- 18:37vector is a 1D tensor but its own
- 18:39dimensions are different. You will
- 18:42understand this now. Okay? And to
- 18:44explain this whole thing, I will take
- 18:45an example. Let's say we have a dataset
- 18:48about students. We have discussed this
- 18:50dataset before as well. We have a
- 18:52dataset of students. And in that
- 18:55dataset, we have four columns. The
- 18:57first column is CGPA. The second column
- 19:02is IQ. The third column is state. Which
- 19:05state are they from? West Bengal,
- 19:07Kerala, or whatever it is. Okay? And
- 19:09here it is whether they got placed or
- 19:12not. Which is your target column that
- 19:14you need to predict. Classification
- 19:16problem. Okay? Now, let's assume I have
- 19:20data for 10,000 students, and obviously
- 19:22, my end goal is to build a
- 19:25classification model, so if I provide a
- 19:28new student's details—meaning IQ,
- 19:30CGPA, and state—it should tell me
- 19:33whether they will be placed or not. I
- 19:36need to make this prediction. Right? So
- 19:38, what do I do here? I will show you an
- 19:41example of a 1D tensor. I'll show you
- 19:44an example of a 1D tensor or a vector.
- 19:47For that, you'll have to forget the
- 19:4910,000 students and focus on just one
- 19:52student. If you focus only on the first
- 19:54student. Let's say our first student
- 19:57has a CGPA of 8.1, an IQ of 91, is from
- 20:01West Bengal, and they got placed. For
- 20:05now, we are ignoring the placement
- 20:07column because we are going to focus on
- 20:09the input columns. So, if you take
- 20:12their input, if you take that student's
- 20:14input, basically it is a set of three
- 20:16things. Now, let's assume for a moment
- 20:19that our data only contains students
- 20:21from two states. The first state is
- 20:23West Bengal, the second is Bengaluru,
- 20:26Karnataka. Right? And assume we are
- 20:29calling West Bengal zero and Karnataka
- 20:32one. We have performed numerical
- 20:34encoding. This is called label encoding
- 20:35. You will understand this in future
- 20:37videos. So, basically, instead of West
- 20:40Bengal, I wrote zero. So if we only
- 20:43talk about the first student, we have
- 20:46these numbers: 8.1, 91, and 0, and
- 20:49guess what? This is a tensor, a 1D
- 20:52tensor. Or you could call it a vector.
- 20:56This is a vector. And what is the
- 20:58dimension of this tensor? 1D, because
- 21:00it has only one axis. There is only one
- 21:03axis. But what is the dimension of this
- 21:05vector? It is a 3D vector. Why am I
- 21:08saying that? Because there are three
- 21:11axes here. There are three axes here.
- 21:13What is the first axis? CGPA. What is
- 21:16the second axis? IQ, and what is the
- 21:19third axis? State. And this student is
- 21:24somewhere here. There is that vector.
- 21:27This is the vector. Now, suppose if
- 21:31there were another student, 7.2 IQ, 102
- 21:33, Karnataka, no placement, so assume
- 21:36they would lie somewhere here,
- 21:38something like this. Right? They would
- 21:42have their own separate vector. So,
- 21:45basically, if you talk about it, in
- 21:47this three-dimensional space, you have
- 21:49data for 10,000 students. Data for
- 21:5110,000 students. Meaning you have
- 21:5410,000 vectors. Each vector represents
- 21:56a student in this three-dimensional
- 21:59coordinate space. So when you are
- 22:01talking about vectors, this is a 3D
- 22:02space. But when you represent those
- 22:05numbers in the form of a tensor, it is
- 22:07a 1D tensor. So these are two different
- 22:09things. Do not get confused between
- 22:11them. Are you talking about the
- 22:13dimension of the tensor or the
- 22:15dimension of the vector? There is a
- 22:17difference in that. Right? It became 3D
- 22:19here because you had three input
- 22:21columns. If you have 50 input columns,
- 22:24then you are working in a 50-
- 22:26dimensional coordinate space. But it
- 22:28will still remain a 1D tensor. Please,
- 22:31I hope you are understanding this point
- 22:33. Okay? One more example where you will
- 22:36find a 1D tensor. One example is
- 22:37already this. Whenever you get any
- 22:39machine learning tabular data, each row
- 22:42is actually a 1D tensor or a vector.
- 22:45Okay? One more example is, now think if
- 22:47you had data for these 10,000 students
- 22:49here, then everyone's output would look
- 22:52something like this. In this way. Now
- 22:54if you take out all these numbers. If
- 22:57you extract all these numbers and
- 22:59represent them in this manner as well.
- 23:06Then there will be a total of 10,000
- 23:09numbers in it. But this is also a 1D
- 23:13tensor. This is also a 1D tensor. Okay?
- 23:18So I hope you are understanding a bit
- 23:20by now where you can find 1D tensors in
- 23:22machine learning. Okay? Now let me show
- 23:25you where you will find 2D tensors.
- 23:28Where will you find 2D tensors? It is a
- 23:30very simple thing, guys. What is a 2D
- 23:32tensor? A 2D tensor is basically a
- 23:34collection of 1D tensors. Right? So if
- 23:37you have the data we had earlier of
- 23:41CGPA, IQ, state, and placement. Now
- 23:45let's forget about placement for a
- 23:47moment. If we only focus on the input
- 23:48columns. We had data for 10,000
- 23:50students. Like this. So here, every
- 23:55student's data is actually a vector or
- 23:59a 1D tensor. So a collection of 10,000
- 24:02vectors would be what? A matrix, right?
- 24:06So you can actually write this entire
- 24:08thing inside a matrix. So whenever you
- 24:12get data in machine learning, its input
- 24:15—the collection of input columns—
- 24:18you can store all that data in a matrix
- 24:21. That is generally called a 2D tensor
- 24:23in machine learning. It is denoted by
- 24:25D. Okay? So it will look something like
- 24:28this. Data of the first student, data
- 24:31of the second student, data of the
- 24:34third student, data of 10,000 students.
- 24:38And this becomes your 2D tensor. Okay?
- 24:43Which is a collection of 10,000 vectors
- 24:46that are themselves 3D vectors. I hope
- 24:48you are understanding this so far. And
- 24:51this placement, whether it happened or
- 24:54not, is itself a 10,000-dimensional
- 24:57vector. And basically, it is a 1D
- 25:00tensor. This is a 1D tensor. Okay? This
- 25:03is a 2D tensor. I hope you understood
- 25:06this entire discussion. You will
- 25:08encounter this every time. If you ever
- 25:10work with tabular data, you are working
- 25:13with 1D tensors as well as 2D tensors.
- 25:16Basically, you are working with
- 25:18matrices and also with vectors. Or in
- 25:20summary, if I tell you, when you
- 25:23separate all the inputs, you can call
- 25:25it a matrix or a 2D tensor. And when
- 25:28you isolate the entire input, which is
- 25:30a single column, it becomes your vector
- 25:31or your 1D tensor. Okay? So, I guess
- 25:34you understand where you find 1D and 2D
- 25:36tensors. Now let's move on to 3D
- 25:38tensors. So now let's focus our
- 25:40attention on 3D tensors. To be honest,
- 25:423D tensors are a bit rare. Calling them
- 25:45rare would actually be wrong. It's just
- 25:47that they aren't as frequent as your 1D
- 25:50and 2D tensors. If you want to see a
- 25:52practical example of 3D tensors. In
- 25:53fact, I will show you two practical
- 25:55examples. One of them, a very intuitive
- 25:57and interesting practical example, is
- 26:00from the domain of NLP, Natural
- 26:01Language Processing. Meaning, if you
- 26:04are ever working with any textual data,
- 26:07you will see 3D tensors there. Let me
- 26:09show you how. So let's say I have some
- 26:12text written. Hi Nitish. Hi Rahul and
- 26:20Hi Ankit. These are three texts. And
- 26:24assume these three texts are my input.
- 26:27I have to give these three texts to the
- 26:29algorithm to train. Now, obviously, you
- 26:32know that machine learning algorithms
- 26:33are pure mathematics. They don't
- 26:35understand text or strings. So you will
- 26:38have to convert this text into numbers,
- 26:41or, in other words, into vectors. This
- 26:43entire process is actually called
- 26:45vectorization in the NLP domain. Where
- 26:47you convert a given text into numbers
- 26:50or vectors. So that is a very important
- 26:52thing. And there are many different
- 26:54vectorization techniques. But we are
- 26:55going to focus on a very simple and
- 26:57very intuitive technique. Let me show
- 26:59you. So what I will do is, I need to
- 27:02convert all these sentences into
- 27:04numbers. For that, what I will do is, I
- 27:08will list down all my words, all my
- 27:11unique words. Like 'Hi' once, 'Nitish'
- 27:14once, basically I wrote down my entire
- 27:17vocabulary. Vocabulary means the set of
- 27:20all unique words present in my corpus,
- 27:22as it is called here. Basically, in my
- 27:25text. Okay? Now what will we do? We
- 27:29will represent every word, every
- 27:31particular word, with a vector. For
- 27:34instance, if I have to represent 'Hi',
- 27:37then 'Hi' becomes 1 0 0 0. Whenever I
- 27:42find 'Hi' written anywhere, I can write
- 27:44this instead. If I find 'Nitish'
- 27:46written anywhere, I will write 0 1 0 0;
- 27:50that becomes 'Nitish'. Okay? If I have
- 27:53to represent 'Rahul', then it becomes
- 27:55this. And if I have to represent 'Ankit
- 27:58', then it becomes this. Right? Now, if
- 28:01I talk about these sentences. If I talk
- 28:03about these sentences, let's say my
- 28:05first sentence is what? 'Hi Nitish'. So
- 28:07what is this actually? It is a set or
- 28:10collection of two vectors. So the first
- 28:14vector is 1 0 0 0 and the second vector
- 28:18is 0 1 0 0; this is my first sentence.
- 28:24If you notice carefully, then the inner
- 28:27vector is a 1D tensor, and there are
- 28:30two such 1D tensors. So it means this
- 28:32entire thing, the outer one, is a 2D
- 28:34tensor. Right? Now, if the first
- 28:37sentence is a 2D tensor. Then the
- 28:40second sentence will also be a 2D
- 28:41tensor. And the third sentence will
- 28:43also be a 2D tensor. Basically, you
- 28:46have a collection of three 2D tensors.
- 28:49So what has it basically become? A 3D
- 28:51tensor. Right? So if I want to write it
- 28:53, I will write it here again. This is
- 29:02my second 2D tensor and this is my
- 29:06third sentence. Right? And if I want to
- 29:11write their collection, then this
- 29:15became my 3D tensor. Right? If I have
- 29:19to write its shape, how would the shape
- 29:21be? We have vectors of four. Like,
- 29:27vectors of size four. And the matrices
- 29:30I have are of size 2/4. Okay? Meaning,
- 29:35basically two rows, four columns, and I
- 29:38have three such matrices. So this is
- 29:42the shape, guys, of my 3D tensor. There
- 29:45are three axes here. Meaning, it is
- 29:47rank three. And how many items are
- 29:48there? 3*2*4, 24 items, which is
- 29:52actually correct. Okay? So, this is one
- 29:56good example of a 3D tensor. Okay? I
- 29:59will give you one more example of a 3D
- 30:01tensor. Okay? Uh, watch carefully. So,
- 30:13another good example of a 3D tensor is
- 30:15time series data. Time series data
- 30:23means data that you collect after a
- 30:27frequent time period. For example,
- 30:30share market data that you collect on a
- 30:32daily basis, or sometimes you collect
- 30:35every hour. Depends. Okay? I will show
- 30:37you one example. Let's say, what data
- 30:39are you storing? The highest and lowest
- 30:44price of any stock during the day. Okay
- 30:51? And you are filling this data once a
- 30:54day for this entire stock, and you are
- 30:57doing this for the whole year. Okay? So
- 31:01the data you will have will be 365 by 2
- 31:04, think about it, because it will look
- 31:07like this: Day one, some number, some
- 31:10number; Day two, some number, some
- 31:13number; Day 365, some number, some
- 31:16number. Right? So basically, you have
- 31:19365 on the row axis and two on the
- 31:21column axis, because you have two input
- 31:23columns. So this data you have is a 2D
- 31:28tensor. If you are tracking two
- 31:30quantities throughout the day for the
- 31:32whole year, then it is a 2D tensor. But
- 31:35what if I have 10 years of data for
- 31:38this same stock? I have tracked this
- 31:41thing for 10 years. So, how many such
- 31:442D tensors do I have? 10, so the
- 31:48resultant tensor will be 10 cross 365
- 31:52cross 2, which is a 3D tensor. A
- 31:58collection of 10 2D tensors is a 3D
- 32:01tensor. Right? So, whenever you get
- 32:04time series data, it is 3D data. And
- 32:06the middle axis in it, this axis here,
- 32:09is your time axis. This is your time
- 32:12axis. Okay? So you will work a lot on
- 32:14time series later on. Most financial
- 32:16data, or often medical data, is time
- 32:18series-based data, and a lot of
- 32:20analysis is done on it, and it is a
- 32:23very important thing, and this is an
- 32:25example where 3D tensors are used. Okay
- 32:28, I hope you are understanding up to
- 32:29this point. Now we have 4D tensors and
- 32:315D tensors left, let's complete them,
- 32:33let's cover them. Now let's focus on 4D
- 32:35tensors. Okay, a great example of a 4D
- 32:38tensor is images, right? Image-based
- 32:42data. So, if you ever work in the
- 32:46domain of computer vision, or work with
- 32:49images, then the tensors you encounter
- 32:52there are 4D tensors. Let me show you
- 32:55how. Okay? If you have ever studied
- 32:58even a little bit of image processing,
- 33:01you might know that any image is
- 33:03basically a collection of pixels.
- 33:06Pixels look like this. There are very
- 33:09small pixels on your screen like this.
- 33:12And every pixel has a numerical value
- 33:15because of which you see something.
- 33:18Like, if this value is black, this
- 33:20value is black, and all these values
- 33:23are black, then you see a two on your
- 33:25screen. Everything else was one. Only
- 33:28these values are black. So this is a
- 33:31two on your screen. So by controlling
- 33:33these pixels, you can display any image
- 33:36. Now, if you want to show a color
- 33:39image, you take three channels like
- 33:42this. This is the first channel that I
- 33:45showed you. You take three channels
- 33:47like this. This is R, this is G, and
- 33:49this is B. Red, Green, Blue. Okay? And
- 33:52you stack these three on top of each
- 33:53other like this. Right? So suppose
- 33:56these three are obviously matrices.
- 34:01These three are actually 2D tensors.
- 34:03And if you combine three 2D tensors, it
- 34:05becomes a 3D tensor. Right? Suppose one
- 34:09has a shape of 1200 by 800, and you
- 34:12have three such channels, so combining
- 34:17them you get 3, 1200, 800—three
- 34:20channels and 1200*800 pixels in each
- 34:24channel, okay? So that's for one image.
- 34:28If you are talking about that, it is a
- 34:313D tensor, right? But what if you have
- 34:34multiple images? Suppose you have 50
- 34:37color photographs like this. If you
- 34:40have 50 color images. In that case, you
- 34:45get a 50 by 3 by 1200 by 800 tensor.
- 34:51And this is your 4D tensor. So in the
- 34:55future, if you ever do image processing
- 34:57or study convolutional neural networks
- 34:59in deep learning, when you work with
- 35:01images there, you work with batches of
- 35:03images. And those batches of images are
- 35:06a great example of a 4D tensor. Okay?
- 35:09So I hope you understood this example
- 35:11as well. Now let's move on to 5D
- 35:13tensors. So I guess you must have
- 35:15already understood 5D tensors. A very
- 35:17good example of that is videos. Right?
- 35:22Because what are videos? If you think
- 35:24carefully. You probably already know.
- 35:26Videos are basically images that are
- 35:29passed in front of your eyes very
- 35:31quickly. Because they pass at such a
- 35:35speed, our eyes and our brain cannot
- 35:37distinguish that they are two separate
- 35:40images. Because the speed at which the
- 35:44images are crossed is higher than our
- 35:47persistence of vision. So I will show
- 35:50you, it is written here that our
- 35:53persistence of vision can only classify
- 35:5512 separate images in one second. If
- 35:58you pass more than 12 images in front
- 36:01of our eyes in one second. Then it
- 36:03gives us a feeling of a video. Right?
- 36:06And this generally happens with 30fps,
- 36:0860fps, 120fps; I am not sure about
- 36:10120fps. But videos are passed at up to
- 36:1460 frames per second. So obviously
- 36:16those images are passing so fast that
- 36:18we feel it is in continuity. It is a
- 36:20video. Right? So in short, videos are
- 36:25basically frames. Right? A collection
- 36:30of frames. Right? A single frame means
- 36:32one image. One frame means one image.
- 36:35Right? So let's say you have a 60-
- 36:41second video that was shot at 30 FPS
- 36:49and is 480p. 480p means its resolution
- 36:54is 480 by 720. Obviously, this is color
- 36:58, so it will have three channels. RGB.
- 37:01Right. So now look, a single image
- 37:04inside it is obviously 480. 720, 3, a
- 37:09single image, right. Now I have a 60-
- 37:14second video, shot at 30 FPS, so how
- 37:17many images do you have? If you want to
- 37:21calculate this, you are getting 30
- 37:23videos in one second, 30 frames per
- 37:25second. In one second you are getting
- 37:2830 videos, sorry, 30 images. And you
- 37:30have 60 seconds like that. So that
- 37:32means you have 1800 images. Right? If
- 37:36there are 30 in 1 second, then in 60...
- 37:39sorry, 1800, please ignore if there is
- 37:41any calculation mistake. You have 1800
- 37:45images. Right? This is already your 4D
- 37:48data. Now suppose you have four video
- 37:50clips like this. You have got four
- 37:53videos. So now the data you have, this
- 37:56collection of four videos, is actually
- 37:58a 5D tensor. This is a 5D tensor. The
- 38:03collection of videos is a 5D tensor.
- 38:05Where the first thing tells you how
- 38:07many videos there are. This part tells
- 38:09you how many images are in a single
- 38:11video, and this is the size of each
- 38:13image and how many color channels it
- 38:14has. So, this is a 5D tensor. Now, if
- 38:17you multiply these numbers. If you
- 38:19multiply these numbers, it becomes a
- 38:21very large number. Right? Let me show
- 38:23you how large this number will be. This
- 38:27number will be: we have four videos
- 38:30multiplied by 1800 images, multiplied
- 38:34by 480 by 720. I want to show you the
- 38:38calculation I am doing. Multiplied by 3
- 38:42, this is the number, guys. This is the
- 38:44number and this is a big number. Right?
- 38:47Now think, if you have this many values
- 38:49, basically there are this many numbers
- 38:51here. Right? Because if you multiply
- 38:54all these, the tensor has that many
- 38:55values. That many items. So, there are
- 38:58this many items here. Now think, if you
- 39:01store each item in a float, in a
- 39:04float32. Basically, if you store every
- 39:07single item in a 32-bit float, how much
- 39:10total storage will you need? Multiply
- 39:14by 32, meaning you would need this many
- 39:17bits. So, how much would this number be
- 39:19in bytes? Divided by 8, you would need
- 39:22this many bytes. If I want to convert
- 39:25this into kilobytes, I will divide it
- 39:28by 1024. Here is the number in
- 39:31kilobytes. If I want to convert it into
- 39:34megabytes, I will divide it again by
- 39:371024. Such a huge megabyte. And if I
- 39:41divide it again by 1024, here is our GB
- 39:45, 27 GB. Meaning, if you want to store
- 39:50four 60-second videos, or process them,
- 39:54or run machine learning or deep
- 39:57learning on them, you would need 27 GB
- 40:01of space. Obviously, you don't do that,
- 40:06and that is where video codec formats
- 40:10come in, video encoding formats like
- 40:13MKV or MP4. Okay, what do they do?
- 40:17Whatever you are giving to a single
- 40:19item, they reduce it significantly, and
- 40:22you can then do all this work with much
- 40:25less data. Honestly speaking, video
- 40:27processing is somewhat rare, unless you
- 40:29are building an application like
- 40:30YouTube where you have to process every
- 40:32video before it gets uploaded. You most
- 40:34likely won't be working on videos, but
- 40:36you might if you are working in the
- 40:38computer vision domain. You never know,
- 40:40right? But yeah, you personally won't
- 40:42have sensors beyond 5D, there aren't
- 40:44really any use cases beyond that. Okay,
- 40:47so 5D will be the max and that's it.
- 40:50That's what I wanted to discuss in this
- 40:51video. I know it has become a quite
- 40:53long video, it must be around 40
- 40:55minutes, I guess. So what can I say, I
- 40:58taught it in a bit of detail. I hope
- 41:01you understood it and this will provide
- 41:04us a good start when we do things ahead
- 41:07. Okay, so yeah, that's it guys. If you
- 41:10liked the video, please consider
- 41:12subscribing. If possible, do share it
- 41:14with someone if they are learning
- 41:15machine learning. And let's meet
- 41:17tomorrow, tomorrow we will do an
- 41:19end-to-end machine learning example so
- 41:21that you get a bird's eye overview of
- 41:23the whole process. So yeah, thanks for
- 41:25watching, will meet tomorrow, bye.
About this transcript
This page contains the full transcript of What are Tensors | Tensor In-depth Explanation | Tensor in Machine Learning by CampusX, generated from the public captions YouTube serves with the video. The transcript has 6,534 words across 909 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.