提交 6df163f0 authored 作者: Brandon T. Willard's avatar Brandon T. Willard

Replace theano.tensor alias T with tt in tests.compile

上级 33667eb7
......@@ -3,13 +3,14 @@ import numpy as np
import pytest
import theano
import theano.tensor as tt
from theano import config, shared
from theano.gradient import DisconnectedType
from theano.gof.null_type import NullType
from theano.compile import function
from theano import tensor as T
from theano.tensor.shared_randomstreams import RandomStreams
from theano.compile.builders import OpFromGraph
......@@ -22,7 +23,7 @@ class TestOpFromGraph(unittest_tools.InferShapeTester):
"cls_ofg", [OpFromGraph, partial(OpFromGraph, inline=True)]
)
def test_straightforward(self, cls_ofg):
x, y, z = T.matrices("xyz")
x, y, z = tt.matrices("xyz")
e = x + y * z
op = cls_ofg([x, y, z], [e])
# (1+3*5=array of 16) - (3+1*5=array of 8)
......@@ -42,8 +43,8 @@ class TestOpFromGraph(unittest_tools.InferShapeTester):
"cls_ofg", [OpFromGraph, partial(OpFromGraph, inline=True)]
)
def test_size_changes(self, cls_ofg):
x, y, z = T.matrices("xyz")
e = T.dot(x, y)
x, y, z = tt.matrices("xyz")
e = tt.dot(x, y)
op = cls_ofg([x, y], [e])
f = op(x, op(y, z))
fn = function([x, y, z], f)
......@@ -61,11 +62,11 @@ class TestOpFromGraph(unittest_tools.InferShapeTester):
"cls_ofg", [OpFromGraph, partial(OpFromGraph, inline=True)]
)
def test_grad(self, cls_ofg):
x, y, z = T.matrices("xyz")
x, y, z = tt.matrices("xyz")
e = x + y * z
op = cls_ofg([x, y, z], [e])
f = op(x, y, z)
f = f - T.grad(T.sum(f), y)
f = f - tt.grad(tt.sum(f), y)
fn = function([x, y, z], f)
xv = np.ones((2, 2), dtype=config.floatX)
yv = np.ones((2, 2), dtype=config.floatX) * 3
......@@ -76,12 +77,12 @@ class TestOpFromGraph(unittest_tools.InferShapeTester):
"cls_ofg", [OpFromGraph, partial(OpFromGraph, inline=True)]
)
def test_grad_grad(self, cls_ofg):
x, y, z = T.matrices("xyz")
x, y, z = tt.matrices("xyz")
e = x + y * z
op = cls_ofg([x, y, z], [e])
f = op(x, y, z)
f = f - T.grad(T.sum(f), y)
f = f - T.grad(T.sum(f), y)
f = f - tt.grad(tt.sum(f), y)
f = f - tt.grad(tt.sum(f), y)
fn = function([x, y, z], f)
xv = np.ones((2, 2), dtype=config.floatX)
yv = np.ones((2, 2), dtype=config.floatX) * 3
......@@ -92,7 +93,7 @@ class TestOpFromGraph(unittest_tools.InferShapeTester):
"cls_ofg", [OpFromGraph, partial(OpFromGraph, inline=True)]
)
def test_shared(self, cls_ofg):
x, y, z = T.matrices("xyz")
x, y, z = tt.matrices("xyz")
s = shared(np.random.rand(2, 2).astype(config.floatX))
e = x + y * z + s
op = cls_ofg([x, y, z], [e])
......@@ -112,12 +113,12 @@ class TestOpFromGraph(unittest_tools.InferShapeTester):
"cls_ofg", [OpFromGraph, partial(OpFromGraph, inline=True)]
)
def test_shared_grad(self, cls_ofg):
x, y, z = T.matrices("xyz")
x, y, z = tt.matrices("xyz")
s = shared(np.random.rand(2, 2).astype(config.floatX))
e = x + y * z + s
op = cls_ofg([x, y, z], [e])
f = op(x, y, z)
f = f - T.grad(T.sum(f), y)
f = f - tt.grad(tt.sum(f), y)
fn = function([x, y, z], f)
xv = np.ones((2, 2), dtype=config.floatX)
yv = np.ones((2, 2), dtype=config.floatX) * 3
......@@ -126,7 +127,7 @@ class TestOpFromGraph(unittest_tools.InferShapeTester):
# grad again the shared variable
f = op(x, y, z)
f = f - T.grad(T.sum(f), s)
f = f - tt.grad(tt.sum(f), s)
fn = function([x, y, z], f)
assert np.allclose(15.0 + s.get_value(), fn(xv, yv, zv))
......@@ -134,24 +135,24 @@ class TestOpFromGraph(unittest_tools.InferShapeTester):
"cls_ofg", [OpFromGraph, partial(OpFromGraph, inline=True)]
)
def test_grad_override(self, cls_ofg):
x, y = T.vectors("xy")
x, y = tt.vectors("xy")
def go(inps, gs):
x, y = inps
(g,) = gs
return [g * y * 2, g * x * 1.5]
dedz = T.vector("dedz")
dedz = tt.vector("dedz")
op_mul_grad = cls_ofg([x, y, dedz], go([x, y], [dedz]))
op_mul = cls_ofg([x, y], [x * y], grad_overrides=go)
op_mul2 = cls_ofg([x, y], [x * y], grad_overrides=op_mul_grad)
# single override case (function or OfG instance)
xx, yy = T.vector("xx"), T.vector("yy")
xx, yy = tt.vector("xx"), tt.vector("yy")
for op in [op_mul, op_mul2]:
zz = T.sum(op(xx, yy))
dx, dy = T.grad(zz, [xx, yy])
zz = tt.sum(op(xx, yy))
dx, dy = tt.grad(zz, [xx, yy])
fn = function([xx, yy], [dx, dy])
xv = np.random.rand(16).astype(config.floatX)
yv = np.random.rand(16).astype(config.floatX)
......@@ -170,14 +171,14 @@ class TestOpFromGraph(unittest_tools.InferShapeTester):
g = gs[0]
return g * x * 1.5
w, b = T.vectors("wb")
w, b = tt.vectors("wb")
# we make the 3rd gradient default (no override)
op_linear = cls_ofg(
[x, w, b], [x * w + b], grad_overrides=[go1, go2, "default"]
)
xx, ww, bb = T.vector("xx"), T.vector("yy"), T.vector("bb")
zz = T.sum(op_linear(xx, ww, bb))
dx, dw, db = T.grad(zz, [xx, ww, bb])
xx, ww, bb = tt.vector("xx"), tt.vector("yy"), tt.vector("bb")
zz = tt.sum(op_linear(xx, ww, bb))
dx, dw, db = tt.grad(zz, [xx, ww, bb])
fn = function([xx, ww, bb], [dx, dw, db])
xv = np.random.rand(16).astype(config.floatX)
wv = np.random.rand(16).astype(config.floatX)
......@@ -193,15 +194,15 @@ class TestOpFromGraph(unittest_tools.InferShapeTester):
[x * w + b],
grad_overrides=[go1, NullType()(), DisconnectedType()()],
)
zz2 = T.sum(op_linear2(xx, ww, bb))
dx2, dw2, db2 = T.grad(
zz2 = tt.sum(op_linear2(xx, ww, bb))
dx2, dw2, db2 = tt.grad(
zz2,
[xx, ww, bb],
return_disconnected="Disconnected",
disconnected_inputs="ignore",
null_gradients="return",
)
assert isinstance(dx2.type, T.TensorType)
assert isinstance(dx2.type, tt.TensorType)
assert dx2.ndim == 1
assert isinstance(dw2.type, NullType)
assert isinstance(db2.type, DisconnectedType)
......@@ -210,25 +211,25 @@ class TestOpFromGraph(unittest_tools.InferShapeTester):
"cls_ofg", [OpFromGraph, partial(OpFromGraph, inline=True)]
)
def test_lop_override(self, cls_ofg):
x = T.vector()
y = 1.0 / (1.0 + T.exp(-x))
x = tt.vector()
y = 1.0 / (1.0 + tt.exp(-x))
def lop_ov(inps, outs, grads):
(y_,) = outs
(dedy_,) = grads
return [2.0 * y_ * (1.0 - y_) * dedy_]
y_, dedy = T.vector(), T.vector()
y_, dedy = tt.vector(), tt.vector()
op_lop_ov = cls_ofg([x, y_, dedy], [2.0 * y_ * (1.0 - y_) * dedy])
xx = T.vector()
yy1 = T.sum(T.nnet.sigmoid(xx))
gyy1 = 2.0 * T.grad(yy1, xx)
xx = tt.vector()
yy1 = tt.sum(tt.nnet.sigmoid(xx))
gyy1 = 2.0 * tt.grad(yy1, xx)
for ov in [lop_ov, op_lop_ov]:
op = cls_ofg([x], [y], lop_overrides=ov)
yy2 = T.sum(op(xx))
gyy2 = T.grad(yy2, xx)
yy2 = tt.sum(op(xx))
gyy2 = tt.grad(yy2, xx)
fn = function([xx], [gyy1, gyy2])
xval = np.random.rand(32).astype(config.floatX)
......@@ -239,15 +240,15 @@ class TestOpFromGraph(unittest_tools.InferShapeTester):
"cls_ofg", [OpFromGraph, partial(OpFromGraph, inline=True)]
)
def test_rop(self, cls_ofg):
a = T.vector()
M = T.matrix()
b = T.dot(a, M)
a = tt.vector()
M = tt.matrix()
b = tt.dot(a, M)
op_matmul = cls_ofg([a, M], [b])
x = T.vector()
W = T.matrix()
x = tt.vector()
W = tt.matrix()
y = op_matmul(x, W)
du = T.vector()
dv = T.Rop(y, x, du)
du = tt.vector()
dv = tt.Rop(y, x, du)
fn = function([x, W, du], dv)
xval = np.random.rand(16).astype(config.floatX)
Wval = np.random.rand(16, 16).astype(config.floatX)
......@@ -260,24 +261,24 @@ class TestOpFromGraph(unittest_tools.InferShapeTester):
"cls_ofg", [OpFromGraph, partial(OpFromGraph, inline=True)]
)
def test_rop_override(self, cls_ofg):
x, y = T.vectors("xy")
x, y = tt.vectors("xy")
def ro(inps, epts):
x, y = inps
u, v = epts
return [u * y * 2.0 + x * v * 1.5]
u, v = T.vectors("uv")
u, v = tt.vectors("uv")
op_mul_rop = cls_ofg([x, y, u, v], ro([x, y], [u, v]))
op_mul = cls_ofg([x, y], [x * y], rop_overrides=ro)
op_mul2 = cls_ofg([x, y], [x * y], rop_overrides=op_mul_rop)
# single override case
xx, yy = T.vector("xx"), T.vector("yy")
du, dv = T.vector("du"), T.vector("dv")
xx, yy = tt.vector("xx"), tt.vector("yy")
du, dv = tt.vector("du"), tt.vector("dv")
for op in [op_mul, op_mul2]:
zz = op_mul(xx, yy)
dw = T.Rop(zz, [xx, yy], [du, dv])
dw = tt.Rop(zz, [xx, yy], [du, dv])
fn = function([xx, yy, du, dv], dw)
vals = np.random.rand(4, 32).astype(config.floatX)
dwval = fn(*vals)
......@@ -289,13 +290,13 @@ class TestOpFromGraph(unittest_tools.InferShapeTester):
"cls_ofg", [OpFromGraph, partial(OpFromGraph, inline=True)]
)
def test_connection_pattern_override(self, cls_ofg):
x, y = T.vectors("xy")
x, y = tt.vectors("xy")
def f1(x, y):
del x
# but we know how to backpropagate for x for some reasons
# and we don't care about the gradient wrt y.
return y + T.round(y)
return y + tt.round(y)
def f1_back(inputs, output_gradients):
return [output_gradients[0], theano.gradient.disconnected_type()]
......@@ -321,12 +322,12 @@ class TestOpFromGraph(unittest_tools.InferShapeTester):
"cls_ofg", [OpFromGraph, partial(OpFromGraph, inline=True)]
)
def test_nested(self, cls_ofg):
x, y = T.vectors("xy")
x, y = tt.vectors("xy")
u, v = x + y, x - y
op_ft = cls_ofg([x, y], [u, v])
op_ift = cls_ofg([x, y], [u / 2, v / 2])
xx, yy = T.vector("xx"), T.vector("yy")
xx, yy = tt.vector("xx"), tt.vector("yy")
xx2, yy2 = op_ift(*op_ft(xx, yy))
fn = function([xx, yy], [xx2, yy2])
......@@ -341,7 +342,7 @@ class TestOpFromGraph(unittest_tools.InferShapeTester):
)
def test_connection_pattern(self, cls_ofg):
# Basic case
x, y, z = T.matrices("xyz")
x, y, z = tt.matrices("xyz")
out1 = x * y
out2 = y * z
......@@ -352,7 +353,7 @@ class TestOpFromGraph(unittest_tools.InferShapeTester):
# Graph with ops that don't have a 'full' connection pattern
# and with ops that have multiple outputs
m, n, p, q = T.matrices("mnpq")
m, n, p, q = tt.matrices("mnpq")
o1, o2 = op1(m, n, p)
out1, out2 = op1(o1, q, o2)
op2 = cls_ofg([m, n, p, q], [out1, out2])
......@@ -364,7 +365,7 @@ class TestOpFromGraph(unittest_tools.InferShapeTester):
# Inner graph where some computation doesn't rely on explicit inputs
srng = RandomStreams(seed=234)
rv_u = srng.uniform((2, 2))
x, y = T.matrices("xy")
x, y = tt.matrices("xy")
out1 = x + rv_u
out2 = y + 3
out3 = 3 + rv_u
......@@ -381,14 +382,14 @@ class TestOpFromGraph(unittest_tools.InferShapeTester):
def test_infer_shape(self):
# test infer shape does not need to against inline case
# since the Op is remove during optimization phase
x = T.matrix("x")
y = T.matrix("y")
x = tt.matrix("x")
y = tt.matrix("y")
o1 = x + y
o2 = x * y
op_graph = OpFromGraph([x, y], [o1, o2])
q = T.matrix("q")
p = T.matrix("p")
q = tt.matrix("q")
p = tt.matrix("p")
self._compile_and_check(
[q, p],
op_graph(q, p),
......@@ -401,11 +402,11 @@ class TestOpFromGraph(unittest_tools.InferShapeTester):
@theano.change_flags(compute_test_value="raise")
def test_compute_test_value(self):
x = T.scalar("x")
x = tt.scalar("x")
x.tag.test_value = np.array(1.0, dtype=config.floatX)
op = OpFromGraph([x], [x ** 3])
y = T.scalar("y")
y = tt.scalar("y")
y.tag.test_value = np.array(1.0, dtype=config.floatX)
f = op(y)
grad_f = T.grad(f, y)
grad_f = tt.grad(f, y)
assert grad_f.tag.test_value is not None
......@@ -5,9 +5,9 @@ import pytest
import time
import theano
import theano.tensor as tt
import theano.gpuarray
from theano import tensor as T
from theano import config, gof
from theano.compile.io import In, Out
from theano.compile import function
......@@ -39,9 +39,9 @@ class TestFunction:
def test_none(self):
fn = function([], None) # ok
rval = fn()
assert rval != [], (
"See #254: Using None as function output leads " "to [] return value"
)
assert (
rval != []
), "See #254: Using None as function output leads to [] return value"
assert rval is None
def test_empty(self):
......@@ -49,83 +49,83 @@ class TestFunction:
assert fn() == []
def test_extra_inputs(self):
x, s = T.scalars("xs")
x, s = tt.scalars("xs")
fn = function([x], [x])
with pytest.raises(TypeError):
fn(1, 2)
def test_missing_inputs(self):
def fn():
x, s = T.scalars("xs")
x, s = tt.scalars("xs")
function([], [x])
checkfor(self, fn, MissingInputError)
def fn():
x, s = T.scalars("xs")
x, s = tt.scalars("xs")
# Ignore unused input s, as it hides the other error
function([s], [x], on_unused_input="ignore")
checkfor(self, fn, MissingInputError)
def fn():
x, s = T.scalars("xs")
x, s = tt.scalars("xs")
function([s], [x])
checkfor(self, fn, UnusedInputError)
def fn():
x, s = T.scalars("xs")
x, s = tt.scalars("xs")
# Ignore unused input s, as it hides the other error
function([s], x, on_unused_input="ignore")
checkfor(self, fn, MissingInputError)
def fn():
x, s = T.scalars("xs")
x, s = tt.scalars("xs")
function([s], x)
checkfor(self, fn, UnusedInputError)
def fn():
x, s = T.scalars("xs")
x, s = tt.scalars("xs")
# Ignore unused input s, as it hides the other error
function([s], Out(x), on_unused_input="ignore")
checkfor(self, fn, MissingInputError)
def fn():
x, s = T.scalars("xs")
x, s = tt.scalars("xs")
function([s], Out(x))
checkfor(self, fn, UnusedInputError)
def fn():
x, s = T.scalars("xs")
x, s = tt.scalars("xs")
function([In(x, update=s + x)], x)
checkfor(self, fn, MissingInputError)
def fn():
x, s = T.scalars("xs")
x, s = tt.scalars("xs")
function([In(x, update=((s * s) + x))], x)
checkfor(self, fn, MissingInputError)
def test_input_anon_singleton(self):
x, s = T.scalars("xs")
x, s = tt.scalars("xs")
fn = function([s, x], [x + s])
assert fn(2, 3) == [5]
# no state
assert fn(2, 3) == [5]
def test_input_anon_unpack(self):
x, s = T.scalars("xs")
x, s = tt.scalars("xs")
fn = function([s, x], x + s)
assert fn(2, 3) == 5
def test_naming_rule0(self):
x, s = T.scalars("xs")
x, s = tt.scalars("xs")
f = function([x, s], x / s)
assert f(1, 2) == 0.5
assert f(2, 1) == 2.0
......@@ -143,8 +143,8 @@ class TestFunction:
) # takes exactly 2 non-keyword arguments (0 given)
def test_naming_rule1(self):
a = T.scalar() # the a is for 'anonymous' (un-named).
x, s = T.scalars("xs")
a = tt.scalar() # the a is for 'anonymous' (un-named).
x, s = tt.scalars("xs")
f = function([a, s], a / s)
assert f(1, 2) == 0.5
assert f(2, 1) == 2.0
......@@ -157,8 +157,8 @@ class TestFunction:
) # got unexpected keyword argument 'a'
def test_naming_rule2(self):
a = T.scalar() # the a is for 'anonymous' (un-named).
x, s = T.scalars("xs")
a = tt.scalar() # the a is for 'anonymous' (un-named).
x, s = tt.scalars("xs")
# x's name is ignored because it is followed by anonymous parameter a.
# Ignore unused input x, as it hides the other error
......@@ -174,8 +174,8 @@ class TestFunction:
) # got unexpected keyword argument 'x'
def test_naming_rule3(self):
a = T.scalar() # the a is for 'anonymous' (un-named).
x, s = T.scalars("xs")
a = tt.scalar() # the a is for 'anonymous' (un-named).
x, s = tt.scalars("xs")
# x's name is not ignored (as in test_naming_rule2) because a has a default value.
f = function([x, In(a, value=1.0), s], a / s + x)
......@@ -194,8 +194,8 @@ class TestFunction:
) # takes exactly 3 non-keyword arguments (1 given)
def test_naming_rule4(self):
a = T.scalar() # the a is for 'anonymous' (un-named).
x, s = T.scalars("xs")
a = tt.scalar() # the a is for 'anonymous' (un-named).
x, s = tt.scalars("xs")
f = function([x, In(a, value=1.0, name="a"), s], a / s + x)
......@@ -213,8 +213,8 @@ class TestFunction:
) # got multiple values for keyword argument 'x'
def test_state_access(self):
a = T.scalar() # the a is for 'anonymous' (un-named).
x, s = T.scalars("xs")
a = tt.scalar() # the a is for 'anonymous' (un-named).
x, s = tt.scalars("xs")
f = function(
[x, In(a, value=1.0, name="a"), In(s, value=0.0, update=s + a * x)],
......@@ -238,14 +238,14 @@ class TestFunction:
assert f[s] == 24.0
def test_same_names(self):
a, x, s = T.scalars("xxx")
a, x, s = tt.scalars("xxx")
# implicit names would cause error. What do we do?
f = function([a, x, s], a + x + s)
assert f(1, 2, 3) == 6
checkfor(self, lambda: f(1, 2, x=3), TypeError)
def test_weird_names(self):
a, x, s = T.scalars("xxx")
a, x, s = tt.scalars("xxx")
checkfor(self, lambda: function([In(a, name=[])], []), TypeError)
......@@ -254,7 +254,7 @@ class TestFunction:
[
In(a, name=set(["adsf", ()]), value=1.0),
In(x, name=(), value=2.0),
In(s, name=T.scalar(), value=3.0),
In(s, name=tt.scalar(), value=3.0),
],
a + x + s,
)
......@@ -263,8 +263,8 @@ class TestFunction:
checkfor(self, t, TypeError)
def test_copy(self):
a = T.scalar() # the a is for 'anonymous' (un-named).
x, s = T.scalars("xs")
a = tt.scalar() # the a is for 'anonymous' (un-named).
x, s = tt.scalars("xs")
f = function(
[
......@@ -298,11 +298,11 @@ class TestFunction:
assert f(1, 2) != g(1, 2) # they should not be equal anymore.
def test_copy_share_memory(self):
x = T.fscalar("x")
x = tt.fscalar("x")
# SharedVariable for tests, one of them has update
y = theano.shared(value=1)
z = theano.shared(value=2)
out = T.tanh((x + y + 2) / (x + z - 0.2) ** 2)
out = tt.tanh((x + y + 2) / (x + z - 0.2) ** 2)
# Test for different linkers
for mode in ["FAST_RUN", "FAST_COMPILE"]:
......@@ -321,7 +321,7 @@ class TestFunction:
l = [
val
for key, val in storage_map_cpy.items()
if key not in i_o_variables or isinstance(key, T.Constant)
if key not in i_o_variables or isinstance(key, tt.Constant)
]
for storage in l:
assert any([storage is s for s in ori_storages])
......@@ -333,10 +333,10 @@ class TestFunction:
assert here.data is there.data
def test_swap_SharedVariable(self):
i = T.iscalar()
i = tt.iscalar()
x_list = theano.shared(value=np.random.rand(10).astype(config.floatX))
x = T.scalar("x")
x = tt.scalar("x")
# SharedVariable for tests, one of them has update
y = theano.shared(value=1, name="y")
z = theano.shared(value=2, name="z")
......@@ -407,11 +407,11 @@ class TestFunction:
train_y = theano.shared(value=np.random.rand(10, 1).astype(config.floatX))
test_y = theano.shared(value=np.random.rand(10, 1).astype(config.floatX))
i = T.iscalar("index")
x = T.vector("x")
y = T.vector("y")
i = tt.iscalar("index")
x = tt.vector("x")
y = tt.vector("y")
# this formular has no sense but for a test
out = (T.sum(x) - y) ** 2
out = (tt.sum(x) - y) ** 2
train = theano.function(
[i],
out,
......@@ -428,8 +428,8 @@ class TestFunction:
assert in1.value is in2.value
def test_copy_delete_updates(self):
w = T.iscalar("w")
x = T.fscalar("x")
w = tt.iscalar("w")
x = tt.fscalar("x")
# SharedVariable for tests, one of them has update
y = theano.shared(value=1, name="y")
z = theano.shared(value=2, name="z")
......@@ -456,8 +456,8 @@ class TestFunction:
cpy = ori.copy(delete_updates=True)
def test_shared_state0(self):
a = T.scalar() # the a is for 'anonymous' (un-named).
x, s = T.scalars("xs")
a = tt.scalar() # the a is for 'anonymous' (un-named).
x, s = tt.scalars("xs")
f = function(
[
......@@ -484,8 +484,8 @@ class TestFunction:
assert g[s] == 0
def test_shared_state1(self):
a = T.scalar() # the a is for 'anonymous' (un-named).
x, s = T.scalars("xs")
a = tt.scalar() # the a is for 'anonymous' (un-named).
x, s = tt.scalars("xs")
f = function(
[
......@@ -508,8 +508,8 @@ class TestFunction:
assert g[s] == 4
def test_shared_state2(self):
a = T.scalar() # the a is for 'anonymous' (un-named).
x, s = T.scalars("xs")
a = tt.scalar() # the a is for 'anonymous' (un-named).
x, s = tt.scalars("xs")
f = function(
[
......@@ -538,7 +538,7 @@ class TestFunction:
# doc/topics/function.txt. If it does not pass anymore and yet the
# behavior is still intended the doc and the test should both be
# updated accordingly.
x, s = T.scalars("xs")
x, s = tt.scalars("xs")
inc = function([x, In(s, update=(s + x), value=10.0)], [])
dec = function(
[x, In(s, update=(s - x), value=inc.container[s], implicit=False)], []
......@@ -554,7 +554,7 @@ class TestFunction:
def test_constant_output(self):
# Test that if the output is a constant, we respect the theano memory interface
f = theano.function([], T.constant([4]))
f = theano.function([], tt.constant([4]))
# print f.maker.fgraph.toposort()
out = f()
assert (out == 4).all()
......@@ -565,7 +565,7 @@ class TestFunction:
assert (out2 == 4).all()
# Test that if the output is a constant and borrow, we respect the theano memory interface
f = theano.function([], Out(T.constant([4]), borrow=True))
f = theano.function([], Out(tt.constant([4]), borrow=True))
# print f.maker.fgraph.toposort()
out = f()
assert (out == 4).all()
......@@ -585,7 +585,7 @@ class TestFunction:
# either through a view-map or a destroy map. New tests should be added in the future
# when borrow=True is implemented.
a = T.dmatrix()
a = tt.dmatrix()
aval = np.random.rand(3, 3)
# when borrow=False, test that a destroy map cannot alias output to input
......@@ -599,7 +599,7 @@ class TestFunction:
assert not np.may_share_memory(aval, f(aval))
def test_borrow_output(self):
a = T.dmatrix()
a = tt.dmatrix()
f = function([a], Out(a, borrow=False))
o = np.ones((3, 3))
assert o is not f(o) # function no longer permits aliasing outputs to inputs
......@@ -626,15 +626,15 @@ class TestFunction:
assert np.all(four == 4)
def test_disconnected_input(self):
a = T.scalar("a")
v = T.vector("v")
a = tt.scalar("a")
v = tt.vector("v")
with pytest.raises(UnusedInputError):
function([a, v], v * 2)
function([a, v], v * 2, on_unused_input="ignore")
def test_masked_input(self):
m = T.matrix("m")
m = tt.matrix("m")
mt = m.T
mt.name = "m.T"
with pytest.raises(UnusedInputError):
......@@ -644,7 +644,7 @@ class TestFunction:
def test_givens_input_var(self):
# Ensure error is raised when trying to replace an input variable.
x = T.scalar("x")
x = tt.scalar("x")
y = x * 2
with pytest.raises(RuntimeError):
function([x], y, givens={x: x + 1})
......@@ -652,7 +652,7 @@ class TestFunction:
def test_free(self):
# Make test on free() function
x = T.vector("x")
x = tt.vector("x")
func = function([x], x + 1)
func.fn.allow_gc = False
func([1])
......@@ -673,7 +673,7 @@ class TestFunction:
# Check that default values are restored
# when an exception occurs in interactive mode.
a, b = T.dscalars("a", "b")
a, b = tt.dscalars("a", "b")
c = a + b
func = theano.function(
[theano.In(a, name="first"), theano.In(b, value=1, name="second")], c
......@@ -688,7 +688,7 @@ class TestFunction:
b = np.random.rand(5, 4)
s1 = theano.shared(b)
s2 = theano.shared(b)
x1 = T.vector()
x1 = tt.vector()
# Assert cases we should not check for aliased inputs
for d in [
......@@ -729,8 +729,8 @@ class TestFunction:
class TestPicklefunction:
def test_deepcopy(self):
a = T.scalar() # the a is for 'anonymous' (un-named).
x, s = T.scalars("xs")
a = tt.scalar() # the a is for 'anonymous' (un-named).
x, s = tt.scalars("xs")
f = function(
[
......@@ -785,8 +785,8 @@ class TestPicklefunction:
assert f(3) == g(3) # They should be in sync again.
def test_deepcopy_trust_input(self):
a = T.dscalar() # the a is for 'anonymous' (un-named).
x, s = T.dscalars("xs")
a = tt.dscalar() # the a is for 'anonymous' (un-named).
x, s = tt.dscalars("xs")
f = function(
[
......@@ -817,7 +817,7 @@ class TestPicklefunction:
g(2.0)
def test_output_keys(self):
x = T.vector()
x = tt.vector()
f = theano.function([x], {"vec": x ** 2})
o = f([2, 3, 4])
assert isinstance(o, dict)
......@@ -829,7 +829,7 @@ class TestPicklefunction:
def test_deepcopy_shared_container(self):
# Ensure that shared containers remain shared after a deep copy.
a, x = T.scalars("ax")
a, x = tt.scalars("ax")
h = function([In(a, value=0.0)], a)
f = function([x, In(a, value=h.container[a], implicit=True)], x + a)
......@@ -852,8 +852,8 @@ class TestPicklefunction:
assert fc[ac] == 2
def test_pickle(self):
a = T.scalar() # the a is for 'anonymous' (un-named).
x, s = T.scalars("xs")
a = tt.scalar() # the a is for 'anonymous' (un-named).
x, s = tt.scalars("xs")
f = function(
[
......@@ -900,15 +900,15 @@ class TestPicklefunction:
assert f(1, 2) != g(1, 2) # they should not be equal anymore.
def test_optimizations_preserved(self):
a = T.dvector() # the a is for 'anonymous' (un-named).
x = T.dvector("x")
s = T.dvector("s")
xm = T.dmatrix("x")
sm = T.dmatrix("s")
a = tt.dvector() # the a is for 'anonymous' (un-named).
x = tt.dvector("x")
s = tt.dvector("s")
xm = tt.dmatrix("x")
sm = tt.dmatrix("s")
f = function(
[a, x, s, xm, sm],
((a.T.T) * (T.dot(xm, (sm.T.T.T)) + x).T * (x / x) + s),
((a.T.T) * (tt.dot(xm, (sm.T.T.T)) + x).T * (x / x) + s),
)
old_default_mode = config.mode
old_default_opt = config.optimizer
......@@ -946,9 +946,9 @@ class TestPicklefunction:
assert [i.type for i in nf.outputs] == [i.type for i in ng.outputs]
def test_multiple_functions(self):
a = T.scalar() # the a is for 'anonymous' (un-named).
x, s = T.scalars("xs")
v = T.vector("v")
a = tt.scalar() # the a is for 'anonymous' (un-named).
x, s = tt.scalars("xs")
v = tt.vector("v")
# put in some inputs
list_of_things = [s, x, v]
......@@ -1045,10 +1045,10 @@ class TestPicklefunction:
b = np.random.rand(5, 4)
x = T.matrix()
x = tt.matrix()
y = theano.shared(b)
f = theano.function([x], T.dot(x, y))
f = theano.function([x], tt.dot(x, y))
from theano.compat import BytesIO
......@@ -1094,9 +1094,9 @@ class TestPicklefunction:
class SomethingToPickle(object):
def __init__(self):
a = T.scalar() # the a is for 'anonymous' (un-named).
x, s = T.scalars("xs")
v = T.vector("v")
a = tt.scalar() # the a is for 'anonymous' (un-named).
x, s = tt.scalars("xs")
v = tt.vector("v")
self.s = s
self.x = x
......@@ -1128,7 +1128,7 @@ def test_empty_givens_updates():
# Empty givens / updates dictionaries were not properly detected before,
# triggering useless crashes at compile time.
x = T.scalar()
x = tt.scalar()
y = x * 2
function([theano.In(x)], y, givens={})
function([theano.In(x)], y, updates={})
......@@ -1162,7 +1162,7 @@ def test_sync_update():
target=tests.gpuarray.config.test_ctx_name,
)
updates = [(w, w + np.asarray(0.001, "float32") * T.dot(x, x))]
updates = [(w, w + np.asarray(0.001, "float32") * tt.dot(x, x))]
f = theano.function([], updates=updates, mode=tests.gpuarray.config.mode_with_gpu)
assert len(f.maker.fgraph.apply_nodes) == 1
......
import pytest
import theano
import theano.tensor as T
import theano.tensor as tt
from theano.compile.mode import Mode, AddFeatureOptimizer
from theano.gof.toolbox import NoOutputFromInplace
......@@ -11,10 +11,10 @@ from theano.gof.toolbox import NoOutputFromInplace
not theano.config.cxx, reason="G++ not available, so we need to skip this test."
)
def test_no_output_from_implace():
x = T.matrix()
y = T.matrix()
a = T.dot(x, y)
b = T.tanh(a)
x = tt.matrix()
y = tt.matrix()
a = tt.dot(x, y)
b = tt.tanh(a)
# Ensure that the elemwise op that produces the output is inplace when
# using a mode that does not include the optimization
......
......@@ -5,7 +5,7 @@ Test compilation modes
import copy
import theano
import theano.tensor as T
import theano.tensor as tt
from theano.compile import Mode
......@@ -25,8 +25,8 @@ class TestBunchOfModes:
modes = predef_modes + [Mode(linker, "fast_run") for linker in linkers]
for mode in modes:
x = T.matrix()
y = T.vector()
x = tt.matrix()
y = tt.vector()
f = theano.function([x, y], x + y, mode=mode)
# test that it runs something
f([[1, 2], [3, 4]], [5, 6])
......
......@@ -2,23 +2,23 @@
This test is for testing the NanGuardMode.
"""
import logging
import pytest
import numpy as np
from theano.compile.nanguardmode import NanGuardMode
import theano
import theano.tensor as T
import theano.tensor as tt
from theano.compile.nanguardmode import NanGuardMode
def test_NanGuardMode():
# Tests if NanGuardMode is working by feeding in numpy.inf and numpy.nans
# intentionally. A working implementation should be able to capture all
# the abnormalties.
x = T.matrix()
x = tt.matrix()
w = theano.shared(np.random.randn(5, 7).astype(theano.config.floatX))
y = T.dot(x, w)
y = tt.dot(x, w)
fun = theano.function(
[x], y, mode=NanGuardMode(nan_is_error=True, inf_is_error=True)
......@@ -51,8 +51,8 @@ def test_NanGuardMode():
nana = np.tile(np.asarray(np.nan).astype(theano.config.floatX), (3, 4, 5))
biga = np.tile(np.asarray(1e20).astype(theano.config.floatX), (3, 4, 5))
x = T.tensor3()
y = x[:, T.arange(2), T.arange(2), None]
x = tt.tensor3()
y = x[:, tt.arange(2), tt.arange(2), None]
fun = theano.function(
[x], y, mode=NanGuardMode(nan_is_error=True, inf_is_error=True)
)
......
......@@ -4,7 +4,7 @@
import numpy as np
import theano
import theano.tensor as T
import theano.tensor as tt
from six.moves import StringIO
from theano.ifelse import ifelse
......@@ -23,10 +23,10 @@ class TestProfiling:
theano.config.profile_memory = True
theano.config.profiling.min_peak_memory = True
x = [T.fvector("val%i" % i) for i in range(3)]
x = [tt.fvector("val%i" % i) for i in range(3)]
z = []
z += [T.outer(x[i], x[i + 1]).sum(axis=1) for i in range(len(x) - 1)]
z += [tt.outer(x[i], x[i + 1]).sum(axis=1) for i in range(len(x) - 1)]
z += [x[i] + x[i + 1] for i in range(len(x) - 1)]
p = theano.ProfileStats(False, gpu_checks=False)
......@@ -79,10 +79,10 @@ class TestProfiling:
theano.config.profile = True
theano.config.profile_memory = True
a, b = T.scalars("a", "b")
x, y = T.scalars("x", "y")
a, b = tt.scalars("a", "b")
x, y = tt.scalars("x", "y")
z = ifelse(T.lt(a, b), x * 2, y * 2)
z = ifelse(tt.lt(a, b), x * 2, y * 2)
p = theano.ProfileStats(False, gpu_checks=False)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论