■ Cost 함수를 그래프로 표시한다.
▶Matplotlib를 이용한 그래프 그리기
import matplotlib.pyplot as plt
- matplotlib를 임포트한다.
W_history=[]
cost-history=[]
- x축과 y축에 사용할 배열을 할당한다.
- x축은 W_history , y축은 cost_history 이다.
curr_cost = sess.run(cost,feed_dict={W:curr_W})
W_history.append(curr_W)
cost_history.append(curr_cost)
- sess.run() 을 통하여 cost ops를 실행하고 각각의 값을 넣는다.
plt.plot(W_history, cost_history)
plt.show()
- 그래프를 그리고 화면에 출력한다.
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | # Lab 3-1 Minimizing Cost show graph import tensorflow as tf import matplotlib.pyplot as plt tf.set_random_seed(777) # for reproducibility X = [1, 2, 3] Y = [1, 2, 3] W = tf.placeholder(tf.float32) hypothesis = X * W # cost/loss function cost = tf.reduce_mean(tf.square(hypothesis - Y)) # Launch the graph in a session. sess = tf.Session() # Variables for plotting cost function W_history = [] cost_history = [] for i in range(-30, 50): curr_W = i * 0.1 curr_cost = sess.run(cost, feed_dict={W: curr_W}) W_history.append(curr_W) cost_history.append(curr_cost) # Show the cost function plt.plot(W_history, cost_history) plt.show() | cs |
반응형
'잡다한 IT > 머신러닝 & 딥러닝' 카테고리의 다른 글
03-3 Minimizing Cost tf optimizer (0) | 2018.08.07 |
---|---|
03-2 Minimizing Cost without tensorflow APIs (0) | 2018.08.07 |
02-3 Linear Regression : feed from variable (0) | 2018.08.07 |
02-2 Linear Regression : feed (0) | 2018.08.07 |
02-1 Linear Regression (0) | 2018.08.07 |