Machine learning, Deep learning

Multi-variable Linear Regression

Connor Kim 2020. 7. 3. 21:08

여러개 변수를 사용하는 다변수 선형 회귀

 

하나의 변수에서 하나의 결과값을 가지는건, '단순선형회귀'

 

 

여러가지 변수를 고려 ->  다변수 선형 회귀

 

변수가 하나일 떄, Hypothesis를 구하는 법

변수가 여러 개일 떄, Hypothesis를 구하는 법

 

 

만약, 변수들이 너무 많다? 그러면 다음과 같은걸 고려해볼 수 있다.

Matrix를 고려한다.

 

 

 

x매트릭스를 w매트릭스로 뭔가를 구해주는 방식으로 변한다. 

 

 

Tensorflow로 구현

 

# data and label
x1 = [73., 93., 89., 96., 73.]
x2 = [80., 88., 91., 98., 66.]
x3 = [75., 93., 90., 100., 70.]
Y = [152., 185., 180., 196., 142.]

# weights
w1 = tf.Variable(10.)
w2 = tf.Variable(10.)
w3 = tf.Variable(10.)
b = tf.Variable(10.)

hypothesis = w1 * x1 + w2 * x2 + w3 * x3 + b

 

 

다르게 코딩으로 구현

# data and label
x1 = [73., 93., 89., 96., 73.]
x2 = [80., 88., 91., 98., 66.]
x3 = [75., 93., 90., 100., 70.]
Y = [152., 185., 180., 196., 142.]

# random weights
# 보통 랜덤값을 준다.
w1 = tf.Variable(tf.random.normal([1]))
w2 = tf.Variable(tf.random.normal([1]))
w3 = tf.Variable(tf.random.normal([1]))
b = tf.Variable(tf.random.normal([1]))

learning_rate = 0.000001

for i in range(1000 + 1): # weight 업데이트를 천 번 한다.
    # tf.GradientTape() to record the gradient of the cost function
    with tf.GradientTape() as tape: # gradient 와 cost를 tape에 기록
        hypothesis = w1 * x1 + w2 * x2 + w3 * x3 + b
        cost = tf.reduce_mean(tf.square(hypothesis - Y))
    
    # calculates the gradients of the cost
    w1_grad, w2_grad, w3_grad, b_grad = tape.gradient(cost, [w1, w2, w3, b])
    
    w1.assign_sub(learning_rate * w1_grad)
    w2.assign_sub(learning_rate * w2_grad)
    w3.assign_sub(learning_rate * w3_grad)
    b.assign_sub(learning_rate * b_grad)
    
    if i % 50 == 0:
        print('{:5} | {:12.4f}'.format(i, cost.numpy()))
        

X = data[:, :-1]
y = data[:, [-1]] # 위의 Y의 열이 1이기 떄문

W = tf.Variable(tf.random.normal([3,1])) # X열이 3개
b = tf.Variable(tf.random.normal([1]))

learning_rate = 0.000001

# hypothesis: prediction function
def predict(X):
    return tf.matmul(X, W) + b

n_epochs = 2000
for i in range(n_epochs + 1):
    # record the gradient of the cost function
    with tf.GradientTape() as tape:
        cost = tf.reduce_mean((tf.square(predict(X) - y)))
        
    # calculates the gradient of the cost
    W_grad, b_grad = tape.gradient(cost, [W, b])
    
    # updates parameters(W and b)
    W.assign_sub(learning_rate * W_grad)
    b.assign_sub(learning_rate * b_grad)
    
    if i % 100 ==0:
        print("{:5} | {:10.4f}".format(i, cost.numpy()))