YouTube2Text

What are Tensors | Tensor In-depth Explanation | Tensor in Machine Learning — Transcript

by CampusX · 6,534 words · 909 segments · language en · Watch on YouTube

Full transcript

  1. 0:00Hello guys, welcome to my YouTube
  2. 0:01channel. This is 100 Days of Machine
  3. 0:03Learning and today is day 11. So, 10
  4. 0:07days have passed and we have discussed
  5. 0:10many things, mostly related to the "why
  6. 0:12" and "what" parts of machine learning.
  7. 0:16The "why" and the "what," right? But
  8. 0:19from now on, from today onwards, we are
  9. 0:21going to focus on the "how" parts. How
  10. 0:23does machine learning happen? We are
  11. 0:25starting practical machine learning
  12. 0:26from today. And our topic for today is
  13. 0:29a very important one, because it is by
  14. 0:31using this that machine learning is
  15. 0:33able to function. Okay? Today we are
  16. 0:36going to study about tensors. Okay? I
  17. 0:38hope you have heard the name at some
  18. 0:40point. What are tensors? And why are we
  19. 0:44studying tensors today? I will answer
  20. 0:46these two questions for you very
  21. 0:47quickly. First, let me answer why we
  22. 0:49are studying tensors. First of all.
  23. 0:51Right? We could have studied something
  24. 0:53else. But why is the first thing we
  25. 0:54started in practical machine learning
  26. 0:56tensors? Let's discuss that. So, a
  27. 0:59tensor is basically a data structure. I
  28. 1:03mean, if I explain it to you in the
  29. 1:05simplest terms, a tensor is nothing but
  30. 1:07a data structure. A data structure
  31. 1:09means a way to store data. Now, why are
  32. 1:11we studying tensors first? The reason
  33. 1:14is that today, all machine learning
  34. 1:17systems and all the leading libraries
  35. 1:19like Scikit-learn and TensorFlow use
  36. 1:21the tensor as their most basic data
  37. 1:24structure. Meaning, whatever machine
  38. 1:27learning problem you are solving, in
  39. 1:29the machine learning space or the deep
  40. 1:31learning space, you will have to deal
  41. 1:33with tensors. So, as a beginner, I
  42. 1:35honestly skipped this topic. And later
  43. 1:38I realized that things wouldn't work
  44. 1:40without studying tensors. So, so that
  45. 1:42you don't make this mistake, we are
  46. 1:44starting with tensors. What you learn
  47. 1:47today will not only help you in machine
  48. 1:49learning but will also help you in deep
  49. 1:51learning. Okay? So, it is a very
  50. 1:53general topic. A very important topic.
  51. 1:55Let's start with the topic. Okay? So,
  52. 1:57one more thing I forgot to mention.
  53. 2:00Tensors are so important that the
  54. 2:02number one deep learning library today,
  55. 2:05Google's TensorFlow, is named after
  56. 2:08tensors. So, trust me, it is a very
  57. 2:09important concept. Okay? So, yeah,
  58. 2:11anyways, what is a tensor? Let's start
  59. 2:13the discussion from there again. A
  60. 2:14tensor is nothing but a data structure.
  61. 2:17Data structure means a way to store
  62. 2:19data. Right? And if we break down
  63. 2:22tensors into even simpler terms, a
  64. 2:24tensor is just a container where you
  65. 2:25store numbers. That's it. A tensor is
  66. 2:29nothing but a container for numbers.
  67. 2:31Sometimes you store characters or
  68. 2:33strings too. But that is very rare.
  69. 2:35Almost never do you do that. 99.99%of
  70. 2:38the time, it is going to be a container
  71. 2:41for numbers. Right? Now guess what? You
  72. 2:44have used tensors before. I mean, even
  73. 2:47if you haven't heard the name, you have
  74. 2:48used them. So if you have ever studied
  75. 2:51about vectors or matrices in your life,
  76. 2:53they are actually tensors. So when it
  77. 2:56is zero-dimensional, you call it a
  78. 2:58scalar. A single number. When there is
  79. 3:01a list of numbers, you call it a vector
  80. 3:03. When there are numbers in 2D, you
  81. 3:06call it a matrix. Right? Now think, if
  82. 3:09there are numbers in 3D, what would you
  83. 3:10call it? Or what would you call it in
  84. 3:124D? So that is why computer scientists,
  85. 3:15in fact, this is an area combining
  86. 3:17physics and math. They decided.
  87. 3:20Scientists decided to coin a general
  88. 3:22term. And they use, they call it a
  89. 3:25tensor. Right? So let's study more in
  90. 3:27detail about tensors. I will share my
  91. 3:29screen. So yeah, tensors are basically
  92. 3:33a container for storing numbers. We
  93. 3:37have discussed this. Let's talk about
  94. 3:390D tensors, which are scalars. Right?
  95. 3:43So whenever you are storing a single
  96. 3:45number somewhere like this or like this
  97. 3:48. These are basically called scalars.
  98. 3:52Right? Or you can call it a 0D tensor.
  99. 3:58Right? 0D means that the number of
  100. 4:02dimensions of this particular tensor is
  101. 4:06zero. There are no dimensions. Right?
  102. 4:10If I show you practically, what I can
  103. 4:13do is I can go to Google Colab. If you
  104. 4:16don't know Google Colab, trust me, it's
  105. 4:19a very simple tool where you can run
  106. 4:22Python code online. Right? You don't
  107. 4:24need to know more than that. I am
  108. 4:26importing a library. Called NumPy,
  109. 4:28which we will use a lot in this video,
  110. 4:30and in machine learning in general,
  111. 4:31this library is used heavily. If you
  112. 4:33want to study NumPy, I will put a link
  113. 4:36to a playlist from my own channel in
  114. 4:38the description of this video. You can
  115. 4:40study NumPy from there. NumPy is very
  116. 4:42simple. All right? Now, if you want to
  117. 4:45create a scalar using NumPy. Then you
  118. 4:47don't have to write much. You just need
  119. 4:48to write one line of code. Let's say we
  120. 4:50are creating a variable A in which we
  121. 4:51will store the scalar. This will be
  122. 4:54equal to np.array and here I put a
  123. 4:58number and ran it. And as soon as I
  124. 5:03printed A, which is on your screen,
  125. 5:06this is actually a scalar or a 0D
  126. 5:09tensor. All right? How do we know it's
  127. 5:120D? Whenever you are creating tensors
  128. 5:14in NumPy. See, one more thing to
  129. 5:16remember, if you are from a computer
  130. 5:18science background, then a tensor and
  131. 5:20an n-dimensional array are the same
  132. 5:22thing. An array means one-dimensional.
  133. 5:24A 2D array means an array within an
  134. 5:26array. A 3D array means an array within
  135. 5:27an array within an array. All right? So
  136. 5:29in that way, n-dimensional array is the
  137. 5:31general term if you come from a math or
  138. 5:33physics background, it's called a
  139. 5:35tensor. So the machine learning
  140. 5:36community has adopted that term. So
  141. 5:37whenever you see an n-d array, just
  142. 5:40understand that it is a tensor. All
  143. 5:42right? So here you have a 0D array.
  144. 5:44Because there is no array at all. There
  145. 5:45is just one number. So you call this a
  146. 5:470D tensor. So whenever you create
  147. 5:49arrays or tensors in NumPy, you can
  148. 5:52find their dimension. You have an
  149. 5:54attribute called ndim. It's clear from
  150. 5:57the name. Number of dimensions. As soon
  151. 5:59as you run this, you get zero. From
  152. 6:02this, you understand that the dimension
  153. 6:03of this particular tensor is zero. And
  154. 6:06that is why it is a 0D tensor. All
  155. 6:08right? So I showed you a small
  156. 6:10demonstration of this. Let's quickly
  157. 6:12discuss a 1D tensor. So if you have a
  158. 6:15list of numbers like this 1, 2, 3, 4,
  159. 6:20then this is a 1D tensor, and you can
  160. 6:25also call it a vector, and in
  161. 6:29programming, you can call it a 1D array
  162. 6:35or simply an array. All right? Now, for
  163. 6:39this thing, this tensor, what will its
  164. 6:42ndim be? It would be one. Meaning, this
  165. 6:46is a tensor that has one dimension. All
  166. 6:48right? Now you need to understand one
  167. 6:51more thing, what is an axis in terms of
  168. 6:53tensors? So an axis is basically how
  169. 6:56many dimensions you have. All right?
  170. 6:58Basically, if you have two dimensions.
  171. 7:01Let's say your tensor has two
  172. 7:02dimensions. We will discuss 2D tensors
  173. 7:04a little later. So this means there are
  174. 7:06two axes there. Okay? If it is a 3D
  175. 7:09tensor, then you have three axes. Okay?
  176. 7:11So dimensions and axes are related.
  177. 7:15Okay? Whatever the D of the tensor is,
  178. 7:17whatever the dimension of the tensor is
  179. 7:19, you will have that many axes. And one
  180. 7:22more thing, the number of axes is also
  181. 7:25called rank in some places, and you
  182. 7:28also call that dimension. Okay? So,
  183. 7:32there shouldn't be any confusion here.
  184. 7:34If you go and read in different places,
  185. 7:35you will see different things. I am
  186. 7:37just letting you know. Number of axes
  187. 7:39is equal to rank, equal to the
  188. 7:40dimension of the tensor. Okay? Let me
  189. 7:43quickly show you how you can create a
  190. 7:461D tensor using NumPy. So, I am
  191. 7:49creating an array. And here, what I am
  192. 7:52doing is sending a Python list inside
  193. 7:54the array that contains these values.
  194. 7:58And now if I print the array, this,
  195. 8:00which is on your screen, is a 1D tensor
  196. 8:03. Okay? And if I write arr.ndim, you
  197. 8:07would see that its n-dimension is one.
  198. 8:11So, the number of axes inside it is one
  199. 8:13. Its rank is also one, and it is a 1D
  200. 8:16tensor. Okay? I guess you have no
  201. 8:18confusion up to this point. Now I will
  202. 8:19say something that might confuse you a
  203. 8:21little. But it is very important to
  204. 8:22understand. Okay? Listen carefully.
  205. 8:25This 1D tensor, you also call this a
  206. 8:32vector. This is also a vector. You can
  207. 8:35call it a vector. Okay? But if someone
  208. 8:37asks you what the dimension of this
  209. 8:39vector is? Then, since there are four
  210. 8:42numbers here, you would say the
  211. 8:44dimension of this vector is 4D. Now you
  212. 8:47will say that I just said that this is
  213. 8:491D. I said it is a 1D tensor. This is a
  214. 8:521D tensor. And since it is a 1D tensor,
  215. 8:55it means it is a vector. But how many
  216. 8:57dimensions are inside this vector? Four
  217. 8:59dimensions. Now this might seem a
  218. 9:01little confusing to you. But I will
  219. 9:03give you an example in a little while
  220. 9:04that will make this crystal clear as to
  221. 9:06what I am trying to say. At this point,
  222. 9:10just create a distinction in your mind
  223. 9:12that whenever you create a 1D tensor,
  224. 9:14it is a vector, and the number of
  225. 9:16values in that vector determines the
  226. 9:18dimension of that vector. Meaning, if I
  227. 9:22give you another example, let's say one
  228. 9:25and two, this is still a 1D tensor. And
  229. 9:29it is also a vector. But if someone
  230. 9:31asks you, how many dimensions does this
  231. 9:32vector have? Then you would say two,
  232. 9:34because there are two numbers in it. I
  233. 9:35hope you guys are following along so
  234. 9:38far. Okay? If you are following along
  235. 9:41so far, you must have also noticed one
  236. 9:43thing: if you keep adding more scalars
  237. 9:46to a scalar, then it becomes a vector.
  238. 9:52Meaning, if you added four scalars here
  239. 9:56, you got a vector. Always remember
  240. 9:59this. In the case of tensors, as you
  241. 10:01increase the dimension, how do those
  242. 10:03dimensions always increase? If you keep
  243. 10:06adding the previous dimension, you get
  244. 10:08the next dimension. Meaning, if you
  245. 10:11want a 1D tensor, basically it is a
  246. 10:13collection of multiple 0D tensors. If
  247. 10:17you want a vector, it is basically a
  248. 10:18collection of scalars. If you want
  249. 10:21matrices, it is basically a collection
  250. 10:23of vectors. So, you must always
  251. 10:24remember this fundamental. Okay? Now
  252. 10:27let me show you an example of a 2D
  253. 10:28tensor, which we also call matrices.
  254. 10:30Okay? So now let's discuss what are 2D
  255. 10:33tensors or matrices. So, imagine if you
  256. 10:37have multiple vectors, like. So If you
  257. 10:47add or collect these vectors, then we
  258. 10:52call it a matrix. Okay? Something like
  259. 10:55this. 1 2 3 4 5 6 7 8 9, so basically
  260. 11:04matrices are nothing but a collection
  261. 11:07of vectors. Okay? And obviously, its
  262. 11:11dimension is 2D because there are two
  263. 11:14axes here. Okay? This one axis is the
  264. 11:16column axis, and this one axis is the
  265. 11:18row axis. Okay? So, I hope you are
  266. 11:20understanding this. Its rank is two,
  267. 11:23which is equal to the dimension, and
  268. 11:26there are two axes: rows and columns.
  269. 11:29Let me quickly show you how this is
  270. 11:31created. It is very simple. You will
  271. 11:34use NumPy again. Let's say I am
  272. 11:36creating a variable called MAT, and MAT
  273. 11:40is equal to np.array, and here what
  274. 11:43will I do? I will pass a list of lists
  275. 11:45in Python, something like this. 1, 2, 3
  276. 11:47, then a comma; the second item itself
  277. 11:50is going to be a list 4, 5, 6. And 7, 8
  278. 11:56, 9, okay. And if we print the MAT, you
  279. 12:00will get a structure like this; this is
  280. 12:02a matrix, okay. If you calculate its MM
  281. 12:06, then as expected, it would be two. 2D
  282. 12:13Tensor. Okay, now you can easily
  283. 12:15understand that if you want to create
  284. 12:17higher dimension tensors from this,
  285. 12:19what will you do? Basically, if you
  286. 12:22want to create a 4D tensor. Sorry, if
  287. 12:25you want to create a 3D tensor, you
  288. 12:27will have a matrix that looks like this
  289. 12:30. You will take more matrices like this
  290. 12:35. This is one matrix. This is the
  291. 12:38second one. This is the third one. So,
  292. 12:42basically, suppose if all these
  293. 12:44matrices have 3 by 3 numbers and you
  294. 12:47are taking four such matrices, then you
  295. 12:51will get a 4 by 3 by 3 tensor, which
  296. 12:54you will call a 3D tensor, and this
  297. 12:57time there are three axes here. Okay?
  298. 13:01So you have a column axis. You have a
  299. 13:04row axis and you have a depth axis.
  300. 13:08Okay? This is a 3D tensor. Okay? Or you
  301. 13:11can actually see it in terms of a
  302. 13:13cuboid. Right? Something like this.
  303. 13:19Right? Now, if someone asks you what a
  304. 13:224D tensor would look like? What would a
  305. 13:244D tensor be like? So a 4D tensor is
  306. 13:26basically a collection of 3D tensors.
  307. 13:28If this is a 3D tensor, then a 4D
  308. 13:30tensor will look like this. Basically,
  309. 13:36it will be a vector of 3D tensors. See
  310. 13:38like this. Now, sorry for my drawing.
  311. 13:42But these lines have 3D tensors placed
  312. 13:45in this way. These are what you call a
  313. 13:484D tensor. Right? Now, if someone asks
  314. 13:51you what a 5D tensor would look like?
  315. 13:53So a 5D tensor will be a matrix of 3D
  316. 13:55tensors. Basically, more cuboids will
  317. 13:59come underneath it like this. Like,
  318. 14:02this is a 5D tensor. Right? And that is
  319. 14:08how this entire thing works. This is a
  320. 14:115D tensor. I mean, if you focus only on
  321. 14:13this much. If you focus only on this
  322. 14:16much, then this is a 4D tensor. But if
  323. 14:22you focus on the whole thing. Focus on
  324. 14:24this whole thing, this is a 5D tensor.
  325. 14:26Okay? And that is how this entire thing
  326. 14:29is stacked on top of each other. Now,
  327. 14:30if you make a collection of 5D, it will
  328. 14:33become 6D. If you make a collection of
  329. 14:356D, it will become 7D. That is the
  330. 14:36whole idea. So where you were
  331. 14:38restricted to thinking only about
  332. 14:40scalars, vectors, and matrices, now you
  333. 14:42are completely free. Because you have a
  334. 14:45general concept that we call this thing
  335. 14:47a tensor, and in terms of that tensor,
  336. 14:49you can think or do things in any
  337. 14:51number of dimensions. Okay? Now, if you
  338. 14:54talk about machine learning, you mostly
  339. 14:56revolve between zero to 5D. You won't
  340. 14:59really find tensors with higher
  341. 15:00dimensions than that. Okay? So that’s
  342. 15:02why, in today’s video, I will show
  343. 15:04you one example of each of the five
  344. 15:06tensors. Okay? To be honest, there’s
  345. 15:09no need to show an example of a scalar.
  346. 15:11So I will show you all examples from 1D
  347. 15:13up to 5D. Practical examples, the kind
  348. 15:15of examples you might encounter later
  349. 15:16on. Okay? But before we go there, I
  350. 15:18just want to tell you one more thing.
  351. 15:20This is the last concept. Although we
  352. 15:22have discussed it a little bit already.
  353. 15:23What do we call rank, axis, and shape?
  354. 15:25Okay? Let’s discuss it quickly. Rank
  355. 15:28is just that. The number of dimensions
  356. 15:30your tensor has is what is called its
  357. 15:32rank. It is also called the number of
  358. 15:35axes. Number of axes is equal to rank,
  359. 15:40is equal to the number of dimensions.
  360. 15:45These three things are the same, guys.
  361. 15:46Okay? You must always remember this.
  362. 15:48Let me explain it to you in terms of a
  363. 15:51matrix. Suppose you have this matrix.
  364. 15:53It is a 3x3 matrix. So, obviously, it
  365. 15:56has two axes. One row axis and one
  366. 15:59column axis. One more thing comes up
  367. 16:02here. Then what is shape? Shape is how
  368. 16:07many items you have in any particular
  369. 16:10axis. For example, if you talk about
  370. 16:13the row axis, how many maximum items
  371. 16:15can you store in the row axis? Three.
  372. 16:18Right? It’s 3x3, so you can only
  373. 16:20store three. So the shape of this
  374. 16:23matrix is 3x3, but you can also have a
  375. 16:25matrix like this. This is also a matrix
  376. 16:30. If you calculate its ndim or its rank
  377. 16:34, it will still be two. Because it
  378. 16:38still has a column axis and a row axis
  379. 16:40here. But this time, if you calculate
  380. 16:43the shape, it will be 2x3. Why am I
  381. 16:46saying that? Because in the row, in the
  382. 16:48row axis, how many maximum items can
  383. 16:50you store? Two. And how many can you
  384. 16:52store in the column axis? Three. Okay?
  385. 16:55Let’s take one more example. This is
  386. 17:004x2. Okay? There is one more thing,
  387. 17:03size. Size of a tensor. So size means
  388. 17:06how many items are in that tensor. So
  389. 17:08simply multiply its shape, all the
  390. 17:10numbers. Multiply these. 2x3 is six.
  391. 17:14Meaning there are six items inside it.
  392. 17:16If you multiply 3 here, you get nine.
  393. 17:18Meaning there are nine items here. Here
  394. 17:20, multiply 4 by 2, you get eight.
  395. 17:21Meaning there are eight items. Okay? So
  396. 17:23, whenever you want to find out how
  397. 17:25many items are in a particular tensor,
  398. 17:27which we call size, it is very simple
  399. 17:28to calculate. You just find its shape
  400. 17:30and multiply all the numbers. Okay?
  401. 17:32This is just not true for scalars. For
  402. 17:34a scalar, the size is always equal to
  403. 17:37one. It's simple. If it is a scalar,
  404. 17:39its size will be one. For tensors above
  405. 17:41that, you simply have to multiply their
  406. 17:43shape. If you have a vector, a 1D
  407. 17:45tensor, it will look something like
  408. 17:48this. So in this case, your shape will
  409. 17:52be three. Okay? Because here, in one
  410. 17:55single place, there are three items.
  411. 17:57Okay? So here, three is your number of
  412. 18:00items. Okay? I hope you are
  413. 18:02understanding. It is very simple. Once
  414. 18:04you grasp the concepts. Okay? So rank,
  415. 18:06axis, and shape, these are the three
  416. 18:08things we discussed. In fact, we also
  417. 18:10discussed size. Now, the most important
  418. 18:13part of this video: we will see
  419. 18:14practical examples of tensors from 1D
  420. 18:17to 5D that you will encounter later in
  421. 18:19your life while doing machine learning
  422. 18:21and deep learning projects. We are
  423. 18:23going to do that next. Let me now give
  424. 18:26you two practical examples of a 1D
  425. 18:27tensor that you will see everywhere in
  426. 18:29machine learning in the future. Okay?
  427. 18:32And this is where you will understand
  428. 18:35why I said a little while ago that a
  429. 18:37vector is a 1D tensor but its own
  430. 18:39dimensions are different. You will
  431. 18:42understand this now. Okay? And to
  432. 18:44explain this whole thing, I will take
  433. 18:45an example. Let's say we have a dataset
  434. 18:48about students. We have discussed this
  435. 18:50dataset before as well. We have a
  436. 18:52dataset of students. And in that
  437. 18:55dataset, we have four columns. The
  438. 18:57first column is CGPA. The second column
  439. 19:02is IQ. The third column is state. Which
  440. 19:05state are they from? West Bengal,
  441. 19:07Kerala, or whatever it is. Okay? And
  442. 19:09here it is whether they got placed or
  443. 19:12not. Which is your target column that
  444. 19:14you need to predict. Classification
  445. 19:16problem. Okay? Now, let's assume I have
  446. 19:20data for 10,000 students, and obviously
  447. 19:22, my end goal is to build a
  448. 19:25classification model, so if I provide a
  449. 19:28new student's details—meaning IQ,
  450. 19:30CGPA, and state—it should tell me
  451. 19:33whether they will be placed or not. I
  452. 19:36need to make this prediction. Right? So
  453. 19:38, what do I do here? I will show you an
  454. 19:41example of a 1D tensor. I'll show you
  455. 19:44an example of a 1D tensor or a vector.
  456. 19:47For that, you'll have to forget the
  457. 19:4910,000 students and focus on just one
  458. 19:52student. If you focus only on the first
  459. 19:54student. Let's say our first student
  460. 19:57has a CGPA of 8.1, an IQ of 91, is from
  461. 20:01West Bengal, and they got placed. For
  462. 20:05now, we are ignoring the placement
  463. 20:07column because we are going to focus on
  464. 20:09the input columns. So, if you take
  465. 20:12their input, if you take that student's
  466. 20:14input, basically it is a set of three
  467. 20:16things. Now, let's assume for a moment
  468. 20:19that our data only contains students
  469. 20:21from two states. The first state is
  470. 20:23West Bengal, the second is Bengaluru,
  471. 20:26Karnataka. Right? And assume we are
  472. 20:29calling West Bengal zero and Karnataka
  473. 20:32one. We have performed numerical
  474. 20:34encoding. This is called label encoding
  475. 20:35. You will understand this in future
  476. 20:37videos. So, basically, instead of West
  477. 20:40Bengal, I wrote zero. So if we only
  478. 20:43talk about the first student, we have
  479. 20:46these numbers: 8.1, 91, and 0, and
  480. 20:49guess what? This is a tensor, a 1D
  481. 20:52tensor. Or you could call it a vector.
  482. 20:56This is a vector. And what is the
  483. 20:58dimension of this tensor? 1D, because
  484. 21:00it has only one axis. There is only one
  485. 21:03axis. But what is the dimension of this
  486. 21:05vector? It is a 3D vector. Why am I
  487. 21:08saying that? Because there are three
  488. 21:11axes here. There are three axes here.
  489. 21:13What is the first axis? CGPA. What is
  490. 21:16the second axis? IQ, and what is the
  491. 21:19third axis? State. And this student is
  492. 21:24somewhere here. There is that vector.
  493. 21:27This is the vector. Now, suppose if
  494. 21:31there were another student, 7.2 IQ, 102
  495. 21:33, Karnataka, no placement, so assume
  496. 21:36they would lie somewhere here,
  497. 21:38something like this. Right? They would
  498. 21:42have their own separate vector. So,
  499. 21:45basically, if you talk about it, in
  500. 21:47this three-dimensional space, you have
  501. 21:49data for 10,000 students. Data for
  502. 21:5110,000 students. Meaning you have
  503. 21:5410,000 vectors. Each vector represents
  504. 21:56a student in this three-dimensional
  505. 21:59coordinate space. So when you are
  506. 22:01talking about vectors, this is a 3D
  507. 22:02space. But when you represent those
  508. 22:05numbers in the form of a tensor, it is
  509. 22:07a 1D tensor. So these are two different
  510. 22:09things. Do not get confused between
  511. 22:11them. Are you talking about the
  512. 22:13dimension of the tensor or the
  513. 22:15dimension of the vector? There is a
  514. 22:17difference in that. Right? It became 3D
  515. 22:19here because you had three input
  516. 22:21columns. If you have 50 input columns,
  517. 22:24then you are working in a 50-
  518. 22:26dimensional coordinate space. But it
  519. 22:28will still remain a 1D tensor. Please,
  520. 22:31I hope you are understanding this point
  521. 22:33. Okay? One more example where you will
  522. 22:36find a 1D tensor. One example is
  523. 22:37already this. Whenever you get any
  524. 22:39machine learning tabular data, each row
  525. 22:42is actually a 1D tensor or a vector.
  526. 22:45Okay? One more example is, now think if
  527. 22:47you had data for these 10,000 students
  528. 22:49here, then everyone's output would look
  529. 22:52something like this. In this way. Now
  530. 22:54if you take out all these numbers. If
  531. 22:57you extract all these numbers and
  532. 22:59represent them in this manner as well.
  533. 23:06Then there will be a total of 10,000
  534. 23:09numbers in it. But this is also a 1D
  535. 23:13tensor. This is also a 1D tensor. Okay?
  536. 23:18So I hope you are understanding a bit
  537. 23:20by now where you can find 1D tensors in
  538. 23:22machine learning. Okay? Now let me show
  539. 23:25you where you will find 2D tensors.
  540. 23:28Where will you find 2D tensors? It is a
  541. 23:30very simple thing, guys. What is a 2D
  542. 23:32tensor? A 2D tensor is basically a
  543. 23:34collection of 1D tensors. Right? So if
  544. 23:37you have the data we had earlier of
  545. 23:41CGPA, IQ, state, and placement. Now
  546. 23:45let's forget about placement for a
  547. 23:47moment. If we only focus on the input
  548. 23:48columns. We had data for 10,000
  549. 23:50students. Like this. So here, every
  550. 23:55student's data is actually a vector or
  551. 23:59a 1D tensor. So a collection of 10,000
  552. 24:02vectors would be what? A matrix, right?
  553. 24:06So you can actually write this entire
  554. 24:08thing inside a matrix. So whenever you
  555. 24:12get data in machine learning, its input
  556. 24:15—the collection of input columns—
  557. 24:18you can store all that data in a matrix
  558. 24:21. That is generally called a 2D tensor
  559. 24:23in machine learning. It is denoted by
  560. 24:25D. Okay? So it will look something like
  561. 24:28this. Data of the first student, data
  562. 24:31of the second student, data of the
  563. 24:34third student, data of 10,000 students.
  564. 24:38And this becomes your 2D tensor. Okay?
  565. 24:43Which is a collection of 10,000 vectors
  566. 24:46that are themselves 3D vectors. I hope
  567. 24:48you are understanding this so far. And
  568. 24:51this placement, whether it happened or
  569. 24:54not, is itself a 10,000-dimensional
  570. 24:57vector. And basically, it is a 1D
  571. 25:00tensor. This is a 1D tensor. Okay? This
  572. 25:03is a 2D tensor. I hope you understood
  573. 25:06this entire discussion. You will
  574. 25:08encounter this every time. If you ever
  575. 25:10work with tabular data, you are working
  576. 25:13with 1D tensors as well as 2D tensors.
  577. 25:16Basically, you are working with
  578. 25:18matrices and also with vectors. Or in
  579. 25:20summary, if I tell you, when you
  580. 25:23separate all the inputs, you can call
  581. 25:25it a matrix or a 2D tensor. And when
  582. 25:28you isolate the entire input, which is
  583. 25:30a single column, it becomes your vector
  584. 25:31or your 1D tensor. Okay? So, I guess
  585. 25:34you understand where you find 1D and 2D
  586. 25:36tensors. Now let's move on to 3D
  587. 25:38tensors. So now let's focus our
  588. 25:40attention on 3D tensors. To be honest,
  589. 25:423D tensors are a bit rare. Calling them
  590. 25:45rare would actually be wrong. It's just
  591. 25:47that they aren't as frequent as your 1D
  592. 25:50and 2D tensors. If you want to see a
  593. 25:52practical example of 3D tensors. In
  594. 25:53fact, I will show you two practical
  595. 25:55examples. One of them, a very intuitive
  596. 25:57and interesting practical example, is
  597. 26:00from the domain of NLP, Natural
  598. 26:01Language Processing. Meaning, if you
  599. 26:04are ever working with any textual data,
  600. 26:07you will see 3D tensors there. Let me
  601. 26:09show you how. So let's say I have some
  602. 26:12text written. Hi Nitish. Hi Rahul and
  603. 26:20Hi Ankit. These are three texts. And
  604. 26:24assume these three texts are my input.
  605. 26:27I have to give these three texts to the
  606. 26:29algorithm to train. Now, obviously, you
  607. 26:32know that machine learning algorithms
  608. 26:33are pure mathematics. They don't
  609. 26:35understand text or strings. So you will
  610. 26:38have to convert this text into numbers,
  611. 26:41or, in other words, into vectors. This
  612. 26:43entire process is actually called
  613. 26:45vectorization in the NLP domain. Where
  614. 26:47you convert a given text into numbers
  615. 26:50or vectors. So that is a very important
  616. 26:52thing. And there are many different
  617. 26:54vectorization techniques. But we are
  618. 26:55going to focus on a very simple and
  619. 26:57very intuitive technique. Let me show
  620. 26:59you. So what I will do is, I need to
  621. 27:02convert all these sentences into
  622. 27:04numbers. For that, what I will do is, I
  623. 27:08will list down all my words, all my
  624. 27:11unique words. Like 'Hi' once, 'Nitish'
  625. 27:14once, basically I wrote down my entire
  626. 27:17vocabulary. Vocabulary means the set of
  627. 27:20all unique words present in my corpus,
  628. 27:22as it is called here. Basically, in my
  629. 27:25text. Okay? Now what will we do? We
  630. 27:29will represent every word, every
  631. 27:31particular word, with a vector. For
  632. 27:34instance, if I have to represent 'Hi',
  633. 27:37then 'Hi' becomes 1 0 0 0. Whenever I
  634. 27:42find 'Hi' written anywhere, I can write
  635. 27:44this instead. If I find 'Nitish'
  636. 27:46written anywhere, I will write 0 1 0 0;
  637. 27:50that becomes 'Nitish'. Okay? If I have
  638. 27:53to represent 'Rahul', then it becomes
  639. 27:55this. And if I have to represent 'Ankit
  640. 27:58', then it becomes this. Right? Now, if
  641. 28:01I talk about these sentences. If I talk
  642. 28:03about these sentences, let's say my
  643. 28:05first sentence is what? 'Hi Nitish'. So
  644. 28:07what is this actually? It is a set or
  645. 28:10collection of two vectors. So the first
  646. 28:14vector is 1 0 0 0 and the second vector
  647. 28:18is 0 1 0 0; this is my first sentence.
  648. 28:24If you notice carefully, then the inner
  649. 28:27vector is a 1D tensor, and there are
  650. 28:30two such 1D tensors. So it means this
  651. 28:32entire thing, the outer one, is a 2D
  652. 28:34tensor. Right? Now, if the first
  653. 28:37sentence is a 2D tensor. Then the
  654. 28:40second sentence will also be a 2D
  655. 28:41tensor. And the third sentence will
  656. 28:43also be a 2D tensor. Basically, you
  657. 28:46have a collection of three 2D tensors.
  658. 28:49So what has it basically become? A 3D
  659. 28:51tensor. Right? So if I want to write it
  660. 28:53, I will write it here again. This is
  661. 29:02my second 2D tensor and this is my
  662. 29:06third sentence. Right? And if I want to
  663. 29:11write their collection, then this
  664. 29:15became my 3D tensor. Right? If I have
  665. 29:19to write its shape, how would the shape
  666. 29:21be? We have vectors of four. Like,
  667. 29:27vectors of size four. And the matrices
  668. 29:30I have are of size 2/4. Okay? Meaning,
  669. 29:35basically two rows, four columns, and I
  670. 29:38have three such matrices. So this is
  671. 29:42the shape, guys, of my 3D tensor. There
  672. 29:45are three axes here. Meaning, it is
  673. 29:47rank three. And how many items are
  674. 29:48there? 3*2*4, 24 items, which is
  675. 29:52actually correct. Okay? So, this is one
  676. 29:56good example of a 3D tensor. Okay? I
  677. 29:59will give you one more example of a 3D
  678. 30:01tensor. Okay? Uh, watch carefully. So,
  679. 30:13another good example of a 3D tensor is
  680. 30:15time series data. Time series data
  681. 30:23means data that you collect after a
  682. 30:27frequent time period. For example,
  683. 30:30share market data that you collect on a
  684. 30:32daily basis, or sometimes you collect
  685. 30:35every hour. Depends. Okay? I will show
  686. 30:37you one example. Let's say, what data
  687. 30:39are you storing? The highest and lowest
  688. 30:44price of any stock during the day. Okay
  689. 30:51? And you are filling this data once a
  690. 30:54day for this entire stock, and you are
  691. 30:57doing this for the whole year. Okay? So
  692. 31:01the data you will have will be 365 by 2
  693. 31:04, think about it, because it will look
  694. 31:07like this: Day one, some number, some
  695. 31:10number; Day two, some number, some
  696. 31:13number; Day 365, some number, some
  697. 31:16number. Right? So basically, you have
  698. 31:19365 on the row axis and two on the
  699. 31:21column axis, because you have two input
  700. 31:23columns. So this data you have is a 2D
  701. 31:28tensor. If you are tracking two
  702. 31:30quantities throughout the day for the
  703. 31:32whole year, then it is a 2D tensor. But
  704. 31:35what if I have 10 years of data for
  705. 31:38this same stock? I have tracked this
  706. 31:41thing for 10 years. So, how many such
  707. 31:442D tensors do I have? 10, so the
  708. 31:48resultant tensor will be 10 cross 365
  709. 31:52cross 2, which is a 3D tensor. A
  710. 31:58collection of 10 2D tensors is a 3D
  711. 32:01tensor. Right? So, whenever you get
  712. 32:04time series data, it is 3D data. And
  713. 32:06the middle axis in it, this axis here,
  714. 32:09is your time axis. This is your time
  715. 32:12axis. Okay? So you will work a lot on
  716. 32:14time series later on. Most financial
  717. 32:16data, or often medical data, is time
  718. 32:18series-based data, and a lot of
  719. 32:20analysis is done on it, and it is a
  720. 32:23very important thing, and this is an
  721. 32:25example where 3D tensors are used. Okay
  722. 32:28, I hope you are understanding up to
  723. 32:29this point. Now we have 4D tensors and
  724. 32:315D tensors left, let's complete them,
  725. 32:33let's cover them. Now let's focus on 4D
  726. 32:35tensors. Okay, a great example of a 4D
  727. 32:38tensor is images, right? Image-based
  728. 32:42data. So, if you ever work in the
  729. 32:46domain of computer vision, or work with
  730. 32:49images, then the tensors you encounter
  731. 32:52there are 4D tensors. Let me show you
  732. 32:55how. Okay? If you have ever studied
  733. 32:58even a little bit of image processing,
  734. 33:01you might know that any image is
  735. 33:03basically a collection of pixels.
  736. 33:06Pixels look like this. There are very
  737. 33:09small pixels on your screen like this.
  738. 33:12And every pixel has a numerical value
  739. 33:15because of which you see something.
  740. 33:18Like, if this value is black, this
  741. 33:20value is black, and all these values
  742. 33:23are black, then you see a two on your
  743. 33:25screen. Everything else was one. Only
  744. 33:28these values are black. So this is a
  745. 33:31two on your screen. So by controlling
  746. 33:33these pixels, you can display any image
  747. 33:36. Now, if you want to show a color
  748. 33:39image, you take three channels like
  749. 33:42this. This is the first channel that I
  750. 33:45showed you. You take three channels
  751. 33:47like this. This is R, this is G, and
  752. 33:49this is B. Red, Green, Blue. Okay? And
  753. 33:52you stack these three on top of each
  754. 33:53other like this. Right? So suppose
  755. 33:56these three are obviously matrices.
  756. 34:01These three are actually 2D tensors.
  757. 34:03And if you combine three 2D tensors, it
  758. 34:05becomes a 3D tensor. Right? Suppose one
  759. 34:09has a shape of 1200 by 800, and you
  760. 34:12have three such channels, so combining
  761. 34:17them you get 3, 1200, 800—three
  762. 34:20channels and 1200*800 pixels in each
  763. 34:24channel, okay? So that's for one image.
  764. 34:28If you are talking about that, it is a
  765. 34:313D tensor, right? But what if you have
  766. 34:34multiple images? Suppose you have 50
  767. 34:37color photographs like this. If you
  768. 34:40have 50 color images. In that case, you
  769. 34:45get a 50 by 3 by 1200 by 800 tensor.
  770. 34:51And this is your 4D tensor. So in the
  771. 34:55future, if you ever do image processing
  772. 34:57or study convolutional neural networks
  773. 34:59in deep learning, when you work with
  774. 35:01images there, you work with batches of
  775. 35:03images. And those batches of images are
  776. 35:06a great example of a 4D tensor. Okay?
  777. 35:09So I hope you understood this example
  778. 35:11as well. Now let's move on to 5D
  779. 35:13tensors. So I guess you must have
  780. 35:15already understood 5D tensors. A very
  781. 35:17good example of that is videos. Right?
  782. 35:22Because what are videos? If you think
  783. 35:24carefully. You probably already know.
  784. 35:26Videos are basically images that are
  785. 35:29passed in front of your eyes very
  786. 35:31quickly. Because they pass at such a
  787. 35:35speed, our eyes and our brain cannot
  788. 35:37distinguish that they are two separate
  789. 35:40images. Because the speed at which the
  790. 35:44images are crossed is higher than our
  791. 35:47persistence of vision. So I will show
  792. 35:50you, it is written here that our
  793. 35:53persistence of vision can only classify
  794. 35:5512 separate images in one second. If
  795. 35:58you pass more than 12 images in front
  796. 36:01of our eyes in one second. Then it
  797. 36:03gives us a feeling of a video. Right?
  798. 36:06And this generally happens with 30fps,
  799. 36:0860fps, 120fps; I am not sure about
  800. 36:10120fps. But videos are passed at up to
  801. 36:1460 frames per second. So obviously
  802. 36:16those images are passing so fast that
  803. 36:18we feel it is in continuity. It is a
  804. 36:20video. Right? So in short, videos are
  805. 36:25basically frames. Right? A collection
  806. 36:30of frames. Right? A single frame means
  807. 36:32one image. One frame means one image.
  808. 36:35Right? So let's say you have a 60-
  809. 36:41second video that was shot at 30 FPS
  810. 36:49and is 480p. 480p means its resolution
  811. 36:54is 480 by 720. Obviously, this is color
  812. 36:58, so it will have three channels. RGB.
  813. 37:01Right. So now look, a single image
  814. 37:04inside it is obviously 480. 720, 3, a
  815. 37:09single image, right. Now I have a 60-
  816. 37:14second video, shot at 30 FPS, so how
  817. 37:17many images do you have? If you want to
  818. 37:21calculate this, you are getting 30
  819. 37:23videos in one second, 30 frames per
  820. 37:25second. In one second you are getting
  821. 37:2830 videos, sorry, 30 images. And you
  822. 37:30have 60 seconds like that. So that
  823. 37:32means you have 1800 images. Right? If
  824. 37:36there are 30 in 1 second, then in 60...
  825. 37:39sorry, 1800, please ignore if there is
  826. 37:41any calculation mistake. You have 1800
  827. 37:45images. Right? This is already your 4D
  828. 37:48data. Now suppose you have four video
  829. 37:50clips like this. You have got four
  830. 37:53videos. So now the data you have, this
  831. 37:56collection of four videos, is actually
  832. 37:58a 5D tensor. This is a 5D tensor. The
  833. 38:03collection of videos is a 5D tensor.
  834. 38:05Where the first thing tells you how
  835. 38:07many videos there are. This part tells
  836. 38:09you how many images are in a single
  837. 38:11video, and this is the size of each
  838. 38:13image and how many color channels it
  839. 38:14has. So, this is a 5D tensor. Now, if
  840. 38:17you multiply these numbers. If you
  841. 38:19multiply these numbers, it becomes a
  842. 38:21very large number. Right? Let me show
  843. 38:23you how large this number will be. This
  844. 38:27number will be: we have four videos
  845. 38:30multiplied by 1800 images, multiplied
  846. 38:34by 480 by 720. I want to show you the
  847. 38:38calculation I am doing. Multiplied by 3
  848. 38:42, this is the number, guys. This is the
  849. 38:44number and this is a big number. Right?
  850. 38:47Now think, if you have this many values
  851. 38:49, basically there are this many numbers
  852. 38:51here. Right? Because if you multiply
  853. 38:54all these, the tensor has that many
  854. 38:55values. That many items. So, there are
  855. 38:58this many items here. Now think, if you
  856. 39:01store each item in a float, in a
  857. 39:04float32. Basically, if you store every
  858. 39:07single item in a 32-bit float, how much
  859. 39:10total storage will you need? Multiply
  860. 39:14by 32, meaning you would need this many
  861. 39:17bits. So, how much would this number be
  862. 39:19in bytes? Divided by 8, you would need
  863. 39:22this many bytes. If I want to convert
  864. 39:25this into kilobytes, I will divide it
  865. 39:28by 1024. Here is the number in
  866. 39:31kilobytes. If I want to convert it into
  867. 39:34megabytes, I will divide it again by
  868. 39:371024. Such a huge megabyte. And if I
  869. 39:41divide it again by 1024, here is our GB
  870. 39:45, 27 GB. Meaning, if you want to store
  871. 39:50four 60-second videos, or process them,
  872. 39:54or run machine learning or deep
  873. 39:57learning on them, you would need 27 GB
  874. 40:01of space. Obviously, you don't do that,
  875. 40:06and that is where video codec formats
  876. 40:10come in, video encoding formats like
  877. 40:13MKV or MP4. Okay, what do they do?
  878. 40:17Whatever you are giving to a single
  879. 40:19item, they reduce it significantly, and
  880. 40:22you can then do all this work with much
  881. 40:25less data. Honestly speaking, video
  882. 40:27processing is somewhat rare, unless you
  883. 40:29are building an application like
  884. 40:30YouTube where you have to process every
  885. 40:32video before it gets uploaded. You most
  886. 40:34likely won't be working on videos, but
  887. 40:36you might if you are working in the
  888. 40:38computer vision domain. You never know,
  889. 40:40right? But yeah, you personally won't
  890. 40:42have sensors beyond 5D, there aren't
  891. 40:44really any use cases beyond that. Okay,
  892. 40:47so 5D will be the max and that's it.
  893. 40:50That's what I wanted to discuss in this
  894. 40:51video. I know it has become a quite
  895. 40:53long video, it must be around 40
  896. 40:55minutes, I guess. So what can I say, I
  897. 40:58taught it in a bit of detail. I hope
  898. 41:01you understood it and this will provide
  899. 41:04us a good start when we do things ahead
  900. 41:07. Okay, so yeah, that's it guys. If you
  901. 41:10liked the video, please consider
  902. 41:12subscribing. If possible, do share it
  903. 41:14with someone if they are learning
  904. 41:15machine learning. And let's meet
  905. 41:17tomorrow, tomorrow we will do an
  906. 41:19end-to-end machine learning example so
  907. 41:21that you get a bird's eye overview of
  908. 41:23the whole process. So yeah, thanks for
  909. 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.