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
4 changed files
with
54 additions
and
0 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,22 @@ | ||
from scalmulop import ScalMulV1 | ||
from doubleop import DoubleOp | ||
|
||
from theano.gof import local_optimizer | ||
|
||
from theano.tensor.opt import register_specialize | ||
|
||
@register_specialize | ||
@local_optimizer([ScalMulV1]) | ||
def local_scalmul_double_v1(node): | ||
if not (isinstance(node.op, ScalMulV1) | ||
and node.op.scal == 2): | ||
return False | ||
|
||
return [DoubleOp()(node.inputs[0])] | ||
|
||
from theano.gof.opt import OpSub | ||
|
||
local_scalmul_double_v2 = OpSub(ScalMulV1(2), DoubleOp()) | ||
|
||
register_specialize(local_scalmul_double_v2, | ||
name='local_scalmul_double_v2') |
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,15 @@ | ||
from doubleop import DoubleOp | ||
from doublec import DoubleC | ||
|
||
from theano.gof import local_optimizer | ||
|
||
from theano.tensor.opt import register_specialize | ||
|
||
@register_specialize | ||
@local_optimizer([DoubleOp]) | ||
def local_scalmul_double_v1(node): | ||
if not (isinstance(node.op, DoubleOp) | ||
and node.inputs[0].ndim == 1): | ||
return False | ||
|
||
return [DoubleC()(node.inputs[0])] |
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,16 @@ | ||
import theano | ||
|
||
from scalmulop import ScalMulV1 | ||
from doubleop import DoubleOp | ||
import opt | ||
|
||
def test_scalmul_double(): | ||
x = theano.tensor.matrix() | ||
y = ScalMulV1(2)(x) | ||
f = theano.function([x], y) | ||
|
||
assert not any(isinstance(n.op, ScalMulV1) | ||
for n in f.maker.fgraph.toposort()) | ||
assert any(isinstance(n.op, DoubleOp) | ||
for n in f.maker.fgraph.toposort()) | ||
|