-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add linear & module & tensor & tests
- Loading branch information
1 parent
eea62a3
commit 2551842
Showing
15 changed files
with
423 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import numpy | ||
|
||
|
||
class Tensor: | ||
def __init__(self, data, type): | ||
self.data = numpy.array(data, dtype=type) | ||
|
||
def __add__(self, other): | ||
return self.data+other | ||
|
||
def __sub__(self, other): | ||
return self.data-other | ||
|
||
def __mul__(self, other): | ||
if isinstance(other, Tensor): | ||
return numpy.matmul(self.data, other.data) | ||
return self.data*other | ||
|
||
def reshape(self, row, col): | ||
self.data = self.data.reshape(row, col) |
Binary file not shown.
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,23 @@ | ||
from .Module import Module | ||
import numpy | ||
from .Parameter import Parameter | ||
|
||
|
||
class Linear(Module): | ||
def __init__(self, row, col): | ||
self.parameter = Parameter((row, col)) | ||
self.inputs = [] | ||
self.data = None | ||
|
||
def forward(self, x): | ||
self.inputs.append(x) | ||
self.data = numpy.matmul(x.data, self.parameter.data) | ||
return self | ||
|
||
def __call__(self, x): | ||
return self.forward(x) | ||
|
||
def backward(self, grad): | ||
self.parameter.gradient = numpy.matmul(self.inputs[0].data.T, self.data) | ||
if(isinstance(self.inputs[0], Module)): | ||
self.inputs[0].backward() |
Oops, something went wrong.