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

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

上级 ac7b5225
差异被折叠。
差异被折叠。
import numpy as np import numpy as np
import pytest import pytest
import theano import theano
import theano.tensor as tt
from theano import tensor as T
from theano.tensor import fft from theano.tensor import fft
from tests import unittest_tools as utt from tests import unittest_tools as utt
...@@ -31,7 +32,7 @@ class TestFFT: ...@@ -31,7 +32,7 @@ class TestFFT:
def test_1Drfft(self): def test_1Drfft(self):
inputs_val = np.random.random((1, N)).astype(theano.config.floatX) inputs_val = np.random.random((1, N)).astype(theano.config.floatX)
x = T.matrix("x") x = tt.matrix("x")
rfft = fft.rfft(x) rfft = fft.rfft(x)
f_rfft = theano.function([x], rfft) f_rfft = theano.function([x], rfft)
res_rfft = f_rfft(inputs_val) res_rfft = f_rfft(inputs_val)
......
import numpy as np import time
import six.moves.cPickle as pickle import six.moves.cPickle as pickle
import numpy as np
import theano import theano
from theano import tensor as T import theano.tensor as tt
import time
def test_no_reuse(): def test_no_reuse():
x = T.lvector() x = tt.lvector()
y = T.lvector() y = tt.lvector()
f = theano.function([x, y], x + y) f = theano.function([x, y], x + y)
# provide both inputs in the first call # provide both inputs in the first call
...@@ -22,7 +24,7 @@ def test_no_reuse(): ...@@ -22,7 +24,7 @@ def test_no_reuse():
def test_gc_never_pickles_temporaries(): def test_gc_never_pickles_temporaries():
x = T.dvector() x = tt.dvector()
r = x r = x
for i in range(2): # TODO: 30 causes like LONG compilation due to MERGE for i in range(2): # TODO: 30 causes like LONG compilation due to MERGE
...@@ -105,7 +107,7 @@ def test_merge_opt_runtime(): ...@@ -105,7 +107,7 @@ def test_merge_opt_runtime():
# #
# Ironically, there is actually no merging to do in this graph. # Ironically, there is actually no merging to do in this graph.
x = T.dvector() x = tt.dvector()
r = x r = x
for i in range(50): for i in range(50):
r = r + r / 10 r = r + r / 10
......
import numpy as np import numpy as np
import theano.tensor.basic as tt
from theano.gof.type import Type from theano.gof.type import Type
from theano.gof.graph import Variable, Apply from theano.gof.graph import Variable, Apply
from theano.gof.op import Op from theano.gof.op import Op
from theano.gof.opt import MergeOptimizer from theano.gof.opt import MergeOptimizer
from theano.gof.fg import FunctionGraph as Env from theano.gof.fg import FunctionGraph
import theano.tensor.basic as T
def as_variable(x): def is_variable(x):
if not isinstance(x, Variable): if not isinstance(x, Variable):
raise TypeError("not a Variable", x) raise TypeError("not a Variable", x)
return x return x
...@@ -30,7 +31,7 @@ class MyOp(Op): ...@@ -30,7 +31,7 @@ class MyOp(Op):
self.x = x self.x = x
def make_node(self, *inputs): def make_node(self, *inputs):
inputs = list(map(as_variable, inputs)) inputs = list(map(is_variable, inputs))
for input in inputs: for input in inputs:
if not isinstance(input.type, MyType): if not isinstance(input.type, MyType):
raise Exception("Error 1") raise Exception("Error 1")
...@@ -65,9 +66,9 @@ def test_merge_with_weird_eq(): ...@@ -65,9 +66,9 @@ def test_merge_with_weird_eq():
# numpy arrays don't compare equal like other python objects # numpy arrays don't compare equal like other python objects
# SCALAR CASE # SCALAR CASE
x = T.constant(np.asarray(1), name="x") x = tt.constant(np.asarray(1), name="x")
y = T.constant(np.asarray(1), name="y") y = tt.constant(np.asarray(1), name="y")
g = Env([x, y], [x + y]) g = FunctionGraph([x, y], [x + y])
MergeOptimizer().optimize(g) MergeOptimizer().optimize(g)
assert len(g.apply_nodes) == 1 assert len(g.apply_nodes) == 1
...@@ -77,9 +78,9 @@ def test_merge_with_weird_eq(): ...@@ -77,9 +78,9 @@ def test_merge_with_weird_eq():
# NONSCALAR CASE # NONSCALAR CASE
# This was created to test TensorConstantSignature # This was created to test TensorConstantSignature
x = T.constant(np.ones(5), name="x") x = tt.constant(np.ones(5), name="x")
y = T.constant(np.ones(5), name="y") y = tt.constant(np.ones(5), name="y")
g = Env([x, y], [x + y]) g = FunctionGraph([x, y], [x + y])
MergeOptimizer().optimize(g) MergeOptimizer().optimize(g)
assert len(g.apply_nodes) == 1 assert len(g.apply_nodes) == 1
......
""" """
This is a minimized version of the mlp.py in the tutorial. We removed stuff that make this mlp don't work. This is a minimized version of the mlp.py in the tutorial. We removed stuff
But this test a bug that we saw. This bug made the Shape_i object not being lifted, that caused the CrossentropySoftmax... op not being inserted. that make this mlp don't work. But this test a bug that we saw. This bug made
the Shape_i object not being lifted, that caused the CrossentropySoftmax... op
not being inserted.
""" """
__docformat__ = "restructedtext en" __docformat__ = "restructedtext en"
from collections import OrderedDict from collections import OrderedDict
import numpy as np import numpy as np
import theano import theano
import theano.tensor as T import theano.tensor as tt
def gen_data(): def gen_data():
...@@ -49,7 +49,7 @@ def gen_data(): ...@@ -49,7 +49,7 @@ def gen_data():
# floats it doesn't make sense) therefore instead of returning # floats it doesn't make sense) therefore instead of returning
# ``shared_y`` we will have to cast it to int. This little hack # ``shared_y`` we will have to cast it to int. This little hack
# lets ous get around this issue # lets ous get around this issue
return shared_x, T.cast(shared_y, "int32") return shared_x, tt.cast(shared_y, "int32")
test_set_x, test_set_y = shared_dataset(test_set) test_set_x, test_set_y = shared_dataset(test_set)
valid_set_x, valid_set_y = shared_dataset(valid_set) valid_set_x, valid_set_y = shared_dataset(valid_set)
...@@ -96,11 +96,11 @@ class LogisticRegression(object): ...@@ -96,11 +96,11 @@ class LogisticRegression(object):
) )
# compute vector of class-membership probabilities in symbolic form # compute vector of class-membership probabilities in symbolic form
self.p_y_given_x = T.nnet.softmax(T.dot(input, self.W)) self.p_y_given_x = tt.nnet.softmax(tt.dot(input, self.W))
# compute prediction as class whose probability is maximal in # compute prediction as class whose probability is maximal in
# symbolic form # symbolic form
self.y_pred = T.argmax(self.p_y_given_x, axis=1) self.y_pred = tt.argmax(self.p_y_given_x, axis=1)
# parameters of the model # parameters of the model
self.params = [self.W] self.params = [self.W]
...@@ -128,11 +128,11 @@ class LogisticRegression(object): ...@@ -128,11 +128,11 @@ class LogisticRegression(object):
# LP[T.arange(y.shape[0]),y] is a vector v containing [LP[0,y[0]], LP[1,y[1]], LP[2,y[2]], ..., LP[n-1,y[n-1]]] # LP[T.arange(y.shape[0]),y] is a vector v containing [LP[0,y[0]], LP[1,y[1]], LP[2,y[2]], ..., LP[n-1,y[n-1]]]
# and T.mean(LP[T.arange(y.shape[0]),y]) is the mean (across minibatch examples) of the elements in v, # and T.mean(LP[T.arange(y.shape[0]),y]) is the mean (across minibatch examples) of the elements in v,
# i.e., the mean log-likelihood across the minibatch. # i.e., the mean log-likelihood across the minibatch.
return T.log(self.p_y_given_x[T.arange(y.shape[0]), y]) return tt.log(self.p_y_given_x[tt.arange(y.shape[0]), y])
class HiddenLayer(object): class HiddenLayer(object):
def __init__(self, rng, input, n_in, n_out, activation=T.tanh, name_prefix=""): def __init__(self, rng, input, n_in, n_out, activation=tt.tanh, name_prefix=""):
""" """
Typical hidden layer of a MLP: units are fully-connected and have Typical hidden layer of a MLP: units are fully-connected and have
sigmoidal activation function. Weight matrix W is of shape (n_in,n_out) sigmoidal activation function. Weight matrix W is of shape (n_in,n_out)
...@@ -174,7 +174,7 @@ class HiddenLayer(object): ...@@ -174,7 +174,7 @@ class HiddenLayer(object):
) )
self.W = theano.shared(value=W_values, name=name_prefix + "W") self.W = theano.shared(value=W_values, name=name_prefix + "W")
self.output = T.dot(input, self.W) self.output = tt.dot(input, self.W)
# parameters of the model # parameters of the model
self.params = [self.W] self.params = [self.W]
...@@ -222,7 +222,7 @@ class MLP(object): ...@@ -222,7 +222,7 @@ class MLP(object):
input=input, input=input,
n_in=n_in, n_in=n_in,
n_out=n_hidden, n_out=n_hidden,
activation=T.tanh, activation=tt.tanh,
name_prefix="hid_", name_prefix="hid_",
) )
...@@ -284,9 +284,9 @@ def test_mlp(): ...@@ -284,9 +284,9 @@ def test_mlp():
# print '... building the model' # print '... building the model'
# allocate symbolic variables for the data # allocate symbolic variables for the data
index = T.lscalar() # index to a [mini]batch index = tt.lscalar() # index to a [mini]batch
x = T.matrix("x") # the data is presented as rasterized images x = tt.matrix("x") # the data is presented as rasterized images
y = T.ivector("y") # the labels are presented as 1D vector of y = tt.ivector("y") # the labels are presented as 1D vector of
# [int] labels # [int] labels
rng = np.random.RandomState(1234) rng = np.random.RandomState(1234)
...@@ -303,7 +303,7 @@ def test_mlp(): ...@@ -303,7 +303,7 @@ def test_mlp():
# the resulting gradients will be stored in a list gparams # the resulting gradients will be stored in a list gparams
gparams = [] gparams = []
for param in classifier.params: for param in classifier.params:
gparam = T.grad(cost, param) gparam = tt.grad(cost, param)
gparams.append(gparam) gparams.append(gparam)
# Some optimizations needed are tagged with 'fast_run' # Some optimizations needed are tagged with 'fast_run'
...@@ -312,7 +312,7 @@ def test_mlp(): ...@@ -312,7 +312,7 @@ def test_mlp():
updates2 = OrderedDict() updates2 = OrderedDict()
updates2[classifier.hiddenLayer.params[0]] = T.grad( updates2[classifier.hiddenLayer.params[0]] = tt.grad(
cost, classifier.hiddenLayer.params[0] cost, classifier.hiddenLayer.params[0]
) )
train_model = theano.function( train_model = theano.function(
...@@ -328,7 +328,7 @@ def test_mlp(): ...@@ -328,7 +328,7 @@ def test_mlp():
# theano.printing.debugprint(train_model, print_type=True) # theano.printing.debugprint(train_model, print_type=True)
assert any( assert any(
[ [
isinstance(i.op, T.nnet.CrossentropySoftmax1HotWithBiasDx) isinstance(i.op, tt.nnet.CrossentropySoftmax1HotWithBiasDx)
for i in train_model.maker.fgraph.toposort() for i in train_model.maker.fgraph.toposort()
] ]
) )
...@@ -348,11 +348,7 @@ def test_mlp(): ...@@ -348,11 +348,7 @@ def test_mlp():
# theano.printing.debugprint(train_model, print_type=True) # theano.printing.debugprint(train_model, print_type=True)
assert any( assert any(
[ [
isinstance(i.op, T.nnet.CrossentropySoftmax1HotWithBiasDx) isinstance(i.op, tt.nnet.CrossentropySoftmax1HotWithBiasDx)
for i in train_model.maker.fgraph.toposort() for i in train_model.maker.fgraph.toposort()
] ]
) )
if __name__ == "__main__":
test_mlp()
This source diff could not be displayed because it is too large. You can view the blob instead.
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论