티스토리 뷰
원본 소스 출처 : https://github.com/hunkim/DeepLearningZeroToAll/blob/master/lab-02-3-linear_regression_tensorflow.org.py
import tensorflow as tf # Model parameters W = tf.Variable([.3], tf.float32) b = tf.Variable([-.3], tf.float32) # Model input and output x = tf.placeholder(tf.float32) y = tf.placeholder(tf.float32) linear_model = x * W + b # cost/loss function loss = tf.reduce_sum(tf.square(linear_model - y)) # sum of the squares # optimizer optimizer = tf.train.GradientDescentOptimizer(0.01) train = optimizer.minimize(loss) # training data x_train = [1, 2, 3, 4] y_train = [0, -1, -2, -3] # training loop init = tf.global_variables_initializer() sess = tf.Session() sess.run(init) # reset values to wrong for i in range(1000): sess.run(train, {x: x_train, y: y_train}) # evaluate training accuracy curr_W, curr_b, curr_loss = sess.run([W, b, loss], {x: x_train, y: y_train}) print("W: %s b: %s loss: %s" % (curr_W, curr_b, curr_loss))
한줄 한줄 살펴보면,
import tensorflow as tf
tensorflow 라이브러리를 import 하여 tf로 약칭을 정한다. (이후로 tf 라고 타이핑한다면 tensorflow 라이브러리를 사용할 수 있도록 만든다.)
W = tf.Variable([.3], tf.float32)
b = tf.Variable([-.3], tf.float32)
x = tf.placeholder(tf.float32) y = tf.placeholder(tf.float32)
linear_model = x * W + b
이는, 선형 회귀의 그래프를 그릴 수 있는 방정식이며, 뒤에서 주어지는 x_train 값을 넣었을때의 계산 결과 Y가 뒤에서 주어질 y_train 값과 일치하는 횟수가 가장 많은 최적의 W값과 b값을 구하기 위해 만든다.
loss = tf.reduce_sum(tf.square(linear_model - y)) # sum of the squares
loss라는 변수를 만들고, tensorflow 내장함수인 reduce_sum 함수와 square 함수를 이용하여 손실함수를 할당한다.
square는 제곱이며, 학습되어 예상된 Y값과 정답으로 주어진 Y값의 오차를 구해 제곱한 뒤 reduce_sum 함수로 값들을 모두 더한다.
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)
x_train = [1, 2, 3, 4] y_train = [0, -1, -2, -3]
init = tf.global_variables_initializer()
sess = tf.Session() sess.run(init) # reset values to wrong
for i in range(1000): sess.run(train, {x: x_train, y: y_train})
curr_W, curr_b, curr_loss = sess.run([W, b, loss], {x: x_train, y: y_train})
for문을 빠져나왔으면 (학습이 n번 완료되었으면), curr_w 변수와 curr_b 변수, curr_loss 변수를 각각 만들어,
curr_w 에는 학습 후 최적이라 판단된 W 의 값,
curr_b 에는 학습 후 최적이라 판단된 b의 값,
curr_loss 에는 학습 후 최적이라 판단된 최후의 오차값을 할당한다.
print("W: %s b: %s loss: %s" % (curr_W, curr_b, curr_loss))
최적의 값들을 출력해 본다.
실제 출력 결과이다.
W 는 -1에 수렴하고있으며, b는 1에 수렴하고 있다. loss는 매우 적게 표시됨을 확인 할 수 있다.
'동아리 활동' 카테고리의 다른 글
[JAVA 기초] 클래스, 상속, 접근지시제어자, 오버라이드 (0) | 2018.05.20 |
---|---|
[JAVA 기초] 오버라이드(OverRide) 예제코드 + 소스코드 리뷰 (0) | 2018.05.20 |
[JAVA 기초] 상속을 활용한 자바 프로그램 예제 (0) | 2018.05.20 |
[Java vs Kotlin] 자바와 코틀린의 차이점 (0) | 2018.05.15 |
딥러닝을 활용한 제품 예시 3가지 (0) | 2018.05.13 |
- Total
- Today
- Yesterday
- release
- SJVA
- 파이썬 문자열
- 분석도구
- 파이썬 설치
- 파이썬
- DB분석
- 파이썬이란
- 파이썬 IDE
- Python Number
- 파이썬 장점
- 파이썬 개요
- 모니터링툴
- 모니터링도구
- python
- 그라파나
- Python IDE
- 윈도우 11
- 파이썬 실행
- python3
- 알림센터
- 파이썬3
- 파이썬 개발환경
- 파이썬 문자형
- 유튜브 동영상 다운로드
- python3.9
- youtube_dl
- #baekjoon
- 파이썬 숫자타입
- Grafana
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
29 | 30 |