vlab-kaist / NN101_23S

MIT License
6 stars 7 forks source link

[LAB] Week 1_Problem 2_이서현 #58

Closed seo-rii closed 1 year ago

seo-rii commented 1 year ago

Problem

Week 1_Problem 2

Source Code

import torch
from random import random
from typing import Callable

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

def solution(func: Callable, start_point: float) -> float:  # DO NOT MODIFY FUNCTION NAME
    epoch = 10000
    alpha = 0.09
    beta = 0.001

    x = torch.tensor(start_point, requires_grad=True)
    v = torch.tensor(0.0, requires_grad=True)

    for _ in range(epoch):
        y = func(x + alpha * v)
        y.backward()
        v.data = alpha * v.data - beta * x.grad
        x.data += v.data
        x.grad.zero_()
        v.grad.zero_()

    return x.item()

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

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

Description

ㅇㅇ

Output (Optional)

No response

github-actions[bot] commented 1 year ago

This is an auto-generated grading output. Checking code of seo-rii {'seo-rii': 97.0}

github-actions[bot] commented 1 year ago

This is an auto-generated grading output. Checking code of seo-rii {'seo-rii': 0.0}

github-actions[bot] commented 1 year ago

This is an auto-generated grading output. Checking code of seo-rii {'seo-rii': 46.0}