sources:
https://www.kaggle.com/code/ryanholbrook/deep-neural-networks
Deep Neural Networks
Explore and run machine learning code with Kaggle Notebooks | Using data from DL Course Data
www.kaggle.com
Introduction
In this lesson we're going to see how we can build neural networks capable of learning the complex kinds of relationships deep neural nets are famous for.
The key idea here is modularity, building up a complex network from simpler functional units. We've seen how a linear unit computes a linear function -- now we'll see how to combine and modify these single units to model more complex relationships.
이번레슨에서는 어떻게 뉴럴네트워크(신경망) 모델을 만들고 딥뉴럴네트워크(심층신경망)개념에 대해 살펴볼 것.
가장 중요한 아이디어는 모듈화(modularity)이며 이것은 단순한 기능 유닛을 모듈화하고 이것을 기초로 복잡한 네트워크 모델을 만드는 것.
앞서 우리는 단순선형모델이 작동하는 방법을 공부했고 이를 이용해서 좀 더 복잡한 모델이 어떻게 생성되는지 알아보자.
Layers
레이어
Neural networks typically organize their neurons into layers. When we collect together linear units having a common set of inputs we get a dense layer.
신경망은 기본적으로 뉴런을 이용해서 레이어를 형성한다. 공통의 입력 셋(set of inputs; 입력 집합)을 갖는 다수의 선형 유닛((LU)을 모아놓으면 하나의 밀집 레이어(dense layer; 덴스 레이어, 밀집계층)가 형성된다.
A stack of three circles in an input layer connected to two circles in a dense layer.
A dense layer of two linear units receiving two inputs and a bias.
[위의 다이어그램에 대한 설명]
3개의 인풋 레이어가 2개의 덴스 레이어에 연결되어 있고 2개의 선형유닛(LU)으로 이루어진 덴스 레이어는 각각 2개의 인풋과 1개의 바이어스를 입력으로 하고 있다.
You could think of each layer in a neural network as performing some kind of relatively simple transformation.
신경망에서 각 레이어는 일종의 단순 변환(transformation)함수로서의 역할을 하고 있다고 생각할 수 있다.
Through a deep stack of layers, a neural network can transform its inputs in more and more complex ways. In a well-trained neural network, each layer is a transformation getting us a little bit closer to a solution.
딥스택레이어(deep stacks of layers)에서 뉴럴네트워크(신경망)는 인풋을 매우 복잡한 방향으로 변환할 수 있으며 잘 훈련된 뉴럴네트워크에서는 각 레이어가 변환을 진행하면서 점진적으로 원하는 출력에 가까워 지게 된다.
Many Kinds of Layers
다양한종류의 레이어
A "layer" in Keras is a very general kind of thing. A layer can be, essentially, any kind of data transformation. Many layers, like the convolutional and recurrent layers, transform data through use of neurons and differ primarily in the pattern of connections they form. Others though are used for feature engineering or just simple arithmetic. There's a whole world of layers to discover -- check them out!
Keras에서 레이어는 매우 기본적인 항목으로 어떤 종류의 데이터 변환에도 레이어가 사용된다.
#Convolutional NN layer : 합성곱 레이어
#recurrent NN layer : 순환 신경망 레이어
합성곱 레이어나 순환 레이어 같은 다양한 레이어가 뉴런간의 다양한 연결을 통해 데이터 변환을 처리하고 있고 단순한 수학적 계산에서부터 복잡한 피쳐(feature)의 처리까지 이루고 있다.
The Activation Function
활성화함수
It turns out, however, that two dense layers with nothing in between are no better than a single dense layer by itself. Dense layers by themselves can never move us out of the world of lines and planes. What we need is something nonlinear. What we need are activation functions.
그런데 단순 선형적인 두개의 덴스 레이어가 싱글 덴스 레이어 보다 성능이 뛰어나지 않다는 점이 발견되었다.
덴스 레이어 자체는 대부분 선형적으로 직선이나 평면에서 벗어날 수 없기 때문에, 성능을 높이려면 우리는 무엇인가 '비선형'적인 것이 필요하게 되었다. 이 비선형적인 유틸을 우리는 활성화 함수(activation function)이라고 한다.
Without activation functions, neural networks can only learn linear relationships. In order to fit curves, we'll need to use activation functions.
만약 활성화 함수가 없다면 뉴럴네트워크는 오로지 선형적인 관계만 학습할 수 있다. 활성화 함수를 이용해야 비선형적인 관계학습이 가능.
(생략)
An activation function is simply some function we apply to each of a layer's outputs (its activations). The most common is the rectifier function
활성화 함수는 각 레이어의 아웃풋에 적용하여 각 레이어가 활성화되느냐 않느냐를 정해준다.
가장 일반적인 형태는 교정함수(rectifier function) 다.
A graph of the rectifier function. The line y=x when x>0 and y=0 when x<0, making a 'hinge' shape like '_/'.
위 그래프가 정류 함수의 그래프인데, x>0일때 y=x이고 x<0일때는 y=0인 비선형적 그래프로 힌지(Hinge; 돌쩌귀; 경첩)모양을 갖는다.
#rectify는 교정, 정류. 정류는 원하는 파형만 걸러내는 작용을 의미 예를들어 교류 전류를 직류 전류로 걸러내는 것 (즉 플러스 전류만 받고 마이너스 전류는 걸러내는)을 정류라고 한다. 정류라고 하면 교류 직류 변환이 떠올라서 여기서는 교정이라는 단어로 번역했다. rectify의 의미가 교정하다 정류하다의 의미라는 점이 중요.
The rectifier function has a graph that's a line with the negative part "rectified" to zero. Applying the function to the outputs of a neuron will put a bend in the data, moving us away from simple lines.
이 교정 함수는 x가 마이너스인 경우 함수값을 0으로 교정(rectify)한다. 이 함수를 뉴런의 아웃풋에 적용하면 데이터에 곡선을 주게 되고 단순한 직선이 아닌 곡선을 나타낼 수 있게 된다.
When we attach the rectifier to a linear unit, we get a rectified linear unit or ReLU. (For this reason, it's common to call the rectifier function the "ReLU function".) Applying a ReLU activation to a linear unit means the output becomes max(0, w * x + b), which we might draw in a diagram like:
이 교정기를 선형유닛(LU)에 붙이게 되면 우리는 rectified linear unit 교정된 선형유닛 --> ReLU를 얻게 된다. 우리는 통상 교정함수로 ReLU를 사용한다라고 이야기 한다.
선형 유닛에 ReLU 활성화 함수를 적용하게 되면 아웃풋은 max(0, w*x+b)처럼 표현할 수 있다. 이를 다이어그램으로 나타내면 아래 그림과 같다.
Stacking Dense Layers
스태킹 덴스 레이어 (다층 밀집 계층)
Now that we have some nonlinearity, let's see how we can stack layers to get complex data transformations.
이제 우리는 어떤 비선형적 구조를 갖게 되었고 이 레이어를 샇아서 어떻게 복잡한 데이터 변환을 이룰 수 있는지 생각해보자.
An input layer, two hidden layers, and a final linear layer.
인풋 레이어, 두개의 숨김레이어, 그리고 마지막으로 선형 레이어가 있다.
The layers before the output layer are sometimes called hidden since we never see their outputs directly.
아웃풋레이어 이전에 놓인 레이어를 히든 레이어라고 부르기도 하는데, 왜냐하면 그 레이어의 아웃풋을 직접 억세스할 수 없기 때문이다.
Now, notice that the final (output) layer is a linear unit (meaning, no activation function). That makes this network appropriate to a regression task, where we are trying to predict some arbitrary numeric value. Other tasks (like classification) might require an activation function on the output.
마지막 출력 레이어가 선형 유닛이기 때문에 이 네트워크는 머신러닝의 회귀 문제에 적합한 모델이 된다. 분류 문제는 다시 출력단에 활성화 함수가 필요할 수 있다.
Building Sequential Models
Sequential 모델 구성
#여기서 Sequential은 keras의 라이브러리 함수
The Sequential model we've been using will connect together a list of layers in order from first to last: the first layer gets the input, the last layer produces the output. This creates the model in the figure above:
Sequential 모델은 일련의 레이어를 순서대로 연결했다. 첫 레이어는 입력단이고 마지막 레이어는 출력단이 될 것이다. 이것이 반복되면 위의 스태킹 덴스 레이어가 된다.
# <파이썬코딩>
from tensorflow import keras
from tensorflow.keras import layers
#앞서 배운 Single LU의 구성과 유사한데 layer가 늘어난다.
model = keras.Sequential([
# the hidden ReLU layers
layers.Dense(units=4, activation='relu', input_shape=[2]),
layers.Dense(units=3, activation='relu'),
# the linear output layer
layers.Dense(units=1),
])
##
Be sure to pass all the layers together in a list, like [layer, layer, layer, ...], instead of as separate arguments. To add an activation function to a layer, just give its name in the activation argument.
우선 앞서 배운 것 처럼 모든 레이어가 리스트로 [layer1, layer2, layer3, ...]처럼 구성되는 점에 유의하고 activation 함수를 지정한 인자인 activation ='relu' , 그리고 input_shape도 리스트 형태로 준 것 기억하기 바란다.
<끝>
[딥러닝초급] Overfitting and Underfitting (0) | 2025.02.10 |
---|---|
[딥러닝기초] Stochastic Gradient Descent (0) | 2025.02.05 |
[딥러닝초급] A Single Neuron (0) | 2025.02.03 |
[머신러닝중급]Cross-Validation + XGBoost (0) | 2025.01.23 |
[머신러닝중급] Pipelines (0) | 2025.01.23 |