02_linear_regression

선형 회귀|center

  • 주어진 x와 y 값을 가지고 서로 간의 관계를 파악
  • 새로운 x값이 주어졌을 때 y값을 쉽게 알 수 있음
In [1]:
import tensorflow as tf
In [2]:
x_data = [1, 2, 3]
y_data = [1, 2, 3]


data 생성

In [3]:
W = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
b = tf.Variable(tf.random_uniform([1], -1.0, 1.0))


placeholder 설정

In [4]:
X = tf.placeholder(tf.float32, name="X")
Y = tf.placeholder(tf.float32, name="Y")


model 설정

In [5]:
model = W*X + b


cost function

In [6]:
cost = tf.reduce_mean(tf.square(model- Y))


gradient desent

In [7]:
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1)
train_op = optimizer.minimize(cost)

@경사하강법|center


modeling

In [8]:
sess =  tf.Session()
sess.run(tf.global_variables_initializer())
for step in range(500):
    _, cost_val = sess.run([train_op, cost], feed_dict={X: x_data, Y: y_data})
    if step % 25 == 0:
        print("step: {}, cost_val: {:.5f}, W: {}, b: {}".format(step, cost_val, sess.run(W), sess.run(b)))
step: 0, cost_val: 0.45853, W: [0.71138144], b: [0.7272724]
step: 25, cost_val: 0.02285, W: [0.828638], b: [0.389546]
step: 50, cost_val: 0.00677, W: [0.9067342], b: [0.2120151]
step: 75, cost_val: 0.00201, W: [0.9492389], b: [0.11539178]
step: 100, cost_val: 0.00059, W: [0.97237265], b: [0.06280342]
step: 125, cost_val: 0.00018, W: [0.9849635], b: [0.03418154]
step: 150, cost_val: 0.00005, W: [0.99181616], b: [0.01860373]
step: 175, cost_val: 0.00002, W: [0.99554586], b: [0.01012532]
step: 200, cost_val: 0.00000, W: [0.9975758], b: [0.00551084]
step: 225, cost_val: 0.00000, W: [0.9986806], b: [0.00299932]
step: 250, cost_val: 0.00000, W: [0.99928194], b: [0.00163241]
step: 275, cost_val: 0.00000, W: [0.9996092], b: [0.00088852]
step: 300, cost_val: 0.00000, W: [0.9997873], b: [0.00048356]
step: 325, cost_val: 0.00000, W: [0.9998842], b: [0.00026316]
step: 350, cost_val: 0.00000, W: [0.99993694], b: [0.00014328]
step: 375, cost_val: 0.00000, W: [0.9999657], b: [7.794145e-05]
step: 400, cost_val: 0.00000, W: [0.99998134], b: [4.2393247e-05]
step: 425, cost_val: 0.00000, W: [0.9999898], b: [2.3081342e-05]
step: 450, cost_val: 0.00000, W: [0.99999446], b: [1.2575029e-05]
step: 475, cost_val: 0.00000, W: [0.99999696], b: [6.8529835e-06]


test

In [9]:
class prediction:
            
    def run(self, input):  
        self.input = input
        output = sess.run(model, feed_dict={X: self.input})
        print("X: {}, Y-result: {}".format(self.input, output))
        
pred = prediction()
In [10]:
pred.run(2.5)
pred.run(5)
pred.run(10)
X: 2.5, Y-result: [2.4999995]
X: 5, Y-result: [4.999995]
X: 10, Y-result: [9.999987]
In [11]:
from IPython.core.display import HTML, display

display(HTML("<style> .container{width:100% !important;}</style>"))

'Deep_Learning' 카테고리의 다른 글

06.tensorboard01_example  (0) 2018.12.09
05.deep_neural_net_Costfun2  (0) 2018.12.09
04.deep_neural_net_Costfun1  (0) 2018.12.09
03.classification  (0) 2018.12.09
01.tesnsor_and_graph  (0) 2018.12.09

+ Recent posts