‘Deep Learning with Python’ 세미나 3

이번 장에서 설명하려는 것들은 사실 별도의 책 한 권으로 다루어야 만한 주제입니다. 한 장에서 간단하게 설명을 하려다 보니 딥러닝에 대한 충분한 사전 학습이 없으신 분들에게는 내용 이해가 어려울 수 있을 것 같습니다.

실제적인 케라스 적용이 더 우선하다면 이 장의 개념들을 간단히 정리하고 이해할 수 있는 수준에서 이해하고 넘어가면 됩니다. 이들 개념들에 대한 깊은 이해 없이도 케라스를 사용해서 실제적인 적용을 할 수 있기 때문입니다. 하지만 딥러닝에 대한 개념적 이해를 더 우선시 하시는 분들이라면 참고 자료 [1]을 먼저 학습하기를 권합니다.

Before we begin: the mathematical building blocks of neural networks

Understanding deep learning requires familiarity with many simple mathematical concepts: tensors, tensor operations, differentiation, gradient descent, and so on.

딥러닝을 이해하려면 먼저 딥러닝을 구성하는 수학적 개념들을 알아야 합니다.

To add some context for tensors and gradient descent, we’ll begin the chapter with a practical example of a neural network.

아직 배운 것도 없는데 케라스로 작성한 예제를 진행하는 것이 조금은 부담스러울 수 있습니다. 설명 수준으로 이해하고 넘어갑니다.

2.1 A first look at a neural network

이 예제를 직접 코딩해 볼 필요는 없습니다. 예제를 작성하는 것이 도움이 될 수도 있지만 이 장의 목표에만 충실한다면 책 내용을 읽어나가는 정도로도 충분합니다.

In machine learning, a category in a classification problem is called a class. Data points are called samples. The class associated with a specific sample is called a label.

이 정도 용어는 기억하기 위해 조금은 노력할 필요가 있습니다. 클래스, 샘플, 레이블

Listing 2.1 Loading the MNIST dataset in Keras 코드를 보면 훈련(학습)용 이미지와 레이블이 있고 테스트(검증)용 이미지와 레이블로 구분된 것을 볼 수 있습니다. 여기서는 훈련 데이터와 테스트 데이터를 나누는 이유 정도는 한 번 체크하고 넘어가는 것이 좋을 것 같습니다.

사용할 수 있는 데이터의 양은 제한적입니다. 제한적인 양의 데이터로 훈련했을 때 신경망은 그 데이터에 익숙해질 수 있습니다. 훈련한 데이터로는 정답을 잘 맞히지만 새로운 데이터에 대해서는 제대로 맞히지 못할 수 있습니다. 훈련 데이터에 과하게 적합화 된 것(과적합, 오버피팅)입니다. 이러한 이유로 데이터를 훈련 데이터와 테스트 데이터로 나누는 것입니다. 훈련 시에 사용한 데이터가 아닌 테스트 데이터로 제대로 학습 된 것인지를 검증하는 것입니다. 이렇게 하지 않았을 경우 훈련 시에 높은 정확도로 맞힌 신경망이 실제에서는 낮은 수준으로 정답을 맞히게 되는 경험을 하실 수 있습니다.

2.2 Data representations for neural networks

책을 읽어 나가면서 파이썬 코드는 직접 작성해 봅니다. 텐서를 넘파이로 어떻게 나타내는지 주의하면서 코드를 작성합니다.

이번 절의 내용을 다 읽고 난 후, 아래 정리된 내용을 다시 한 번 읽습니다.

In general, all current machine-learning systems use tensors as their basic data structure. At its core, a tensor is a container for data—almost always numerical data. Tensors are a generalization of matrices to an arbitrary number of dimensions. Note that in the context of tensors, a dimension is often called an axis.

2.2.1 Scalars (0D tensors)

A tensor that contains only one number is called a scalar (or scalar tensor, or 0-dimensional tensor, or 0D tensor).

2.2.2 Vectors (1D tensors)

An array of numbers is called a vector, or 1D tensor.

2.2.3 Matrices (2D tensors)

An array of vectors is a matrix, or 2D tensor. A matrix has two axes (often referred to rows and columns).

2.2.4 3D tensors and higher-dimensional tensors

If you pack such matrices in a new array, you obtain a 3D tensor, which you can visually interpret as a cube of numbers.

딥러닝에서는 보통 0D에서 4D까지의 텐서를 다룹니다. 동영상 데이터를 다룰 경우에는 5D 텐서까지 가기도 합니다.

2.2.5 Key attributes

A tensor is defined by three key attributes:

  • Number of axes (rank)
  • Shape—This is a tuple of integers that describes how many dimensions the tensor has along each axis.
    • 행렬에서 shape은 꼴로 번역됩니다. 행렬은 텐서의 일종이고 우리가 다루는 것은 텐서이니, 여기서는 그냥 shape이라고 하죠.
    • 예를 들어 2.2.3의 행렬의 shape은 (3, 5)이고, 2.2.4의 3D 텐서의 shape은 (3, 3, 5)가 됩니다.
  • Data type—This is the type of the data contained in the tensor; for instance, a tensor’s type could be float32, uint8, float64, and so on.
