Notice
Recent Posts
Recent Comments
Link
250x250
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- OpenCV
- 코딩
- C언어
- python
- TensorFlow
- error
- 쉘
- 영상처리
- linux
- 알고리즘
- CV
- 리눅스
- C++
- 회귀
- 시스템프로그래밍
- 텐서플로우
- Computer Vision
- shell
- Windows10
- 학습
- 프로세스
- 턱걸이
- 운영체제
- 공부
- 딥러닝
- c
- 백준알고리즘
- Windows 10
- 프로그래밍
- 백준
Archives
- Today
- Total
줘이리의 인생적기
[tensorflow 12] 다항회귀 본문
728x90
이번에는 직선이 아니라 곡선 회귀선을 구해보도록 하겠습니다.
직선인 1차 함수에서 곡선 2차 함수로 조금 복잡해지기 때문에 함수명을 조금 간단하게 바꾸어보도록 하겠습니다.
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import random
#X = school_age_population, Y = elderly_population
X = [16.4, 14.7, 17.2, 16.6, 17.1, 20.3, 17.2, 15.6, 17.4, 18.7, 16.9, 17.1, 15.8, 19.3, 16.6, 15.1, 17.9]
Y = [11.4, 13.2, 11.1, 17.6, 16.3, 9.2, 15.2, 18.4, 18.5, 11.6, 13.7, 9.7, 21.5, 12.0, 14.5, 15.8, 14.0]
# a, b, c 랜덤값 초기화
a = tf.Variable(random.random())
b = tf.Variable(random.random())
c = tf.Variable(random.random())
#optimizer 설정
optimizer = tf.keras.optimizers.Adam(lr=0.1)
# 잔차의 제곱평균 구하기
def compute_loss():
y = a * X * X + b * X + c # 2차 함수
loss = tf.reduce_mean((Y - y) ** 2)
return loss
for i in range(3000):#3000번 반복
# loss 최소화
optimizer.minimize(compute_loss, var_list=[a, b, c])
if i % 200 == 1:
print(i, 'a:', a.numpy(), 'b:', b.numpy(), 'c:', c.numpy(), 'loss:', compute_loss().numpy())
#회귀선
line_x = np.arange(min(X), max(X), 0.1)
line_y = a * line_x * line_x + b * line_x + c #2차 함수
# 그래프
plt.plot(line_x, line_y, 'r-')
plt.plot(X, Y, 'ko')
plt.xlabel('school_age_population(%)')
plt.ylabel('elderly_population(%)')
plt.show()
어떤가요? 앞서 포스팅한 직선보다는 경향성을 조금 더 잘 나타내 주고 있는 것 같습니다.
크게 변하지는 않았지만 손실 또한 직선보다는 감소했습니다
3차함수까지 가보겠습니다
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import random
#X = school_age_population, Y = elderly_population
X = [16.4, 14.7, 17.2, 16.6, 17.1, 20.3, 17.2, 15.6, 17.4, 18.7, 16.9, 17.1, 15.8, 19.3, 16.6, 15.1, 17.9]
Y = [11.4, 13.2, 11.1, 17.6, 16.3, 9.2, 15.2, 18.4, 18.5, 11.6, 13.7, 9.7, 21.5, 12.0, 14.5, 15.8, 14.0]
# a, b, c, d 랜덤값 초기화
a = tf.Variable(random.random())
b = tf.Variable(random.random())
c = tf.Variable(random.random())
d = tf.Variable(random.random())
#optimizer 설정
optimizer = tf.keras.optimizers.Adam(lr=0.1)
# 잔차의 제곱평균 구하기
def compute_loss():
y = a * X * X * X + b * X * X + c * X + d # 3차 함수
loss = tf.reduce_mean((Y - y) ** 2)
return loss
for i in range(3000):#3000번 반복
# loss 최소화
optimizer.minimize(compute_loss, var_list=[a, b, c, d])
if i % 200 == 1:
print(i, 'a:', a.numpy(), 'b:', b.numpy(), 'c:', c.numpy(), 'd:', d.numpy(), 'loss:', compute_loss().numpy())
#회귀선
line_x = np.arange(min(X), max(X), 0.1)
line_y = a * line_x * line_x * line_x + b * line_x * line_x + c * line_x + d # 3차 함수
# 그래프
plt.plot(line_x, line_y, 'r-')
plt.plot(X, Y, 'ko')
plt.xlabel('school_age_population(%)')
plt.ylabel('elderly_population(%)')
plt.show()
거창한 3차함수가 나올 줄 알았는데 2차 함수와 크게 차이 나지 않는 회귀선이 나왔네요
이렇게 다항 회귀 말고도 지수, 로그, 분수 등 비선형 회귀를 적용해 볼 수 있습니다.
어느 것이 적절한 회귀인지 계속 바꿔보며 확인하는 것이 좋은 방법일까요?
'공부 > tensorflow' 카테고리의 다른 글
[tensorflow 14] 보스턴 주택 가격 예측 네트워크01 (0) | 2021.04.23 |
---|---|
[tensorflow 13] 딥러닝 네트워크 회귀 (0) | 2021.04.22 |
[tensorflow 11] 선형회귀02 (0) | 2021.04.20 |
[tensorflow 10] 선형회귀01 (0) | 2021.04.14 |
[tensorflow 09] XOR 네트워크 시각화 (0) | 2021.04.13 |