vlab-kaist / NN101_23S

MIT License
6 stars 7 forks source link

[LAB] Week 2_Problem 1_정석현 #97

Closed supermathking closed 1 year ago

supermathking 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                   ##
##                                                            ##

# 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

    ### IMPLEMENT FROM HERE
    y_min = min(y_train)
    y_max = max(y_train)

    def normalize(y):
        return (y - y_min) / (y_max - y_min)

    y_train_nor = [normalize(y) for y in y_train]

    w = torch.tensor(random(), requires_grad=True)
    b = torch.tensor(random(), requires_grad=True)
    lr = 0.01
    cy = 30000

    x_tensor = torch.tensor(x_train, requires_grad=True)
    y_tensor = torch.tensor(y_train_nor, requires_grad=True)

    optimizer = torch.optim.Adam([w, b], lr=lr)

    for i in range(cy):
        optimizer.zero_grad()

        y_tensor_pre = w * x_tensor + b
        err_tensor = (y_tensor - y_tensor_pre)
        err = err_tensor.pow(2).sum()
        err.backward()
        optimizer.step()

    return [w.data.item() * (y_max - y_min), b.data.item() * (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)

    re = torch.tensor(x_test) * w + b

    return re.tolist()

if __name__ == "__main__":
    x_train = [-1.0, 1.0, 2.0, 3.0, 4.0]
    y_train = [0.0, 6.0, 9.0, 12.0, 15.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

just hard coding f : mean square

Output (Optional)

No response

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 supermathking {'supermathking': 62.6}

github-actions[bot] commented 1 year ago

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

github-actions[bot] commented 1 year ago

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

github-actions[bot] commented 1 year ago

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

github-actions[bot] commented 1 year ago

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

github-actions[bot] commented 1 year ago

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

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 supermathking {'supermathking': 78.8}

github-actions[bot] commented 1 year ago

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

github-actions[bot] commented 1 year ago

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

github-actions[bot] commented 1 year ago

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

github-actions[bot] commented 1 year ago

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

github-actions[bot] commented 1 year ago

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

github-actions[bot] commented 1 year ago

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

Dongyeongkim commented 1 year ago

This issue is closed now because of the lack of progress.