提交 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:
......
......@@ -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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论