In [34]:
import tensorflow as tf

#x_train = [1,2,3]
#y_train = [1,2,3]

#위의 x와 y를 placeholder로 줄 수 있다. 

x_train = tf.placeholder(tf.float32, shape=[None])
y_train = tf.placeholder(tf.float32, shape=[None])

 # H(x) = wx + b

In [35]:
#여기서 Variable는 텐서플로가 사용하는 것이다.  즉 텐서플로를 실행시키면 텐서플로가 자동으로 변경시키는 값이다. 
#텐서플로가 학습을 하면서 자동을 변경을 시키는 값을 나타낸다. 
# w와 b는 최초에 랜덤 값을 부여받아 시작한다. 
# 그리고 shape를 결정해준다. [1]은 1차원 array를 뜻한다. 랭크가 1이다. 
W = tf.Variable(tf.random_normal([1]), name = 'weight')
b = tf.Variable(tf.random_normal([1]), name = 'bias')

In [36]:
hypothesis = x_train * W + b


#reduce_mean은 어떤 텐서 예를 들면 t = [1,2,3,4,5] 가  주어졌을때 평균을 내주는 것이다. 
cost = tf.reduce_mean(tf.square(hypothesis - y_train))

In [37]:
#cost 펑션까지 주어졌는데 이젠 이걸 최소화하는 것이 필요하다.
#텐서플로에서는 크래디언트디센트를 사용할 수 있다. 

optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)

#텐서플로에서 train이라는 크래프의 노드를 만들었다. 
train = optimizer.minimize(cost)

In [38]:
#위에까지는 텐서플로의 그래프를 구현해 준것이다.
#이걸 실행 할려면 세션(아마 서버같은 개념일 듯)을 개설 해주고 그 세션에서 돌려야 한다. 
#그래프는 세션 안에서 실행이 된다. 
sess = tf.Session()

#global_variables를 사용했는데 W,b  반드시 초기화를 해줘야 한다. 
sess.run(tf.global_variables_initializer())

In [40]:
#아래에서 sess.run의 결과값들을 리턴 받아 변수들에게 입력하는데 왜 저렇게 해야 에러가 안나는지 모르겠음

for step in range(2001):
    cost_val, w_val, b_val, _ = sess.run([cost,W,b,train], feed_dict={x_train: [1, 2, 3, 4, 5], y_train: [2.1, 3.1, 4.1, 5.1, 6.1]})
    if step % 20 == 0:
        print(step, cost_val, w_val, b_val)
    #    print(step, sess.run(cost), sess.run(W), sess.run(b))


