hubring

Linear Regression 본문

AI/Tensorflow

Linear Regression

Hubring 2021. 2. 23. 16:35

[참고] 모두를 위한 딥러닝 - 기본적인 머신러닝과 딥러닝 강좌

Regression (DATA)

x y
1 1
2 2
3 3

(Linear) Hypothesis

  • 선형식을 찾는 과정
  • 가설 식 : H(x) = Wx + b
  • 어떤 식이 좋은 가?
  • 가설과 실제 데이터를 거리를 비교하여 오차가 적은것.
    => cost function
    => H(x) - y

Cost function

Goal : minimize cost

  • minimize cost(W, b)

tensorflow 구현 (Variable 이용)

 import numpy as np
 import tensorflow.compat.v1 as tf
 import matplotlib.pyplot as plt
 tf.disable_v2_behavior()

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

 W = tf.Variable(tf.random_normal([1]), name='weight')
 b = tf.Variable(tf.random_normal([1]), name='bias')

 hypotheis = x_train * W + b

 cost = tf.reduce_mean(tf.square(hypotheis - y_train))

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

 sess = tf.Session()

 sess.run(tf.global_variables_initializer())

 for step in range(2001) :
  sess.run(train)
  if step % 20 == 0 :
    print(step, sess.run(cost), sess.run(W), sess.run(b))

tensorflow 구현 (Placeholder 이용)

  import numpy as np
  import tensorflow.compat.v1 as tf
  import matplotlib.pyplot as plt
  tf.disable_v2_behavior()

  X = tf.placeholder(tf.float32, shape=[None])
  Y = tf.placeholder(tf.float32, shape=[None])

  W = tf.Variable(tf.random_normal([1]), name='weight')
  b = tf.Variable(tf.random_normal([1]), name='bias')

  hypotheis = X * W + b
  cost = tf.reduce_mean(tf.square(hypotheis - Y))

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

  sess = tf.Session()
  sess.run(tf.global_variables_initializer())


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

'AI > Tensorflow' 카테고리의 다른 글

TensorFlow로 파일에서 데이터 읽어오기  (0) 2021.03.05
Multi-variable linear regression  (0) 2021.02.24
Linear Regression의 cost 최소화 원리  (0) 2021.02.23
머신 러닝 기본  (0) 2021.02.23
k-인접이웃 분류모형  (0) 2019.09.18