vlab-kaist / NN101_23S

MIT License
6 stars 7 forks source link

[LAB] Week 1_Problem 1_정석현 #39

Closed supermathking closed 1 year ago

supermathking commented 1 year ago

Problem

Week 1_Problem 1

Source Code

import torch
from random import random
from typing import Callable

##                        Problem 1                           ##
##                                                            ##
##         Arbitrary quadratic function will be given.        ##
## Return the optimal point(global minimum) of given function ##
##          Condition: highest order term is positive         ##
##                  Made by @jangyoujin0917                   ##
##                                                            ##

lr = 0.001

def solution(func: Callable, start_point: float) -> float:  # DO NOT MODIFY FUNCTION NAME
    x = torch.tensor([start_point], requires_grad=True)
    for i in range(10000):
        y = func(x)
        grad = torch.tensor([1.0] * 1)
        y.backward(grad)
        x.data -= x.grad.data.item() * lr
        #if i % 1000 == 0:
            #print(f"{x}")
        x.grad.data.zero_()
    return x.data.item()

if __name__ == "__main__":
    def test_func(x):  # function for testing;function for evaluation will be different.
        return x ** 2

    t = 10 * random()
    print(solution(test_func, t))

Description

y : func(x) x : x

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 supermathking {'supermathking': 100.0}