vlab-kaist / NN101_23S

MIT License
6 stars 7 forks source link

[LAB] Week 2_Problem 1_이현석 #59

Closed RadioActiveBlackTi closed 1 year ago

RadioActiveBlackTi commented 1 year ago

Problem

Week 2_Problem 1

Source Code

import torch
from random import random 
from typing import Callable

##                         Problem 1                          ##
##                                                            ##
##            Arbitary x_train, y_train are given.            ##
##   Suppose that x and y have linear correlation, y=wx+b.    ##
##     In function training(), you should return [w, b].      ##
##          In function predict(), you should return          ##
##            list y_test corresponding to x_test.            ##
##                  Made by @jangyoujin0917                   ##
##                                                            ##

# attempt

# NOTE : Feel free to use torch.optim and tensor.

def training(x_train : list[float], y_train : list[float]) -> list[float]: # DO NOT MODIFY FUNCTION NAME
    # Data normalization code (prevents overflow when calculating MSE, prevents underfitting)
    # Note that you need to convert [w, b] to the original scale before returning value
    # w = w * (y_max - y_min)
    # b = b * (y_max - y_min) + y_min
    y_min = min(y_train)
    y_max = max(y_train)
    normalize = lambda y : (y - y_min)/(y_max - y_min)

    ### IMPLEMENT FROM HERE

    epochs = 20000

    y_train_normalized = list(map(normalize, y_train))
    x = torch.FloatTensor(x_train)
    y = torch.FloatTensor(y_train_normalized)
    m = len(y_train)

    w = torch.tensor([1], requires_grad=True, dtype=torch.float32)
    b = torch.tensor([1], requires_grad=True, dtype=torch.float32)

    optimizer = torch.optim.Adam([w, b], lr=5e-3)

    for i in range(epochs):
        lf = w * x + b

        cost = torch.sum((lf - y)**2) / (2*m)

        optimizer.zero_grad()
        cost.backward()
        optimizer.step()

    return [float(w) * (y_max - y_min), float(b) * (y_max - y_min) + y_min]

def predict(x_train : list[float], y_train : list[float], x_test : list[float]) -> list[float]: # DO NOT MODIFY FUNCTION NAME
    ### IMPLEMENT FROM HERE
    w,b = training(x_train, y_train)
    y_test = []
    for x in x_test:
        y_test.append(w * x + b)
    return y_test

if __name__ == "__main__":
    x_train = [0.0, 1.0, 2.0, 3.0, 4.0]
    y_train = [2.0, 4.0, 6.0, 8.0, 10.0] # Note that not all test cases give clear line.
    x_test = [5.0, 10.0, 8.0]

    w, b = training(x_train, y_train)
    y_test = predict(x_train, y_train, x_test)

    print(w, b)
    print(y_test)

Description

ㅁㄴㅇㄹ

Output (Optional)

ㅁㄴㅇㄹ

github-actions[bot] commented 1 year ago

This is an auto-generated grading output. Your code failed to run. Please check again.

github-actions[bot] commented 1 year ago

This is an auto-generated grading output. Your code failed to run. Please check again.

github-actions[bot] commented 1 year ago

This is an auto-generated grading output. Your code failed to run. Please check again.

github-actions[bot] commented 1 year ago

This is an auto-generated grading output. Your code failed to run. Please check again.

github-actions[bot] commented 1 year ago

This is an auto-generated grading output. Checking code of RadioActiveBlackTi {'RadioActiveBlackTi': nan}

github-actions[bot] commented 1 year ago

This is an auto-generated grading output. Checking code of RadioActiveBlackTi {'RadioActiveBlackTi': nan}

github-actions[bot] commented 1 year ago

This is an auto-generated grading output. Checking code of RadioActiveBlackTi {'RadioActiveBlackTi': nan}

github-actions[bot] commented 1 year ago

This is an auto-generated grading output. Checking code of RadioActiveBlackTi {'RadioActiveBlackTi': nan}

github-actions[bot] commented 1 year ago

This is an auto-generated grading output. Checking code of RadioActiveBlackTi {'RadioActiveBlackTi': nan}

github-actions[bot] commented 1 year ago

This is an auto-generated grading output. Checking code of RadioActiveBlackTi {'RadioActiveBlackTi': nan}

github-actions[bot] commented 1 year ago

This is an auto-generated grading output. Checking code of RadioActiveBlackTi {'RadioActiveBlackTi': 69.6}

github-actions[bot] commented 1 year ago

This is an auto-generated grading output. Checking code of RadioActiveBlackTi {'RadioActiveBlackTi': 41.6}

github-actions[bot] commented 1 year ago

This is an auto-generated grading output. Checking code of RadioActiveBlackTi {'RadioActiveBlackTi': 41.0}

github-actions[bot] commented 1 year ago

This is an auto-generated grading output. Checking code of RadioActiveBlackTi {'RadioActiveBlackTi': 75.4}