[ML] 4. Multivariable linear regression

김성 교수의 '모두를 위한 머신러닝/딥러닝' 정리


Multivariable Linear Regression


  • Recap : 
    • Hypothesis
      - H(x) = Wx + b   :: 여기서 W 와 b를 학습
    • Cost function
      - cost(W,b) = 1/m sig ( H(x) - y ) ^2 :: 의 최솟값을 찾아라 즉 실제값과 차이의 제곱
    • Gradient descent algorithm
      - 2차원곡선의 경사면을 따라내려가 최소값을 찾는다.
  • Multivariable: 
    이런경우에는 H(x1,x2,x3) = w1x1 + w2x2 + w3x3 + b 이런식으로 확장 시켜줘야함
    그런데 이렇게 옆으로 계속 늘리면 너무 길어짐 -> Matrix로 처리하면된다 

    numpy 의 none의 경우에는 n개가 들어올수있음
    추가적으로 H(X) = XW식으로 바뀌면서 matmul이라는 개념이 들어온다 



Tensor

  • 기본코드

    # Lab 4 Multi-variable linear regression
    import tensorflow as tf
    tf.set_random_seed(777)  # for reproducibility
    
    x1_data = [73., 93., 89., 96., 73.]
    x2_data = [80., 88., 91., 98., 66.]
    x3_data = [75., 93., 90., 100., 70.]
    
    y_data = [152., 185., 180., 196., 142.]
    
    # placeholders for a tensor that will be always fed.
    x1 = tf.placeholder(tf.float32)
    x2 = tf.placeholder(tf.float32)
    x3 = tf.placeholder(tf.float32)
    
    Y = tf.placeholder(tf.float32)
    
    w1 = tf.Variable(tf.random_normal([1]), name='weight1')
    w2 = tf.Variable(tf.random_normal([1]), name='weight2')
    w3 = tf.Variable(tf.random_normal([1]), name='weight3')
    b = tf.Variable(tf.random_normal([1]), name='bias')
    
    hypothesis = x1 * w1 + x2 * w2 + x3 * w3 + b
    print(hypothesis)
    
    # cost/loss function
    cost = tf.reduce_mean(tf.square(hypothesis - Y))
    
    # Minimize. Need a very small learning rate for this data set
    optimizer = tf.train.GradientDescentOptimizer(learning_rate=1e-5)
    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())
    
    for step in range(2001):
        cost_val, hy_val, _ = sess.run([cost, hypothesis, train],
                                       feed_dict={x1: x1_data, x2: x2_data, x3: x3_data, Y: y_data})
        if step % 10 == 0:
            print(step, "Cost: ", cost_val, "\nPrediction:\n", hy_val)
    

  • Matrix 곱으로 구현

    # Lab 4 Multi-variable linear regression
    import tensorflow as tf
    tf.set_random_seed(777)  # for reproducibility
    
    x_data = [[73., 80., 75.],
              [93., 88., 93.],
              [89., 91., 90.],
              [96., 98., 100.],
              [73., 66., 70.]]
    y_data = [[152.],
              [185.],
              [180.],
              [196.],
              [142.]]
    
    
    # placeholders for a tensor that will be always fed.
    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')
    
    # Hypothesis
    hypothesis = tf.matmul(X, W) + b
    
    # Simplified cost/loss function
    cost = tf.reduce_mean(tf.square(hypothesis - Y))
    
    # Minimize
    optimizer = tf.train.GradientDescentOptimizer(learning_rate=1e-5)
    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())
    
    for step in range(2001):
        cost_val, hy_val, _ = sess.run(
            [cost, hypothesis, train], feed_dict={X: x_data, Y: y_data})
        if step % 10 == 0:
            print(step, "Cost: ", cost_val, "\nPrediction:\n", hy_val)
    


다음 포스팅

  • Logistic (Regression) Classification

No comments:

Post a Comment