vlab-kaist / NN101_23S

MIT License
6 stars 7 forks source link

[LAB] Week 3_Problem 1_노영래 #102

Closed surodoot closed 1 year ago

surodoot 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[list[float]], y_train: list[float]) -> tuple[list[float], float]:
    x_train_tensor = torch.tensor(x_train, dtype=torch.float32)
    y_train_tensor = torch.tensor(y_train, dtype=torch.float32).view(-1, 1)

    w = torch.randn(1, x_train_tensor.shape[1], requires_grad=True)
    b = torch.randn(1, 1, requires_grad=True)
    learning_rate = 0.011
    epochs = 90000

    for _ in range(epochs):
        logits = torch.matmul(x_train_tensor, w.t()) + b
        y_pred = torch.sigmoid(logits)
        loss = F.binary_cross_entropy(y_pred, y_train_tensor)

        loss.backward()

        with torch.no_grad():
            w -= learning_rate * w.grad
            b -= learning_rate * b.grad

            w.grad.zero_()
            b.grad.zero_()

    return w.tolist(), b.item()

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)
    ### IMPLEMENT FROM HERE
    w_tensor = torch.tensor(w, dtype=torch.float32)
    b_tensor = torch.tensor(b, dtype=torch.float32)
    x_test_tensor = torch.tensor(x_test, dtype=torch.float32)

    logits = torch.matmul(x_test_tensor, w_tensor.t()) + b_tensor
    y_pred = torch.sigmoid(logits)

    return y_pred.squeeze().tolist()

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

test

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 surodoot {'surodoot': 74.5}

github-actions[bot] commented 1 year ago

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

github-actions[bot] commented 1 year ago

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

github-actions[bot] commented 1 year ago

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

github-actions[bot] commented 1 year ago

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

jangyoujin0917 commented 1 year ago

Good!

github-actions[bot] commented 1 year ago

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