提交 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,60 +1678,113 @@ def zeros_like(model): ...@@ -1678,60 +1678,113 @@ 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"""
def __init__(self, value, ndim, dtype = 'float64'):
self.value = value
self.ndim = ndim
self.dtype = dtype
self.type = TensorType(dtype = dtype,
broadcastable = (False,)*ndim)
def make_node(self, dims):
dims = as_tensor_variable(dims)
return gof.Apply(self, [dims], [self.type()])
def perform(self, node, (dims,), (out,)):
if out[0] is not None:
out[0].resize(dims, refcheck = 0)
out[0].fill(self.value)
else:
if self.value == 0:
out[0] = numpy.zeros(dims, dtype = self.dtype)
elif self.value == 1:
out[0] = numpy.ones(dims, dtype = self.dtype)
else:
out[0] = numpy.ones(dims, dtype = self.dtype) * self.value
def grad(self, (dims,), (gout,)):
return None,
def __eq__(self, other):
return type(self) == type(other) and self.ndim == other.ndim and self.dtype == other.dtype
def __hash__(self):
return hash(self.ndim) ^ hash(self.dtype)
Zeros = partial(Filler, 0)
"""WRITEME"""
Ones = partial(Filler, 1)
"""WRITEME""" """WRITEME"""
def __init__(self, value, ndim, dtype = 'float64'):
self.value = value
self.ndim = ndim
self.dtype = dtype
self.type = TensorType(dtype = dtype,
broadcastable = (False,)*ndim)
def make_node(self, dims): @constructor
dims = as_tensor_variable(dims) def zero():
return gof.Apply(self, [dims], [self.type()]) """
Return a scalar zero, e.g. for initializing sums.
"""
return Zeros(0)([])
def perform(self, node, (dims,), (out,)): @constructor
if out[0] is not None: def one():
out[0].resize(dims, refcheck = 0) """WRITEME"""
out[0].fill(self.value) return Ones(0)([])
else:
if self.value == 0:
out[0] = numpy.zeros(dims, dtype = self.dtype)
elif self.value == 1:
out[0] = numpy.ones(dims, dtype = self.dtype)
else:
out[0] = numpy.ones(dims, dtype = self.dtype) * self.value
def grad(self, (dims,), (gout,)): pprint.assign(lambda pstate, r: r.owner and isinstance(r.owner.op, Filler) and r.owner.op.value == 0, printing.FunctionPrinter('zeros'))
return None, pprint.assign(lambda pstate, r: r.owner and isinstance(r.owner.op, Filler) and r.owner.op.value == 1, printing.FunctionPrinter('ones'))
def __eq__(self, other): class Alloc(gof.Op):
return type(self) == type(other) and self.ndim == other.ndim and self.dtype == other.dtype """Create a Tensor from an initial value and a desired shape
def __hash__(self): alloc(value, shape0, shape1, ..., shapeN)
return hash(self.ndim) ^ hash(self.dtype)
Zeros = partial(Filler, 0) Returns an N-dimensional tensor initialized by `value` using something equivalent to
"""WRITEME""" >>> z = numpy.zeros(shape, value.dtype)
>>> z += value
Ones = partial(Filler, 1) The result has N dimensions, has the dtype of `value` and is obtained by broadcasting value
"""WRITEME""" over the output ndarray.
@constructor This Op is used to replace fill() during optimizations because after shapes are lifted,
def zero(): the first argument to fill can often be pruned from the graph.
"""
Return a scalar zero, e.g. for initializing sums.
""" """
return Zeros(0)([]) def __init__(self, dtype):
self.dtype = dtype
@constructor def __eq__(self, other):
def one(): return type(self) == type(other) and self.dtype == other.dtype
"""WRITEME"""
return Ones(0)([]) 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]
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'))
@_redefine(elemwise.Elemwise(scal.identity)) @_redefine(elemwise.Elemwise(scal.identity))
def tensor_copy(a): def tensor_copy(a):
...@@ -1851,33 +1904,36 @@ def var(input, axis = None): ...@@ -1851,33 +1904,36 @@ 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
def make_node(self, input, repeats, axis): ## TODO (DOCUMENT AND WRITE TESTS) OR DELETE
assert isinstance(input.type, TensorType) class Repeat(gof.Op):
assert repeats.type == iscalar
assert axis.type == iscalar def make_node(self, input, repeats, axis):
broadcastable = [] assert isinstance(input.type, TensorType)
for i,x in enumerate(input.broadcastable): assert repeats.type == iscalar
if i==axis: assert axis.type == iscalar
broadcastable += [False] broadcastable = []
else: for i,x in enumerate(input.broadcastable):
broadcastable += [x] if i==axis:
broadcastable += [False]
type = TensorType(dtype = input.type.dtype, broadcastable = \ else:
broadcastable) broadcastable += [x]
#backport
#type = TensorType(dtype = input.type.dtype, type = TensorType(dtype = input.type.dtype, broadcastable = \
# broadcastable = [False if i==axis else x for i, x in enumerate(input.broadcastable)]) broadcastable)
return gof.Apply(self, [inputs, repeats, axis], [type()]) #backport
#type = TensorType(dtype = input.type.dtype,
def perform(self, node, (input, repeats, axis), (out, )): # broadcastable = [False if i==axis else x for i, x in enumerate(input.broadcastable)])
out[0] = numpy.repeat(input, repeats, axis) return gof.Apply(self, [inputs, repeats, axis], [type()])
def grad(self, (input, repeats, axis), (gout, )): def perform(self, node, (input, repeats, axis), (out, )):
return add.grad((input, gout), (gout,))[:1] out[0] = numpy.repeat(input, repeats, axis)
repeat = Repeat() def grad(self, (input, repeats, axis), (gout, )):
return add.grad((input, gout), (gout,))[:1]
repeat = Repeat()
class Default(gof.Op): class Default(gof.Op):
""" """
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论