brunch

You can make anything
by writing

C.S.Lewis

by 라인하트 Oct 05. 2020

앤드류 응의 머신러닝 (3-2) : 덧셈과 스칼라 곱

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


Linear Algebra Review 

선형 대수 리뷰 


Addition and scalar multiplication (덧셈과 스칼라곱)


   In this video we'll talk about matrix addition and subtraction, as well as how to multiply a matrix by a number, also called Scalar Multiplication. 


   이번 강의에서는 행렬의 덧셈과 뺄셈, 그리고 스칼라 곱이라 불리는 행렬과 상수 간의 곱셈을 설명합니다. 


   Let's start an example. Given two matrices like these, let's say I want to add them together. How do I do that? And so, what does addition of matrices mean? 


   예제를 하나 보겠습니다. 두 개의 행렬을 더하려면 어떻게 해야 할까요? 그리고, 행렬 간의 덧셈은 어떤 의미가 있을까요?  



   It turns out that if you want to add two matrices, what you do is you just add up the elements of these matrices one at a time. So, my result of adding two matrices is going to be itself another matrix and the first element again just by taking 1 and 4 and multiplying them and adding them together, so I get 5. The second element I get by taking 2 and 2 and adding them, so I get 4; 3 plus 0 is 3, and so on. I'm going to stop changing colors, I guess. And, on the right is 0.5, 10, 2.


   두 행렬의 합은 단순히 각 행렬의 같은 위치에 있는 행렬 성분을 더합니다. 

   예를 들면, 각 행렬의 첫 번째 성분 1과 4를 더하면 5입니다. 각 행렬의 두 번째 행의 첫 열 성분 2와 2를 더하면 4입니다. 각 행렬의 세 번째 행의 첫 열 성분 3과 0을 더하면 0입니다. 이런 식으로 행렬의 덧셈은 각 행렬 성분들을 더합니다. 색깔을 바꾸는 것은 그만 하겠습니다. 두 번째 열은 0.5, 10 , 2입니다.

      

   And it turns out you can add only two matrices that are of the same dimensions. So this example is a three by two matrix, because this has 3 rows and 2 columns, so it's 3 by 2. This is also a 3 by 2 matrix, and the result of adding these two matrices is a 3 by 2 matrix again. So you can only add matrices of the same dimension, and the result will be another matrix that's of the same dimension as the ones you just added.


   그리고 같은 차원의 두 행렬만 서로 더할 수 있습니다. 예제에서 왼쪽 행렬은 3 X 2차원 행렬이고, 오른쪽 행렬은 3 X 2차원 행렬입니다. 덧셈의 결과도 마찬가지로 3 X 2 행렬입니다. 



   Where as in contrast, if you were to take these two matrices, so this one is a 3 by 2 matrix, okay, 3 rows, 2 columns. This here is a 2 by 2 matrix. And because these two matrices are not of the same dimension, you know, this is an error, so you cannot add these two matrices and, you know, their sum is not well-defined. So that's matrix addition. 


   반면에, 여기 두 행렬이 있습니다. 왼쪽 행렬은 3 X 2 행렬이고, 오른쪽 행렬은 2 X 2 행렬입니다. 같은 차원의 행렬이 아닙니다.  


   이것은 오류입니다. 이런 덧셈은 정의되지 않았기 때문에 두 행렬을 더할 수 없습니다. 이것이 행렬 덧셈입니다.

   Next, let's talk about multiplying matrices by a scalar number. And the scalar is just a, maybe a overly fancy term for, you know, a number or a real number. Alright, this means real number. So let's take the number 3 and multiply it by this matrix. And if you do that, the result is pretty much what you'll expect. You just take your elements of the matrix and multiply them by 3, one at a time. So, you know, one times three is three. What, two times three is six, 3 times 3 is 9, and let's see, I'm going to stop changing colors again. Zero times 3 is zero. Three times 5 is 15, and 3 times 1 is three. And so this matrix is the result of multiplying that matrix on the left by 3. And you notice, again, this is a 3 by 2 matrix and the result is a matrix of the same dimension. This is a 3 by 2, both of these are 3 by 2 dimensional matrices. And by the way, you can write multiplication, you know, either way. So, I have three times this matrix. I could also have written this matrix and 0, 2, 5, 3, 1, right. I just copied this matrix over to the right. I can also take this matrix and multiply this by three. So whether it's you know, 3 times the matrix or the matrix times three is the same thing and this thing here in the middle is the result.


   다음은 행렬의 스칼라 곱입니다. 스칼라는 너무 화려한 용어이지만 실수를 의미합니다. 스칼라는 실수라는 뜻입니다. 예를 들면, 숫자 3을 행렬에 곱한다면, 결과는 여러분이 상상하는 것과 비슷할 겁니다. 스칼라 곱은 상수를 행렬의 각 성분에 곱하는 것입니다

   예를 들면, 3과 행렬의 첫 번째 성분 1을 곱하면 3입니다. 3과 행렬의 두 번째 행의 첫 열 성분 2를 곱하면 6입니다. 3과 행렬의 세 번째 행의 첫 열 성분 3을 곱하면 9입니다. 이런 식으로 행렬의 스칼라곱은 상수와 행렬 성분들을 곱합니다.  


   그리고, 3 X 2 행렬에 상수를 곱한 결과도 같은 차원인 3 X 2 행렬입니다. 스칼라 곱은 왼쪽 또는 오른쪽에서 상수를 곱해도 똑같습니다. '3 X 행렬'과 '행렬 X 3'의 결과는 동일합니다. 이것이 스칼라곱의 특성입니다.


   You can also take a matrix and divide it by a number. So, turns out taking this matrix and dividing it by four, this is actually the same as taking the number one quarter, and multiplying it by this matrix. 4, 0, 6, 3 and so, you can figure the answer, the result of this product is, one quarter times four is one, one quarter times zero is zero. One quarter times six is, what, three halves, about six over four is three halves, and one quarter times three is three quarters. And so that's the results of computing this matrix divided by four. Vectors give you the result. 


   행렬을 상수로 나눌 수 있습니다. 행렬을 4로 나눈다는 것은 1/4을 곱한다는 것과 같습니다. 



  예를 들면, 행렬의 첫 번째 성분 4와 1/4을 곱하면 1입니다. 행렬의 두 번째 행의 첫 열 성분 6과 1/4을 곱하면 3/2입니다. 행렬의 첫 번째 행의 두 번째 열 성분 0과 1/4을 곱하면 0입니다. 이런 식으로 행렬의 스칼라곱은 상수와 행렬 성분들을 더하는 것입니다. 


   Finally, for a slightly more complicated example, you can also take these operations and combine them together. So in this calculation, I have three times a vector plus a vector minus another vector divided by three. So just make sure we know where these are, right?


   마지막으로, 조금 더 복잡한 예제가 있습니다. 이것은 지금까지 배운 사칙연산을 모두 포함합니다.   


   This multiplication. This is an example of scalar multiplication because I am taking three and multiplying it. And this is, you know, another scalar multiplication. Or more like scalar division, I guess. It really just means one zero times this. And so if we evaluate these two operations first, then. what we get is this thing is equal to, let's see, so three times that vector is [3,12,6] + my vector in the middle which is a [0,0,5] minus one, zero, two-thirds, right? 


   녹색 박스의 두 스칼라 곱을 먼저 계산합니다. 첫 번째 연산은 3을 행렬에 곱하는 스칼라 곱입니다. 두 번째 연산은 세 번째 행렬에 1/3을 곱하는 스칼라 곱이자 3으로 나누는 스칼라 나눗셈입니다. 

 


   And again, just to make sure we understand what is going on here, this plus symbol, that is matrix addition, right? I really, since these are vectors, remember, vectors are special cases of matrices, right? This, you can also call  this vector addition This minus sign here, this is again a matrix subtraction, but because this is an n by 1, really a three by one matrix, that this is actually a vector, so this is also vector, this column. We call this matrix a vector subtraction, as well. OK?  And finally to wrap this up. This therefore gives me a vector, whose first element is going to be 3+0-1, so that's 3-1, which is 2. The second element is 12+0-0, which is 12. And the third element of this is, what, 6+5-(2/3), which is 11-(2/3), so that's 10 and one-third and see, you close this square bracket. And so this gives me a 3 by 1 matrix, which is also just called a 3 dimensional vector, which is the outcome of this calculation over here. So that's how you add and subtract matrices and vectors and multiply them by scalars or by row numbers. 


   그다음 행렬 연산을 합니다.  첫 번째 연산은 행렬 덧셈입니다. 세 행렬은 엄밀하게 벡터입니다. 벡터는 하나의 열(Column)만을 가진 특별한 행렬입니다. n X 1차원 행렬이자 3 X 1차원 벡터입니다. 벡터 덧셈입니다. 두 번째 연산은 행렬 뺄셈이자 벡터 뺄셈입니다. 마지막으로 연산을 마무리합니다. 



  결과는 벡터입니다. 첫 번째 성분은 3 + 0 - 1 = 2입니다. 두 번째 성분은 12 + 0 - 0 = 12입니다. 그리고 세 번째 성분은 6+5-(2/3) = 10과 1/3입니다. 결과는 3 X 1 행렬이자 3차원 벡터입니다. 


   So far I have only talked about how to multiply matrices and vectors by scalars, by row numbers. In the next video we will talk about a much more interesting step, of taking 2 matrices and multiplying 2 matrices together.


   지금까지 행렬과 벡터의 덧셈, 뺄셈, 그리고 스칼라 곱을 하는 방법을 배웠습니다. 다음 강의에서 두 행렬을 곱하는 흥미로운 과정을 공부할 것입니다. 




정리하며 

 행렬의 덧셈과 뺄셈은 같은 차원의 행렬끼리만 가능하고, 행렬의 같은 위치에 있는 성분을 끼리 연산합니다. 


행렬 덧셈 : [a b c d] + [w x y z] = [a+w b+x c+y d+z]

행렬 뺄셈 : [a b c d]−[w  x y z ]= [a−w b−x c−y d−z]



   행렬의 스칼라 곱은 상수를 행렬의 성분에 각각 곱하는 것입니다. 스칼라 나누기는 역수를 곱하는 것과 같습니다. 여기서 스칼라는 실수라는 뜻입니다. 예를 들어 행렬 / 3 = 행렬 X 1/3입니다. 그리고, 3 X 행렬 = 행렬 X 3입니다. 


   행렬 스칼라 곱하기 : [a b c d] ∗ x = [a∗x b∗x c∗x d∗x]

   행렬 스칼라 나누기 : [a b c d] / x = [a/x b/x c/x d/x] 



문제풀이


   다음의 문제의 정답은?  

   정답은 첫 번째입니다. 



   다음 문제를 풀어 봅시다. 

   정답은 두 번째입니다. 


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