hubring

Multi-variable linear regression 본문

AI/Tensorflow

Multi-variable linear regression

Hubring 2021. 2. 24. 13:41

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

하나의 변수가 아닌 여러개의 변수를 이용하여 선형 회귀하는 방법을 알아보자.

Hypothesis

Cost function

Matrix

  • 위 식으로 하면 많은 인자를 나열하기 힘들어짐..
  • 집합의 곱을 이용하여 좀 더 간단하게 표현할 수 있다.
  • 실제로 연산을 할때 x1, x2, x3 의 각 데이터(instance) 값은 여러개가 존재한다.
  • 이 경우 아래와 같이 구하면 전체를 Matrix를 이용하여 원하는 값을 한번에 구할 수 있다.

Tensorflow 예제코드

   x_data = [
             [73., 80., 75.],
             [93., 88., 93.],
             [89., 91., 90.],
             [96., 98., 100.],
             [73., 66., 70.],
             ]

  y_data = [[152.], [185.], [180.], [196.], [142.]]

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

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

  hypotheis = tf.matmul(X,W) + b

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

  optimizer = tf.train.GradientDescentOptimizer(learning_rate = 1e-5)
  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:x_data, Y:y_data})
   if step % 20 == 0 :
     print(step, cost_val, W_val, b_val)

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

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