2.2.6 Manipulating tensors in Numpy
2.2.7 The notion of data batches

Deep-learning models don’t process an entire dataset at once; rather, they break the data into small batches. 이것이 첫 번째 축(샘플 축, 샘플 차원이라고 부르기도 함)이 됩니다.

2.2.8 Real-world examples of data tensors

The data you’ll manipulate will almost always fall into one of the following categories:

  • Vector data—2D tensors of shape (samples, features)
  • Timeseries data or sequence data—3D tensors of shape (samples, timesteps, features)
  • Images—4D tensors of shape (samples, height, width, channels) or (samples, channels, height, width)
  • Video—5D tensors of shape (samples, frames, height, width, channels) or (samples, frames, channels, height, width)

이미지나 동영상은 위의 분류에 따라 큰 고민 없이 적용할 수 있습니다. 고민이 필요한 부분은 벡터나 시계열 데이터의 features입니다. 수치 데이터가 아닌 features를 볼 때 어떻게 수치 데이터로 표현할 수 있을지, features에 포함되는 feature가 모두 수치이지만 측정 단위가 다른 경우 어떻게 균일하게 맞출지에 대한 고민이 필요합니다.

2.3 The gears of neural networks: tensor operations

2.3.4까지는 어떤 연산들이 있고, 어떻게 연산을 하는지만 간단하게 짚으면서 넘어갑니다.

2.3.6은 깨달음이 필요한 부분입니다. 2.3.5는 2.3.6을 설명하기 위한 것입니다.

2.3.1 Element-wise operations

텐서 연산은 텐서에 포함된 원소 별로 연산이 가능합니다. 원소 별로 연산이 가능하다는 것은 특정 원소의 계산 결과가 다른 원소의 계산 결과에 영향을 미치지 않는 연산이라는 것입니다. 이 이야기는 원소 별 연산이 각각 독립적이기 때문에 병렬처리가 가능한 하드웨어에서 병렬처리가 가능하다는 이야기입니다. 넘파이는 원소별 연산을 지원합니다.

2.3.2 Broadcasting

shape이 다른 텐서에 대해 연산을 해야 할 때 브로드캐스팅을 합니다. shape이 작은 텐서를 shape이 큰 텐서의 shape으로 맞춥니다. 어떻게 맞추는지는 코드도 좋지만 그림이 있으면 더 좋습니다. 아래 링크 내용을 참조합니다.

http://www.astroml.org/book_figures/appendix/fig_broadcast_visual.html

2.3.3 Tensor dot

행렬 곱을 생각하시면 됩니다.

원소별 곱과 텐서 곱을 구분하기 위해서, 넘파이는 원소별 곱에 ‘*’를 사용하고 텐서 곱에 ‘dot’를 사용합니다.

행렬의 곱이 가능하려면 그림 2.5에서와 같이 첫 번째 행렬의 열의 개수와 두 번째 행렬의 행위 개수가 같아야 합니다. 그리고 그 결과의 shape은 첫 번째 행렬의 행의 개수와 두 번째 행렬의 열의 개수가 됩니다. 예를 들어 shape이 (2, 3)인 행렬에 행렬 곱이 가능하게 하려면 shape의 행의 개수는 3이어야 합니다. shape이 (2, 3)인 행렬에 shape이 (3, 4)인 행렬을 행렬 곱하면 그 결과 shape은 (2, 4)가 됩니다.

이것을 다차원 텐서에 일반화하면 첫 번째 텐서의 마지막 shape 값과 두 번째 텐서의 첫 번째 shape이 같아야 텐서 곱이 가능합니다. 또한 텐서 곱의 결과는 첫 번째 텐서의 shape에서 마지막 값을 제외한 것을 써주고, 두 번째 텐서의 shape에서 첫 번째 것을 제외하고 써주면 됩니다. 예를 들어 (a, b, c, d) · (d,)의 결과는 d를 제외하고 (a, b, c)가 됩니다.  (a, b, c, d) · (d, e)는 (a, b, c, e) 가 됩니다.

2.3.4 Tensor reshaping

Reshaping a tensor means rearranging its rows and columns to match a target shape.

넘파이 reshape 함수로 shape을 변경할 수 있습니다. transpose는행과 열을 바꾸어 주는 넘파이 함수입니다.

2.3.5 Geometric interpretation of tensor operations

딥러닝의 기하학적 해석을 설명하기 위해 먼저 텐서 연산의 기하학적 해석에 대해 설명합니다.

Because the contents of the tensors manipulated by tensor operations can be interpreted as coordinates of points in some geometric space, all tensor operations have a geometric interpretation. In general, elementary geometric operations such as affine transformations, rotations, scaling, and so on can be expressed as tensor operations.

