(출처) inflearn 모두를 위한 딥러닝, Sung kim
이론 (이전 포스팅 참고)
2019/10/03 - [bigdata/#Machine Learning] - Linear Regression의 Hypothesis와 cost 설명
Lab 2
▤ 텐서플로우 구동 매커니즘
- 그래프 Build하기
- Session 생성 후 Run
- Return/ Update의 실행 결과 출력
1. Bulid graph using TF operations
#X and Y data
x_train = [1, 2, 3] #전 포스트에서 다룬 간단한 그래프
y_train = [1, 2, 3]
W = tf.Variable(tf.random_normal([1]), name = 'weight') #W와 b의 값을 정의
b = tf.Variable(tf.random_normal([1]), name = 'bias') #W와 b의 값을 random하게 주는 방법 (1차원의 array를 줌)
#our hypothesis XW + b
hypothesis = x_train * W + b
cost = tf.reduce_mean(tf.square(hypothesis - y_train))
- 여기에서 'Variable'은 기존 프로그램에서의 '변수'와는 다른 개념
- Tensorflow가 사용하는 변수 (실행 시 자체적으로 변동 되는 데이터 : Traideble data)
- Cost함수도 마찬가지로 Tensorflow에 그대로 구현됨
Reduce_mean
: Tensor의 평균을 구하는 함수
t = [1., 2., 3., 4.]
tf.reduce_mean(t) == 2.5
Minimize
: GradientDescent 사용
#Minimize
optimizer = tf.train.GradientDescentOptimizer(learning_rate = 0.01)
train = optimizer.minimize(cost)
◐ 아직까지는 개념정도만 알아두기
2 & 3 Run/ Update graph and get results
sess = tf.Session()
sess.run(tf.global_variables_initializer()) #변수를 실행하기 위한 코드
for step in range(2001):
sess.run(train) #최소화하는 명령을 지정한 train
if step % 20 == 0: #20번에 한 번씩 출력
print(step, sess.run(cost), sess.run(W), sess.run(b)) #cost, W, b의 값을 출력
- train ↔ cost ↔ hypothesis ↔ W, b의 연결관계
- 따라서 sess.run(train) 명령으로 train만 실행시켜도 마지막 단인 W, b까지 연결되어 실행됨
☞ W = 1, b = 0이 되면 fit
◐ 학습이 진행될 수록 cost는 minimize되고, W은 1에, b은 0에 수렴함
Placeholder
: 직접 값을 넣지 않고 placehoder로 지정 후 필요할 때 값을 넣어줌
# X and Y data
x_train = [1, 2, 3]
y_train = [1, 2, 3]
#Now we can use X and Y in place of x_data and y_data
#placeholders for a tensor that will be always fed using feed_dict
X = tf.placeholder(tf.float32)
Y = tf.placeholder(tf.float32)
...
#fit the line
#fit the line
for step in range(2001):
cost_val, W_val, b_val, _ = \ #train에 해당되는 값은 중요하지 않으므로 _ = \로 표기
sess.run([cost, W, b, train], #list형태로 묶어줌
feed_dict = {X: [1, 2, 3], Y: [1, 2, 3]}) #학습시킬 때 feed_dict로 넘겨줌
if step % 20 == 0:
print(step, cost_val, W_val, b_val) #마찬가지로 20번에 한 번씩 출력
▤ placeholder를 사용하는 이유
- 만들어진 모델에 대해 값을 따로 넘겨줄 수 있음
- 선형 회귀모델을 미리 만들어두고 그때그때 지정할 수 있음
▤ shape = [None]
: 해당 데이터셋이 1차원 array이며 개수는 임의로 지정해줄 수 있다는 정의
학습 예제
feed_dict = {X: [1, 2, 3, 4, 5],
Y:[2.1, 3.1, 4.1, 5.1, 6.1]})
→ X와 Y값이 이렇게 매칭되려면 대략 W = 1, b = 1.1의 값이 나와야 함
힉습결과
◐ 학습할 수록 cost값은 0에 수렴, W는 1에 수렴, b는 1.1에 수렴
모델의 실행
print(sess.run(hypothesis, feed_dict = {X: [5]}))
# [6.100451]
print(sess.run(hypothesis, feed_dict = {X: [2.5]}))
# [3.5996404]
print(sess.run(hypothesis, feed_dict = {X: [1.5, 3.5]}))
# [2.5993161 4.5999646]
◐ 굉장히 근사한 값으로 예측함
I'm a Senior Student in Data Science !
데이터 사이언스를 공부하고 있는 4학년 학부생의 TIL 블로그입니다. 게시글이 도움 되셨다면 구독과 좋아요 :)
'Machine Learning > #Framework' 카테고리의 다른 글
[Framework] TensorFlow의 설치 및 작동법 (0) | 2019.10.04 |
---|