-
Notifications
You must be signed in to change notification settings - Fork 4
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
d73bc0c
commit 777bff5
Showing
7 changed files
with
160 additions
and
5 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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import numpy as np | ||
|
||
from tricycle_v2.ops import to_tensor | ||
|
||
|
||
def init_xavier(shape, name: str = ""): | ||
""" | ||
Initialize a tensor with xavier/glorot initialisation | ||
""" | ||
f_in, f_out = shape | ||
bound = np.sqrt(6) / np.sqrt(f_in + f_out) | ||
return to_tensor(np.random.uniform(low=-bound, high=bound, size=shape), name=name) |
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
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,32 @@ | ||
import numpy as np | ||
|
||
from tricycle_v2.ops import softmax | ||
from tricycle_v2.reduce import radd | ||
from tricycle_v2.tensor import to_tensor | ||
|
||
|
||
def test_softmax(): | ||
in_tensor = to_tensor(np.arange(12).reshape(3, 4), name='in_tensor') | ||
|
||
out_tensor = softmax(in_tensor) | ||
|
||
assert out_tensor.shape == (3, 4) | ||
assert np.allclose(radd(out_tensor, "ij->i"), [1, 1, 1]) | ||
assert np.allclose( | ||
out_tensor, | ||
np.array( | ||
[ | ||
[0.0320586, 0.08714432, 0.23688282, 0.64391426], | ||
[0.0320586, 0.08714432, 0.23688282, 0.64391426], | ||
[0.0320586, 0.08714432, 0.23688282, 0.64391426], | ||
] | ||
), | ||
) | ||
|
||
out_tensor.backward() | ||
|
||
breakpoint() | ||
assert np.allclose( | ||
in_tensor.grad, | ||
np.ones_like(in_tensor), | ||
) |
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