vlab-kaist / NN101_23S

MIT License
6 stars 7 forks source link

. #100

Closed Nucobi closed 1 year ago

Nucobi commented 1 year ago

import math import torch from random import random from typing import Callable, List, Tuple

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]): # DO NOT MODIFY FUNCTION NAME

IMPLEMENT FROM HERE

# w = torch.tensor([[0. for _ in range(len(x_train[0]))] for _ in range(len(x_train))], requires_grad=True)
w = torch.zeros(len(x_train[0]), requires_grad=True)
b = torch.zeros(1, requires_grad=True)

x_tensor = torch.FloatTensor(x_train)
y_tensor = torch.FloatTensor(y_train)

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

epoch = 20000
for _ in range(epoch):
    # print(w @ torch.transpose(x_tensor,0,1))
    logistic = torch.sigmoid(w @ torch.transpose(x_tensor,0,1)+b)
    # print(logistic)
    loss = torch.nn.functional.binary_cross_entropy(logistic,y_tensor)

    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

    if ((_ + 1) % 100 == 0): print(f"Epoch: {_ + 1}/{epoch} - w:{w},b:{b} , loss: {loss}")

W = [_.item() for _ in list(w)]
B = b.item()
return [W, B]

def predict(x_train: list[list[float]], y_train: list[float], x_test: list[list[float]]) -> list[float]: # DO NOT MODIFY FUNCTION NAME

IMPLEMENT FROM HERE

w, b = training(x_train, y_train)
y_test = []
e = math.e
for inp in x_test:
    wx_b = inp[0]*w[0]+inp[1]*w[1]
    result = 1 if(1/(1+e**(-1*wx_b)) > 0.5) else 0
    y_test.append(result)
return y_test

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)
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 Nucobi {'Nucobi': 70.0}

github-actions[bot] commented 1 year ago

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

jangyoujin0917 commented 1 year ago

I think this is not enough. Since answer is either 0 or 1, maybe you can get 50/100 when you always return 0.

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.