printf("ho_tari\n");

ep.30 딥러닝개론4 본문

두산 로보틱스 부트캠프 ROKEY/Computer Vision 교육

ep.30 딥러닝개론4

호타리 2024. 8. 19. 16:29

2024.8.19

 

import numpy as np
import matplotlib.pyplot as plt

x_data = [1.0, 2.0, 3.0]
y_data = [2.0, 4.0, 6.0]

w = 1.0

def forward(x):
  return x * w

def loss(x, y):
  y_pred = forward(x)
  return (y_pred - y) * (y_pred - y)

w_list = []
mse_list = []

for w in np.arange(0.0, 4.1, 0.1):
  print("w=", w)
  l_sum = 0
  for x_val, y_val in zip(x_data, y_data):
    y_pred_val = forward(x_val)
    l = loss(x_val, y_val)
    l_sum =+ l
    print("\t", x_val, y_val, y_pred_val, l)

  print("MSE=", l_sum / 3)
  w_list.append(w)
  mse_list.append(l_sum / 3)

plt.plot(w_list, mse_list)
plt.ylabel('Loss')
plt.xlabel('w')
plt.show()

x_data = [1.0, 2.0, 3.0]
y_data = [2.0, 4.0, 6.0]

w = 1.0

def forward(x):
  return x * w

def loss(x, y):
  y_pred = forward(x)
  return (y_pred - y) * (y_pred - y)

def gradient(x, y):
  return 2 * x * (x * w - y)

print("predict (before training)", 4, forward(4))

for epoch in range(100):
  for x_val, y_val in zip(x_data, y_data):
    grad = gradient(x_val, y_val)
    w = w - 0.01 * grad
    print("\tgrad: ", x_val, y_val, grad)
    l = loss(x_val, y_val)

  print("progress:", epoch, "w=", w, "loss=", l)

print("predict (after training)", "4 hours", forward(4))

import torch
from torch.autograd import Variable

x_data = [1.0, 2.0, 3.0]
y_data = [2.0, 4.0, 6.0]

w = Variable(torch.Tensor([1.0]), requires_grad=True)

x = Variable(torch.randn(1, 10))
prev_h = Variable(torch.randn(1, 20))
W_h = Variable(torch.randn(20, 20))
W_x = Variable(torch.randn(20, 10))

def forward(x):
  return x * w

def loss(x, y):
  y_pred = forward(x)
  return (y_pred - y) * (y_pred - y)

print("predict (before training)", 4, forward(4).data[0])

for epoch in range(10):
  for x_val, y_val in zip(x_data, y_data):
    l = loss(x_val, y_val)
    l.backward()
    print("\tgrad: ", x_val, y_val, w.grad.data[0])
    w.data = w.data - 0.01 * w.grad.data

    w.grad.data.zero_()
  print("progress:", epoch, l.data[0])

print("predict (after training)", 4, forward(4).data[0])

DR-01408_박성호_딥러닝개론_4차시.ipynb
0.06MB

'두산 로보틱스 부트캠프 ROKEY > Computer Vision 교육' 카테고리의 다른 글

ep.32 딥러닝개론6  (0) 2024.08.21
ep.31 딥러닝개론5  (0) 2024.08.20
ep.29 딥러닝개론3  (0) 2024.08.16
ep.28 딥러닝개론2  (0) 2024.08.14
ep.27 딥러닝개론1  (0) 2024.08.13