提交 b87ac81a authored 作者: James Bergstra's avatar James Bergstra

more misc bug fixes trying to help Joseph

上级 8f460289
...@@ -635,6 +635,13 @@ class NavigatorOptimizer(Optimizer): ...@@ -635,6 +635,13 @@ class NavigatorOptimizer(Optimizer):
except Exception, e: except Exception, e:
if self.failure_callback is not None: if self.failure_callback is not None:
self.failure_callback(e, self, repl_pairs) self.failure_callback(e, self, repl_pairs)
#DEBUG DONT PUSH
#print lopt
#print dir(lopt)
#raise
#END
return False return False
else: else:
raise raise
......
...@@ -361,7 +361,7 @@ class Dot22(GemmRelated): ...@@ -361,7 +361,7 @@ class Dot22(GemmRelated):
def make_node(self, x, y): def make_node(self, x, y):
assert _is_real_matrix(x) assert _is_real_matrix(x)
assert y.type == x.type #makes sure y is a matrix assert y.type == x.type #makes sure y is a matrix
bz = [x.type.broadcastable[0], y.type.broadcastable[1]] bz = [False, False]
outputs = [T.tensor(x.type.dtype, bz)] outputs = [T.tensor(x.type.dtype, bz)]
return Apply(self, [x,y], outputs) return Apply(self, [x,y], outputs)
...@@ -435,8 +435,9 @@ def _as_scalar(res): ...@@ -435,8 +435,9 @@ def _as_scalar(res):
return None return None
def _is_real_matrix(res): def _is_real_matrix(res):
return res.type in T.float_matrix_types\ return res.type in T.float_matrix_types \
and res.broadcastable == (False, False) and res.broadcastable[0] == False \
and res.broadcastable[1] == False #cope with tuple vs. list
def _as_isolated_scalar_times_matrix(res): def _as_isolated_scalar_times_matrix(res):
if _is_a(res, T.mul, 1): if _is_a(res, T.mul, 1):
......
...@@ -505,9 +505,30 @@ class Canonizer(gof.LocalOptimizer): ...@@ -505,9 +505,30 @@ class Canonizer(gof.LocalOptimizer):
return False return False
new = self.merge_num_denum(num, denum) new = self.merge_num_denum(num, denum)
if new.type != out.type: if new.dtype != out.dtype:
#new = T.fill(out, new) #new = T.fill(out, new)
new = T.fill(out, T.Elemwise(scalar.Identity(scalar.specific_out(getattr(scalar, out.type.dtype))))(new)) elem_op = T.Elemwise(scalar.Identity(scalar.specific_out(getattr(scalar, out.type.dtype))))
new = T.fill(out, elem_op(new))
if new.broadcastable != out.broadcastable:
#this case is tricky... we need to provide exactly the same kind of broadcastable
#pattern, but only if legal...
dlen = len(new.broadcastable) - len(out.broadcastable)
if dlen > 0:
#try to take the leading ranks of new.broadcastable, which should be broadcastable
# ranks
#if this means skipping over nonbroadcastable ranks, then DimShuffle will fail
dimshuffle_op = T.DimShuffle(new.broadcastable,
range(dlen, len(new.broadcastable)))
new = dimshuffle_op(new)
elif dlen < 0:
#we have to boost up a scalar or something
dimshuffle_op = T.DimShuffle(new.broadcastable,
['x' for x in range(-dlen)] + range(0, len(new.broadcastable)))
new = dimshuffle_op(new)
# if our if's above worked, this should be true. OTW investigate.
assert new.type == out.type
return [new] return [new]
def __str__(self): def __str__(self):
...@@ -616,9 +637,9 @@ def local_mul_specialize(node): ...@@ -616,9 +637,9 @@ def local_mul_specialize(node):
new_inputs.append(input) new_inputs.append(input)
if len(new_inputs) < len(node.inputs): if len(new_inputs) < len(node.inputs):
if len(new_inputs) == 0: if len(new_inputs) == 0:
newval = -1 if neg else 1 newval = -y.flatten()[0] if neg else y.flatten()[0]
return [T.TensorConstant(T.Tensor(dtype=node.outputs[0].type.dtype, return [T.TensorConstant(T.Tensor(dtype=node.outputs[0].type.dtype,
broadcastable = ()), N.asarray(newval))] broadcastable = [True] * node.outputs[0].ndim), N.asarray(newval))]
if len(new_inputs) == 1: if len(new_inputs) == 1:
return [-new_inputs[0]] if neg else new_inputs return [-new_inputs[0]] if neg else new_inputs
......
...@@ -3,7 +3,7 @@ import theano.tensor as T ...@@ -3,7 +3,7 @@ import theano.tensor as T
from ...gof import Env from ...gof import Env
import numpy import numpy
from theano.tensor.blas import * from theano.tensor.blas import *
from theano.tensor.blas import _as_scalar, _dot22 from theano.tensor.blas import _as_scalar, _dot22, _is_real_matrix
from unittest import TestCase from unittest import TestCase
from copy import copy from copy import copy
...@@ -222,6 +222,11 @@ class t_as_scalar(TestCase): ...@@ -222,6 +222,11 @@ class t_as_scalar(TestCase):
self.failUnless(None == _as_scalar(a)) self.failUnless(None == _as_scalar(a))
self.failUnless(None == _as_scalar(T.DimShuffle([False, False], [0,'x', 1])(a))) self.failUnless(None == _as_scalar(T.DimShuffle([False, False], [0,'x', 1])(a)))
class T_real_matrix(TestCase):
def test0(self):
self.failUnless(_is_real_matrix(T.DimShuffle([False,False], [1, 0])(T.dmatrix())))
self.failUnless(not _is_real_matrix(T.DimShuffle([False], ['x', 0])(T.dvector())))
class T_gemm_opt(TestCase): class T_gemm_opt(TestCase):
"""This test suite ensures that Gemm is inserted where it belongs, and that the resulting """This test suite ensures that Gemm is inserted where it belongs, and that the resulting
functions compute the same things as the originals.""" functions compute the same things as the originals."""
...@@ -298,6 +303,8 @@ class T_gemm_opt(TestCase): ...@@ -298,6 +303,8 @@ class T_gemm_opt(TestCase):
u,v = T.dvector(), T.dvector() u,v = T.dvector(), T.dvector()
f = function([a, u, v], a + T.dot(u,v), mode='FAST_RUN') f = function([a, u, v], a + T.dot(u,v), mode='FAST_RUN')
self.failIf(gemm in [n.op for n in f.maker.env.nodes])
print f.maker.env.nodes
f = function([a, u, X,Y], a * u + T.dot(X,Y), mode='FAST_RUN')
self.failIf(gemm in [n.op for n in f.maker.env.nodes])
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论