提交 2b281d22 authored 作者: Ian Goodfellow's avatar Ian Goodfellow

added deprecation warnings for module class

上级 99f22814
...@@ -10,7 +10,8 @@ from theano import gof ...@@ -10,7 +10,8 @@ from theano import gof
from theano.printing import pprint from theano.printing import pprint
import io, sys import io, sys
from theano.gof.python25 import any, all, defaultdict, partial from theano.gof.python25 import all
import warnings
from itertools import chain from itertools import chain
...@@ -18,7 +19,18 @@ import function_module as F ...@@ -18,7 +19,18 @@ import function_module as F
import mode as get_mode import mode as get_mode
#This module is imported by other parts of theano, and worse still, other
#parts of theano do "from module import *"
#So rather than printing out a warning if you import from this module, we
#must stick a warning inside all of the components
def deprecation_warning():
warnings.warn("theano modules are deprecated and will be removed in release 0.7",
stacklevel = 3)
def name_join(*args): def name_join(*args):
deprecation_warning()
""" """
Creates a string representation for the given names: Creates a string representation for the given names:
join('a', 'b', 'c') => 'a.b.c' join('a', 'b', 'c') => 'a.b.c'
...@@ -26,6 +38,7 @@ def name_join(*args): ...@@ -26,6 +38,7 @@ def name_join(*args):
return ".".join(arg for arg in args if arg) return ".".join(arg for arg in args if arg)
def name_split(sym, n=-1): def name_split(sym, n=-1):
deprecation_warning()
""" """
Gets the names from their joined representation Gets the names from their joined representation
split('a.b.c') => ['a', 'b', 'c'] split('a.b.c') => ['a', 'b', 'c']
...@@ -47,7 +60,9 @@ class Component(object): ...@@ -47,7 +60,9 @@ class Component(object):
Method, etc.) Method, etc.)
""" """
def __init__(self): def __init__(self, no_warn = False):
if not no_warn:
deprecation_warning()
self.__dict__['_name'] = '' self.__dict__['_name'] = ''
self.__dict__['parent'] = None self.__dict__['parent'] = None
...@@ -140,6 +155,7 @@ class _RComponent(Component): ...@@ -140,6 +155,7 @@ class _RComponent(Component):
""" """
def __init__(self, r): def __init__(self, r):
deprecation_warning()
super(_RComponent, self).__init__() super(_RComponent, self).__init__()
self.r = r self.r = r
# If self.owns_name is True, then the name of the variable # If self.owns_name is True, then the name of the variable
...@@ -172,6 +188,7 @@ class External(_RComponent): ...@@ -172,6 +188,7 @@ class External(_RComponent):
""" """
def allocate(self, memo): def allocate(self, memo):
deprecation_warning()
# nothing to allocate # nothing to allocate
return None return None
...@@ -201,6 +218,7 @@ class Member(_RComponent): ...@@ -201,6 +218,7 @@ class Member(_RComponent):
If the memo does not have a Container associated to this If the memo does not have a Container associated to this
Member's Variable, instantiates one and sets it in the memo. Member's Variable, instantiates one and sets it in the memo.
""" """
deprecation_warning()
r = self.r r = self.r
if memo and r in memo: if memo and r in memo:
return memo[r] return memo[r]
...@@ -283,6 +301,7 @@ class Method(Component): ...@@ -283,6 +301,7 @@ class Method(Component):
:type mode: None or any mode accepted by `compile.function` :type mode: None or any mode accepted by `compile.function`
""" """
deprecation_warning()
if updates is None: if updates is None:
updates = {} updates = {}
super(Method, self).__init__() super(Method, self).__init__()
...@@ -529,6 +548,7 @@ class CompositeInstance(object): ...@@ -529,6 +548,7 @@ class CompositeInstance(object):
""" """
def __init__(self, component, __items__): def __init__(self, component, __items__):
deprecation_warning()
# The Component that built this CompositeInstance # The Component that built this CompositeInstance
self.__dict__['component'] = component self.__dict__['component'] = component
# Some data structure indexable using [] # Some data structure indexable using []
...@@ -618,6 +638,7 @@ class Composite(Component): ...@@ -618,6 +638,7 @@ class Composite(Component):
""" """
Does allocation for each component in the composite. Does allocation for each component in the composite.
""" """
deprecation_warning()
for member in self.components(): for member in self.components():
member.allocate(memo) member.allocate(memo)
...@@ -668,6 +689,7 @@ class ComponentListInstance(CompositeInstance): ...@@ -668,6 +689,7 @@ class ComponentListInstance(CompositeInstance):
return len(self.__items__) return len(self.__items__)
def initialize(self, init): def initialize(self, init):
deprecation_warning()
for i, initv in enumerate(init): for i, initv in enumerate(init):
self[i] = initv self[i] = initv
...@@ -678,6 +700,7 @@ class ComponentList(Composite): ...@@ -678,6 +700,7 @@ class ComponentList(Composite):
""" """
def __init__(self, *_components): def __init__(self, *_components):
deprecation_warning()
super(ComponentList, self).__init__() super(ComponentList, self).__init__()
if len(_components) == 1 and isinstance(_components[0], (list, tuple)): if len(_components) == 1 and isinstance(_components[0], (list, tuple)):
_components = _components[0] _components = _components[0]
...@@ -763,6 +786,7 @@ class ComponentList(Composite): ...@@ -763,6 +786,7 @@ class ComponentList(Composite):
def default_initialize(self, init=None, **kwinit): def default_initialize(self, init=None, **kwinit):
deprecation_warning()
if init is None: if init is None:
init = {} init = {}
for k, initv in dict(init, **kwinit).iteritems(): for k, initv in dict(init, **kwinit).iteritems():
...@@ -795,6 +819,7 @@ class ComponentDictInstance(ComponentDictInstanceNoInit): ...@@ -795,6 +819,7 @@ class ComponentDictInstance(ComponentDictInstanceNoInit):
""" """
def initialize(self, init=None, **kwinit): def initialize(self, init=None, **kwinit):
deprecation_warning()
if init is None: if init is None:
init = {} init = {}
for k, initv in dict(init, **kwinit).iteritems(): for k, initv in dict(init, **kwinit).iteritems():
...@@ -806,6 +831,7 @@ class ComponentDict(Composite): ...@@ -806,6 +831,7 @@ class ComponentDict(Composite):
InstanceType = ComponentDictInstance # Type used by build() to make the instance InstanceType = ComponentDictInstance # Type used by build() to make the instance
def __init__(self, components=None, **kwcomponents): def __init__(self, components=None, **kwcomponents):
deprecation_warning()
if components is None: if components is None:
components = {} components = {}
super(ComponentDict, self).__init__() super(ComponentDict, self).__init__()
...@@ -872,7 +898,7 @@ class ComponentDict(Composite): ...@@ -872,7 +898,7 @@ class ComponentDict(Composite):
__autowrappers = [] __autowrappers = []
def register_wrapper(condition, wrapper): def register_wrapper(condition, wrapper, no_warn = False):
""" """
:type condition: function x -> bool :type condition: function x -> bool
...@@ -884,12 +910,15 @@ def register_wrapper(condition, wrapper): ...@@ -884,12 +910,15 @@ def register_wrapper(condition, wrapper):
:param wrapper: this function should convert `x` into an instance of :param wrapper: this function should convert `x` into an instance of
a Component subclass. a Component subclass.
""" """
if not no_warn:
deprecation_warning()
__autowrappers.append((condition, wrapper)) __autowrappers.append((condition, wrapper))
def wrapper(x): def wrapper(x):
"""Returns a wrapper function appropriate for `x` """Returns a wrapper function appropriate for `x`
Returns None if not appropriate wrapper is found Returns None if not appropriate wrapper is found
""" """
deprecation_warning()
for condition, wrap_fn in __autowrappers: for condition, wrap_fn in __autowrappers:
if condition(x): if condition(x):
return wrap_fn return wrap_fn
...@@ -905,6 +934,7 @@ def wrap(x): ...@@ -905,6 +934,7 @@ def wrap(x):
`Component.make` to fail. `Component.make` to fail.
""" """
deprecation_warning()
w = wrapper(x) w = wrapper(x)
if w is not None: if w is not None:
return w(x) return w(x)
...@@ -912,6 +942,7 @@ def wrap(x): ...@@ -912,6 +942,7 @@ def wrap(x):
return x return x
def dict_wrap(d): def dict_wrap(d):
deprecation_warning()
d_copy = {} d_copy = {}
for k,v in d.iteritems(): for k,v in d.iteritems():
d_copy[k]=wrap(v) d_copy[k]=wrap(v)
...@@ -919,28 +950,29 @@ def dict_wrap(d): ...@@ -919,28 +950,29 @@ def dict_wrap(d):
# Component -> itself # Component -> itself
register_wrapper(lambda x: isinstance(x, Component), register_wrapper(lambda x: isinstance(x, Component),
lambda x: x) lambda x: x, no_warn = True)
# Variable -> Member # Variable -> Member
register_wrapper(lambda x: isinstance(x, gof.Variable) and not x.owner, register_wrapper(lambda x: isinstance(x, gof.Variable) and not x.owner,
lambda x: Member(x)) lambda x: Member(x), no_warn = True)
# Variable -> External # Variable -> External
register_wrapper(lambda x: isinstance(x, gof.Variable) and x.owner, register_wrapper(lambda x: isinstance(x, gof.Variable) and x.owner,
lambda x: External(x)) lambda x: External(x), no_warn = True)
# [[Variable1], {Variable2}, Variable3...] -> ComponentList(Member(Variable1), Member(Variable2), ...) # [[Variable1], {Variable2}, Variable3...] -> ComponentList(Member(Variable1), Member(Variable2), ...)
register_wrapper(lambda x: isinstance(x, (list, tuple)) \ register_wrapper(lambda x: isinstance(x, (list, tuple)) \
and all(wrapper(r) is not None for r in x), and all(wrapper(r) is not None for r in x),
lambda x: ComponentList(*map(wrap, x))) lambda x: ComponentList(*map(wrap, x)), no_warn = True)
#{ "name1":{Component,Variable,list,tuple,dict},...} -> ComponentDict({Component,Variable,list,tuple,dict},...) #{ "name1":{Component,Variable,list,tuple,dict},...} -> ComponentDict({Component,Variable,list,tuple,dict},...)
register_wrapper(lambda x: isinstance(x, dict) \ register_wrapper(lambda x: isinstance(x, dict) \
and all(wrapper(r) is not None for r in x.itervalues()), and all(wrapper(r) is not None for r in x.itervalues()),
lambda x: ComponentDict(dict_wrap(x))) lambda x: ComponentDict(dict_wrap(x)),no_warn = True)
class Curry: class Curry:
def __init__(self, obj, name, arg): def __init__(self, obj, name, arg):
deprecation_warning()
self.obj = obj self.obj = obj
self.name = name self.name = name
self.meth = getattr(self.obj, self.name) self.meth = getattr(self.obj, self.name)
...@@ -989,6 +1021,7 @@ class Module(ComponentDict): ...@@ -989,6 +1021,7 @@ class Module(ComponentDict):
InstanceType = ModuleInstance # By default, we use build ModuleInstance InstanceType = ModuleInstance # By default, we use build ModuleInstance
def __init__(self, *args, **kw): def __init__(self, *args, **kw):
deprecation_warning()
super(Module, self).__init__(*args, **kw) super(Module, self).__init__(*args, **kw)
self.__dict__["local_attr"]={} self.__dict__["local_attr"]={}
self.__dict__["_components"]={} self.__dict__["_components"]={}
...@@ -1187,6 +1220,7 @@ def func_to_mod(f): ...@@ -1187,6 +1220,7 @@ def func_to_mod(f):
output <= f(**kwinit) output <= f(**kwinit)
""" """
deprecation_warning()
def make(**kwinit): def make(**kwinit):
m = Module() m = Module()
outputs = f(**kwinit) outputs = f(**kwinit)
......
...@@ -404,7 +404,7 @@ def _obj_is_wrappable_as_tensor(x): ...@@ -404,7 +404,7 @@ def _obj_is_wrappable_as_tensor(x):
def _wrap_tensor_into_member(x): def _wrap_tensor_into_member(x):
return compile.module.Member(constant(x)) return compile.module.Member(constant(x))
compile.module.register_wrapper(_obj_is_wrappable_as_tensor, compile.module.register_wrapper(_obj_is_wrappable_as_tensor,
_wrap_tensor_into_member) _wrap_tensor_into_member, no_warn = True)
if int(config.tensor.cmp_sloppy) > 1: if int(config.tensor.cmp_sloppy) > 1:
......
...@@ -129,7 +129,7 @@ class RandomStreams(Component, raw_random.RandomStreamsBase): ...@@ -129,7 +129,7 @@ class RandomStreams(Component, raw_random.RandomStreamsBase):
for more details. for more details.
""" """
super(RandomStreams, self).__init__() super(RandomStreams, self).__init__(no_warn = True)
self.random_state_variables = [] self.random_state_variables = []
self.default_instance_seed = seed self.default_instance_seed = seed
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论