提交 3f47baa5 authored 作者: Frederic's avatar Frederic

use floatX in tests.

上级 223a6ab1
...@@ -27,7 +27,7 @@ from theano.tensor.nnet import (categorical_crossentropy, ...@@ -27,7 +27,7 @@ from theano.tensor.nnet import (categorical_crossentropy,
softmax_with_bias, SoftmaxGrad, softmax_with_bias, SoftmaxGrad,
Prepend_scalar_constant_to_each_row, Prepend_scalar_constant_to_each_row,
Prepend_scalar_to_each_row) Prepend_scalar_to_each_row)
from theano.tensor import dmatrix, dvector, lvector, dscalar from theano.tensor import matrix, vector, lvector, scalar
class T_sigmoid(unittest.TestCase): class T_sigmoid(unittest.TestCase):
...@@ -71,8 +71,8 @@ class T_Softmax(utt.InferShapeTester): ...@@ -71,8 +71,8 @@ class T_Softmax(utt.InferShapeTester):
utt.verify_grad(f, [numpy.random.rand(3, 4)]) utt.verify_grad(f, [numpy.random.rand(3, 4)])
def test_infer_shape(self): def test_infer_shape(self):
admat = dmatrix() admat = matrix()
admat_val = numpy.random.rand(3, 4) admat_val = numpy.random.rand(3, 4).astype(config.floatX)
self._compile_and_check([admat], [Softmax()(admat)], self._compile_and_check([admat], [Softmax()(admat)],
[admat_val], Softmax) [admat_val], Softmax)
...@@ -136,10 +136,10 @@ class T_SoftmaxWithBias(utt.InferShapeTester): ...@@ -136,10 +136,10 @@ class T_SoftmaxWithBias(utt.InferShapeTester):
#print f.maker.fgraph.toposort() #print f.maker.fgraph.toposort()
def test_infer_shape(self): def test_infer_shape(self):
admat = dmatrix() admat = matrix()
advec = dvector() advec = vector()
admat_val = numpy.random.rand(3, 4) admat_val = numpy.random.rand(3, 4).astype(config.floatX)
advec_val = numpy.random.rand(4) advec_val = numpy.random.rand(4).astype(config.floatX)
self._compile_and_check([admat, advec], self._compile_and_check([admat, advec],
[SoftmaxWithBias()(admat, advec)], [SoftmaxWithBias()(admat, advec)],
[admat_val, advec_val], SoftmaxWithBias) [admat_val, advec_val], SoftmaxWithBias)
...@@ -148,10 +148,10 @@ class T_SoftmaxWithBias(utt.InferShapeTester): ...@@ -148,10 +148,10 @@ class T_SoftmaxWithBias(utt.InferShapeTester):
class T_SoftmaxGrad(utt.InferShapeTester): class T_SoftmaxGrad(utt.InferShapeTester):
def test_infer_shape(self): def test_infer_shape(self):
admat = dmatrix() admat = matrix()
bdmat = dmatrix() bdmat = matrix()
admat_val = numpy.random.rand(3, 4) admat_val = numpy.random.rand(3, 4).astype(config.floatX)
bdmat_val = numpy.random.rand(3, 4) bdmat_val = numpy.random.rand(3, 4).astype(config.floatX)
self._compile_and_check([admat, bdmat], [SoftmaxGrad()(admat, bdmat)], self._compile_and_check([admat, bdmat], [SoftmaxGrad()(admat, bdmat)],
[admat_val, bdmat_val], SoftmaxGrad) [admat_val, bdmat_val], SoftmaxGrad)
...@@ -218,13 +218,13 @@ class T_CrossentropySoftmax1HotWithBiasDx(utt.InferShapeTester): ...@@ -218,13 +218,13 @@ class T_CrossentropySoftmax1HotWithBiasDx(utt.InferShapeTester):
utt.verify_grad(f, [rng.rand(10)]) utt.verify_grad(f, [rng.rand(10)])
def test_infer_shape(self): def test_infer_shape(self):
admat = dmatrix() admat = matrix()
advec = dvector() advec = vector()
alvec = lvector() alvec = lvector()
rng = numpy.random.RandomState(utt.fetch_seed()) rng = numpy.random.RandomState(utt.fetch_seed())
admat_val = rng.rand(10, 5) admat_val = rng.rand(10, 5).astype(config.floatX)
admat_val /= admat_val.sum(axis=1).reshape(10, 1) admat_val /= admat_val.sum(axis=1).reshape(10, 1)
advec_val = rng.rand(10) advec_val = rng.rand(10).astype(config.floatX)
alvec_val = rng.randint(low=0, high=5, size=10) alvec_val = rng.randint(low=0, high=5, size=10)
self._compile_and_check([advec, admat, alvec], self._compile_and_check([advec, admat, alvec],
[CrossentropySoftmax1HotWithBiasDx()(advec, admat, alvec)], [CrossentropySoftmax1HotWithBiasDx()(advec, admat, alvec)],
...@@ -258,12 +258,12 @@ class T_CrossentropySoftmaxArgmax1HotWithBias(utt.InferShapeTester): ...@@ -258,12 +258,12 @@ class T_CrossentropySoftmaxArgmax1HotWithBias(utt.InferShapeTester):
numpy.random.rand(n_classes)]) numpy.random.rand(n_classes)])
def test_infer_shape(self): def test_infer_shape(self):
admat = dmatrix() admat = matrix()
advec = dvector() advec = vector()
alvec = lvector() alvec = lvector()
rng = numpy.random.RandomState(utt.fetch_seed()) rng = numpy.random.RandomState(utt.fetch_seed())
admat_val = rng.rand(3, 5) admat_val = rng.rand(3, 5).astype(config.floatX)
advec_val = rng.rand(5) advec_val = rng.rand(5).astype(config.floatX)
alvec_val = rng.randint(low=0, high=5, size=3) alvec_val = rng.randint(low=0, high=5, size=3)
self._compile_and_check([admat, advec, alvec], self._compile_and_check([admat, advec, alvec],
CrossentropySoftmaxArgmax1HotWithBias()(admat, advec, alvec), CrossentropySoftmaxArgmax1HotWithBias()(admat, advec, alvec),
...@@ -293,11 +293,11 @@ class T_prepend(utt.InferShapeTester): ...@@ -293,11 +293,11 @@ class T_prepend(utt.InferShapeTester):
self.assertTrue(numpy.all(my[:, 0] == 5.0)) self.assertTrue(numpy.all(my[:, 0] == 5.0))
def test_infer_shape(self): def test_infer_shape(self):
admat = dmatrix() admat = matrix()
adscal = dscalar() adscal = scalar()
rng = numpy.random.RandomState(utt.fetch_seed()) rng = numpy.random.RandomState(utt.fetch_seed())
admat_val = rng.rand(3, 5) admat_val = rng.rand(3, 5).astype(config.floatX)
adscal_val = rng.rand() adscal_val = numpy.asarray(rng.rand(), dtype=config.floatX).item()
self._compile_and_check([admat], self._compile_and_check([admat],
[Prepend_scalar_constant_to_each_row(adscal_val)(admat)], [Prepend_scalar_constant_to_each_row(adscal_val)(admat)],
[admat_val], [admat_val],
...@@ -312,12 +312,12 @@ class T_prepend(utt.InferShapeTester): ...@@ -312,12 +312,12 @@ class T_prepend(utt.InferShapeTester):
class T_CrossentropyCategorical1HotGrad(utt.InferShapeTester): class T_CrossentropyCategorical1HotGrad(utt.InferShapeTester):
def test_infer_shape(self): def test_infer_shape(self):
advec = dvector() advec = vector()
admat = dmatrix() admat = matrix()
alvec = lvector() alvec = lvector()
rng = numpy.random.RandomState(utt.fetch_seed()) rng = numpy.random.RandomState(utt.fetch_seed())
advec_val = rng.rand(3) advec_val = rng.rand(3).astype(config.floatX)
admat_val = rng.rand(3, 2) admat_val = rng.rand(3, 2).astype(config.floatX)
alvec_val = [0, 1, 0] alvec_val = [0, 1, 0]
self._compile_and_check([advec, admat, alvec], self._compile_and_check([advec, admat, alvec],
[CrossentropyCategorical1HotGrad()(advec, admat, alvec)], [CrossentropyCategorical1HotGrad()(advec, admat, alvec)],
...@@ -345,10 +345,10 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester): ...@@ -345,10 +345,10 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
# see issue gh-788 # see issue gh-788
def est_infer_shape(self): def est_infer_shape(self):
admat = dmatrix() admat = matrix()
alvec = lvector() alvec = lvector()
rng = numpy.random.RandomState(utt.fetch_seed()) rng = numpy.random.RandomState(utt.fetch_seed())
admat_val = rng.rand(3, 2) admat_val = rng.rand(3, 2).astype(config.floatX)
alvec_val = [0, 1, 0] alvec_val = [0, 1, 0]
self._compile_and_check([admat, alvec], self._compile_and_check([admat, alvec],
[CrossentropyCategorical1Hot()(admat, alvec)], [CrossentropyCategorical1Hot()(admat, alvec)],
...@@ -570,11 +570,11 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester): ...@@ -570,11 +570,11 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
if mode == theano.compile.mode.get_mode('FAST_COMPILE'): if mode == theano.compile.mode.get_mode('FAST_COMPILE'):
mode = 'FAST_RUN' mode = 'FAST_RUN'
rng = numpy.random.RandomState(utt.fetch_seed()) rng = numpy.random.RandomState(utt.fetch_seed())
x_val = rng.randn(3, 5) x_val = rng.randn(3, 5).astype(config.floatX)
b_val = rng.randn(5) b_val = rng.randn(5).astype(config.floatX)
y_val = numpy.asarray([2, 4, 1]) y_val = numpy.asarray([2, 4, 1])
x = T.dmatrix('x') x = T.matrix('x')
b = T.dvector('b') b = T.vector('b')
y = T.lvector('y') y = T.lvector('y')
## Basic case ## Basic case
...@@ -696,11 +696,11 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester): ...@@ -696,11 +696,11 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
if mode == theano.compile.mode.get_mode('FAST_COMPILE'): if mode == theano.compile.mode.get_mode('FAST_COMPILE'):
mode = 'FAST_RUN' mode = 'FAST_RUN'
rng = numpy.random.RandomState(utt.fetch_seed()) rng = numpy.random.RandomState(utt.fetch_seed())
x_val = rng.randn(3, 5) x_val = rng.randn(3, 5).astype(config.floatX)
b_val = rng.randn(5) b_val = rng.randn(5).astype(config.floatX)
y_val = numpy.asarray([2, 4, 1], dtype='int64') y_val = numpy.asarray([2, 4, 1], dtype='int64')
x = T.dmatrix('x') x = T.matrix('x')
b = T.dvector('b') b = T.vector('b')
y = T.lvector('y') y = T.lvector('y')
yi = T.cast(y, 'int32') yi = T.cast(y, 'int32')
expressions = [ expressions = [
...@@ -739,10 +739,10 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester): ...@@ -739,10 +739,10 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
if mode == theano.compile.mode.get_mode('FAST_COMPILE'): if mode == theano.compile.mode.get_mode('FAST_COMPILE'):
mode = 'FAST_RUN' mode = 'FAST_RUN'
rng = numpy.random.RandomState(utt.fetch_seed()) rng = numpy.random.RandomState(utt.fetch_seed())
x_val = rng.randn(5) x_val = rng.randn(5).astype(config.floatX)
y_val = numpy.asarray([2]) y_val = numpy.asarray([2])
x = T.dvector('x') x = T.vector('x')
y = T.lvector('y') y = T.lvector('y')
def print_graph(func): def print_graph(func):
...@@ -788,12 +788,12 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester): ...@@ -788,12 +788,12 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
if mode == theano.compile.mode.get_mode('FAST_COMPILE'): if mode == theano.compile.mode.get_mode('FAST_COMPILE'):
mode = 'FAST_RUN' mode = 'FAST_RUN'
rng = numpy.random.RandomState(utt.fetch_seed()) rng = numpy.random.RandomState(utt.fetch_seed())
x_val = rng.randn(5) x_val = rng.randn(5).astype(config.floatX)
b_val = rng.randn(5) b_val = rng.randn(5).astype(config.floatX)
y_val = numpy.asarray([2]) y_val = numpy.asarray([2])
x = T.dvector('x') x = T.vector('x')
b = T.dvector('b') b = T.vector('b')
y = T.lvector('y') y = T.lvector('y')
def print_graph(func): def print_graph(func):
...@@ -850,13 +850,13 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester): ...@@ -850,13 +850,13 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
if mode == theano.compile.mode.get_mode('FAST_COMPILE'): if mode == theano.compile.mode.get_mode('FAST_COMPILE'):
mode = 'FAST_RUN' mode = 'FAST_RUN'
rng = numpy.random.RandomState(utt.fetch_seed()) rng = numpy.random.RandomState(utt.fetch_seed())
x_val = rng.randn(3, 5) x_val = rng.randn(3, 5).astype(config.floatX)
b_val = rng.randn(5) b_val = rng.randn(5).astype(config.floatX)
y_val = numpy.asarray([2, 4, 1]) y_val = numpy.asarray([2, 4, 1])
x = T.dmatrix('x') x = T.matrix('x')
b = T.dvector('b') b = T.vector('b')
y = T.lvector('y') y = T.lvector('y')
a = T.dscalar('a') a = T.scalar('a')
def print_graph(func): def print_graph(func):
for i, node in enumerate(func.maker.fgraph.toposort()): for i, node in enumerate(func.maker.fgraph.toposort()):
...@@ -951,7 +951,7 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester): ...@@ -951,7 +951,7 @@ class T_CrossentropyCategorical1Hot(utt.InferShapeTester):
def test_argmax_pushdown(): def test_argmax_pushdown():
x = tensor.dmatrix() x = tensor.matrix()
#test that the max_and_argmax is pushed down if the max is not used #test that the max_and_argmax is pushed down if the max is not used
out = tensor.max_and_argmax( out = tensor.max_and_argmax(
...@@ -969,7 +969,7 @@ def test_argmax_pushdown(): ...@@ -969,7 +969,7 @@ def test_argmax_pushdown():
assert len(fgraph.toposort()) == 2 # an output_guard is second assert len(fgraph.toposort()) == 2 # an output_guard is second
assert fgraph.toposort()[0].op == tensor.basic._max_and_argmax assert fgraph.toposort()[0].op == tensor.basic._max_and_argmax
assert str(fgraph.toposort()[1].op) == 'OutputGuard' assert str(fgraph.toposort()[1].op) == 'OutputGuard'
x = tensor.dmatrix() x = tensor.matrix()
#test that the max_and_argmax is not pushed down if the max is used #test that the max_and_argmax is not pushed down if the max is used
out = tensor.max_and_argmax( out = tensor.max_and_argmax(
softmax(tensor.exp(tensor.tanh(sigmoid(x)))), softmax(tensor.exp(tensor.tanh(sigmoid(x)))),
...@@ -998,8 +998,8 @@ def test_argmax_pushdown(): ...@@ -998,8 +998,8 @@ def test_argmax_pushdown():
def test_argmax_pushdown_bias(): def test_argmax_pushdown_bias():
x = tensor.dmatrix() x = tensor.matrix()
b = tensor.dvector() b = tensor.vector()
out = tensor.argmax(softmax_with_bias(x, b), axis=-1) out = tensor.argmax(softmax_with_bias(x, b), axis=-1)
fgraph = gof.FunctionGraph( fgraph = gof.FunctionGraph(
...@@ -1018,8 +1018,8 @@ def test_argmax_pushdown_bias(): ...@@ -1018,8 +1018,8 @@ def test_argmax_pushdown_bias():
assert isinstance(fgraph.toposort()[2].op, tensor.MaxAndArgmax) assert isinstance(fgraph.toposort()[2].op, tensor.MaxAndArgmax)
assert str(fgraph.toposort()[3].op) == 'OutputGuard' assert str(fgraph.toposort()[3].op) == 'OutputGuard'
x = tensor.dmatrix() x = tensor.matrix()
b = tensor.dvector() b = tensor.vector()
out = tensor.max_and_argmax(softmax_with_bias(x, b), axis=-1)[0] out = tensor.max_and_argmax(softmax_with_bias(x, b), axis=-1)[0]
fgraph = gof.FunctionGraph( fgraph = gof.FunctionGraph(
[x, b], [x, b],
...@@ -1068,8 +1068,8 @@ def test_asymptotic_32(): ...@@ -1068,8 +1068,8 @@ def test_asymptotic_32():
for i, n in enumerate(f.maker.fgraph.toposort()): for i, n in enumerate(f.maker.fgraph.toposort()):
print i, n print i, n
xval = numpy.zeros((5, 5), dtype=dtype) xval = numpy.zeros((5, 5), dtype=dtype).astype(dtype)
x2val = numpy.zeros(5, dtype=xval.dtype) x2val = numpy.zeros(5, dtype=xval.dtype).astype(dtype)
for i in xrange(100): for i in xrange(100):
cval, gxval = f(xval, numpy.arange(5), x2val) cval, gxval = f(xval, numpy.arange(5), x2val)
xval -= 100.3 * gxval xval -= 100.3 * gxval
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论