Repository navigation
Expand file tree
/
Copy pathoptim.py
More file actions
51 lines (37 loc) · 1.85 KB
/
Copy pathoptim.py
File metadata and controls
51 lines (37 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import numpy as np
from numpy.typing import NDArray
from nn import Brain
from variables import learning_rate_decay, scheduler
# differente class of hyperparamater optimization
# https://en.wikipedia.org/wiki/Hyperparameter_optimization
# should get the all Tensor same as GD, and select an item randomly
def stochastic_gradient_descent(brain: Brain, learning_rate: float, tensor: NDArray[np.float64], labels_tensor: NDArray[np.float64]):
# we want to select an element of the tensor randomly and do the prediction
random_index = np.random.randint(tensor.shape[0]) # Randomly select an index along the first axis
selected_item = tensor[random_index]
selected_label = labels_tensor.T[random_index]
brain.predictions(selected_item)
brain.diffs(selected_label)
brain.loss(selected_item, selected_label)
brain.update_weights(selected_item, learning_rate)
return brain._loss
def gradient_descent(brain: Brain, learning_rate: float, tensor: NDArray[np.float64], labels_tensor: NDArray[np.float64]):
brain.predictions(tensor)
brain.diffs(labels_tensor)
brain.loss(tensor, labels_tensor)
brain.update_weights(tensor, learning_rate)
return brain._loss
def learning_rate_scheduler(initial_learning_rate, step):
match scheduler:
case None:
return initial_learning_rate
case "linear":
return linear_scheduler(initial_learning_rate, step)
case "exp":
return exponential_scheduler(initial_learning_rate, step)
case _:
raise ValueError(f"scheduler can only be 'exp' or 'linear'\nscheduler == '{scheduler}'")
def exponential_scheduler(initial_learning_rate, step):
return initial_learning_rate * np.exp(-learning_rate_decay * step)
def linear_scheduler(initial_learning_rate, step):
return (1 / (1 + learning_rate_decay * step)) * initial_learning_rate