提交 45d20c3f authored 作者: James Bergstra's avatar James Bergstra

If 0'd out Filler and Repeat Ops for a lack of documentation, testing and

internal use. I created a new Alloc() Op that is used for fill-lifting Optimizations.
上级 8f9c55c7
...@@ -1678,7 +1678,10 @@ def zeros_like(model): ...@@ -1678,7 +1678,10 @@ def zeros_like(model):
#return Zeros(model.type.ndim)(shape(model)) #return Zeros(model.type.ndim)(shape(model))
return fill(model, constant(0.0, dtype=model.type.dtype)) return fill(model, constant(0.0, dtype=model.type.dtype))
class Filler(gof.Op): if 0:
## COMMENTED OUT FEB 17 2010
## TODO (DOCUMENT AND WRITE TESTS) OR DELETE
class Filler(gof.Op):
"""WRITEME""" """WRITEME"""
def __init__(self, value, ndim, dtype = 'float64'): def __init__(self, value, ndim, dtype = 'float64'):
self.value = value self.value = value
...@@ -1712,26 +1715,76 @@ class Filler(gof.Op): ...@@ -1712,26 +1715,76 @@ class Filler(gof.Op):
def __hash__(self): def __hash__(self):
return hash(self.ndim) ^ hash(self.dtype) return hash(self.ndim) ^ hash(self.dtype)
Zeros = partial(Filler, 0) Zeros = partial(Filler, 0)
"""WRITEME""" """WRITEME"""
Ones = partial(Filler, 1) Ones = partial(Filler, 1)
"""WRITEME""" """WRITEME"""
@constructor @constructor
def zero(): def zero():
""" """
Return a scalar zero, e.g. for initializing sums. Return a scalar zero, e.g. for initializing sums.
""" """
return Zeros(0)([]) return Zeros(0)([])
@constructor @constructor
def one(): def one():
"""WRITEME""" """WRITEME"""
return Ones(0)([]) return Ones(0)([])
pprint.assign(lambda pstate, r: r.owner and isinstance(r.owner.op, Filler) and r.owner.op.value == 0, printing.FunctionPrinter('zeros')) pprint.assign(lambda pstate, r: r.owner and isinstance(r.owner.op, Filler) and r.owner.op.value == 0, printing.FunctionPrinter('zeros'))
pprint.assign(lambda pstate, r: r.owner and isinstance(r.owner.op, Filler) and r.owner.op.value == 1, printing.FunctionPrinter('ones')) pprint.assign(lambda pstate, r: r.owner and isinstance(r.owner.op, Filler) and r.owner.op.value == 1, printing.FunctionPrinter('ones'))
class Alloc(gof.Op):
"""Create a Tensor from an initial value and a desired shape
alloc(value, shape0, shape1, ..., shapeN)
Returns an N-dimensional tensor initialized by `value` using something equivalent to
>>> z = numpy.zeros(shape, value.dtype)
>>> z += value
The result has N dimensions, has the dtype of `value` and is obtained by broadcasting value
over the output ndarray.
This Op is used to replace fill() during optimizations because after shapes are lifted,
the first argument to fill can often be pruned from the graph.
"""
def __init__(self, dtype):
self.dtype = dtype
def __eq__(self, other):
return type(self) == type(other) and self.dtype == other.dtype
def __hash__(self):
return hash(type(self)) ^ hash(self.dtype)
def __str__(self):
return '%s{%s}' % (self.__class__.__name__, self.dtype)
def make_node(self, value, *shape):
v = as_tensor_variable(value)
sh = [as_tensor_variable(s) for s in shape]
bcast = []
for s in sh:
if s.type.dtype[:3] not in ('int', 'uin'):
raise TypeError('Shape arguments must be integers', s)
# if s is constant 1, then we're broadcastable in that dim
bcast.append(isinstance(s, TensorConstant) and (s.data == 1))
otype = TensorType(dtype=self.dtype, broadcastable=bcast)
return gof.Apply(self, [v]+sh, [otype()])
def perform(self, node, inputs, (out,)):
v = inputs[0]
sh = tuple([int(i) for i in inputs[1:]])
if out[0] is None or out[0].shape != sh:
out[0] = numpy.zeros(sh, dtype=self.dtype)
out[0][...] += v # broadcast v to fill us up
def grad(self, inputs, (gout,)):
return [None for i in inputs]
@_redefine(elemwise.Elemwise(scal.identity)) @_redefine(elemwise.Elemwise(scal.identity))
def tensor_copy(a): def tensor_copy(a):
...@@ -1851,7 +1904,10 @@ def var(input, axis = None): ...@@ -1851,7 +1904,10 @@ def var(input, axis = None):
#return the mean sqr #return the mean sqr
return mean(centered_input**2, axis) return mean(centered_input**2, axis)
class Repeat(gof.Op): if 0:
## COMMENTED OUT FEB 17 2010
## TODO (DOCUMENT AND WRITE TESTS) OR DELETE
class Repeat(gof.Op):
def make_node(self, input, repeats, axis): def make_node(self, input, repeats, axis):
assert isinstance(input.type, TensorType) assert isinstance(input.type, TensorType)
...@@ -1877,7 +1933,7 @@ class Repeat(gof.Op): ...@@ -1877,7 +1933,7 @@ class Repeat(gof.Op):
def grad(self, (input, repeats, axis), (gout, )): def grad(self, (input, repeats, axis), (gout, )):
return add.grad((input, gout), (gout,))[:1] return add.grad((input, gout), (gout,))[:1]
repeat = Repeat() repeat = Repeat()
class Default(gof.Op): class Default(gof.Op):
""" """
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论