vlab-kaist / NN101_23S

MIT License
6 stars 7 forks source link

[LAB] Week 4_Problem 1_송준 #113

Closed songjoon closed 1 year ago

songjoon commented 1 year ago

Problem

Week 4_Problem 1

Source Code

from unittest import result
import torch
import torch.nn.functional as F
import numpy as np
import math

##                         Problem 1                          ##
##                                                            ##
##           Given Iris_train.csv, train your model           ##
##              to predict the species of iris.               ##
##            In the implementation of functions,             ##
##                  opening file is invalid.                  ##
## Checker will provide Iris_train.csv with x_train, y_train. ##
## LIMITS : Do not reference dataset on Internet in the code. ##
##        (Since test datas are also on the Internet.)        ##
##                  Made by @jangyoujin0917                   ##
##                                                            ##

# NOTE : Data normalization may help you to get good score.

iris = ["Iris-setosa", "Iris-versicolor", "Iris-virginica"]
def iris_onehot(x : str) -> list[int]:
    index = iris.index(x)
    return list(np.eye(len(iris))[index])
def training(x_train : torch.Tensor, y_train : torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]: # DO NOT MODIFY FUNCTION NAME
    w = torch.Tensor(len(x_train[0]), requires_grad = True)
    b = torch.Tensor(len(x_train[0]), requires_grad = True)
    for _ in range(1000):
       print(cost(x_train, y_train, w, b))
       w-= w.grad;
       b -= b.grad;
    return (w,b)
def softmax(x : torch.Tensor) -> torch.Tensor:
    s = 0
    for i in x:
        s += math.e ** i
    return torch.Tensor([(math.e ** i) /s for i in x], dtype=torch.float32)
def predict(x_train : torch.Tensor, y_train : torch.Tensor, x_test : torch.Tensor) -> torch.Tensor: # DO NOT MODIFY FUNCTION NAME
    # predict() should return the index of the answer.
    # Therefore, the return value is 1d-tensor that only contains 0, 1, 2. 
    w, b = training(x_train, y_train)
    for i in range(len(x_train)):
        print(softmax(w* x_train[i] + b))
    ### IMPLEMENT FROM HERE

def cost(x : torch.Tensor, y : torch.Tensor, w : torch.Tensor, b : torch.Tensor):
    result =0
    for i in range(len(x)):
       result -= y[i] * torch.log(softmax(w*  x + b))
    return result

if __name__ == "__main__":
    import csv
    with open("Iris_train.csv", "r") as f: # Make sure that Iris_train.csv is in same directory.
        rdr = csv.reader(f)
        contents = [i for i in rdr]
    contents = contents[1:]
    x = torch.tensor([[float(j) for j in i[:-1]] for i in contents], dtype=torch.float32)
    y = torch.tensor([iris_onehot(i[-1]) for i in contents], dtype=torch.float32)
    test = torch.tensor([[5.1,3.0,1.2,0.3], [5.1,3.4,4.1,1.3]], dtype=torch.float32)

    prediction = predict(x, y, test)
    print(prediction)

Description

ㅁㄴㅇ

Output (Optional)

ㅁㄴㅇ

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. Your code failed to run. Please check again.