웹사이트 검색

파이썬의 ReLu 함수


Relu 또는 Rectified Linear Activation Function은 딥 러닝 세계에서 가장 일반적인 활성화 함수입니다. Relu는 최첨단 결과를 제공하며 동시에 계산적으로 매우 효율적입니다.

Relu 활성화 함수의 기본 개념은 다음과 같습니다.

Return 0 if the input is negative otherwise return the input as it is.

다음과 같이 수학적으로 표현할 수 있습니다.

Relu의 의사 코드는 다음과 같습니다.

if input > 0:
	return input
else:
	return 0

이 튜토리얼에서는 자체 ReLu 기능을 구현하는 방법과 단점에 대해 알아보고 더 나은 버전의 ReLu에 대해 알아봅니다.

권장 읽기: 기계 학습을 위한 선형 대수학 [1부/2부]

시작하자!

Python에서 ReLu 기능 구현

파이썬에서 우리만의 Relu 구현을 작성해 봅시다. 이를 구현하기 위해 내장된 max 함수를 사용할 것입니다.

ReLu의 코드는 다음과 같습니다.

def relu(x):
	return max(0.0, x)

함수를 테스트하기 위해 몇 가지 입력에서 실행해 보겠습니다.

x = 1.0
print('Applying Relu on (%.1f) gives %.1f' % (x, relu(x)))
x = -10.0
print('Applying Relu on (%.1f) gives %.1f' % (x, relu(x)))
x = 0.0
print('Applying Relu on (%.1f) gives %.1f' % (x, relu(x)))
x = 15.0
print('Applying Relu on (%.1f) gives %.1f' % (x, relu(x)))
x = -20.0
print('Applying Relu on (%.1f) gives %.1f' % (x, relu(x)))

완전한 코드

전체 코드는 다음과 같습니다.

def relu(x):
	return max(0.0, x)

x = 1.0
print('Applying Relu on (%.1f) gives %.1f' % (x, relu(x)))
x = -10.0
print('Applying Relu on (%.1f) gives %.1f' % (x, relu(x)))
x = 0.0
print('Applying Relu on (%.1f) gives %.1f' % (x, relu(x)))
x = 15.0
print('Applying Relu on (%.1f) gives %.1f' % (x, relu(x)))
x = -20.0
print('Applying Relu on (%.1f) gives %.1f' % (x, relu(x)))

출력 :

Applying Relu on (1.0) gives 1.0
Applying Relu on (-10.0) gives 0.0
Applying Relu on (0.0) gives 0.0
Applying Relu on (15.0) gives 15.0
Applying Relu on (-20.0) gives 0.0

ReLu 함수의 기울기

ReLu 함수의 기울기(도함수)가 무엇인지 살펴보겠습니다. 차별화하면 다음 기능을 얻을 수 있습니다.

f'(x) = 1, x>=0
      = 0, x<0

x 값이 0보다 작은 경우 기울기가 0임을 알 수 있습니다. 이는 일부 뉴런의 가중치와 편향이 업데이트되지 않음을 의미합니다. 훈련 과정에서 문제가 될 수 있습니다.

이 문제를 극복하기 위해 Leaky ReLu 기능이 있습니다. 다음에 대해 알아보겠습니다.

리키 렐루 함수

Leaky ReLu 함수는 일반 ReLu 함수를 즉흥적으로 만든 것입니다. 음수 값에 대한 기울기가 0인 문제를 해결하기 위해 Leaky ReLu는 음수 입력에 x의 매우 작은 선형 구성 요소를 제공합니다.

Leaky ReLu를 수학적으로 다음과 같이 표현할 수 있습니다.

f(x)= 0.01x, x<0
    = x,   x>=0

수학적으로:

  • f(x)=1 (x<0)
  • (αx)+1 (x>=0)(x)

여기서 a는 위에서 취한 0.01과 같은 작은 상수입니다.

그래픽으로 다음과 같이 표시할 수 있습니다.

Leaky ReLu의 기울기

Leaky ReLu 함수의 그래디언트를 계산해 봅시다. 기울기는 다음과 같이 나올 수 있습니다.

f'(x) = 1,  x>=0
      = 0.01, x<0

이 경우 음수 입력의 기울기는 0이 아닙니다. 이는 모든 뉴런이 업데이트됨을 의미합니다.

Python에서 Leaky ReLu 구현

Leaky ReLu의 구현은 다음과 같습니다.

def relu(x):
  if x>0 :
    return x
  else :
    return 0.01*x

현장 입력을 사용해 봅시다.

 
x = 1.0
print('Applying Leaky Relu on (%.1f) gives %.1f' % (x, leaky_relu(x)))
x = -10.0
print('Applying Leaky Relu on (%.1f) gives %.1f' % (x, leaky_relu(x)))
x = 0.0
print('Applying Leaky Relu on (%.1f) gives %.1f' % (x, leaky_relu(x)))
x = 15.0
print('Applying Leaky Relu on (%.1f) gives %.1f' % (x, leaky_relu(x)))
x = -20.0
print('Applying Leaky Relu on (%.1f) gives %.1f' % (x, leaky_relu(x)))

완전한 코드

Leaky ReLu의 전체 코드는 다음과 같습니다.

def leaky_relu(x):
  if x>0 :
    return x
  else :
    return 0.01*x
 
x = 1.0
print('Applying Leaky Relu on (%.1f) gives %.1f' % (x, leaky_relu(x)))
x = -10.0
print('Applying Leaky Relu on (%.1f) gives %.1f' % (x, leaky_relu(x)))
x = 0.0
print('Applying Leaky Relu on (%.1f) gives %.1f' % (x, leaky_relu(x)))
x = 15.0
print('Applying Leaky Relu on (%.1f) gives %.1f' % (x, leaky_relu(x)))
x = -20.0
print('Applying Leaky Relu on (%.1f) gives %.1f' % (x, leaky_relu(x)))

출력 :

Applying Leaky Relu on (1.0) gives 1.0
Applying Leaky Relu on (-10.0) gives -0.1
Applying Leaky Relu on (0.0) gives 0.0
Applying Leaky Relu on (15.0) gives 15.0
Applying Leaky Relu on (-20.0) gives -0.2

결론

이 튜토리얼은 Python의 ReLu 기능에 관한 것입니다. ReLu 기능의 개선된 버전도 보았습니다. Leaky ReLu는 ReLu 함수에서 음수 값에 대한 기울기가 0인 문제를 해결합니다.