brunch

You can make anything
by writing

C.S.Lewis

by 라인하트 Oct 07. 2020

앤드류 응의 머신러닝 (3-5) : 행렬 곱셈의 속성

  온라인 강의 플랫폼 코세라의 창립자인 앤드류 응 (Andrew Ng) 교수는 인공지능 업계의 거장입니다. 그가 스탠퍼드 대학에서 머신 러닝 입문자에게 한 강의를 그대로 코세라 온라인 강의 (Coursera.org)에서 무료로 배울 수 있습니다. 이 강의는 머신러닝 입문자들의 필수코스입니다. 인공지능과 머신러닝을 혼자 공부하면서 자연스럽게 만나게 되는 강의입니다. 


Linear Algebra Review 

선형 대수 리뷰  


Matrix Multiplication Properties (행렬 곱셈의 속성)


   Matrix multiplication is really useful, since you can pack a lot of computation into just one matrix multiplication operation. But you should be careful of how you use them. In this video, I wanna tell you about a few properties of matrix multiplication.


   행렬 곱셈은 수많은 연산을 단 하나의 행렬 연산으로 만들어 줍니다. 행렬 곱셈은 매우 유용하지만 행렬의 속성은 실수의 곱셈과 다른 속성이 있습니다. 이번 강의에서 주의해야 할 행렬 곱셈의 속성 몇 가지를 설명합니다.   


   

   When working with just real numbers or when working with scalars, multiplication is commutative. And what I mean by that is that if you take 3 times 5, that is equal to 5 times 3. And the ordering of this multiplication doesn't matter. And this is called the commutative property of multiplication of real numbers. It turns out this property, they can reverse the order in which you multiply things. This is not true for matrix multiplication. So concretely, if A and B are matrices. Then in general, A times B is not equal to B times A. So, just be careful of that. Its not okay to arbitrarily reverse the order in which you multiply matrices. Matrix multiplication in not commutative, is the fancy way of saying it. 


   실수 또는 스칼라 곱셈은 교환 법칙이 성립합니다. 예를 들면, '3 × 5 = 5 × 3'입니다. 실수 곱셈은 곱셈의 순서를 바꿀 수 있습니다. 하지만, 행렬 곱셈은 교환 법칙이 성립하지 않습니다. 예를 들면, 행렬 A와 행렬 B 가 있을 때, A×B와 B×A는 다릅니다. 행렬은 곱셈 순서를 바꾸면 결과가 달라집니다. 



   As a concrete example, here are two matrices. This matrix 1 1 0 0 times 0 0 2 0 and if you multiply these two matrices you get this result on the right. Now let's swap around the order of these two matrices. So I'm gonna take this two matrices and just reverse them. It turns out if you multiply these two matrices, you get the second answer on the right. And well clearly, right, these two matrices are not equal to each other.


   예를 들면, 여기 행렬이 두 개 있습니다. 상단의 두 행렬을 곱한 결과 행렬은 [2, 0 ; 0, 0]입니다. 하단의 행렬은 상단의 두 행렬의 위치를 바꾸었습니다. 하단의 두 행렬을 곱한 결과 행렬은 [0, 0; 2, 2]입니다. 실제로 2개의 행렬은 서로 같지 않습니다. 행렬 곱셈은 교환 법칙이 성립하지 않기 때문에 주의해야 합니다.



   So, in fact, in general if you have a matrix operation like A times B, if A is an m by n matrix, and B is an n by m matrix, just as an example. Then, it turns out that the matrix A times B, right, is going to be an m by m matrix. Whereas the matrix B times A is going to be an n by n matrix. So the dimensions don't even match, right? So if A x B and B x A may not even be the same dimension. In the example on the left, I have all two by two matrices. So the dimensions were the same, but in general, reversing the order of the matrices can even change the dimension of the outcome. So, matrix multiplication is not commutative.


   일반적으로 행렬 곱셈은 A × B라고 표현합니다. 행렬 A는 m×n 행렬이고 행렬 B는 n×m 행렬입니다. 행렬 A와 행렬 B를 곱셈한 결과는 m×m 행렬입니다. 이와 대조적으로 행렬의 위치를 바꾼 행렬 B×A의 결과는 n×n 행렬입니다. 행렬의 순서가 바뀌면 행렬의 차원이 달라집니다. 예제는 2 X 2 행렬을 다루기 때문에 위치가 바뀌어도 차원이 같습니다. 일반적으로 행렬 A X B의 곱셈 행렬과 B X A 곱셈 행렬의 차원은 다릅니다. 행렬의 곱셈은 교환 법칙이 성립하지 않습니다. 


   

   Here's the next property I want to talk about. So, when talking about real numbers or scalars, let's say I have 3 x 5 x 2. I can either multiply 5 x 2 first. Then I can compute this as 3 x 10. Or, I can multiply 3 x 5 first, and I can compute this as 15 x 2. And both of these give you the same answer, right? Both of these is equal to 30. So it doesn't matter whether I multiply 5 x 2 first or whether I multiply 3 x 5 first, because sort of, well, 3 x (5 x 2) = (3 x 5) x 2. And this is called the associative property of real number multiplication.


   우선, 실수 또는 스칼라에서 속성을 다룹니다. 3 × 5 × 2를 계산할 때, 먼저 5 × 2를 계산하고 3 × 10을 계산할 수 있습니다. 또는 먼저 3 × 5를 계산하고 15 × 2를 계산할 수 있습니다. 두 가지 방법은 모두 결과 값이 같습니다. 두 가지 방식의 결과는 30으로 같습니다. 어떤 곱셈을 먼저 할지는 문제가 되지 않습니다. 정리하면, 3 X (5 X 2) = (3 X 5) X 2 = 30입니다. 이것을 실수의 결합 법칙이라고 합니다. 



    It turns out that matrix multiplication is associative. So concretely, let's say I have a product of three matrices A x B x C. Then, I can compute this either as A x (B x C) or I can computer this as (A x B) x C, and these will actually give me the same answer. I'm not gonna prove this but you can just take my word for it I guess. So just be clear, what I mean by these two cases. Let's look at the first one, right. This first case. What I mean by that is if you actually wanna compute A x B x C. What you can do is you can first compute B x C. So that D = B x C then compute A x D. And so this here is really computing A x B x C. Or, for this second case, you can compute this as, you can set E = A x B, then compute E times C. And this is then the same as A x B x C, and it turns out that both of these options will give you this guarantee to give you the same answer. And so we say that matrix multiplication thus enjoy the associative property. Okay? 


   여기 3 개의 행렬을 모두 곱하는 식 A X B X C가 있습니다. 이 식은 A X (B X C) 또는 (A X B) X C로 계산할 수 있습니다. 두 가지 모두 결과 행렬은 같습니다. 이것을 증명할 수 없지만 충분히 개연성은 있습니다. 첫 번째 케이스 A×(B×C)를 계산합니다. B×C를 계산한 결과를 D라고 하면, B×C = D입니다. 그리고 A×D를 계산합니다. 두 번째 케이스 (A×B)×C를 계산합니다. A×B를 먼저 계산한 결과를 E라고 하면, A X B = E입니다. 그리고 E X C를 계산합니다. A×B×C를 계산하는 두 케이스의 결과는 모두 같습니다. 행렬의 곱셈은 결합 법칙이 성립합니다.  


   And don't worry about the terminology associative and commutative. That's what it's called, but I'm not really going to use this terminology later in this class, so don't worry about memorizing those terms. 


   그리고 결합 법칙과 교환 법칙에 대한 정확한 정의를 외울 필요는 없습니다. 이 강의에서 이 용어가 중요하지 않습니다. 단지 행렬의 곱셈에서 두 가지를 주의해야 합니다. 


   Finally, I want to tell you about the Identity Matrix, which is a special matrix. So let's again make the analogy to what we know of real numbers. When dealing with real numbers or scalar numbers, the number 1, you can think of it as the identity of multiplication. And what I mean by that is that for any number z, 1 x z = z x 1. And that's just equal to the number z for any real number z. So 1 is the identity operation and so it satisfies this equation.


   마지막으로 항등 행렬 또는 단위행렬을 설명합니다. 먼저 실수와 스칼라의 항등 행렬 또는 단위행렬을 먼저 설명합니다. 실수나 스칼라 값에서 항등 값은 1입니다. 항등원은 어떤 수 z에  항등원을 곱하면 결과값이 그대로 z입니다. 즉, 1 X z = z X 1 = z입니다. 어떤 실수 z에 대해도 항상 동일합니다. 그래서, 숫자 1은 실수에 대한 항등원입니다.  



   So it turns out, that this in the space of matrices there's an identity matrix as well and it's usually denoted I or sometimes we write it as I of n x n if we want to make it explicit to dimensions. So I subscript n x n is the n x n identity matrix. And so that's a different identity matrix for each dimension n. And here are few examples. Here's the 2 x 2 identity matrix, here's the 3 x 3 identity matrix, here's the 4 x 4 matrix. So the identity matrix has the property that it has ones along the diagonals. All right, and so on. And 0 everywhere else. And so, by the way, the 1 x 1 identity matrix is just a number 1, and so the 1 x 1 matrix with just 1 in it. So it's not a very interesting identity matrix. And informally, when I or others are being sloppy, very often we'll write the identity matrices in fine notation. We'll draw square brackets, just write one one one dot dot dot dot one, and then we'll maybe somewhat sloppily write a bunch of zeros there. And these zeroes on the, this big zero and this big zero, that's meant to denote that this matrix is zero everywhere except for the diagonal. So this is just how I might swap you the right identity matrix. 


   항등 행렬(Identity Matrix)은 'I' 또는 In×n으로 표기합니다. Inxn은 n×n 항등 행렬로 차원에 따라 항등 행렬의 모양은 다릅니다. 여기 몇 가지 예가 있습니다. 


   왼쪽은 2 × 2 항등 행렬이고, 중간은 3 × 3 항등 행렬, 오른쪽은 4 ×4 항등 행렬입니다. 항등 행렬은 대각선 성분은 모두 1이고 나머지는 0입니다. 숫자 1 하나를 가지는 행렬 [1]은 1 ×1 행렬입니다. 이것은 의미가 없습니다.  



   비공식적으로 귀찮아서 항등 행렬을 다음과 같이 표현하기도 합니다. 우선은 큰 대괄호를 그리고 대각선 방향으로 1 1 1....... 1을 씁니다. 그리고 나머지 공간에는 귀찮아서 큰 0을 씁니다. 다수의 0을 큰 0으로 대체합니다. 큰 0은 대각선을 제외한 모든 곳이 0으로 채워져 있다는 의미입니다. 이것은 항등 행렬을 표기할 때만 사용합니다.  


   And it turns out that the identity matrix has its property that for any matrix A, A times identity equals I times A equals A so that's a lot like this equation that we have up here. Right? So 1 times z equals z times 1 equals z itself. So I times A equals A times I equals A. Just to make sure we have the dimensions right. So if A is an m by n matrix, then this identity matrix here, that's an n by n identity matrix. And if is and by then, then this identity matrix, right? For matrix multiplication to make sense, that has to be an m by m matrix. Because this m has the match up that m, and in either case, the outcome of this process is you get back the matrix A which is m by n. So whenever we write the identity matrix I, you know, very often the dimension Mention, right, will be implicit from the content. So these two I's, they're actually different dimension matrices. One may be n by n, the other is n by m. But when we want to make the dimension of the matrix explicit, then sometimes we'll write to this I subscript n by n, kind of like we had up here. But very often, the dimension will be implicit.


   항등 행렬은 교환 법칙이 성립합니다. 'A × I = I × A'입니다. '1 ×z = 1 ×z = z'인 같은 것처럼 'I×A = A×I = A'입니다. 단지 차원을 정확히 맞추어야 합니다. 만약 A 행렬이 m×n 행렬이면 항등 행렬은 n×n 행렬입니다. 행렬 곱셈이 만족하려면 항등 행렬 Im×m 이어야 합니다. 곱셈의 결과는 A 행렬과 같은 m×n 행렬입니다. 항등 행렬을 사용할 때마다 행렬의 차원을 고려해야 합니다. 사실 'A × I = I × A'에 사용하는 항등 행렬 I는 차원이 다릅니다. A 행렬이 m X n 일 때, A X I를 연산할 때 항등 행렬은 In×n이고, I X A를 연산할 때 항등 행렬은 Im×m입니다. 항등 행렬의 차원을 확인하기 위해 Inxn으로 표기하기도 하고, 차원을 생략하여 I라고도 합니다. 

  

   Finally, I just wanna point out that earlier I said that AB is not, in general, equal to BA. Right? For most matrices A and B, this is not true. But when B is the identity matrix, this does hold true, that A times the identity matrix does indeed equal to identity times A is just that you know this is not true for other matrices B in general. So, that's it for the properties of matrix multiplication and special matrices like the identity matrix I want to tell you about.


   정리하면, 행렬의 곱셉에서 교환 법칙은 성립하지 않습니다.  'AB= BA'가 아니지만, B가 항등 행렬 일 때 'AI= IA' 성립합니다.  행렬 곱셈의 속성과  항등 행렬의 속성을 설명했습니다. 


 In the next and final video on our linear algebra review, I'm going to quickly tell you about a couple of special matrix operations and after that everything you need to know about linear algebra for this class.


   다음 강의에서 2 가지 특별한 행렬 연산을 다룰 것입니다. 두 가지는 선형 대수 강의에서 반드시 알아야 합니다. 



정리하며 - 행렬의 속성

 

   행렬 곱셈의 교환 법칙은 성립하지 않습니다. 

      A∗B!= B *A 


   행렬 곱셈의 결합 법칙은 성립합니다. 


      (A∗B)∗C = A∗(B∗C)


   항등 행렬은 교환 법칙과 결합 법칙이 모두 성립합니다.


        A*I = I*A = A


   항등 행렬은 다음과 같이 표현합니다.


문제 풀이


   다음의 문제를 풉니다.


정답은 세 번째입니다. 옥타브 프로그램으로 계산해 보았습니다.

   


매거진의 이전글 앤드류 응의 머신러닝 (3-4) : 행렬과 행렬의 곱셈
작품 선택
키워드 선택 0 / 3 0
댓글여부
afliean
브런치는 최신 브라우저에 최적화 되어있습니다. IE chrome safari