提交 dc7a2384 authored 作者: Ian Goodfellow's avatar Ian Goodfellow

changed old tests, gradients must now be variables, not numpy

ambiguous whether to turn numpy into sparse or dense variable
上级 07ee83bf
...@@ -20,9 +20,9 @@ from theano import gof ...@@ -20,9 +20,9 @@ from theano import gof
from theano.gof import Variable from theano.gof import Variable
from theano.gof.python25 import all from theano.gof.python25 import all
import theano.gof.utils import theano.gof.utils
tensor = None
from theano.gof.nan_type import NaNType from theano.gof.nan_type import NaNType
from theano.printing import min_informative_str from theano.printing import min_informative_str
tensor = None
_msg_retType = 'op.grad(...) returned a non-list' _msg_retType = 'op.grad(...) returned a non-list'
...@@ -604,13 +604,14 @@ def grad(cost, wrt, g_cost = None, consider_constant = None, warn_type = 'ignore ...@@ -604,13 +604,14 @@ def grad(cost, wrt, g_cost = None, consider_constant = None, warn_type = 'ignore
def grad_sources_inputs(sources, graph_inputs, warn_type = 'ignored'): def grad_sources_inputs(sources, graph_inputs, warn_type = 'ignored'):
global tensor
if tensor is None:
from theano import tensor
outputs, output_grads = zip(*sources) outputs, output_grads = zip(*sources)
for output_grad in output_grads:
if not hasattr(output_grad, 'type'):
raise TypeError('output grads must be theano variables.'
'Ambiguous whether %s should be made into tensor'
' or sparse theano variable' % str(type(output_grad)))
if graph_inputs is None: if graph_inputs is None:
graph_inputs = gof.graph.inputs(outputs) graph_inputs = gof.graph.inputs(outputs)
......
...@@ -11,6 +11,7 @@ from theano import gradient ...@@ -11,6 +11,7 @@ from theano import gradient
from theano.tensor.nnet.Conv3D import conv3D from theano.tensor.nnet.Conv3D import conv3D
from theano import config from theano import config
one = theano.tensor.as_tensor_variable(1.)
def _grad_sources_inputs(*args): def _grad_sources_inputs(*args):
# warn_type was introduced after this code, it complains throughout for nothing. # warn_type was introduced after this code, it complains throughout for nothing.
...@@ -31,7 +32,7 @@ class test_grad_sources_inputs(unittest.TestCase): ...@@ -31,7 +32,7 @@ class test_grad_sources_inputs(unittest.TestCase):
pass pass
a = retNone().make_node() a = retNone().make_node()
try: try:
_grad_sources_inputs([(a.out, 1)], None) _grad_sources_inputs([(a.out, one)], None)
except ValueError, e: except ValueError, e:
self.assertTrue(e[0] is gradient._msg_retType) self.assertTrue(e[0] is gradient._msg_retType)
return return
...@@ -49,10 +50,10 @@ class test_grad_sources_inputs(unittest.TestCase): ...@@ -49,10 +50,10 @@ class test_grad_sources_inputs(unittest.TestCase):
i = theano.tensor.vector() i = theano.tensor.vector()
j = theano.tensor.vector() j = theano.tensor.vector()
a1 = retNone().make_node(i) a1 = retNone().make_node(i)
g = _grad_sources_inputs([(a1.out, 1)], None) g = _grad_sources_inputs([(a1.out, one)], None)
a2 = retNone().make_node(i,j) a2 = retNone().make_node(i,j)
try: try:
g = _grad_sources_inputs([(a2.out, 1)], None) g = _grad_sources_inputs([(a2.out, one)], None)
except ValueError, e: except ValueError, e:
return return
self.fail() self.fail()
...@@ -68,7 +69,7 @@ class test_grad_sources_inputs(unittest.TestCase): ...@@ -68,7 +69,7 @@ class test_grad_sources_inputs(unittest.TestCase):
def grad(self, inp, grads): def grad(self, inp, grads):
return gval, return gval,
a1 = O().make_node() a1 = O().make_node()
g = _grad_sources_inputs([(a1.outputs[0], 1)], None) g = _grad_sources_inputs([(a1.outputs[0], one)], None)
self.assertTrue(g[a1.inputs[0]] is gval) self.assertTrue(g[a1.inputs[0]] is gval)
def test_1in_Nout(self): def test_1in_Nout(self):
...@@ -84,7 +85,7 @@ class test_grad_sources_inputs(unittest.TestCase): ...@@ -84,7 +85,7 @@ class test_grad_sources_inputs(unittest.TestCase):
gz1, gz2 = grads gz1, gz2 = grads
return gval, return gval,
a1 = O().make_node() a1 = O().make_node()
g = _grad_sources_inputs([(a1.outputs[0], 1)], None) g = _grad_sources_inputs([(a1.outputs[0], one)], None)
self.assertTrue(g[a1.inputs[0]] is gval) self.assertTrue(g[a1.inputs[0]] is gval)
def test_Nin_1out(self): def test_Nin_1out(self):
...@@ -101,7 +102,7 @@ class test_grad_sources_inputs(unittest.TestCase): ...@@ -101,7 +102,7 @@ class test_grad_sources_inputs(unittest.TestCase):
gz, = grads gz, = grads
return (gval0, gval1) return (gval0, gval1)
a1 = O().make_node() a1 = O().make_node()
g = _grad_sources_inputs([(a1.outputs[0], 1)], None) g = _grad_sources_inputs([(a1.outputs[0], one)], None)
self.assertTrue(g[a1.inputs[0]] is gval0) self.assertTrue(g[a1.inputs[0]] is gval0)
self.assertTrue(g[a1.inputs[1]] is gval1) self.assertTrue(g[a1.inputs[1]] is gval1)
...@@ -117,7 +118,7 @@ class test_grad_sources_inputs(unittest.TestCase): ...@@ -117,7 +118,7 @@ class test_grad_sources_inputs(unittest.TestCase):
def grad(self, inp, grads): def grad(self, inp, grads):
return gval0, gval1 return gval0, gval1
a1 = O().make_node() a1 = O().make_node()
g = _grad_sources_inputs([(a1.outputs[0], 1)], None) g = _grad_sources_inputs([(a1.outputs[0], one)], None)
self.assertTrue(g[a1.inputs[0]] is gval0) self.assertTrue(g[a1.inputs[0]] is gval0)
self.assertTrue(g[a1.inputs[1]] is gval1) self.assertTrue(g[a1.inputs[1]] is gval1)
...@@ -133,7 +134,7 @@ class test_grad_sources_inputs(unittest.TestCase): ...@@ -133,7 +134,7 @@ class test_grad_sources_inputs(unittest.TestCase):
return [1] return [1]
i = theano.tensor.matrix() i = theano.tensor.matrix()
a1 = O(self).make_node(i) a1 = O(self).make_node(i)
g = grad_sources_inputs([(a1.outputs[0], 1)], None, warn_type=False) g = grad_sources_inputs([(a1.outputs[0], one)], None, warn_type=False)
self.assertTrue(g[i] is 1) self.assertTrue(g[i] is 1)
def test_some_None_igrads(self): def test_some_None_igrads(self):
...@@ -155,12 +156,12 @@ class test_grad_sources_inputs(unittest.TestCase): ...@@ -155,12 +156,12 @@ class test_grad_sources_inputs(unittest.TestCase):
k = theano.tensor.matrix() k = theano.tensor.matrix()
a1 = O(self, True).make_node(i,j) a1 = O(self, True).make_node(i,j)
a2 = O(self, True).make_node(a1.outputs[1], k) a2 = O(self, True).make_node(a1.outputs[1], k)
g = grad_sources_inputs([(a2.outputs[0], 1)], None, warn_type=False) g = grad_sources_inputs([(a2.outputs[0], one)], None, warn_type=False)
self.assertTrue(g[i] is 1 and j not in g and k not in g) self.assertTrue(g[i] is 1 and j not in g and k not in g)
a1 = O(self, True).make_node(i,j) a1 = O(self, True).make_node(i,j)
a2 = O(self, True).make_node(k, a1.outputs[1]) a2 = O(self, True).make_node(k, a1.outputs[1])
g = _grad_sources_inputs([(a2.outputs[0], 1)], None) g = _grad_sources_inputs([(a2.outputs[0], one)], None)
self.assertTrue(g[k] is 1 and i not in g and j not in g) self.assertTrue(g[k] is 1 and i not in g and j not in g)
def test_inputs(self): def test_inputs(self):
...@@ -186,7 +187,7 @@ class test_grad_sources_inputs(unittest.TestCase): ...@@ -186,7 +187,7 @@ class test_grad_sources_inputs(unittest.TestCase):
k = theano.tensor.matrix() k = theano.tensor.matrix()
a1 = O(self, True).make_node(i,j) a1 = O(self, True).make_node(i,j)
a2 = O(self, True).make_node(k,a1.outputs[1]) a2 = O(self, True).make_node(k,a1.outputs[1])
g = _grad_sources_inputs([(a2.outputs[0], 1), (a1.outputs[1],4), g = _grad_sources_inputs([(a2.outputs[0], one), (a1.outputs[1],4),
(a1.outputs[0], 3), (a1.outputs[0], 3)], a1.outputs) (a1.outputs[0], 3), (a1.outputs[0], 3)], a1.outputs)
self.assertTrue(g[a2.inputs[0]] == 1) self.assertTrue(g[a2.inputs[0]] == 1)
self.assertTrue(g[a2.inputs[1]] == 5) self.assertTrue(g[a2.inputs[1]] == 5)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论