Miscellaneous scripts
This repository contains miscellaneous scripts that does not fit in one repository, yet I will use them sometimes for my personal use. Note that some of the scripts might contain hardcoded paths and opinionated presets, and you are advised to inspect them before actually using.
Loading...
Searching...
No Matches
gradientdescent.py
Go to the documentation of this file.
1# eric15342335
2def derivative(x: float) -> int: # derivative of f(x) as given by the question
3 if x > 0:
4 return 1
5 elif x < 0:
6 return -1
7 else:
8 return 0 # given
9
10
11def learn(x: int) -> float: # learning rate n
12 return 1 / (x + 1)
13
14
15def func(x: int) -> float:
16 if x == 0: # initial state x^(0)
17 return 2.5
18 elif x in grad: # cache of x^(t)
19 return grad[x]
20 else: # not in cache
21 return func(x - 1) - learn(x - 1) * derivative(func(x - 1))
22
23
24grad = {}
25case = 99 # what value of t you want to calculate
26for i in range(case + 1):
27 grad[i] = func(i) # store gradient descent value to cache
28for i in range(case + 1):
29 print(i, func(i)) # print value
float func(int x)
int derivative(float x)
float learn(int x)