-
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
5e521a1
commit 4403b59
Showing
5 changed files
with
52 additions
and
43 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -143,4 +143,5 @@ cython_debug/ | |
|
||
# Development notebooks | ||
/*.ipynb | ||
*.gz | ||
*.gz | ||
test.py |
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 |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
def model(x): | ||
|
||
out_1 = (x @ w1) + b1 | ||
|
||
output = (out_1 @ w2) + b2 | ||
|
||
return output | ||
|
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,9 +1,34 @@ | ||
# TODO: Module class to represent neural networks | ||
import inspect | ||
from matterix.tensor import Tensor | ||
|
||
|
||
class Module: | ||
def __init__(self) -> None: | ||
pass | ||
|
||
def parameters(self): | ||
params = dict() | ||
# Not sure if this account for all the cases | ||
for i in inspect.getmembers(self): | ||
if not i[0].startswith("_"): | ||
if not inspect.ismethod(i[1]): | ||
params[i[0]] = i[1] | ||
|
||
return params | ||
|
||
def __call__(self, x) -> Tensor: | ||
|
||
forward_fn = getattr(self, "forward", None) # None is a default value | ||
if callable(forward_fn): | ||
return self.forward(x) | ||
else: | ||
raise NotImplementedError("Forward function is not implemented") | ||
|
||
def zero_grad(self) -> None: | ||
pass | ||
|
||
params = self.parameters() | ||
for k, v in params.items(): | ||
v.zero_grad() | ||
params[k] = v | ||
|
||
self.__dict__.update(params) |
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 +1,22 @@ | ||
# TODO: SGD, Adam, RMSProp | ||
|
||
# Model (params) | ||
# -> Optimizer (which updates the parameters) | ||
# -> Needs to be reflected in the Model (params) | ||
|
||
|
||
class SGD: | ||
def __init__(self, model, parameters, lr: float = 0.001) -> None: | ||
self.model = model | ||
self.params = parameters | ||
self.lr = lr | ||
|
||
def step(self): | ||
|
||
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: | ||
self.model.zero_grad() |