提交 fbfb0081 authored 作者: David Warde-Farley's avatar David Warde-Farley

Fix mutable default arguments (config.foo values).

config.blah should not be used as a default argument as the values can change during execution, but default arguments are only evaluated at function definition time. The correct idiom is to use a sentinel value such as None and then check for it, and read out the config value in the function body.
上级 fb8087c2
...@@ -17,8 +17,11 @@ purpose of it is to hack it to investigate what your own particular program is d ...@@ -17,8 +17,11 @@ purpose of it is to hack it to investigate what your own particular program is d
predefined_optimizers) predefined_optimizers)
class StepMode(Mode): class StepMode(Mode):
def __init__(self, linker=config.linker, optimizer=config.optimizer): def __init__(self, linker=None, optimizer=None):
if linker is None:
linker = config.linker
if optimizer is None:
optimizer = config.optimizer
def blah(i, node, th): def blah(i, node, th):
# This function will be run for each node in your compiled program. # This function will be run for each node in your compiled program.
# here you can inspect all the values as they are computed, # here you can inspect all the values as they are computed,
......
...@@ -245,7 +245,11 @@ class Mode(object): ...@@ -245,7 +245,11 @@ class Mode(object):
predefined_modes. predefined_modes.
""" """
def __init__(self, linker = config.linker, optimizer = config.optimizer): def __init__(self, linker=None, optimizer=None):
if linker is None:
linker = config.linker
if optimizer is None:
optimizer = config.optimizer
self.__setstate__((linker, optimizer)) self.__setstate__((linker, optimizer))
#self.provided_optimizer - typically the `optimizer` arg. But if the `optimizer` arg is #self.provided_optimizer - typically the `optimizer` arg. But if the `optimizer` arg is
# keyword corresponding to a predefined Query, then this stores the query # keyword corresponding to a predefined Query, then this stores the query
......
...@@ -82,9 +82,13 @@ class Profile_Maker(FunctionMaker): ...@@ -82,9 +82,13 @@ class Profile_Maker(FunctionMaker):
return ret return ret
class ProfileMode(Mode): class ProfileMode(Mode):
def __init__(self, linker=config.linker, optimizer=config.optimizer): def __init__(self, linker=None, optimizer=None):
message="" if linker is None:
profile_stats={} linker = config.linker
if optimizer is None:
optimizer = config.optimizer
message = ""
profile_stats = {}
self.__setstate__((linker, self.__setstate__((linker,
optimizer, optimizer,
message, message,
......
...@@ -2662,22 +2662,28 @@ def zeros_like(model, dtype=None): ...@@ -2662,22 +2662,28 @@ def zeros_like(model, dtype=None):
return fill(model, constant(0.0, dtype=dtype)) return fill(model, constant(0.0, dtype=dtype))
def zeros(shape, dtype=config.floatX): def zeros(shape, dtype=None):
""" """
Create a Tensor filled with zeros, closer to Numpy's syntax than ``alloc``. Create a Tensor filled with zeros, closer to Numpy's syntax than ``alloc``.
""" """
if dtype is None:
dtype = config.floatX
return alloc(numpy.array(0, dtype=dtype), *shape) return alloc(numpy.array(0, dtype=dtype), *shape)
def ones(shape, dtype=config.floatX): def ones(shape, dtype=None):
""" """
Create a Tensor filled with ones, closer to Numpy's syntax than ``alloc``. Create a Tensor filled with ones, closer to Numpy's syntax than ``alloc``.
""" """
if dtype is None:
dtype = config.floatX
return alloc(numpy.array(1, dtype=dtype), *shape) return alloc(numpy.array(1, dtype=dtype), *shape)
class Eye(gof.Op): class Eye(gof.Op):
def __init__(self, dtype=config.floatX): def __init__(self, dtype=None):
if dtype is None:
dtype = config.floatX
self.dtype = dtype self.dtype = dtype
def make_node(self, n, m, k): def make_node(self, n, m, k):
...@@ -2702,7 +2708,9 @@ class Eye(gof.Op): ...@@ -2702,7 +2708,9 @@ class Eye(gof.Op):
return hash(self.dtype) ^ hash(type(self)) return hash(self.dtype) ^ hash(type(self))
def eye(n, m=None, k=0, dtype=config.floatX): def eye(n, m=None, k=0, dtype=None):
if dtype is None:
dtype = config.floatX
if m is None: if m is None:
m = n m = n
localop = Eye(dtype) localop = Eye(dtype)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论