提交 7223e9ed authored 作者: khaotik's avatar khaotik

cleanup 2

上级 f15bc27c
...@@ -80,7 +80,9 @@ class OpFromGraph(gof.Op): ...@@ -80,7 +80,9 @@ class OpFromGraph(gof.Op):
suggestion to compiler which is not guaranteed. Currently only suggestion to compiler which is not guaranteed. Currently only
works with "fast_compile" or "fast_run" mode. works with "fast_compile" or "fast_run" mode.
- The function(s) supplied for overrding gradient/rop will be called - The function(s) supplied for overrding gradient/rop will be called
only once only once at the first call to grad/R_op, and will be converted to
OfG instances. Any side effect (modifying non local states) of the
overriding function should not be relied on.
Examples Examples
-------- --------
...@@ -89,10 +91,10 @@ class OpFromGraph(gof.Op): ...@@ -89,10 +91,10 @@ class OpFromGraph(gof.Op):
.. code-block:: python .. code-block:: python
from theano import function, op_from_graph, tensor from theano import function, tensor
x, y, z = tensor.scalars('xyz') x, y, z = tensor.scalars('xyz')
e = x + y * z e = x + y * z
op = op_from_graph([x, y, z], [e]) op = OpFromGraph([x, y, z], [e])
# op behaves like a normal theano op # op behaves like a normal theano op
e2 = op(x, y, z) + op(z, y, x) e2 = op(x, y, z) + op(z, y, x)
fn = function([x, y, z], [e2]) fn = function([x, y, z], [e2])
...@@ -103,11 +105,11 @@ class OpFromGraph(gof.Op): ...@@ -103,11 +105,11 @@ class OpFromGraph(gof.Op):
import numpy as np import numpy as np
import theano import theano
from theano import config, function, op_from_graph, tensor from theano import config, function, OpFromGraph, tensor
x, y, z = tensor.scalars('xyz') x, y, z = tensor.scalars('xyz')
s = theano.shared(np.random.rand(2, 2).astype(config.floatX)) s = theano.shared(np.random.rand(2, 2).astype(config.floatX))
e = x + y * z + s e = x + y * z + s
op = op_from_graph([x, y, z], [e]) op = OpFromGraph([x, y, z], [e])
# op behaves like a normal theano op # op behaves like a normal theano op
e2 = op(x, y, z) + op(z, y, x) e2 = op(x, y, z) + op(z, y, x)
fn = function([x, y, z], [e2]) fn = function([x, y, z], [e2])
...@@ -116,14 +118,14 @@ class OpFromGraph(gof.Op): ...@@ -116,14 +118,14 @@ class OpFromGraph(gof.Op):
.. code-block:: python .. code-block:: python
from thenao import funciton, op_from_graph, tensor, grad from thenao import funciton, OpFromGraph, tensor, grad
x, y, z = tensor.scalars('xyz') x, y, z = tensor.scalars('xyz')
e = x + y * z e = x + y * z
def rescale_dy(inps, grads): def rescale_dy(inps, grads):
x, y, z = inps x, y, z = inps
g = grads g = grads
return z*2 return z*2
op = op_from_graph( op = OpFromGraph(
[x, y, z], [e], grad_overrides=[None, rescale_dy, None]) [x, y, z], [e], grad_overrides=[None, rescale_dy, None])
e2 = op(x, y, z) e2 = op(x, y, z)
dx, dy, dz = grad(e2, [x, y, z]) dx, dy, dz = grad(e2, [x, y, z])
...@@ -260,15 +262,13 @@ class OpFromGraph(gof.Op): ...@@ -260,15 +262,13 @@ class OpFromGraph(gof.Op):
odefaults_l = [ odefaults_l = [
lo for lo, rov in izip(self.local_outputs, roverrides_l) lo for lo, rov in izip(self.local_outputs, roverrides_l)
if not rov] if not rov]
# compute non-overriding downsteam grads from upstreams grads
# it's normal some input may be disconnected, thus the 'ignore'
rdefaults_li = theano.gradient.Rop( rdefaults_li = theano.gradient.Rop(
f=odefaults_l, f=odefaults_l,
wrt=self.local_inputs, wrt=self.local_inputs,
eval_points=eval_points eval_points=eval_points
) )
rdefaults = iter(rdefaults_li if odefaults_l else []) rdefaults = iter(rdefaults_li if odefaults_l else [])
# combine overriding gradients # combine overriding Rops
all_rops_l = [] all_rops_l = []
for out, rov in izip(self.local_outputs, roverrides_l): for out, rov in izip(self.local_outputs, roverrides_l):
if rov is None: if rov is None:
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论