vlab-kaist / NN101_23S

MIT License
6 stars 7 forks source link

[LAB] Week 2_Problem 1_채이한 #71

Closed Twenty1111 closed 1 year ago

Twenty1111 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
    # 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)
    norm_x=[]
    norm_y=[]
    for i in range(len(x_train)):
          norm_y.append(normalize(y_train[i]))
    m=len(x_train)
    w,b=0,0
    alpha=0.1
    alpha2=1
    for i in range(1000):
          diff=0
          diff2=0
          for i in range(m):
              diff+=(w*+x_train[i]-norm_y[i])*x_train[i]
              diff2+=(w*x_train[i]+b-norm_y[i])
          w-=alpha*diff/m
          b-=alpha2*diff2/m
    return w*(y_max-y_min),b*(y_max-y_min)+y_min
    ### IMPLEMENT FROM HERE

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

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)

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. 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 Twenty1111 {'Twenty1111': nan}

jangyoujin0917 commented 1 year ago

Since we don't know about the range of x_train and y_train, derivative of loss can be enormous value to update w and b. Therefore, in this code, weights and biases of test case 2, 3 diverges. I think you can fix this problem by adjusting the learning rate.

Dongyeongkim commented 1 year ago

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