2.3.6 A geometric interpretation of deep learning

그림 2.9에 대한 비유는 딥러닝의 대한 깨달음이 깊어지도록 돕습니다. 해당 내용을 꼼꼼히 주의를 기울여 여러 번 반복해서 읽습니다.

It follows that you can interpret a neural network as a very complex geometric transformation in a high-dimensional space, implemented via a long series of simple steps.

2.4 The engine of neural networks: gradient-based optimization

내용 설명이 상세하지 않기 때문에 어려움을 느낄 수도 있습니다. 이해할 수 있는 수준에서 읽고 넘어갑니다. 그렇게 할 수 없는 분들에게는 참고 자료 [1]을 추천합니다.

이번 절의 내용을 쭉 읽은 후, 아래 정리된 내용을 다시 읽으십시오.

손실 함수가 구한 손실 점수로 최적화 함수가 가중치를 갱신한다고 했습니다. 가중치가 갱신된다는 것은 어떤 의미일까요? 가중치 값을 어떤 조건에서는 줄이고, 어떤 조건에서는 늘려서 손실 점수를 최소화하려고 하겠죠. 가중치를 줄이거나 늘린다면 얼마만큼 줄이거나 늘려야 하는지도 문제입니다.

A much better approach is to take advantage of the fact that all operations used in the network are differentiable, and compute the gradient of the loss with regard to the network’s coefficients. You can then move the coefficients in the opposite direction from the
gradient, thus decreasing the loss.

텐서 연산의 변화율을 그래디언트라고 합니다. 그래디언트 기반의 최적화는 지금의 딥러닝을 가능하게 한 일등공신입니다. 그래디언트 기반으로 최적화를 한다는 것은 미분 가능하다는 것입니다. 그래디언트 기반의 최적화 방법을 사용하는 신경망에서는 미분 가능한 연산만 사용할 수 있습니다. 아니 사용해야 합니다.

2.4.1 What’s a derivative?

Because the function is continuous, a small change in x can only result in a small change in y—that’s the intuition behind continuity.

미분 가능하다는 것은 연속함을 함축하고 있다. 연속하지 않은 것에 대해서 한 점으로 수렴하는 한 점의 변화율은 구할 수 없겠지요.

The slope a is called the derivative of f in p.

어떤 점에서의 변화율은 그 점에서의 접선의 기울기와 같습니다.

2.4.2 Derivative of a tensor operation: the gradient

A gradient is the derivative of a tensor operation. It’s the generalization of the concept of derivatives to functions of multidimensional inputs: that is, to functions that take tensors as inputs.

2.4.3 Stochastic gradient descent

Given a differentiable function, it’s theoretically possible to find its minimum analytically: it’s known that a function’s minimum is a point where the derivative is 0, so all you have to do is find all the points where the derivative goes to 0 and check for which of these points the function has the lowest value.

손실 함수의 그래디언트가 0이 되는 점을 찾으면 그 점이 최소 값이 됩니다. 가장 작은 손실 점수를 구한다는 것은 결국 손실 함수의 그래디언트가 0이 되는 점을 찾는 것입니다.

If you update the weights in the opposite direction from the gradient, the loss will be a little less every time.

0그래디언트의 반대 방향으로 가중치를 업데이트하면 매번 손실 점수는 약간씩 더 작아질 것입니다.

What I just described is called mini-batch stochastic gradient descent (minibatch SGD). The term stochastic refers to the fact that each batch of data is drawn at random (stochastic is a scientific synonym of random).

minibatch SGD는 랜덤하게 선택된 작은 훈련 데이터 묶음으로 그래디언트가 최소가 되는 값을 반복적으로 찾습니다.

그림 2.11에서 보듯이 step 값을 적절히 고르는 것도 중요합니다.

Momentum addresses two issues with SGD: convergence speed and local minima.

2.4.4 Chaining derivatives: the Backpropagation algorithm

Applying the chain rule to the computation of the gradient values of a neural network gives rise to an algorithm called Backpropagation. Backpropagation starts with the final loss value and works backward from the top layers to the bottom layers, applying the chain rule to compute the contribution that each parameter had in the loss value.

역전파 알고리즘은 지금의 딥러닝을 가능하게 한 또 다른 일등공신입니다.

2.5 Looking back at our first example

fit 메소드가 호출 될 때 한 에포크마다 수행되는 그래디언트 업데이트 횟수를 보면, mini-batch SGD라는 이름처럼 랜덤하게 선택되지 않음을 알 수 있습니다. 따라서 샘플 데이터가 순서대로 배치를 선택했을 때 문제가 된다면 샘플 데이터를 훈련 전 섞어줄 필요가 있습니다.

참고 자료

About the Author
(주)뉴테크프라임 대표 김현남입니다. 저에 대해 좀 더 알기를 원하시는 분은 아래 링크를 참조하세요. http://www.umlcert.com/kimhn/

Leave a Reply

*