참고 서적
![]() |
도서명: 모두의 딥러닝 저자 : 조태호 출판 : 길벗 발매 : 2020.01.27 |
참 거짓 판단 장치 : 로지스틱 회귀(Logistic regression)
로지스틱 회귀는 참과 거짓 중에 하나를 내놓는 과정이다.
로지스틱 회귀는 선형 회귀와 마찬가지로 적절한 선을 그려가는 과정이다. 다만 직선이 아니라, 참(1)과 거짓(0) 사이를 구분하는 S자 형태의 선을 그어주는 작업이다.
(1) 시그모이드 함수
그런데 위와 같은 S자 형태로 그래프가 그려지는 함수가 있는데, 바로 시그모이드 함수(sigmoid function)이다.
여기서도 구해야 하는 값은 선형 회귀 때와 마찬가지로 ax + b이다. 먼저 a는 그래프의 경사도를 결정한다.
a 값이 커지면 경사가 커지고 a 값이 작아지면 경사가 작아진다.
b는 그래프의 좌우 이동을 의미한다. b 값이 크고 작아짐에 따라 그래프가 이동한다.
따라서 a와 b의 값에 따라 오차가 변한다. a 값이 작아지면 오차는 무한대로 커진다. 그런데 a 값이 커진다고 해서 오차가 무한대로 커지지는 않는다. 한편, b 값이 너무 크거나 작을 경우 오차는 이차 함수 그래프와 유사한 형태로 나타난다.
(2) 오차 공식
시그모이드 함수에서 a와 b의 값을 구하는 방법은 경사 하강법이다. 그런데 경사 하강법은 먼저 오차를 구한 다음 오차가 작은 쪽으로 이동시키는 방법이다.
시그모이드 함수의 특징은 y 값이 0과 1 사이라는 것이다. 따라서 실제 값이 1일 때 예측 값이 0에 가까워지면 오차가 커진다. 반대로, 실제 값이 0일 때 예측 값이 1에 가까워지는 경우에도 오차는 커진다. 이를 공식으로 만들 수 있게 해 주는 함수가 바로 로그 함수이다.
따라서 로지스틱 회귀를 위해서는 시그모이드 함수를 사용한다는 것, 그리고 0부터 1사이의 값을 가지는 특성 때문에 로그 함수를 함께 사용하여야 한다.
파란색 선은 실제 값이 1일 때 사용할 수 있는 그래프이다. 예측 값이 1일 때 오차가 0이고, 반대로 예측 값이 0에 가까울수록 오차는 커진다.
빨간색 선은 반대로 실제 값이 0일 때 사용할 수 있는 함수이다. 예측 값이 0일 때 오차가 없고, 1에 가까워질수록 오차가 매우 커진다.
앞의 파란색과 빨간색 그래프의 식은 각각 -logh와 -log(1 - h)이다. 실제 값이 1일 때는 -logh 그래프를 쓰고, 0일 때는 -log(1 - h) 그래프를 써야 한다. 이는 다음과 같은 방법으로 해결할 수 있다.
실제 값을 y_data라 할 때, 이 값이 1이면 b 부분이 없어진다. 반대로 0이면 a 부분이 없어진다. 따라서 실제 값에 따라 빨간색 그래프와 파란색 그래프를 각각 사용할 수 있게 된다.
이 식을 a와 b로 편미분 하는 과정
미분 공식
로지스틱회귀 코드 구현
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
#공부한 시간 X와 합격여부 Y의 리스트 만들기
data = [[2,0],[4,0],[6,0],[8,1],[10,1],[12,1],[14,1]]
x_data = [i[0] for i in data]
y_data = [i[1] for i in data]
#그래프로 나타내기
plt.scatter(x_data, y_data)
plt.xlim(0,15)
plt.ylim(-.1,1.1)
#기울기 a와 절편 b 값 초기화
a = 0
b = 0
#학습률
lr = 0.05
#시그모이드 함수 정의
def sigmoid(x):
return 1 / (1 + np.e ** (-x))
#경사하강법 실행
#1000번 반복할 때 마다 각 x_dataㄱ밧에 대한 현재의 a값, b값 출력
for i in range(2001):
for x_data, y_data in data:
# a에 관한 편미분. 앞서 정의한 sigmoid 함수 사용
a_diff = x_data*(sigmoid(a*x_data + b) - y_data)
# b에 관한 편미분
b_diff = sigmoid(a*x_data + b) - y_data
# a를 업데이트 하기 위해 a - diff에 학습률 lr을 곱한 값을 a에서 뺌
a = a - lr * a_diff
# b를 업데이트 하기 위해 b - diff에 학습률 lr을 곱한 값을 b에서 뺌
b = b - lr * b_diff
if i % 1000 == 0:
print("epoch = %f, 기울기 =%.04f, 절편=%04f" % (i,a,b))
# 앞서 구한 기울기와 절편을 이용해 그래프 그리기
plt.scatter(x_data, y_data)
plt.xlim(0, 15)
plt.ylim(-.1, 1.1)
x_range = (np.arange(0, 15, 0.1)) # 그래프로 나타낼 x 값의 범위 정하기
plt.plot(np.arange(0, 15, 0.1), np.array([sigmoid(a * x + b) for x in x_range]))
plt.show()
***교재랑 다른 부분이 많아서 교재대로 코딩하면 출력이 잘 안될 것임 이 코드로 출력하면 정상적으로 실행이 된다.
여기서 epoch 값을 바꿔가면서 출력해보자.
1. epoch 값이 2000일 때
출력창
epoch = 0.000000, 기울기 =-0.0500, 절편=-0.025000
epoch = 0.000000, 기울기 =-0.1388, 절편=-0.047199
epoch = 0.000000, 기울기 =-0.2268, 절편=-0.061858
epoch = 0.000000, 기울기 =0.1201, 절편=-0.018502
epoch = 0.000000, 기울기 =0.2374, 절편=-0.006772
epoch = 0.000000, 기울기 =0.2705, 절편=-0.004017
epoch = 0.000000, 기울기 =0.2860, 절편=-0.002904
epoch = 1000.000000, 기울기 =1.4978, 절편=-9.940123
epoch = 1000.000000, 기울기 =1.4940, 절편=-9.941069
epoch = 1000.000000, 기울기 =1.4120, 절편=-9.954745
epoch = 1000.000000, 기울기 =1.4949, 절편=-9.944379
epoch = 1000.000000, 기울기 =1.4982, 절편=-9.944046
epoch = 1000.000000, 기울기 =1.4984, 절편=-9.944030
epoch = 1000.000000, 기울기 =1.4985, 절편=-9.944029
epoch = 2000.000000, 기울기 =1.9065, 절편=-12.948895
epoch = 2000.000000, 기울기 =1.9055, 절편=-12.949138
epoch = 2000.000000, 기울기 =1.8515, 절편=-12.958139
epoch = 2000.000000, 기울기 =1.9057, 절편=-12.951368
epoch = 2000.000000, 기울기 =1.9068, 절편=-12.951256
epoch = 2000.000000, 기울기 =1.9068, 절편=-12.951254
epoch = 2000.000000, 기울기 =1.9068, 절편=-12.951254
2. epoch 값이 6000일 때
출력창
epoch = 0.000000, 기울기 =-0.0500, 절편=-0.025000
epoch = 0.000000, 기울기 =-0.1388, 절편=-0.047199
epoch = 0.000000, 기울기 =-0.2268, 절편=-0.061858
epoch = 0.000000, 기울기 =0.1201, 절편=-0.018502
epoch = 0.000000, 기울기 =0.2374, 절편=-0.006772
epoch = 0.000000, 기울기 =0.2705, 절편=-0.004017
epoch = 0.000000, 기울기 =0.2860, 절편=-0.002904
epoch = 1000.000000, 기울기 =1.4978, 절편=-9.940123
epoch = 1000.000000, 기울기 =1.4940, 절편=-9.941069
epoch = 1000.000000, 기울기 =1.4120, 절편=-9.954745
epoch = 1000.000000, 기울기 =1.4949, 절편=-9.944379
epoch = 1000.000000, 기울기 =1.4982, 절편=-9.944046
epoch = 1000.000000, 기울기 =1.4984, 절편=-9.944030
epoch = 1000.000000, 기울기 =1.4985, 절편=-9.944029
epoch = 2000.000000, 기울기 =1.9065, 절편=-12.948895
epoch = 2000.000000, 기울기 =1.9055, 절편=-12.949138
epoch = 2000.000000, 기울기 =1.8515, 절편=-12.958139
epoch = 2000.000000, 기울기 =1.9057, 절편=-12.951368
epoch = 2000.000000, 기울기 =1.9068, 절편=-12.951256
epoch = 2000.000000, 기울기 =1.9068, 절편=-12.951254
epoch = 2000.000000, 기울기 =1.9068, 절편=-12.951254
epoch = 3000.000000, 기울기 =2.1838, 절편=-14.956608
epoch = 3000.000000, 기울기 =2.1834, 절편=-14.956707
epoch = 3000.000000, 기울기 =2.1428, 절편=-14.963463
epoch = 3000.000000, 기울기 =2.1835, 절편=-14.958381
epoch = 3000.000000, 기울기 =2.1840, 절편=-14.958330
epoch = 3000.000000, 기울기 =2.1840, 절편=-14.958329
epoch = 3000.000000, 기울기 =2.1840, 절편=-14.958329
epoch = 4000.000000, 기울기 =2.3967, 절편=-16.485856
epoch = 4000.000000, 기울기 =2.3965, 절편=-16.485907
epoch = 4000.000000, 기울기 =2.3640, 절편=-16.491329
epoch = 4000.000000, 기울기 =2.3966, 절편=-16.487249
epoch = 4000.000000, 기울기 =2.3969, 절편=-16.487221
epoch = 4000.000000, 기울기 =2.3969, 절편=-16.487220
epoch = 4000.000000, 기울기 =2.3969, 절편=-16.487220
epoch = 5000.000000, 기울기 =2.5705, 절편=-17.727266
epoch = 5000.000000, 기울기 =2.5704, 절편=-17.727295
epoch = 5000.000000, 기울기 =2.5432, 절편=-17.731830
epoch = 5000.000000, 기울기 =2.5705, 절편=-17.728416
epoch = 5000.000000, 기울기 =2.5706, 절편=-17.728399
epoch = 5000.000000, 기울기 =2.5706, 절편=-17.728399
epoch = 5000.000000, 기울기 =2.5706, 절편=-17.728399
epoch = 6000.000000, 기울기 =2.7175, 절편=-18.774546
epoch = 6000.000000, 기울기 =2.7175, 절편=-18.774564
epoch = 6000.000000, 기울기 =2.6941, 절편=-18.778464
epoch = 6000.000000, 기울기 =2.7176, 절편=-18.775527
epoch = 6000.000000, 기울기 =2.7177, 절편=-18.775516
epoch = 6000.000000, 기울기 =2.7177, 절편=-18.775516
epoch = 6000.000000, 기울기 =2.7177, 절편=-18.775516
3. epoch 값이 20000일 때
출력창
epoch = 0.000000, 기울기 =-0.0500, 절편=-0.025000
epoch = 0.000000, 기울기 =-0.1388, 절편=-0.047199
epoch = 0.000000, 기울기 =-0.2268, 절편=-0.061858
epoch = 0.000000, 기울기 =0.1201, 절편=-0.018502
epoch = 0.000000, 기울기 =0.2374, 절편=-0.006772
epoch = 0.000000, 기울기 =0.2705, 절편=-0.004017
epoch = 0.000000, 기울기 =0.2860, 절편=-0.002904
epoch = 1000.000000, 기울기 =1.4978, 절편=-9.940123
epoch = 1000.000000, 기울기 =1.4940, 절편=-9.941069
epoch = 1000.000000, 기울기 =1.4120, 절편=-9.954745
epoch = 1000.000000, 기울기 =1.4949, 절편=-9.944379
epoch = 1000.000000, 기울기 =1.4982, 절편=-9.944046
epoch = 1000.000000, 기울기 =1.4984, 절편=-9.944030
epoch = 1000.000000, 기울기 =1.4985, 절편=-9.944029
epoch = 2000.000000, 기울기 =1.9065, 절편=-12.948895
epoch = 2000.000000, 기울기 =1.9055, 절편=-12.949138
epoch = 2000.000000, 기울기 =1.8515, 절편=-12.958139
epoch = 2000.000000, 기울기 =1.9057, 절편=-12.951368
epoch = 2000.000000, 기울기 =1.9068, 절편=-12.951256
epoch = 2000.000000, 기울기 =1.9068, 절편=-12.951254
epoch = 2000.000000, 기울기 =1.9068, 절편=-12.951254
epoch = 3000.000000, 기울기 =2.1838, 절편=-14.956608
epoch = 3000.000000, 기울기 =2.1834, 절편=-14.956707
epoch = 3000.000000, 기울기 =2.1428, 절편=-14.963463
epoch = 3000.000000, 기울기 =2.1835, 절편=-14.958381
epoch = 3000.000000, 기울기 =2.1840, 절편=-14.958330
epoch = 3000.000000, 기울기 =2.1840, 절편=-14.958329
epoch = 3000.000000, 기울기 =2.1840, 절편=-14.958329
epoch = 4000.000000, 기울기 =2.3967, 절편=-16.485856
epoch = 4000.000000, 기울기 =2.3965, 절편=-16.485907
epoch = 4000.000000, 기울기 =2.3640, 절편=-16.491329
epoch = 4000.000000, 기울기 =2.3966, 절편=-16.487249
epoch = 4000.000000, 기울기 =2.3969, 절편=-16.487221
epoch = 4000.000000, 기울기 =2.3969, 절편=-16.487220
epoch = 4000.000000, 기울기 =2.3969, 절편=-16.487220
epoch = 5000.000000, 기울기 =2.5705, 절편=-17.727266
epoch = 5000.000000, 기울기 =2.5704, 절편=-17.727295
epoch = 5000.000000, 기울기 =2.5432, 절편=-17.731830
epoch = 5000.000000, 기울기 =2.5705, 절편=-17.728416
epoch = 5000.000000, 기울기 =2.5706, 절편=-17.728399
epoch = 5000.000000, 기울기 =2.5706, 절편=-17.728399
epoch = 5000.000000, 기울기 =2.5706, 절편=-17.728399
epoch = 6000.000000, 기울기 =2.7175, 절편=-18.774546
epoch = 6000.000000, 기울기 =2.7175, 절편=-18.774564
epoch = 6000.000000, 기울기 =2.6941, 절편=-18.778464
epoch = 6000.000000, 기울기 =2.7176, 절편=-18.775527
epoch = 6000.000000, 기울기 =2.7177, 절편=-18.775516
epoch = 6000.000000, 기울기 =2.7177, 절편=-18.775516
epoch = 6000.000000, 기울기 =2.7177, 절편=-18.775516
epoch = 7000.000000, 기울기 =2.8452, 절편=-19.681371
epoch = 7000.000000, 기울기 =2.8451, 절편=-19.681383
epoch = 7000.000000, 기울기 =2.8246, 절편=-19.684806
epoch = 7000.000000, 기울기 =2.8452, 절편=-19.682228
epoch = 7000.000000, 기울기 =2.8453, 절편=-19.682220
epoch = 7000.000000, 기울기 =2.8453, 절편=-19.682220
epoch = 7000.000000, 기울기 =2.8453, 절편=-19.682220
epoch = 8000.000000, 기울기 =2.9580, 절편=-20.481573
epoch = 8000.000000, 기울기 =2.9580, 절편=-20.481581
epoch = 8000.000000, 기울기 =2.9397, 절편=-20.484632
epoch = 8000.000000, 기울기 =2.9581, 절편=-20.482333
epoch = 8000.000000, 기울기 =2.9581, 절편=-20.482328
epoch = 8000.000000, 기울기 =2.9581, 절편=-20.482328
epoch = 8000.000000, 기울기 =2.9581, 절편=-20.482328
epoch = 9000.000000, 기울기 =3.0592, 절편=-21.197935
epoch = 9000.000000, 기울기 =3.0591, 절편=-21.197941
epoch = 9000.000000, 기울기 =3.0426, 절편=-21.200693
epoch = 9000.000000, 기울기 =3.0592, 절편=-21.198619
epoch = 9000.000000, 기울기 =3.0593, 절편=-21.198615
epoch = 9000.000000, 기울기 =3.0593, 절편=-21.198615
epoch = 9000.000000, 기울기 =3.0593, 절편=-21.198615
epoch = 10000.000000, 기울기 =3.1508, 절편=-21.846572
epoch = 10000.000000, 기울기 =3.1508, 절편=-21.846577
epoch = 10000.000000, 기울기 =3.1358, 절편=-21.849084
epoch = 10000.000000, 기울기 =3.1509, 절편=-21.847195
epoch = 10000.000000, 기울기 =3.1509, 절편=-21.847191
epoch = 10000.000000, 기울기 =3.1509, 절편=-21.847191
epoch = 10000.000000, 기울기 =3.1509, 절편=-21.847191
epoch = 11000.000000, 기울기 =3.2347, 절편=-22.439320
epoch = 11000.000000, 기울기 =3.2347, 절편=-22.439323
epoch = 11000.000000, 기울기 =3.2209, 절편=-22.441625
epoch = 11000.000000, 기울기 =3.2348, 절편=-22.439890
epoch = 11000.000000, 기울기 =3.2348, 절편=-22.439888
epoch = 11000.000000, 기울기 =3.2348, 절편=-22.439888
epoch = 11000.000000, 기울기 =3.2348, 절편=-22.439888
epoch = 12000.000000, 기울기 =3.3120, 절편=-22.985135
epoch = 12000.000000, 기울기 =3.3120, 절편=-22.985138
epoch = 12000.000000, 기울기 =3.2992, 절편=-22.987266
epoch = 12000.000000, 기울기 =3.3121, 절편=-22.985662
epoch = 12000.000000, 기울기 =3.3121, 절편=-22.985660
epoch = 12000.000000, 기울기 =3.3121, 절편=-22.985660
epoch = 12000.000000, 기울기 =3.3121, 절편=-22.985660
epoch = 13000.000000, 기울기 =3.3837, 절편=-23.490967
epoch = 13000.000000, 기울기 =3.3837, 절편=-23.490969
epoch = 13000.000000, 기울기 =3.3718, 절편=-23.492949
epoch = 13000.000000, 기울기 =3.3837, 절편=-23.491456
epoch = 13000.000000, 기울기 =3.3838, 절편=-23.491455
epoch = 13000.000000, 기울기 =3.3838, 절편=-23.491455
epoch = 13000.000000, 기울기 =3.3838, 절편=-23.491455
epoch = 14000.000000, 기울기 =3.4505, 절편=-23.962319
epoch = 14000.000000, 기울기 =3.4505, 절편=-23.962321
epoch = 14000.000000, 기울기 =3.4394, 절편=-23.964170
epoch = 14000.000000, 기울기 =3.4506, 절편=-23.962776
epoch = 14000.000000, 기울기 =3.4506, 절편=-23.962774
epoch = 14000.000000, 기울기 =3.4506, 절편=-23.962774
epoch = 14000.000000, 기울기 =3.4506, 절편=-23.962774
epoch = 15000.000000, 기울기 =3.5131, 절편=-24.403623
epoch = 15000.000000, 기울기 =3.5131, 절편=-24.403624
epoch = 15000.000000, 기울기 =3.5027, 절편=-24.405360
epoch = 15000.000000, 기울기 =3.5132, 절편=-24.404051
epoch = 15000.000000, 기울기 =3.5132, 절편=-24.404050
epoch = 15000.000000, 기울기 =3.5132, 절편=-24.404050
epoch = 15000.000000, 기울기 =3.5132, 절편=-24.404050
epoch = 16000.000000, 기울기 =3.5720, 절편=-24.818505
epoch = 16000.000000, 기울기 =3.5720, 절편=-24.818506
epoch = 16000.000000, 기울기 =3.5622, 절편=-24.820142
epoch = 16000.000000, 기울기 =3.5720, 절편=-24.818908
epoch = 16000.000000, 기울기 =3.5720, 절편=-24.818907
epoch = 16000.000000, 기울기 =3.5720, 절편=-24.818907
epoch = 16000.000000, 기울기 =3.5720, 절편=-24.818907
epoch = 17000.000000, 기울기 =3.6276, 절편=-25.209968
epoch = 17000.000000, 기울기 =3.6276, 절편=-25.209969
epoch = 17000.000000, 기울기 =3.6183, 절편=-25.211516
epoch = 17000.000000, 기울기 =3.6276, 절편=-25.210349
epoch = 17000.000000, 기울기 =3.6276, 절편=-25.210349
epoch = 17000.000000, 기울기 =3.6276, 절편=-25.210349
epoch = 17000.000000, 기울기 =3.6276, 절편=-25.210349
epoch = 18000.000000, 기울기 =3.6802, 절편=-25.580529
epoch = 18000.000000, 기울기 =3.6802, 절편=-25.580530
epoch = 18000.000000, 기울기 =3.6714, 절편=-25.581997
epoch = 18000.000000, 기울기 =3.6802, 절편=-25.580891
epoch = 18000.000000, 기울기 =3.6802, 절편=-25.580890
epoch = 18000.000000, 기울기 =3.6802, 절편=-25.580890
epoch = 18000.000000, 기울기 =3.6802, 절편=-25.580890
epoch = 19000.000000, 기울기 =3.7302, 절편=-25.932318
epoch = 19000.000000, 기울기 =3.7302, 절편=-25.932319
epoch = 19000.000000, 기울기 =3.7218, 절편=-25.933714
epoch = 19000.000000, 기울기 =3.7302, 절편=-25.932662
epoch = 19000.000000, 기울기 =3.7302, 절편=-25.932662
epoch = 19000.000000, 기울기 =3.7302, 절편=-25.932662
epoch = 19000.000000, 기울기 =3.7302, 절편=-25.932662
epoch = 20000.000000, 기울기 =3.7777, 절편=-26.267155
epoch = 20000.000000, 기울기 =3.7777, 절편=-26.267155
epoch = 20000.000000, 기울기 =3.7698, 절편=-26.268484
epoch = 20000.000000, 기울기 =3.7778, 절편=-26.267482
epoch = 20000.000000, 기울기 =3.7778, 절편=-26.267482
epoch = 20000.000000, 기울기 =3.7778, 절편=-26.267482
epoch = 20000.000000, 기울기 =3.7778, 절편=-26.267482
epoch 값이 증가할 수록 곡선이 가파르게 변하는 것을 알 수 있다.
만약 입력 값이 추가되어 세 개 이상의 입력 값을 다룬다면 시그모이드 함수가 아니라 소프트맥스(softmax)라는 함수를 써야 한다.
'공부정리 > 모두의 딥러닝 (교재 정리)' 카테고리의 다른 글
[deeplearning] 신경망의 이해 - 오차 역전파 (0) | 2022.04.29 |
---|---|
[deeplearning] 신경망의 이해 - 퍼셉트론, 다층 퍼셉트론 (0) | 2022.04.29 |
[deeplearning] 딥러닝의 동작 원리 - 경사 하강법 (0) | 2022.04.14 |
[deeplearning] 딥러닝의 동작 원리 - 선형 회귀 (0) | 2022.04.14 |
[deeplearning] 딥러닝을 위한 기초 수학 (0) | 2022.04.07 |