forked from nouiz/ccw_tutorial_theano
-
Notifications
You must be signed in to change notification settings - Fork 34
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
Showing
6 changed files
with
97 additions
and
63 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from theano import Op, Apply | ||
from theano.tensor import as_tensor_variable | ||
|
||
class DoubleC(Op): | ||
__props__ = () | ||
|
||
def make_node(self, x): | ||
x = as_tensor_variable(x) | ||
if x.ndim != 1: | ||
raise TypeError("DoubleC only works on 1D") | ||
return Apply(self, [x], [x.type()]) | ||
|
||
def c_code(self, node, name, input_names, | ||
output_names, sub): | ||
return """ | ||
Py_XDECREF(%(out)s); | ||
%(out)s = (PyArrayObject *)PyArray_NewLikeArray( | ||
%(inp)s, NPY_ANYORDER, NULL, 0); | ||
if (%(out)s == NULL) { | ||
%(fail)s | ||
} | ||
for (npy_intp i = 0; i < PyArray_DIM(%(inp)s, 0); i++) { | ||
*(dtype_%(out)s *)PyArray_GETPTR1(%(out)s, i) = | ||
(*(dtype_%(inp)s *)PyArray_GETPTR1(%(inp)s, i)) * 2; | ||
} | ||
""" % dict(inp=input_names[0], out=output_names[0], | ||
fail=sub["fail"]) | ||
|
||
def infer_shape(self, node, input_shapes): | ||
return input_shapes | ||
|
||
def grad(self, inputs, output_grads): | ||
return [output_grads[0] * 2] |
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,22 @@ | ||
from theano import Apply | ||
from theano.gof import COp | ||
from theano.tensor import as_tensor_variable | ||
|
||
class DoubleCOp(COp): | ||
__props__ = () | ||
|
||
def __init__(self): | ||
COp.__init__(self, "./doublecop.c", | ||
"APPLY_SPECIFIC(doublecop)") | ||
|
||
def make_node(self, x): | ||
x = as_tensor_variable(x) | ||
if x.ndim != 1: | ||
raise TypeError("DoubleCOp only works with 1D") | ||
return Apply(self, [x], [x.type()]) | ||
|
||
def infer_shape(self, input_shapes): | ||
return input_shapes | ||
|
||
def grad(self, inputs, g): | ||
return [g[0] * 2] |
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,20 @@ | ||
from theano import Op | ||
|
||
class MyOp(Op): | ||
__props__ = () | ||
|
||
def __init__(self, ...): | ||
# set up parameters | ||
|
||
def make_node(self, ...): | ||
# create apply node | ||
|
||
def make_thunk(self, node, storage_map, | ||
compute_map, no_recycling): | ||
# return a thunk | ||
|
||
def infer_shape(self, input_shapes): | ||
# return output shapes | ||
|
||
def grad(self, inputs, output_grads): | ||
# return gradient graph for each input |