vlab-kaist / NN101_23S

MIT License
6 stars 7 forks source link

[LAB] Week 3_Problem 1_박하원 #107

Closed peppermint-herb closed 1 year ago

peppermint-herb commented 1 year ago

Problem

Week 3_Problem 1

Source Code

import torch
import torch.nn.functional as F
from random import random 
from typing import Callable

##                         Problem 1                          ##
##                                                            ##
##            Arbitary x_train, y_train are given.            ##
##          In function predict(), you should return          ##
##            list y_test corresponding to x_test.            ##
##               y_train only contains 0 and 1.               ##
##             Therefore, use logstic regression.             ##
##                  Made by @jangyoujin0917                   ##
##                                                            ##

# NOTE : 1. Feel free to use torch.optim and tensor.
#        2. In this problem, we will only grade "predict" function.
#           Function "training" is only for modularization.

def training(x_train : list, y_train : list) -> tuple: # DO NOT MODIFY FUNCTION NAME
    y_min = min(y_train)
    y_max = max(y_train)
    normalize = lambda y : (y - y_min)/(y_max - y_min)

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

    w = torch.tensor(1., requires_grad=True)
    b = torch.tensor(0., requires_grad=True)

    alpha = 0.0001
    epoch = 1000

    x_train_tensor = torch.tensor(x_train, requires_grad=True)
    y_train_tensor = torch.tensor(normalized_y_train, requires_grad=True)
    optimizer = torch.optim.SGD([w, b], lr=alpha)

    for _ in range(epoch):
        optimizer.zero_grad()
        hypothesis = torch.sigmoid(x_train_tensor * w + b)

        error = -(y_train_tensor * torch.log(hypothesis) + (1 - y_train_tensor) * torch.log(1 - hypothesis)).mean()
        error.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, y_train : list, x_test : list) -> list: # DO NOT MODIFY FUNCTION NAME
    w, b = training(x_train, y_train)

    temp = torch.sigmoid(torch.tensor(x_test) * (torch.tensor(w)) + torch.tensor(b))

    return [0 if i[0] < 0.5 else 1 for i in temp.tolist()]
    ### IMPLEMENT FROM HERE

if __name__ == "__main__":
    # This is very simple case. Passing this testcase do not mean that the code is perfect.
    # Please consider for the practial problems when score is not high.
    x_train = [[0., 1.], [1., 0.], [2., 5.], [3., 1.], [4., 2.]]
    y_train = [0., 0., 1., 0., 1.]
    x_test = [[7., 2.], [1.5, 1.], [2.5, 0.5]]

    y_test = predict(x_train, y_train, x_test)

    print(y_test)

Description

.

Output (Optional)

No response

github-actions[bot] commented 1 year ago

This is an auto-generated grading output. Checking code of peppermint-herb {'peppermint-herb': 49.5}

Dongyeongkim commented 1 year ago

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