0 6.06674e-08 [ 0.99984056] [ 1.10057533]
20 5.29662e-08 [ 0.99985105] [ 1.10053766]
40 4.63249e-08 [ 0.99986076] [ 1.10050261]
60 4.04493e-08 [ 0.99986994] [ 1.10046959]
80 3.5314e-08 [ 0.99987835] [ 1.10043883]
100 3.08393e-08 [ 0.99988633] [ 1.10041022]
120 2.6999e-08 [ 0.99989367] [ 1.10038364]
140 2.35891e-08 [ 0.99990064] [ 1.10035861]
160 2.05348e-08 [ 0.9999072] [ 1.10033476]
180 1.79778e-08 [ 0.99991322] [ 1.10031331]
200 1.56669e-08 [ 0.99991894] [ 1.10029256]
220 1.37171e-08 [ 0.99992418] [ 1.10027349]
240 1.20021e-08 [ 0.99992919] [ 1.10025585]
260 1.04824e-08 [ 0.99993378] [ 1.10023916]
280 9.1542e-09 [ 0.99993819] [ 1.1002233]
300 8.01356e-09 [ 0.99994206] [ 1.100209]
320 6.95715e-09 [ 0.99994594] [ 1.10019481]
340 6.12368e-09 [ 0.9999494] [ 1.10018289]
360 5.36908e-09 [ 0.99995267] [ 1.10017097]
380 4.64298e-09 [ 0.99995589] [ 1.10015905]
400 4.09101e-09 [ 0.99995857] [ 1.10014939]
420 3.57995e-09 [ 0.9999612] [ 1.10013986]
440 3.11607e-09 [ 0.99996382] [ 1.10013032]
460 2.72391e-09 [ 0.99996626] [ 1.10012186]
480 2.42126e-09 [ 0.99996829] [ 1.1001147]
500 2.12876e-09 [ 0.9999702] [ 1.10010755]
520 1.85197e-09 [ 0.99997222] [ 1.1001004]
540 1.59049e-09 [ 0.99997407] [ 1.10009325]
560 1.37912e-09 [ 0.99997592] [ 1.10008681]
580 1.23291e-09 [ 0.99997723] [ 1.10008204]
600 1.09524e-09 [ 0.99997854] [ 1.10007727]
620 9.60381e-10 [ 0.99997979] [ 1.1000725]
640 8.43374e-10 [ 0.99998111] [ 1.10006773]
660 7.32598e-10 [ 0.99998242] [ 1.10006297]
680 6.20651e-10 [ 0.99998373] [ 1.1000582]
700 5.35647e-10 [ 0.99998498] [ 1.10005391]
720 4.72585e-10 [ 0.99998599] [ 1.10005069]
740 4.30191e-10 [ 0.99998665] [ 1.1000483]
760 3.85501e-10 [ 0.9999873] [ 1.10004592]
780 3.49439e-10 [ 0.99998796] [ 1.10004354]
800 3.13094e-10 [ 0.99998856] [ 1.10004115]
820 2.75179e-10 [ 0.99998927] [ 1.10003877]
840 2.41482e-10 [ 0.99998987] [ 1.10003638]
860 2.1214e-10 [ 0.99999052] [ 1.100034]
880 1.81853e-10 [ 0.99999118] [ 1.10003161]
900 1.57411e-10 [ 0.99999189] [ 1.10002923]
920 1.33059e-10 [ 0.99999249] [ 1.10002685]
940 1.09048e-10 [ 0.99999315] [ 1.10002446]
960 9.14042e-11 [ 0.9999938] [ 1.10002208]
980 7.14977e-11 [ 0.99999446] [ 1.10001969]
1000 6.18002e-11 [ 0.99999481] [ 1.10001838]
1020 6.18002e-11 [ 0.99999481] [ 1.10001838]
1040 6.18002e-11 [ 0.99999481] [ 1.10001838]
1060 6.18002e-11 [ 0.99999481] [ 1.10001838]
1080 6.18002e-11 [ 0.99999481] [ 1.10001838]
1100 6.18002e-11 [ 0.99999481] [ 1.10001838]
1120 6.18002e-11 [ 0.99999481] [ 1.10001838]
1140 6.18002e-11 [ 0.99999481] [ 1.10001838]
1160 6.18002e-11 [ 0.99999481] [ 1.10001838]
1180 6.18002e-11 [ 0.99999481] [ 1.10001838]
1200 6.18002e-11 [ 0.99999481] [ 1.10001838]
1220 6.18002e-11 [ 0.99999481] [ 1.10001838]
1240 6.18002e-11 [ 0.99999481] [ 1.10001838]
1260 6.18002e-11 [ 0.99999481] [ 1.10001838]
1280 6.18002e-11 [ 0.99999481] [ 1.10001838]
1300 6.18002e-11 [ 0.99999481] [ 1.10001838]
1320 6.18002e-11 [ 0.99999481] [ 1.10001838]
1340 6.18002e-11 [ 0.99999481] [ 1.10001838]
1360 6.18002e-11 [ 0.99999481] [ 1.10001838]
1380 6.18002e-11 [ 0.99999481] [ 1.10001838]
1400 6.18002e-11 [ 0.99999481] [ 1.10001838]
1420 6.18002e-11 [ 0.99999481] [ 1.10001838]
1440 6.18002e-11 [ 0.99999481] [ 1.10001838]
1460 6.18002e-11 [ 0.99999481] [ 1.10001838]
1480 6.18002e-11 [ 0.99999481] [ 1.10001838]
1500 6.18002e-11 [ 0.99999481] [ 1.10001838]
1520 6.18002e-11 [ 0.99999481] [ 1.10001838]
1540 6.18002e-11 [ 0.99999481] [ 1.10001838]
1560 6.18002e-11 [ 0.99999481] [ 1.10001838]
1580 6.18002e-11 [ 0.99999481] [ 1.10001838]
1600 6.18002e-11 [ 0.99999481] [ 1.10001838]
1620 6.18002e-11 [ 0.99999481] [ 1.10001838]
1640 6.18002e-11 [ 0.99999481] [ 1.10001838]
1660 6.18002e-11 [ 0.99999481] [ 1.10001838]
1680 6.18002e-11 [ 0.99999481] [ 1.10001838]
1700 6.18002e-11 [ 0.99999481] [ 1.10001838]
1720 6.18002e-11 [ 0.99999481] [ 1.10001838]
1740 6.18002e-11 [ 0.99999481] [ 1.10001838]
1760 6.18002e-11 [ 0.99999481] [ 1.10001838]
1780 6.18002e-11 [ 0.99999481] [ 1.10001838]
1800 6.18002e-11 [ 0.99999481] [ 1.10001838]
1820 6.18002e-11 [ 0.99999481] [ 1.10001838]
1840 6.18002e-11 [ 0.99999481] [ 1.10001838]
1860 6.18002e-11 [ 0.99999481] [ 1.10001838]
1880 6.18002e-11 [ 0.99999481] [ 1.10001838]
1900 6.18002e-11 [ 0.99999481] [ 1.10001838]
1920 6.18002e-11 [ 0.99999481] [ 1.10001838]
1940 6.18002e-11 [ 0.99999481] [ 1.10001838]
1960 6.18002e-11 [ 0.99999481] [ 1.10001838]
1980 6.18002e-11 [ 0.99999481] [ 1.10001838]
2000 6.18002e-11 [ 0.99999481] [ 1.10001838]

In [41]:
print(sess.run(hypothesis, feed_dict={x_train: [5]}))


[ 6.09999275]

In [42]:
print(sess.run(hypothesis, feed_dict={x_train: [1, 4, 10]}))


[  2.10001326   5.09999752  11.099967  ]

In [43]:
sess.close()

In [45]:
print(sess.run(hypothesis, feed_dict={x_train: [4]}))


---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-45-08988b28a316> in <module>()
----> 1 print(sess.run(hypothesis, feed_dict={x_train:[4]}))

/Users/jaegyuhan/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
    787     try:
    788       result = self._run(None, fetches, feed_dict, options_ptr,
--> 789                          run_metadata_ptr)
    790       if run_metadata:
    791         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

/Users/jaegyuhan/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
    923     # Check session.
    924     if self._closed:
--> 925       raise RuntimeError('Attempted to use a closed Session.')
    926     if self.graph.version == 0:
    927       raise RuntimeError('The Session graph is empty.  Add operations to the '

RuntimeError: Attempted to use a closed Session.

In [ ]: