vlab-kaist / NN101_23S

MIT License
6 stars 7 forks source link

[LAB] Week 2_Problem 2_송준 #80

Closed songjoon closed 1 year ago

songjoon commented 1 year ago

Problem

Week 2_Problem 2

Source Code

from ctypes.wintypes import SIZE
from random import random 
from typing import Callable
from copy import deepcopy

##                         Problem 2                          ##
##                                                            ##
##            Arbitary x_train, y_train are given.            ##
##     In this problem, x_train is list of list of float.     ##
##   Suppose that x and y have linear correlation, y=wx+b.    ##
##           (In this problem, w will be a vector.)           ##
##     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[list[float]], y_train : list[float]) -> tuple[list[float], float]: # DO NOT MODIFY FUNCTION NAME
    # data normalization
    # 1. Prevents overflow when calculating MSE
    # 2. Prevents underfitting
    # Note that you need to convert [w, b] to the original scale.
    # 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)
    tmp = []
    for i in y_train:
        tmp.append(normalize(i))
    y_train = tmp
    ### IMPLEMENT FROM HERE
    size = len(x_train[0]) 
    w = []
    for _ in range(size):
        w.append(random())
    b = random()
    h = 1e-3
    alpha =0.001
    for _ in range(20000):
        #print(w,b)
        for i in range(size):
            s = 0
            for j in range(len(x_train)):
                s += (arraymult(x_train[j], w) +b - y_train[j])*x_train[j][i]
            w[i] -= alpha * s / len(x_train)
           # print(i, w[i], alpha* s / len(x_train))
        k = 0
        for j in range(len(x_train)):
            k += (arraymult(x_train[j], w) +b - y_train[j])
        b -=alpha * k / len(x_train)
        #print("result", Loss(w,b,x_train, y_train))

    tmp = []
    for i in w:
        tmp.append(i * (y_max - y_min))
    w = tmp
    b = b*(y_max - y_min) + y_min
    return (w,b)

def Loss(w,b,x_train, y_train):
    cnt =0
    for i in range(len(x_train)):
        cnt += (arraymult(w, x_train[i]) + b - y_train[i])**2
    return cnt / len(x_train)

def arraymult(x_train : list[float], w : list[float]):
    s = 0
    for i in range(len(x_train)):
        s += w[i]*x_train[i]
    return s 

def predict(x_train : list[list[float]], y_train : list[float], x_test : list[list[float]]) -> list[float]: # DO NOT MODIFY FUNCTION NAME
    (w, b) = training(x_train, y_train)
    result =[]
    #print(w,b)
    for i in x_test:
        result.append(arraymult(i,w) + b)
    return result

if __name__ == "__main__":
    x_train = [[0., 1.], [1., 0.], [2., 2.], [3., 1.], [4., 3.]]
    y_train = [3., 2., 7., 6., 11.] # y = x_0 + 2*x_1 + 1 # Note that not all test cases give clear line.
    x_test = [[5., 3.], [10., 6.], [8., 9.]]

    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)

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 songjoon Timeout reached for songjoon {'songjoon': 'timed_out'}

github-actions[bot] commented 1 year ago

This is an auto-generated grading output. Checking code of songjoon Timeout reached for songjoon {'songjoon': 'timed_out'}

github-actions[bot] commented 1 year ago

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

github-actions[bot] commented 1 year ago

This is an auto-generated grading output. Checking code of songjoon Timeout reached for songjoon {'songjoon': 'timed_out'}

github-actions[bot] commented 1 year ago

This is an auto-generated grading output. Checking code of songjoon (prob_check pid=404197) [nan, nan, nan] nan (prob_check pid=404199) [nan, nan, nan] nan Timeout reached for songjoon {'songjoon': 'timed_out'}

github-actions[bot] commented 1 year ago

This is an auto-generated grading output. Checking code of songjoon (prob_check pid=405388) [nan, nan, nan] nan (prob_check pid=405390) [nan, nan, nan] nan {'songjoon': nan}

github-actions[bot] commented 1 year ago

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

jangyoujin0917 commented 1 year ago

I think the reason is same with #66.

github-actions[bot] commented 1 year ago

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

github-actions[bot] commented 1 year ago

This is an auto-generated grading output. Checking code of songjoon Timeout reached for songjoon {'songjoon': 'timed_out'}

github-actions[bot] commented 1 year ago

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

github-actions[bot] commented 1 year ago

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

github-actions[bot] commented 1 year ago

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

Dongyeongkim commented 1 year ago

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