■ Place hold 를 입력으로 사용하여 간단한 Linear regression 구현
▶Place hold 사용 - 상수 대입
X = tf.placeholder(tf.float32, shape=[None])
Y = tf.placeholder(tf.float32, shape=[None])
- rank = 1 이고 type이 tf.float32 인 place hold 를 생성한다.
- None은 해당 축의 원소 갯수를 임의로 선정한다는 의미이다.
sess.run([cost,W,b,train],feed_dict={X:[1,2,3],Y:[1,2,3]})
- 세션을 실행할 때 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 34 35 36 37 38 39 40 41 | #Lab 2-1 Linear Regression(variable input) #변수만 사용하여 Gradient Descent 구현 import tensorflow as tf tf.set_random_seed(777) # for reproducibility # X and Y data X=tf.placeholder(tf.float32,shape=[None]) Y=tf.placeholder(tf.float32,shape=[None]) # Try to find values for W and b to compute y_data = x_data*W+b # We know that W should be 1 and b should be 0 # But le TensroFlow figure it out #W=tf.Variable(tf.random_normal([1]),name='weight') #b=tf.Variable(tf.random_normal([1]),name='bias') W=tf.Variable(5.0,name='weight') b=tf.Variable(10.0,name='bias') # Our hypothesis XW+b hypothesis = X*W + b # cost/loss function cost=tf.reduce_mean(tf.square(hypothesis - Y)) # Minimize optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01) train = optimizer.minimize(cost) # Launch the graph in a session sess = tf.Session() # initializes global variables in the graph sess.run(tf.global_variables_initializer()) #Fit the line for step in range(2001): cost_val,W_val,b_val,_=\ sess.run([cost,W,b,train],feed_dict={X:[1,2,3],Y:[1,2,3]}) if step%200 == 0: print(step,cost_val,W_val,b_val) | cs |
반응형
'잡다한 IT > 머신러닝 & 딥러닝' 카테고리의 다른 글
03-1 Minimizing Cost show graph (0) | 2018.08.07 |
---|---|
02-3 Linear Regression : feed from variable (0) | 2018.08.07 |
02-1 Linear Regression (0) | 2018.08.07 |
01 Tensorflow basic (0) | 2018.08.07 |
참고 사이트 (0) | 2018.08.07 |