-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4403b59
commit 0b61720
Showing
6 changed files
with
80 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from numpy import diff | ||
from numpy.random import logseries | ||
from .tensor import Tensor | ||
|
||
# TODO: RMSE, MAE, Binary cross-entropy, Categorical cross-entropy, kullback leibler divergence loss | ||
|
||
|
||
def MSE(y_train: Tensor, y_pred: Tensor) -> Tensor: | ||
|
||
diff = y_train - y_pred | ||
loss = (diff * diff).sum() * (1.0 / diff.numel()) | ||
|
||
return loss | ||
|
||
|
||
def RMSE(y_train: Tensor, y_pred: Tensor) -> Tensor: | ||
|
||
diff = y_train - y_pred | ||
mse = ((diff * diff).sum()) * (1.0 / diff.numel()) | ||
rmse = mse ** (1.0 / 2.0) | ||
|
||
return rmse |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,40 @@ | ||
# TODO: SGD, Adam, RMSProp | ||
from .tensor import Tensor | ||
from .nn import Module | ||
|
||
# Model (params) | ||
# -> Optimizer (which updates the parameters) | ||
# -> Needs to be reflected in the Model (params) | ||
# TODO: Adam, RMSProp | ||
|
||
|
||
class SGD: | ||
def __init__(self, model, parameters, lr: float = 0.001) -> None: | ||
""" | ||
Runs stochastic gradient descent | ||
Parameters | ||
---------- | ||
Arg: model (Module) | ||
model which needs to be optimized | ||
Arg: parameters (dict) | ||
dict of all the parameters which needs to be optimized in the model | ||
Arg: lr (float) | ||
Learning rate. Size of each gradient step | ||
""" | ||
|
||
def __init__(self, model: Module, parameters: dict, lr: float = 0.001) -> None: | ||
self.model = model | ||
self.params = parameters | ||
self.lr = lr | ||
|
||
def step(self): | ||
def step(self) -> None: | ||
"""Updates the parameters of the model""" | ||
|
||
for k, v in self.params.items(): | ||
v -= v.grad * self.lr | ||
self.params[k] = v | ||
|
||
self.model.__dict__.update(self.params) | ||
|
||
def zero_grad(self) -> None: | ||
"""Sets the gradients of all the parameters to zero""" | ||
self.model.zero_grad() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters