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

Misc. style and format updates

上级 e842fd19
from itertools import count
import pickle
import pytest
import numpy as np
from itertools import count
from theano import sparse, shared, tensor
from theano.gof.graph import (
Apply,
......@@ -58,10 +60,6 @@ class MyOp(Op):
MyOp = MyOp()
##########
# inputs #
##########
class TestInputs:
def test_inputs(self):
......@@ -77,11 +75,6 @@ class TestInputs:
assert i == [r1, r2, r5], i
#############
# as_string #
#############
class X:
def leaf_formatter(self, leaf):
return str(leaf.type)
......@@ -126,11 +119,6 @@ class TestStr(X):
assert self.str(node2.inputs, node2.outputs) == ["MyOp(R3, R3)"]
#########
# clone #
#########
class TestClone(X):
def test_accurate(self):
r1, r2 = MyVariable(1), MyVariable(2)
......@@ -186,11 +174,6 @@ class TestClone(X):
assert i[0] is c1 and o[0] is c1
############
# toposort #
############
def prenode(obj):
if isinstance(obj, Variable):
if obj.owner:
......@@ -258,11 +241,6 @@ class TestToposort:
assert all == [o0]
#################
# is_same_graph #
#################
class TestIsSameGraph:
def check(self, expected, debug=True):
"""
......@@ -401,11 +379,6 @@ class TestIsSameGraph:
)
################
# eval #
################
class TestEval:
def setup_method(self):
self.x, self.y = tensor.scalars("x", "y")
......@@ -421,9 +394,6 @@ class TestEval:
), "temporary functions must not be serialized"
################
# autoname #
################
class TestAutoName:
def test_auto_name(self):
# Get counter value
......
import itertools
import numpy as np
import pytest
import numpy as np
import theano
from theano import tensor
from theano.scan_module.scan_utils import equal_computations, map_variables
from theano.tensor.type_other import NoneConst
......@@ -17,9 +21,6 @@ def test_equal_compuations():
assert equal_computations(max_argmax1, max_argmax2)
#################
# map_variables #
#################
class TestMapVariables:
......
import pytest
import numpy as np
import theano
from theano.tensor.extra_ops import (
......@@ -30,15 +32,15 @@ from theano.tensor.extra_ops import (
ravel_multi_index,
RavelMultiIndex,
)
from theano import tensor as T
from theano import config, tensor, function
from theano import tensor as tt
from theano import config, function
from tests import unittest_tools as utt
def test_cpu_contiguous():
a = T.fmatrix("a")
i = T.iscalar("i")
a = tt.fmatrix("a")
i = tt.iscalar("i")
a_val = np.asarray(np.random.rand(4, 5), dtype="float32")
f = theano.function([a, i], cpu_contiguous(a.reshape((5, 4))[::i]))
topo = f.maker.fgraph.toposort()
......@@ -57,8 +59,8 @@ class TestSearchsortedOp(utt.InferShapeTester):
self.op_class = SearchsortedOp
self.op = SearchsortedOp()
self.x = T.vector("x")
self.v = T.tensor3("v")
self.x = tt.vector("x")
self.v = tt.tensor3("v")
self.a = 30 * np.random.random(50).astype(config.floatX)
self.b = 30 * np.random.random((8, 10, 5)).astype(config.floatX)
......@@ -71,7 +73,7 @@ class TestSearchsortedOp(utt.InferShapeTester):
f(self.a[self.idx_sorted], self.b),
)
sorter = T.vector("sorter", dtype="int32")
sorter = tt.vector("sorter", dtype="int32")
f = theano.function(
[self.x, self.v, sorter],
self.x.searchsorted(self.v, sorter=sorter, side="right"),
......@@ -90,14 +92,14 @@ class TestSearchsortedOp(utt.InferShapeTester):
searchsorted(self.x, self.v, side="asdfa")
def test_searchsortedOp_on_no_1d_inp(self):
no_1d = T.dmatrix("no_1d")
no_1d = tt.dmatrix("no_1d")
with pytest.raises(ValueError):
searchsorted(no_1d, self.v)
with pytest.raises(ValueError):
searchsorted(self.x, self.v, sorter=no_1d)
def test_searchsortedOp_on_float_sorter(self):
sorter = T.vector("sorter", dtype="float32")
sorter = tt.vector("sorter", dtype="float32")
with pytest.raises(TypeError):
searchsorted(self.x, self.v, sorter=sorter)
......@@ -107,7 +109,7 @@ class TestSearchsortedOp(utt.InferShapeTester):
compatible_types += ("int64",)
# 'uint8', 'uint16', 'uint32', 'uint64')
for dtype in compatible_types:
sorter = T.vector("sorter", dtype=dtype)
sorter = tt.vector("sorter", dtype=dtype)
f = theano.function(
[self.x, self.v, sorter],
searchsorted(self.x, self.v, sorter=sorter),
......@@ -136,7 +138,7 @@ class TestSearchsortedOp(utt.InferShapeTester):
)
# Test parameter ``sorter``
sorter = T.vector("sorter", dtype="int32")
sorter = tt.vector("sorter", dtype="int32")
self._compile_and_check(
[self.x, self.v, sorter],
[searchsorted(self.x, self.v, sorter=sorter)],
......@@ -165,7 +167,7 @@ class TestCumOp(utt.InferShapeTester):
self.op = CumOp()
def test_cum_op(self):
x = T.tensor3("x")
x = tt.tensor3("x")
a = np.random.random((3, 5, 2)).astype(config.floatX)
# Test axis out of bounds
......@@ -190,7 +192,7 @@ class TestCumOp(utt.InferShapeTester):
assert np.allclose(np.cumprod(a, axis=axis), p)
def test_infer_shape(self):
x = T.tensor3("x")
x = tt.tensor3("x")
a = np.random.random((3, 5, 2)).astype(config.floatX)
# Test axis=None
......@@ -212,7 +214,7 @@ class TestCumOp(utt.InferShapeTester):
class TestBinCount(utt.InferShapeTester):
def test_bincountFn(self):
w = T.vector("w")
w = tt.vector("w")
def ref(data, w=None, minlength=None):
size = int(data.max() + 1)
......@@ -238,7 +240,7 @@ class TestBinCount(utt.InferShapeTester):
"uint32",
"uint64",
):
x = T.vector("x", dtype=dtype)
x = tt.vector("x", dtype=dtype)
a = np.random.randint(1, 51, size=(25)).astype(dtype)
weights = np.random.random((25,)).astype(config.floatX)
......@@ -269,7 +271,7 @@ class TestDiffOp(utt.InferShapeTester):
self.op = DiffOp()
def test_diffOp(self):
x = T.matrix("x")
x = tt.matrix("x")
a = np.random.random((30, 50)).astype(config.floatX)
f = theano.function([x], diff(x))
......@@ -281,7 +283,7 @@ class TestDiffOp(utt.InferShapeTester):
assert np.allclose(np.diff(a, n=k, axis=axis), g(a))
def test_infer_shape(self):
x = T.matrix("x")
x = tt.matrix("x")
a = np.random.random((30, 50)).astype(config.floatX)
self._compile_and_check([x], [self.op(x)], [a], self.op_class)
......@@ -293,14 +295,14 @@ class TestDiffOp(utt.InferShapeTester):
)
def test_grad(self):
x = T.vector("x")
x = tt.vector("x")
a = np.random.random(50).astype(config.floatX)
theano.function([x], T.grad(T.sum(diff(x)), x))
theano.function([x], tt.grad(tt.sum(diff(x)), x))
utt.verify_grad(self.op, [a])
for k in range(TestDiffOp.nb):
theano.function([x], T.grad(T.sum(diff(x, n=k)), x))
theano.function([x], tt.grad(tt.sum(diff(x, n=k)), x))
utt.verify_grad(DiffOp(n=k), [a], eps=7e-3)
......@@ -319,7 +321,7 @@ class TestSqueeze(utt.InferShapeTester):
def test_op(self):
for shape, broadcast in zip(self.shape_list, self.broadcast_list):
data = np.random.random(size=shape).astype(theano.config.floatX)
variable = tensor.TensorType(theano.config.floatX, broadcast)()
variable = tt.TensorType(theano.config.floatX, broadcast)()
f = theano.function([variable], self.op(variable))
......@@ -332,10 +334,10 @@ class TestSqueeze(utt.InferShapeTester):
def test_infer_shape(self):
for shape, broadcast in zip(self.shape_list, self.broadcast_list):
data = np.random.random(size=shape).astype(theano.config.floatX)
variable = tensor.TensorType(theano.config.floatX, broadcast)()
variable = tt.TensorType(theano.config.floatX, broadcast)()
self._compile_and_check(
[variable], [self.op(variable)], [data], tensor.DimShuffle, warn=False
[variable], [self.op(variable)], [data], tt.DimShuffle, warn=False
)
def test_grad(self):
......@@ -348,7 +350,7 @@ class TestSqueeze(utt.InferShapeTester):
# same as test_op, but use a_theano_var.squeeze.
for shape, broadcast in zip(self.shape_list, self.broadcast_list):
data = np.random.random(size=shape).astype(theano.config.floatX)
variable = tensor.TensorType(theano.config.floatX, broadcast)()
variable = tt.TensorType(theano.config.floatX, broadcast)()
f = theano.function([variable], variable.squeeze())
......@@ -359,17 +361,17 @@ class TestSqueeze(utt.InferShapeTester):
assert np.allclose(tested, expected)
def test_axis(self):
variable = tensor.TensorType(theano.config.floatX, [False, True, False])()
variable = tt.TensorType(theano.config.floatX, [False, True, False])()
res = squeeze(variable, axis=1)
assert res.broadcastable == (False, False)
variable = tensor.TensorType(theano.config.floatX, [False, True, False])()
variable = tt.TensorType(theano.config.floatX, [False, True, False])()
res = squeeze(variable, axis=(1,))
assert res.broadcastable == (False, False)
variable = tensor.TensorType(theano.config.floatX, [False, True, False, True])()
variable = tt.TensorType(theano.config.floatX, [False, True, False, True])()
res = squeeze(variable, axis=(1, 3))
assert res.broadcastable == (False, False)
......@@ -426,12 +428,12 @@ class TestRepeatOp(utt.InferShapeTester):
def test_repeatOp(self):
for ndim in [1, 3]:
x = T.TensorType(config.floatX, [False] * ndim)()
x = tt.TensorType(config.floatX, [False] * ndim)()
a = np.random.random((10,) * ndim).astype(config.floatX)
for axis in self._possible_axis(ndim):
for dtype in tensor.integer_dtypes:
r_var = T.scalar(dtype=dtype)
for dtype in tt.integer_dtypes:
r_var = tt.scalar(dtype=dtype)
r = np.asarray(3, dtype=dtype)
if dtype == "uint64" or (
dtype in self.numpy_unsupported_dtypes and r_var.ndim == 1
......@@ -442,7 +444,7 @@ class TestRepeatOp(utt.InferShapeTester):
f = theano.function([x, r_var], repeat(x, r_var, axis=axis))
assert np.allclose(np.repeat(a, r, axis=axis), f(a, r))
r_var = T.vector(dtype=dtype)
r_var = tt.vector(dtype=dtype)
if axis is None:
r = np.random.randint(1, 6, size=a.size).astype(dtype)
else:
......@@ -483,16 +485,16 @@ class TestRepeatOp(utt.InferShapeTester):
@pytest.mark.slow
def test_infer_shape(self):
for ndim in [1, 3]:
x = T.TensorType(config.floatX, [False] * ndim)()
x = tt.TensorType(config.floatX, [False] * ndim)()
shp = (np.arange(ndim) + 1) * 3
a = np.random.random(shp).astype(config.floatX)
for axis in self._possible_axis(ndim):
for dtype in ["int8", "uint8", "uint64"]:
r_var = T.scalar(dtype=dtype)
r_var = tt.scalar(dtype=dtype)
r = np.asarray(3, dtype=dtype)
if dtype in self.numpy_unsupported_dtypes:
r_var = T.vector(dtype=dtype)
r_var = tt.vector(dtype=dtype)
with pytest.raises(TypeError):
repeat(x, r_var)
else:
......@@ -503,7 +505,7 @@ class TestRepeatOp(utt.InferShapeTester):
self.op_class,
)
r_var = T.vector(dtype=dtype)
r_var = tt.vector(dtype=dtype)
if axis is None:
r = np.random.randint(1, 6, size=a.size).astype(dtype)
elif a.size > 0:
......@@ -528,7 +530,7 @@ class TestRepeatOp(utt.InferShapeTester):
utt.verify_grad(lambda x: RepeatOp(axis=axis)(x, 3), [a])
def test_broadcastable(self):
x = T.TensorType(config.floatX, [False, True, False])()
x = tt.TensorType(config.floatX, [False, True, False])()
r = RepeatOp(axis=1)(x, 2)
assert r.broadcastable == (False, False, False)
r = RepeatOp(axis=1)(x, 1)
......@@ -544,7 +546,7 @@ class TestBartlett(utt.InferShapeTester):
self.op = bartlett
def test_perform(self):
x = tensor.lscalar()
x = tt.lscalar()
f = function([x], self.op(x))
M = np.random.randint(3, 51, size=())
assert np.allclose(f(M), np.bartlett(M))
......@@ -554,7 +556,7 @@ class TestBartlett(utt.InferShapeTester):
assert np.allclose(f(b[0]), np.bartlett(b[0]))
def test_infer_shape(self):
x = tensor.lscalar()
x = tt.lscalar()
self._compile_and_check(
[x], [self.op(x)], [np.random.randint(3, 51, size=())], self.op_class
)
......@@ -572,8 +574,8 @@ class TestFillDiagonal(utt.InferShapeTester):
self.op = fill_diagonal
def test_perform(self):
x = tensor.matrix()
y = tensor.scalar()
x = tt.matrix()
y = tt.scalar()
f = function([x, y], fill_diagonal(x, y))
for shp in [(8, 8), (5, 8), (8, 5)]:
a = np.random.rand(*shp).astype(config.floatX)
......@@ -583,10 +585,10 @@ class TestFillDiagonal(utt.InferShapeTester):
assert np.allclose(np.diag(out), val)
assert (out == val).sum() == min(a.shape)
# test for 3d tensor
# test for 3dtt
a = np.random.rand(3, 3, 3).astype(config.floatX)
x = tensor.tensor3()
y = tensor.scalar()
x = tt.tensor3()
y = tt.scalar()
f = function([x, y], fill_diagonal(x, y))
val = np.cast[config.floatX](np.random.rand() + 10)
out = f(a, val)
......@@ -612,9 +614,9 @@ class TestFillDiagonal(utt.InferShapeTester):
)
def test_infer_shape(self):
z = tensor.dtensor3()
x = tensor.dmatrix()
y = tensor.dscalar()
z = tt.dtensor3()
x = tt.dmatrix()
y = tt.dscalar()
self._compile_and_check(
[x, y],
[self.op(x, y)],
......@@ -641,9 +643,9 @@ class TestFillDiagonalOffset(utt.InferShapeTester):
self.op = fill_diagonal_offset
def test_perform(self):
x = tensor.matrix()
y = tensor.scalar()
z = tensor.iscalar()
x = tt.matrix()
y = tt.scalar()
z = tt.iscalar()
f = function([x, y, z], fill_diagonal_offset(x, y, z))
for test_offset in (-5, -4, -1, 0, 1, 4, 5):
......@@ -688,9 +690,9 @@ class TestFillDiagonalOffset(utt.InferShapeTester):
)
def test_infer_shape(self):
x = tensor.dmatrix()
y = tensor.dscalar()
z = tensor.iscalar()
x = tt.dmatrix()
y = tt.dscalar()
z = tt.iscalar()
for test_offset in (-5, -4, -1, 0, 1, 4, 5):
self._compile_and_check(
[x, y, z],
......
......@@ -53,7 +53,7 @@ from tests.tensor.test_basic import inplace_func, rand, randint_ranged
class TestSubtensor(utt.OptimizationTestMixin):
"""
This is build in a way that allow to reuse it to test the equivalent gpu op.
This is designed to be sub-classed (e.g. by the GPU tests).
"""
def setup_method(self):
......
......@@ -197,7 +197,7 @@ def dot(l, r):
def get_scalar_constant_value(v):
"""return the constant scalar(0-D) value underlying variable `v`
"""Return the constant scalar (i.e. 0-D) value underlying variable `v`.
If v is the output of dimshuffles, fills, allocs, rebroadcasts, cast
this function digs through them.
......@@ -208,7 +208,8 @@ def get_scalar_constant_value(v):
tensor.basic.NotScalarConstantError.
"""
# Is it necessary to test for presence of theano.sparse at runtime?
if "sparse" in globals() and isinstance(v.type, sparse.SparseType):
sparse = globals().get("sparse")
if sparse and isinstance(v.type, sparse.SparseType):
if v.owner is not None and isinstance(v.owner.op, sparse.CSM):
data = v.owner.inputs[0]
return tensor.get_scalar_constant_value(data)
......
......@@ -18,7 +18,7 @@ from collections import OrderedDict
from six import integer_types
from theano import gof
from theano.gof import Op, Apply, ParamsType, Variable
def register_view_op_c_code(type, code, version=()):
......@@ -39,7 +39,7 @@ def register_view_op_c_code(type, code, version=()):
ViewOp.c_code_and_version[type] = (code, version)
class ViewOp(gof.Op):
class ViewOp(Op):
"""
Returns an inplace view of the input. Used internally by Theano.
......@@ -54,7 +54,7 @@ class ViewOp(gof.Op):
_f16_ok = True
def make_node(self, x):
return gof.Apply(self, [x], [x.type()])
return Apply(self, [x], [x.type()])
def perform(self, node, inp, out):
(x,) = inp
......@@ -151,7 +151,7 @@ def register_deep_copy_op_c_code(typ, code, version=()):
DeepCopyOp.c_code_and_version[typ] = (code, version)
class DeepCopyOp(gof.Op):
class DeepCopyOp(Op):
# Mapping from Type to C code (and version) to use.
# In the C code, the name of the input variable is %(iname)s,
# the output variable is %(oname)s.
......@@ -165,7 +165,7 @@ class DeepCopyOp(gof.Op):
pass
def make_node(self, x):
return gof.Apply(self, [x], [x.type()])
return Apply(self, [x], [x.type()])
def perform(self, node, args, outs):
if hasattr(args[0], "copy"):
......@@ -235,7 +235,7 @@ def register_shape_c_code(type, code, version=()):
Shape.c_code_and_version[type] = (code, version)
class Shape(gof.Op):
class Shape(Op):
"""
L{Op} to return the shape of a matrix.
......@@ -260,7 +260,7 @@ class Shape(gof.Op):
# This will fail at execution time.
if not isinstance(x, theano.Variable):
x = theano.tensor.as_tensor_variable(x)
return gof.Apply(self, [x], [theano.tensor.lvector()])
return Apply(self, [x], [theano.tensor.lvector()])
def perform(self, node, inp, out_):
(x,) = inp
......@@ -329,7 +329,7 @@ shape = Shape()
_shape = shape # was used in the past, now use shape directly.
class Shape_i(gof.Op):
class Shape_i(Op):
"""
L{Op} to return the shape of a matrix.
......@@ -369,7 +369,7 @@ class Shape_i(gof.Op):
# using params.
@property
def params_type(self):
return gof.ParamsType(i=theano.scalar.basic.int64)
return ParamsType(i=theano.scalar.basic.int64)
def __str__(self):
return "%s{%i}" % (self.__class__.__name__, self.i)
......@@ -540,7 +540,7 @@ def load_back(mod, name):
return obj
class FromFunctionOp(gof.Op):
class FromFunctionOp(Op):
"""
Build a basic Theano Op around a function.
......@@ -666,7 +666,7 @@ def register_rebroadcast_c_code(typ, code, version=()):
Rebroadcast.c_code_and_version[typ] = (code, version)
class Rebroadcast(gof.Op):
class Rebroadcast(Op):
"""
Change the input's broadcastable fields in some predetermined way.
......@@ -737,7 +737,7 @@ class Rebroadcast(gof.Op):
self.axis.get(i, b) for i, b in enumerate(x.type.broadcastable)
]
)
return gof.Apply(self, [x], [t()])
return Apply(self, [x], [t()])
def perform(self, node, inp, out_):
(x,) = inp
......@@ -848,7 +848,7 @@ def register_specify_shape_c_code(typ, code, version=(), c_support_code_apply=No
SpecifyShape.c_code_and_version[typ] = (code, version, c_support_code_apply)
class SpecifyShape(gof.Op):
class SpecifyShape(Op):
"""
L{Op} that puts into the graph the user-provided shape.
......@@ -876,14 +876,14 @@ class SpecifyShape(gof.Op):
_f16_ok = True
def make_node(self, x, shape):
if not isinstance(x, gof.Variable):
if not isinstance(x, Variable):
x = theano.tensor.as_tensor_variable(x)
shape = theano.tensor.as_tensor_variable(shape)
assert shape.ndim == 1
assert shape.dtype in theano.tensor.integer_dtypes
if isinstance(shape, theano.tensor.TensorConstant):
assert shape.data.size == x.ndim
return gof.Apply(self, [x, shape], [x.type()])
return Apply(self, [x, shape], [x.type()])
def perform(self, node, inp, out_):
x, shape = inp
......
from functools import partial
from collections import OrderedDict
import sys
import copy
import time
import inspect
import numpy as np
import theano
from functools import partial
from collections import OrderedDict
from six.moves import StringIO
import theano
from theano import config
from theano.gof import graph
from theano.gof.graph import (
io_toposort,
)
class AlreadyThere(Exception):
......@@ -312,7 +316,7 @@ class Bookkeeper(Feature):
FunctionGraph is initially populated, this is where you should
run checks on the initial contents of the FunctionGraph.
"""
for node in graph.io_toposort(fgraph.inputs, fgraph.outputs):
for node in io_toposort(fgraph.inputs, fgraph.outputs):
self.on_import(fgraph, node, "on_attach")
def on_detach(self, fgraph):
......@@ -320,7 +324,7 @@ class Bookkeeper(Feature):
Should remove any dynamically added functionality
that it installed into the function_graph
"""
for node in graph.io_toposort(fgraph.inputs, fgraph.outputs):
for node in io_toposort(fgraph.inputs, fgraph.outputs):
self.on_prune(fgraph, node, "Bookkeeper.detach")
......
......@@ -62,19 +62,16 @@ from collections import OrderedDict
from six import integer_types
from theano import tensor, scalar
from theano import gof, tensor, scalar
from theano.tensor import opt, get_scalar_constant_value, Alloc, AllocEmpty
from theano import gof
from theano.compile import optdb
from theano.compile.function_module import deep_copy_op
from theano.gof import toolbox, DestroyHandler, InconsistencyError
from theano.gof.opt import Optimizer
from theano.gof.opt import pre_constant_merge, pre_greedy_local_optimizer
from theano.gof.opt import Optimizer, pre_constant_merge, pre_greedy_local_optimizer
from theano.scan_module import scan_op
from theano.scan_module import scan_utils
from theano.scan_module.scan_utils import equal_computations, scan_args
from theano.scan_module import scan_op, scan_utils
__docformat__ = "restructedtext en"
__authors__ = (
......
......@@ -4,15 +4,15 @@ import theano
from collections.abc import Collection
from theano.tensor import basic
from theano.tensor import nlinalg # noqa
from theano import gof, scalar
from theano.gof import Generic, ParamsType, EnumList
from theano import gradient
from theano.gradient import DisconnectedType, disconnected_type
from theano.scalar import int32 as int_t
tensor = basic
from theano.gof import Op, Apply, Generic, ParamsType, EnumList
from theano.tensor import basic, nlinalg
from theano.scalar import int32 as int_t, upcast
from theano.gradient import (
DisconnectedType,
disconnected_type,
_float_zeros_like,
grad_undefined,
)
class CpuContiguous(theano.Op):
......@@ -197,8 +197,8 @@ class SearchsortedOp(theano.Op):
else:
x, v = inputs
x_grad = gradient._float_zeros_like(x)
v_grad = gradient._float_zeros_like(v)
x_grad = _float_zeros_like(x)
v_grad = _float_zeros_like(v)
if num_ins == 3:
return [x_grad, v_grad, disconnected_type()]
else:
......@@ -332,7 +332,7 @@ class CumOp(theano.Op):
def infer_shape(self, node, shapes):
if self.axis is None:
return [(tensor.prod(shapes[0]),)] # Flatten
return [(basic.prod(shapes[0]),)] # Flatten
return shapes
......@@ -658,7 +658,7 @@ class RepeatOp(theano.Op):
x = basic.as_tensor_variable(x)
repeats = basic.as_tensor_variable(repeats)
if repeats.dtype not in tensor.integer_dtypes:
if repeats.dtype not in basic.integer_dtypes:
raise TypeError("repeats.dtype must be an integer.")
# Some dtypes are not supported by numpy's implementation of repeat.
......@@ -784,7 +784,7 @@ def repeat(x, repeats, axis=None):
.. versionadded:: 0.6
"""
repeats = tensor.as_tensor_variable(repeats)
repeats = basic.as_tensor_variable(repeats)
if repeats.ndim > 1:
raise ValueError("The dimension of repeats should not exceed 1.")
......@@ -827,22 +827,22 @@ def repeat(x, repeats, axis=None):
# After the original tensor is duplicated along the additional
# dimension, we reshape it to the expected output shape, and
# return the output z.
z = tensor.alloc(x.dimshuffle(*dims_), *shape_).reshape(shape)
z = basic.alloc(x.dimshuffle(*dims_), *shape_).reshape(shape)
return z
class Bartlett(gof.Op):
class Bartlett(Op):
# See function bartlett for docstring
__props__ = ()
def make_node(self, M):
M = tensor.as_tensor_variable(M)
M = basic.as_tensor_variable(M)
if M.ndim != 0:
raise TypeError("%s only works on scalar input" % self.__class__.__name__)
elif M.dtype not in theano.tensor.integer_dtypes:
# dtype is a theano attribute here
raise TypeError("%s only works on integer input" % self.__class__.__name__)
return gof.Apply(self, [M], [tensor.dvector()])
return Apply(self, [M], [basic.dvector()])
def perform(self, node, inputs, out_):
M = inputs[0]
......@@ -851,7 +851,7 @@ class Bartlett(gof.Op):
def infer_shape(self, node, in_shapes):
temp = node.inputs[0]
M = tensor.switch(tensor.lt(temp, 0), tensor.cast(0, temp.dtype), temp)
M = basic.switch(basic.lt(temp, 0), basic.cast(0, temp.dtype), temp)
return [[M]]
def grad(self, inputs, output_grads):
......@@ -889,7 +889,7 @@ def bartlett(M):
return bartlett_(M)
class FillDiagonal(gof.Op):
class FillDiagonal(Op):
# See function fill_diagonal for docstring
__props__ = ()
......@@ -897,8 +897,8 @@ class FillDiagonal(gof.Op):
return [in_shapes[0]]
def make_node(self, a, val):
a = tensor.as_tensor_variable(a)
val = tensor.as_tensor_variable(val)
a = basic.as_tensor_variable(a)
val = basic.as_tensor_variable(val)
if a.ndim < 2:
raise TypeError(
"%s: first parameter must have at least"
......@@ -908,13 +908,13 @@ class FillDiagonal(gof.Op):
raise TypeError(
"%s: second parameter must be a scalar" % self.__class__.__name__
)
val = tensor.cast(val, dtype=scalar.upcast(a.dtype, val.dtype))
val = basic.cast(val, dtype=upcast(a.dtype, val.dtype))
if val.dtype != a.dtype:
raise TypeError(
"%s: type of second parameter must be the same as"
" the first's" % self.__class__.__name__
)
return gof.Apply(self, [a, val], [a.type()])
return Apply(self, [a, val], [a.type()])
def perform(self, node, inputs, output_storage):
a = inputs[0].copy()
......@@ -950,7 +950,7 @@ class FillDiagonal(gof.Op):
)
wr_a = fill_diagonal(grad, 0) # valid for any number of dimensions
# diag is only valid for matrices
wr_val = theano.tensor.nlinalg.diag(grad).sum()
wr_val = nlinalg.diag(grad).sum()
return [wr_a, wr_val]
......@@ -991,7 +991,7 @@ def fill_diagonal(a, val):
return fill_diagonal_(a, val)
class FillDiagonalOffset(gof.Op):
class FillDiagonalOffset(Op):
# See function fill_diagonal_offset for docstring
__props__ = ()
......@@ -999,9 +999,9 @@ class FillDiagonalOffset(gof.Op):
return [in_shapes[0]]
def make_node(self, a, val, offset):
a = tensor.as_tensor_variable(a)
val = tensor.as_tensor_variable(val)
offset = tensor.as_tensor_variable(offset)
a = basic.as_tensor_variable(a)
val = basic.as_tensor_variable(val)
offset = basic.as_tensor_variable(offset)
if a.ndim != 2:
raise TypeError(
"%s: first parameter must have exactly"
......@@ -1015,7 +1015,7 @@ class FillDiagonalOffset(gof.Op):
raise TypeError(
"%s: third parameter must be a scalar" % self.__class__.__name__
)
val = tensor.cast(val, dtype=scalar.upcast(a.dtype, val.dtype))
val = basic.cast(val, dtype=upcast(a.dtype, val.dtype))
if val.dtype != a.dtype:
raise TypeError(
"%s: type of second parameter must be the same"
......@@ -1028,7 +1028,7 @@ class FillDiagonalOffset(gof.Op):
% self.__class__.__name__
)
return gof.Apply(self, [a, val, offset], [a.type()])
return Apply(self, [a, val, offset], [a.type()])
def perform(self, node, inputs, output_storage):
a = inputs[0].copy()
......@@ -1097,7 +1097,7 @@ class FillDiagonalOffset(gof.Op):
wr_val = grad.flatten()[start:end:step].sum()
wr_offset = theano.gradient.grad_undefined(
wr_offset = grad_undefined(
self,
2,
offset,
......@@ -1291,7 +1291,7 @@ class Unique(theano.Op):
self.axis = None
class UnravelIndex(gof.Op):
class UnravelIndex(Op):
__props__ = ("order",)
def __init__(self, order="C"):
......@@ -1313,7 +1313,7 @@ class UnravelIndex(gof.Op):
if dims.ndim != 1:
raise TypeError("dims must be a 1D array")
return gof.Apply(
return Apply(
self,
[indices, dims],
[
......@@ -1372,7 +1372,7 @@ def unravel_index(indices, dims, order="C"):
return tuple(res)
class RavelMultiIndex(gof.Op):
class RavelMultiIndex(Op):
__props__ = ("mode", "order")
def __init__(self, mode="raise", order="C"):
......@@ -1397,7 +1397,7 @@ class RavelMultiIndex(gof.Op):
if dims.ndim != 1:
raise TypeError("dims must be a 1D array")
return gof.Apply(
return Apply(
self,
multi_index + [dims],
[
......
......@@ -18,16 +18,32 @@ import theano
from functools import reduce
from collections import defaultdict
from six import integer_types
from theano import gof
from theano.gof import opt, InconsistencyError, TopoOptimizer, graph
from theano.gof import Variable, Constant
from theano.gof.opt import copy_stack_trace, in2out
from six import integer_types, StringIO
from theano import (
gof,
config,
scalar,
compile,
) # to register the optimizer built by this file
from theano.gof import (
opt,
InconsistencyError,
TopoOptimizer,
graph,
Variable,
Constant,
toolbox,
)
from theano.gof.opt import (
copy_stack_trace,
in2out,
Optimizer,
pre_constant_merge,
pre_greedy_local_optimizer,
)
from theano.gof.utils import MethodNotDefined
from theano.gradient import DisconnectedType
from theano import config
from theano.tensor.elemwise import Elemwise, DimShuffle
from theano.tensor.subtensor import (
get_idx_list,
......@@ -43,10 +59,8 @@ from theano.tensor.subtensor import (
advanced_inc_subtensor1,
)
from theano.tensor.sort import TopKOp
from theano import scalar
from theano.scalar import basic
from theano.tensor import basic as T
from theano import compile # to register the optimizer built by this file
from theano.compile.ops import Shape, Shape_i
from theano.tensor.type import (
values_eq_approx_remove_inf,
......@@ -54,8 +68,6 @@ from theano.tensor.type import (
values_eq_approx_remove_inf_nan,
)
from theano.gof.opt import Optimizer, pre_constant_merge, pre_greedy_local_optimizer
from theano.gof import toolbox
from theano.tensor.basic import (
Alloc,
get_scalar_constant_value,
......@@ -64,12 +76,9 @@ from theano.tensor.basic import (
NotScalarConstantError,
Reshape,
)
from six import StringIO
_logger = logging.getLogger("theano.tensor.opt")
# Utilities
def _fill_chain(new_out, orig_inputs):
for i in orig_inputs:
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论