본문 바로가기

잡다한 IT/머신러닝 & 딥러닝

02-3 Linear Regression : feed from variable

■ Place hold 사용법을 익힌다. (파이썬 변수를 feed 처리함)


▶Place hold 사용 - 변수 대입

x_train = [1,2,3,4]

y_train = [0,-1,-2,-3]

- place hold 에 feed_dict로 연결할 변수 생성


curr_W,curr_b,curr_loss = sess.run([W,b,loss],{x:x_train,y:y_train})

-세션을 실행할 때 place hold에 변수를 대입한다.

이때 사용되는 것이 feed_dict이다



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
import tensorflow as tf
 
W=tf.Variable([.3],tf.float32)
b=tf.Variable([-.3],tf.float32)
 
# X and Y data
x=tf.placeholder(tf.float32)
y=tf.placeholder(tf.float32)
 
# Our hypothesis XW+b
linear_model = x*+ b
 
# cost/loss function
loss=tf.reduce_sum(tf.square(linear_model-y)) # sum of the squares
 
# Minimize
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)
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],feed_dict={x:x_train,y:y_train})
print("W:%s b:%s loss:%s"%(curr_W,curr_b,curr_loss))
cs


반응형

'잡다한 IT > 머신러닝 & 딥러닝' 카테고리의 다른 글

03-2 Minimizing Cost without tensorflow APIs  (0) 2018.08.07
03-1 Minimizing Cost show graph  (0) 2018.08.07
02-2 Linear Regression : feed  (0) 2018.08.07
02-1 Linear Regression  (0) 2018.08.07
01 Tensorflow basic  (0) 2018.08.07