top of page
스크린샷 2024-01-22 오후 4.08.05.png
kakao_01.png

딥러닝 김예림 강사님 반

공개·회원 5명

Day 14 Lab

10번 문제

np.random.seed(42)

x_train = np.random.rand(100, 2).astype(np.float32) * 10 # (100,2) 형태의 입력값

y_train = 5 * x_train[:, 0] + 2 * x_train[:, 1] + 3 + np.random.normal(0, 1, size=100) # 노이즈 추가

y_train = tf.cast(y_train, dtype=tf.float32)


w = tf.Variable(tf.random.normal([2, 1], dtype=tf.float32)) # 다중 선형 회귀: w 크기 (2,)

b = tf.Variable(tf.random.normal([1], dtype=tf.float32)) # 절편

learning_rate = 0.01

epochs = 500


for i in range(epochs):

with tf.GradientTape() as tape:

y_pred = tf.matmul(x_train, w) + b # 예측값

loss = tf.reduce_mean((y_pred - tf.reshape(y_train, (-1, 1))) ** 2) # MSE 손실 함수


# 기울기 계산

gradients = tape.gradient(loss, [w, b])


# 경사하강법 적용

w.assign_sub(learning_rate * gradients[0])

b.assign_sub(learning_rate * gradients[1])


# 50번마다 출력

if i % 50 == 0:

print(f"Step {i}: w1 = {w.numpy()[0][0]}, w2 = {w.numpy()[1][0]}, b = {b.numpy()[0]}, loss = {loss.numpy()}")


print(f"\n학습 완료: w1 = {w.numpy()[0][0]}, w2 = {w.numpy()[1][0]}, b = {b.numpy()[0]}")

10회 조회

소개

그룹에 오신 것을 환영합니다. 다른 회원과의 교류 및 업데이트 수신, 동영상 공유 등의 활동을 시작하세요.

bottom of page