Lec-04 2 Multi variable linear regression

박준영·2025년 11월 11일

딥러닝 공부

목록 보기
7/23

remind

  • Hypothesis : H(x1,x2,x3)=w1x1+w2x2+w3x3H(x_1, x_2, x_3)=w_1x_1+w_2x_2+w_3x_3
# 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

random weights (스칼라 3개 버전)

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):
    # tf.GradientTape() to record the gradient of the cost function
    with tf.GradientTape() as tape:
    	# 내부에 있는 변수들의 변화량을 tape에 기록한다.
        hypothesis = w1 * x1 + w2 * x2 + w3 * x3 + b
        cost = tf.reduce_mean(tf.square(hypothesis - Y))

    # calculates the gradients of the cost
	# cost와 4개의 변수의 기울기값을 구함
    w1_grad, w2_grad, w3_grad, b_grad = tape.gradient(cost, [w1, w2, w3, b])

    # update w1, w2, w3 and 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()))

numpy slice로 행렬 형태로 전환

import numpy as np

data = np.array([
    # X1,  X2,  X3,    y
    [73., 80., 75., 152.],
    [93., 88., 93., 185.],
    [89., 91., 90., 180.],
    [96., 98., 100.,196.],
    [73., 66., 70., 142.],
], dtype=np.float32)

# slice data
# 앞은 행, 뒤는 열
# :는 "처음부터 끝까지"
# :-1은 "마지막 열 제외"
# [:, [-1]]은 "마지막 열만 2차원 컬럼 벡터로"
X = data[:, :-1]   # 모든 행, 마지막 열을 뺀 특징 3개 → shape [5,3]
y = data[:, [-1]]  # 모든 행, 마지막 열만 → shape [5,1]

W = tf.Variable(tf.random_normal([3, 1]))	# 변수가 3개, 출력이 1개
b = tf.Variable(tf.random_normal([1]))

# hypothesis, prediction function
def predict(X):
	return tf.matmul(X, W) + b	# 이후에 b를 생략할 수도 있음

행렬 가중치 버전

W = tf.Variable(tf.random_normal([3, 1]))  # 변수 3개 → [3,1]
b = tf.Variable(tf.random_normal([1]))

# hypothesis, prediction function
def predict(X):
    return tf.matmul(X, W) + b  # 필요 시 b 생략 가능

full code

import tensorflow as tf
import numpy as np
tf.random.set_seed(0)

data = np.array([
    # X1,   X2,   X3,     y
    [73.,  80.,  75.,  152.],
    [93.,  88.,  93.,  185.],
    [89.,  91.,  90.,  180.],
    [96.,  98., 100.,  196.],
    [73.,  66.,  70.,  142.],
], dtype=np.float32)

# slice data
X = data[:, :-1]
y = data[:, [-1]]

W = tf.Variable(tf.random.normal([3, 1]))
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 = 20000
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 gradients of the loss
    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()))

matrix 사용 여부에 따른 코드 비교

  • [x1 x2 x3][w1w2w3]=x1w1+x2w2+x3w3[\,x_1\ x_2\ x_3\,]\begin{bmatrix}w_1\\ w_2\\ w_3\end{bmatrix}=x_1w_1+x_2w_2+x_3w_3 — 사용 X

스칼라 3개 버전

# initialize W
w1 = tf.Variable(tf.random.normal([1]))
w2 = tf.Variable(tf.random.normal([1]))
w3 = tf.Variable(tf.random.normal([1]))

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

# updates
w1.assign_sub(learning_rate * w1_grad)
w2.assign_sub(learning_rate * w2_grad)
w3.assign_sub(learning_rate * w3_grad)
  • H(X)=XWH(X)=XW 사용

행렬곱 버전

# initialize W
W = tf.Variable(tf.random.normal([3, 1]))

# hypothesis
hypothesis = tf.matmul(X, W) + b

# updates
W.assign_sub(learning_rate * W_grad)

출처: 모두를 위한 딥러닝 강좌 2
https://www.youtube.com/watch?v=7eldOrjQVi0&list=PLQ28Nx3M4Jrguyuwg4xe9d9t2XE639e5C

0개의 댓글