提交 bd8add03 authored 作者: Frederic's avatar Frederic

pep8

上级 16683490
import os, sys, traceback, warnings
import os
import sys
import traceback
import warnings
import numpy
from nose.plugins.skip import SkipTest
......@@ -46,34 +49,33 @@ class TestComputeTestValue(unittest.TestCase):
theano.config.compute_test_value = 'raise'
x = T.matrix('x')
x.tag.test_value = numpy.random.rand(3,4).astype(config.floatX)
x.tag.test_value = numpy.random.rand(3, 4).astype(config.floatX)
y = T.matrix('y')
y.tag.test_value = numpy.random.rand(4,5).astype(config.floatX)
y.tag.test_value = numpy.random.rand(4, 5).astype(config.floatX)
# should work
z = T.dot(x,y)
z = T.dot(x, y)
assert hasattr(z.tag, 'test_value')
f = theano.function([x,y], z)
f = theano.function([x, y], z)
assert _allclose(f(x.tag.test_value, y.tag.test_value),
z.tag.test_value)
# this test should fail
y.tag.test_value = numpy.random.rand(6,5).astype(config.floatX)
y.tag.test_value = numpy.random.rand(6, 5).astype(config.floatX)
self.assertRaises(ValueError, T.dot, x, y)
finally:
theano.config.compute_test_value = orig_compute_test_value
def test_compute_flag(self):
orig_compute_test_value = theano.config.compute_test_value
try:
x = T.matrix('x')
y = T.matrix('y')
y.tag.test_value = numpy.random.rand(4,5).astype(config.floatX)
y.tag.test_value = numpy.random.rand(4, 5).astype(config.floatX)
# should skip computation of test value
theano.config.compute_test_value = 'off'
z = T.dot(x,y)
z = T.dot(x, y)
assert not hasattr(z.tag, 'test_value')
# should fail when asked by user
......@@ -99,25 +101,25 @@ class TestComputeTestValue(unittest.TestCase):
theano.config.compute_test_value = 'raise'
x = T.matrix('x')
x.tag.test_value = numpy.random.rand(3,4).astype(config.floatX)
x.tag.test_value = numpy.random.rand(3, 4).astype(config.floatX)
y = T.matrix('y')
y.tag.test_value = numpy.random.rand(4,5).astype(config.floatX)
y.tag.test_value = numpy.random.rand(4, 5).astype(config.floatX)
z = theano.shared(numpy.random.rand(5,6).astype(config.floatX))
z = theano.shared(numpy.random.rand(5, 6).astype(config.floatX))
# should work
out = T.dot(T.dot(x,y), z)
out = T.dot(T.dot(x, y), z)
assert hasattr(out.tag, 'test_value')
tf = theano.function([x,y], out)
tf = theano.function([x, y], out)
assert _allclose(
tf(x.tag.test_value, y.tag.test_value),
out.tag.test_value)
tf(x.tag.test_value, y.tag.test_value),
out.tag.test_value)
def f(x,y,z):
return T.dot(T.dot(x,y),z)
def f(x, y, z):
return T.dot(T.dot(x, y), z)
# this test should fail
z.set_value(numpy.random.rand(7,6).astype(config.floatX))
z.set_value(numpy.random.rand(7, 6).astype(config.floatX))
self.assertRaises(ValueError, f, x, y, z)
finally:
theano.config.compute_test_value = orig_compute_test_value
......@@ -128,17 +130,18 @@ class TestComputeTestValue(unittest.TestCase):
theano.config.compute_test_value = 'raise'
x = T.matrix('x')
x.tag.test_value = numpy.random.rand(3,4).astype(config.floatX)
y = theano.shared(numpy.random.rand(4,6).astype(config.floatX), 'y')
x.tag.test_value = numpy.random.rand(3, 4).astype(config.floatX)
y = theano.shared(numpy.random.rand(4, 6).astype(config.floatX),
'y')
# should work
z = T.dot(x,y)
z = T.dot(x, y)
assert hasattr(z.tag, 'test_value')
f = theano.function([x], z)
assert _allclose(f(x.tag.test_value), z.tag.test_value)
# this test should fail
y.set_value(numpy.random.rand(5,6).astype(config.floatX))
y.set_value(numpy.random.rand(5, 6).astype(config.floatX))
self.assertRaises(ValueError, T.dot, x, y)
finally:
theano.config.compute_test_value = orig_compute_test_value
......@@ -148,17 +151,18 @@ class TestComputeTestValue(unittest.TestCase):
try:
theano.config.compute_test_value = 'raise'
x = numpy.random.rand(2,3).astype(config.floatX)
y = theano.shared(numpy.random.rand(3,6).astype(config.floatX), 'y')
x = numpy.random.rand(2, 3).astype(config.floatX)
y = theano.shared(numpy.random.rand(3, 6).astype(config.floatX),
'y')
# should work
z = T.dot(x,y)
z = T.dot(x, y)
assert hasattr(z.tag, 'test_value')
f = theano.function([], z)
assert _allclose(f(), z.tag.test_value)
# this test should fail
x = numpy.random.rand(2,4).astype(config.floatX)
x = numpy.random.rand(2, 4).astype(config.floatX)
self.assertRaises(ValueError, T.dot, x, y)
finally:
theano.config.compute_test_value = orig_compute_test_value
......@@ -168,17 +172,18 @@ class TestComputeTestValue(unittest.TestCase):
try:
theano.config.compute_test_value = 'raise'
x = T.constant(numpy.random.rand(2,3), dtype=config.floatX)
y = theano.shared(numpy.random.rand(3,6).astype(config.floatX), 'y')
x = T.constant(numpy.random.rand(2, 3), dtype=config.floatX)
y = theano.shared(numpy.random.rand(3, 6).astype(config.floatX),
'y')
# should work
z = T.dot(x,y)
z = T.dot(x, y)
assert hasattr(z.tag, 'test_value')
f = theano.function([], z)
assert _allclose(f(), z.tag.test_value)
# this test should fail
x = T.constant(numpy.random.rand(2,4), dtype=config.floatX)
x = T.constant(numpy.random.rand(2, 4), dtype=config.floatX)
self.assertRaises(ValueError, T.dot, x, y)
finally:
theano.config.compute_test_value = orig_compute_test_value
......@@ -190,9 +195,9 @@ class TestComputeTestValue(unittest.TestCase):
x = T.fmatrix('x')
# Incorrect dtype (float64) for test_value
x.tag.test_value = numpy.random.rand(3,4)
x.tag.test_value = numpy.random.rand(3, 4)
y = T.dmatrix('y')
y.tag.test_value = numpy.random.rand(4,5)
y.tag.test_value = numpy.random.rand(4, 5)
self.assertRaises(TypeError, T.dot, x, y)
finally:
......@@ -205,9 +210,9 @@ class TestComputeTestValue(unittest.TestCase):
try:
config.compute_test_value = "raise"
x = T.matrix()
x.tag.test_value = numpy.zeros((2,3), dtype=config.floatX)
x.tag.test_value = numpy.zeros((2, 3), dtype=config.floatX)
y = T.matrix()
y.tag.test_value = numpy.zeros((2,2), dtype=config.floatX)
y.tag.test_value = numpy.zeros((2, 2), dtype=config.floatX)
self.assertRaises(ValueError, x.__mul__, y)
finally:
theano.config.compute_test_value = orig_compute_test_value
......@@ -250,7 +255,7 @@ class TestComputeTestValue(unittest.TestCase):
k = T.iscalar("k")
A = T.matrix("A")
k.tag.test_value = 3
A.tag.test_value = numpy.random.rand(5,3).astype(config.floatX)
A.tag.test_value = numpy.random.rand(5, 3).astype(config.floatX)
def fx(prior_result, A):
return T.dot(prior_result, A)
......@@ -259,10 +264,10 @@ class TestComputeTestValue(unittest.TestCase):
# we cannot simply use self.assertRaises()
try:
theano.scan(
fn=fx,
outputs_info=T.ones_like(A),
non_sequences=A,
n_steps=k)
fn=fx,
outputs_info=T.ones_like(A),
non_sequences=A,
n_steps=k)
assert False
except ValueError, e:
# Get traceback
......@@ -286,26 +291,26 @@ class TestComputeTestValue(unittest.TestCase):
k = T.iscalar("k")
A = T.matrix("A")
k.tag.test_value = 3
A.tag.test_value = numpy.random.rand(5,3).astype(config.floatX)
A.tag.test_value = numpy.random.rand(5, 3).astype(config.floatX)
def fx(prior_result, A):
return T.dot(prior_result, A)
self.assertRaises(ValueError,
theano.scan,
fn=fx,
outputs_info=T.ones_like(A.T),
non_sequences=A,
n_steps=k)
theano.scan,
fn=fx,
outputs_info=T.ones_like(A.T),
non_sequences=A,
n_steps=k)
# Since we have to inspect the traceback,
# we cannot simply use self.assertRaises()
try:
theano.scan(
fn=fx,
outputs_info=T.ones_like(A.T),
non_sequences=A,
n_steps=k)
fn=fx,
outputs_info=T.ones_like(A.T),
non_sequences=A,
n_steps=k)
assert False
except ValueError, e:
# The first message is for numpy before 1.6.
......@@ -338,7 +343,6 @@ class TestComputeTestValue(unittest.TestCase):
output, = outputs
output[0] = input + 1
orig_compute_test_value = theano.config.compute_test_value
try:
theano.config.compute_test_value = 'raise'
......@@ -349,9 +353,10 @@ class TestComputeTestValue(unittest.TestCase):
o = IncOnePython()(i)
# Check that the c_code function is not implemented
self.assertRaises((NotImplementedError, utils.MethodNotDefined),
o.owner.op.c_code,
o.owner, 'o', ['x'], 'z', {'fail': ''})
self.assertRaises(
(NotImplementedError, utils.MethodNotDefined),
o.owner.op.c_code,
o.owner, 'o', ['x'], 'z', {'fail': ''})
assert hasattr(o.tag, 'test_value')
assert o.tag.test_value == 4
......@@ -376,8 +381,8 @@ class TestComputeTestValue(unittest.TestCase):
# Check that the perform function is not implemented
self.assertRaises((NotImplementedError, utils.MethodNotDefined),
o.owner.op.perform,
o.owner, 0, [None])
o.owner.op.perform,
o.owner, 0, [None])
assert hasattr(o.tag, 'test_value')
assert o.tag.test_value == 4
......@@ -391,7 +396,8 @@ class TestComputeTestValue(unittest.TestCase):
orig_compute_test_value = theano.config.compute_test_value
try:
theano.config.compute_test_value = 'raise'
init_Mu1 = theano.shared(numpy.zeros((5,),dtype=config.floatX)).dimshuffle('x',0)
init_Mu1 = theano.shared(
numpy.zeros((5,), dtype=config.floatX)).dimshuffle('x', 0)
f = theano.function([], outputs=[init_Mu1])
finally:
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论