Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add aesara ops; move np to approxmath.np
- Loading branch information
1 parent
6d28d3c
commit e082b24
Showing
9 changed files
with
247 additions
and
35 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 |
---|---|---|
|
@@ -2,4 +2,5 @@ build | |
*.so | ||
__pycache__ | ||
dist | ||
MANIFEST | ||
MANIFEST | ||
*.egg-info |
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 |
---|---|---|
|
@@ -2,3 +2,4 @@ numpy==1.21.4 | |
pytest==6.2.4 | ||
pytest-benchmark==3.4.1 | ||
pypandoc==1.7.2 | ||
aesara==2.3.3 |
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 +1,17 @@ | ||
from .approx_log_op import ApproxLogOp | ||
from .basic import ApproxLogOp, ApproxExpOp | ||
from .trig import ApproxCosOp, ApproxSinOp | ||
import aesara.tensor as tt | ||
import aesara | ||
|
||
# initialize and export ops | ||
x_exp = tt.matrix() | ||
exp = aesara.function([x_exp], ApproxExpOp()(x_exp)) | ||
|
||
x_log = tt.matrix() | ||
log = aesara.function([x_log], ApproxLogOp()(x_log)) | ||
|
||
x_cos = tt.matrix() | ||
cos = aesara.function([x_cos], ApproxCosOp()(x_cos)) | ||
|
||
x_sin = tt.matrix() | ||
sin = aesara.function([x_sin], ApproxSinOp()(x_sin)) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import aesara | ||
from aesara.graph.op import Op | ||
from aesara.graph.basic import Apply | ||
import aesara.tensor as tt | ||
import approxmath.np as npa | ||
|
||
class ApproxExpOp(Op): | ||
__props__ = () | ||
|
||
itypes = [aesara.tensor.dmatrix] | ||
otypes = [aesara.tensor.dmatrix] | ||
|
||
def perform(self, node, inputs, output_storage): | ||
x = inputs[0] | ||
z = output_storage[0] | ||
z[0] = npa.exp(x) | ||
|
||
def infer_shape(self, fgraph, node, i0_shapes): | ||
return i0_shapes | ||
|
||
def grad(self, inputs, output_grads): | ||
# d/dx (exp x) => x | ||
return [output_grads[0] * tt.exp(inputs[0])] | ||
|
||
def R_op(self, inputs, eval_points): | ||
# R_op can receive None as eval_points. | ||
# That mean there is no diferientiable path through that input | ||
# If this imply that you cannot compute some outputs, | ||
# return None for those. | ||
if eval_points[0] is None: | ||
return eval_points | ||
return self.grad(inputs, eval_points) | ||
|
||
class ApproxLogOp(Op): | ||
__props__ = () | ||
|
||
itypes = [aesara.tensor.dmatrix] | ||
otypes = [aesara.tensor.dmatrix] | ||
|
||
def perform(self, node, inputs, output_storage): | ||
x = inputs[0] | ||
z = output_storage[0] | ||
z[0] = npa.log(x) | ||
|
||
def infer_shape(self, fgraph, node, i0_shapes): | ||
return i0_shapes | ||
|
||
def grad(self, inputs, output_grads): | ||
# d/dx (ln x) => 1 / x | ||
return [output_grads[0] / inputs[0]] | ||
|
||
def R_op(self, inputs, eval_points): | ||
# R_op can receive None as eval_points. | ||
# That mean there is no diferientiable path through that input | ||
# If this imply that you cannot compute some outputs, | ||
# return None for those. | ||
if eval_points[0] is None: | ||
return eval_points | ||
return self.grad(inputs, eval_points) |
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,59 @@ | ||
import aesara | ||
from aesara.graph.op import Op | ||
from aesara.graph.basic import Apply | ||
import aesara.tensor as tt | ||
import approxmath.np as npa | ||
|
||
class ApproxSinOp(Op): | ||
__props__ = () | ||
|
||
itypes = [aesara.tensor.dmatrix] | ||
otypes = [aesara.tensor.dmatrix] | ||
|
||
def perform(self, node, inputs, output_storage): | ||
x = inputs[0] | ||
z = output_storage[0] | ||
z[0] = npa.sin(x) | ||
|
||
def infer_shape(self, fgraph, node, i0_shapes): | ||
return i0_shapes | ||
|
||
def grad(self, inputs, output_grads): | ||
# d/dx (sin x) => cos x | ||
return [tt.cos(inputs[0]) * output_grads[0]] | ||
|
||
def R_op(self, inputs, eval_points): | ||
# R_op can receive None as eval_points. | ||
# That mean there is no diferientiable path through that input | ||
# If this imply that you cannot compute some outputs, | ||
# return None for those. | ||
if eval_points[0] is None: | ||
return eval_points | ||
return self.grad(inputs, eval_points) | ||
|
||
class ApproxCosOp(Op): | ||
__props__ = () | ||
|
||
itypes = [aesara.tensor.dmatrix] | ||
otypes = [aesara.tensor.dmatrix] | ||
|
||
def perform(self, node, inputs, output_storage): | ||
x = inputs[0] | ||
z = output_storage[0] | ||
z[0] = npa.cos(x) | ||
|
||
def infer_shape(self, fgraph, node, i0_shapes): | ||
return i0_shapes | ||
|
||
def grad(self, inputs, output_grads): | ||
# d/dx (cos x) => -sin x | ||
return [-1 * tt.sin(inputs[0]) * output_grads[0]] | ||
|
||
def R_op(self, inputs, eval_points): | ||
# R_op can receive None as eval_points. | ||
# That mean there is no diferientiable path through that input | ||
# If this imply that you cannot compute some outputs, | ||
# return None for those. | ||
if eval_points[0] is None: | ||
return eval_points | ||
return self.grad(inputs, eval_points) |
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