vlab-kaist / NN101_23S

MIT License
6 stars 7 forks source link

[LAB] Week 1_Problem 2_이예린 #85

Closed abrastone closed 1 year ago

abrastone 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                   ##
##                                                            ##

alpha = 0.001
beta = 0.9

def solution(func: Callable, start_point: float) -> float:  # DO NOT MODIFY FUNCTION NAME
    x = torch.tensor([start_point], requires_grad=True)
    v = 0

    for i in range (50000):
        y = func(x + beta * v)
        y.backward()
        v = beta * v - alpha * x.grad
        x.data += v
        x.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 abrastone {'abrastone': 89.0}

github-actions[bot] commented 1 year ago

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