-
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.
Merge pull request #15 from adtzlr/implement-wrt
Implement with-respect-to argument (`wrt=0` or `wrt="x"`)
- Loading branch information
Showing
9 changed files
with
126 additions
and
44 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
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
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,34 @@ | ||
import tensortrax as tr | ||
import tensortrax.math as tm | ||
import numpy as np | ||
import pytest | ||
|
||
|
||
def fun(x, y): | ||
return x**2 / y + x * tm.log(y) | ||
|
||
|
||
def test_scalar(): | ||
|
||
np.random.seed(6574) | ||
x = np.random.rand(100) | ||
|
||
np.random.seed(54234) | ||
y = np.random.rand(100) | ||
|
||
with pytest.raises(TypeError): | ||
tr.hessian(fun, wrt=[1, 2])(x, y) | ||
|
||
h, g, f = tr.hessian(fun, wrt=0, ntrax=1)(x, y) | ||
|
||
assert np.allclose(g, 2 * x / y + np.log(y)) | ||
assert np.allclose(h, 2 / y) | ||
|
||
h, g, f = tr.hessian(fun, wrt="y", ntrax=1)(x=x, y=y) | ||
|
||
assert np.allclose(g, -(x**2) / y**2 + x / y) | ||
assert np.allclose(h, 2 * x**2 / y**3 - x / y**2) | ||
|
||
|
||
if __name__ == "__main__": | ||
test_scalar() |