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

PEP3113 compliance for theano.sandbox, theano.sparse.sandbox, and…

PEP3113 compliance for theano.sandbox, theano.sparse.sandbox, and theano/scalar/basic_scipy.py. Now only sparse/basic.py and scalar/basic.py to do.
上级 032c5b14
...@@ -42,9 +42,12 @@ class HostFromGpu(Op): ...@@ -42,9 +42,12 @@ class HostFromGpu(Op):
if not isinstance(x.type, CudaNdarrayType): if not isinstance(x.type, CudaNdarrayType):
raise TypeError(x) raise TypeError(x)
return Apply(self, [x], [tensor.TensorType(dtype=x.dtype, broadcastable=x.broadcastable)()]) return Apply(self, [x], [tensor.TensorType(dtype=x.dtype, broadcastable=x.broadcastable)()])
def perform(self, node, (x,), (z,)): def perform(self, node, inp, out):
x, = inp
z, = out
z[0] = numpy.asarray(x) z[0] = numpy.asarray(x)
def grad(self, inputs, (gz,)): def grad(self, inputs, grads):
gz, = grads
return [gpu_from_host(gz)] return [gpu_from_host(gz)]
def infer_shape(self, node, xshp): def infer_shape(self, node, xshp):
return xshp return xshp
...@@ -61,9 +64,12 @@ class GpuFromHost(Op): ...@@ -61,9 +64,12 @@ class GpuFromHost(Op):
if not isinstance(x.type, tensor.TensorType): if not isinstance(x.type, tensor.TensorType):
raise TypeError(x) raise TypeError(x)
return Apply(self, [x], [CudaNdarrayType(broadcastable=x.broadcastable, dtype=x.dtype)()]) return Apply(self, [x], [CudaNdarrayType(broadcastable=x.broadcastable, dtype=x.dtype)()])
def perform(self, node, (x,), (z,)): def perform(self, node, inp, out):
x, = inp
z, = out
z[0] = type_support_filter(theano._asarray(x, dtype='float32'), tuple([0]*x.ndim), 0, z[0]) z[0] = type_support_filter(theano._asarray(x, dtype='float32'), tuple([0]*x.ndim), 0, z[0])
def grad(self, inputs, (gz,)): def grad(self, inputs, grads):
gz, = grads
return [host_from_gpu(gz)] return [host_from_gpu(gz)]
def infer_shape(self, node, xshp): def infer_shape(self, node, xshp):
return xshp return xshp
...@@ -245,7 +251,9 @@ class GpuDimShuffle(Op): ...@@ -245,7 +251,9 @@ class GpuDimShuffle(Op):
def __str__(self): def __str__(self):
return "GpuDimShuffle{%s}" % ",".join(str(x) for x in self.new_order) return "GpuDimShuffle{%s}" % ",".join(str(x) for x in self.new_order)
def c_code(self, node, name, (input,), (res,), sub): def c_code(self, node, name, inp, out, sub):
input, = inp
res, = out
basename = input + '__view_or_copy' basename = input + '__view_or_copy'
nd_in = len(self.input_broadcastable) nd_in = len(self.input_broadcastable)
...@@ -395,10 +403,14 @@ class GpuSum(Op): ...@@ -395,10 +403,14 @@ class GpuSum(Op):
o_broadcast = [x.type.broadcastable[i] for i in xrange(x.type.ndim) if not self.reduce_mask[i]] o_broadcast = [x.type.broadcastable[i] for i in xrange(x.type.ndim) if not self.reduce_mask[i]]
return Apply(self, [x], [CudaNdarrayType(o_broadcast)()]) return Apply(self, [x], [CudaNdarrayType(o_broadcast)()])
def perform(self, node, (x,), (z,)): def perform(self, node, inp, out):
x, = inp
z, = out
z[0] = x.reduce_sum(self.reduce_mask) z[0] = x.reduce_sum(self.reduce_mask)
def c_code(self, node, name, (x,), (z,), sub): def c_code(self, node, name, inp, out, sub):
x, = inp
z, = out
nd_in = node.inputs[0].type.ndim nd_in = node.inputs[0].type.ndim
nd_out = node.outputs[0].type.ndim nd_out = node.outputs[0].type.ndim
...@@ -1672,7 +1684,9 @@ class GpuReshape(tensor.Reshape): ...@@ -1672,7 +1684,9 @@ class GpuReshape(tensor.Reshape):
def make_node(self, x, shp): def make_node(self, x, shp):
host_reshaped = host_from_gpu(x).reshape(shp,ndim=self.ndim) host_reshaped = host_from_gpu(x).reshape(shp,ndim=self.ndim)
return Apply(self, [x, shp], [CudaNdarrayType(host_reshaped.broadcastable)()]) return Apply(self, [x, shp], [CudaNdarrayType(host_reshaped.broadcastable)()])
def perform(self, node, (x, shp), (out,)): def perform(self, node, inp, out_):
x, shp = inp
out, = out_
if (len(shp) != self.ndim): if (len(shp) != self.ndim):
raise ValueError('shape argument to Reshape.perform has incorrect length %i' raise ValueError('shape argument to Reshape.perform has incorrect length %i'
', should be %i' % (len(shp), self.ndim), shp) ', should be %i' % (len(shp), self.ndim), shp)
...@@ -1686,7 +1700,8 @@ class GpuSubtensor(tensor.Subtensor): ...@@ -1686,7 +1700,8 @@ class GpuSubtensor(tensor.Subtensor):
otype = CudaNdarrayType(rval.outputs[0].type.broadcastable) otype = CudaNdarrayType(rval.outputs[0].type.broadcastable)
return Apply(self, [x]+rval.inputs[1:], [otype()]) return Apply(self, [x]+rval.inputs[1:], [otype()])
def perform(self, node, inputs, (out, )): def perform(self, node, inputs, out_):
out, = out_
x = inputs[0] x = inputs[0]
indices = list(reversed(inputs[1:])) indices = list(reversed(inputs[1:]))
...@@ -1745,7 +1760,8 @@ class GpuJoin(tensor.Join): ...@@ -1745,7 +1760,8 @@ class GpuJoin(tensor.Join):
axis, tensors, axis, tensors,
as_tensor_variable_args, output_maker) as_tensor_variable_args, output_maker)
def perform(self, node, axis_and_tensors, (out, )): def perform(self, node, axis_and_tensors, out_):
out, = out_
axis, cndas = axis_and_tensors[0], axis_and_tensors[1:] axis, cndas = axis_and_tensors[0], axis_and_tensors[1:]
# In case axis is numpy.int8 and has no __index__() method # In case axis is numpy.int8 and has no __index__() method
axis = int(axis) axis = int(axis)
...@@ -1832,14 +1848,16 @@ class GpuAlloc(Op): ...@@ -1832,14 +1848,16 @@ class GpuAlloc(Op):
otype = CudaNdarrayType(dtype='float32', broadcastable=bcast) otype = CudaNdarrayType(dtype='float32', broadcastable=bcast)
return Apply(self, [v]+sh, [otype()]) return Apply(self, [v]+sh, [otype()])
def perform(self, node, inputs, (out,)): def perform(self, node, inputs, out_):
out, = out_
v = inputs[0] v = inputs[0]
sh = tuple([int(i) for i in inputs[1:]]) sh = tuple([int(i) for i in inputs[1:]])
if out[0] is None or out[0].shape != sh: if out[0] is None or out[0].shape != sh:
out[0] = cuda_ndarray.cuda_ndarray.CudaNdarray.zeros(sh) out[0] = cuda_ndarray.cuda_ndarray.CudaNdarray.zeros(sh)
out[0][...] = v # broadcast v to fill us up out[0][...] = v # broadcast v to fill us up
def c_code(self, node, name, inputs, (out,), sub): def c_code(self, node, name, inputs, out_, sub):
out, = out_
fail = sub['fail'] fail = sub['fail']
value = inputs[0] value = inputs[0]
shps = inputs[1:] shps = inputs[1:]
...@@ -1866,7 +1884,8 @@ class GpuAlloc(Op): ...@@ -1866,7 +1884,8 @@ class GpuAlloc(Op):
def infer_shape(self, node, input_shapes): def infer_shape(self, node, input_shapes):
return [node.inputs[1:]] return [node.inputs[1:]]
def grad(self, inputs, (gout,)): def grad(self, inputs, grads):
gout, = grads
return [None for i in inputs] return [None for i in inputs]
def c_code_cache_version(self): def c_code_cache_version(self):
...@@ -1888,7 +1907,9 @@ class GpuContiguous(Op): ...@@ -1888,7 +1907,9 @@ class GpuContiguous(Op):
input = as_cuda_ndarray_variable(input) input = as_cuda_ndarray_variable(input)
return Apply(self, [input], [input.type()]) return Apply(self, [input], [input.type()])
def c_code(self, node, name, (input,), (z,), sub): def c_code(self, node, name, inp, out, sub):
input, = inp
z, = out
fail = sub['fail'] fail = sub['fail']
str = """ str = """
{ {
......
...@@ -371,7 +371,9 @@ class GpuConv(Op): ...@@ -371,7 +371,9 @@ class GpuConv(Op):
open(os.path.join(os.path.split(__file__)[0],'conv_full_kernel.cu')).read()+\ open(os.path.join(os.path.split(__file__)[0],'conv_full_kernel.cu')).read()+\
open(os.path.join(os.path.split(__file__)[0],'conv.cu')).read() open(os.path.join(os.path.split(__file__)[0],'conv.cu')).read()
def c_code(self, node, nodename, (img, kern), (out,), sub): def c_code(self, node, nodename, inp, out_, sub):
img, kern = inp
out, = out_
dx = self.subsample[0] dx = self.subsample[0]
dy = self.subsample[1] dy = self.subsample[1]
border_mode = self.border_mode border_mode = self.border_mode
...@@ -435,7 +437,9 @@ class GpuDownsampleFactorMax(Op): ...@@ -435,7 +437,9 @@ class GpuDownsampleFactorMax(Op):
#raise NotImplementedError('only C is implemented') #raise NotImplementedError('only C is implemented')
def c_code_cache_version(self): def c_code_cache_version(self):
return (1) return (1)
def c_code(self, node, nodename, (x,), (z,), sub): def c_code(self, node, nodename, inp, out, sub):
x, = inp
z, = out
fail = sub['fail'] fail = sub['fail']
ds0, ds1 = self.ds ds0, ds1 = self.ds
ignore_border = int(self.ignore_border) ignore_border = int(self.ignore_border)
...@@ -586,7 +590,9 @@ class GpuDownsampleFactorMaxGrad(Op): ...@@ -586,7 +590,9 @@ class GpuDownsampleFactorMaxGrad(Op):
#return () #return ()
return (3,) return (3,)
def c_code(self, node, nodename, (x, z, gz), (gx,), sub): def c_code(self, node, nodename, inp, out, sub):
x, z, gz = inp
gx, = out
fail = sub['fail'] fail = sub['fail']
ds0, ds1 = self.ds ds0, ds1 = self.ds
ignore_border = int(self.ignore_border) ignore_border = int(self.ignore_border)
......
...@@ -77,7 +77,9 @@ class GpuCrossentropySoftmaxArgmax1HotWithBias (Op): ...@@ -77,7 +77,9 @@ class GpuCrossentropySoftmaxArgmax1HotWithBias (Op):
""" """
def c_code(self, node, nodename, (x, b, y_idx), (nll, sm, am), sub): def c_code(self, node, nodename, inp, out, sub):
x, b, y_idx = inp
nll, sm, am = out
classname=self.__class__.__name__ classname=self.__class__.__name__
fail = sub['fail'] fail = sub['fail']
sio = StringIO.StringIO() sio = StringIO.StringIO()
...@@ -191,7 +193,9 @@ class GpuCrossentropySoftmax1HotWithBiasDx (Op): ...@@ -191,7 +193,9 @@ class GpuCrossentropySoftmax1HotWithBiasDx (Op):
def c_code_cache_version(self): def c_code_cache_version(self):
return (3,) return (3,)
#return () #return ()
def c_code(self, node, nodename, (dnll, sm, y_idx), (dx,), sub): def c_code(self, node, nodename, inp, out, sub):
dnll, sm, y_idx = inp
dx, = out
fail = sub['fail'] fail = sub['fail']
return """ return """
if ((%(dnll)s->nd != 1) if ((%(dnll)s->nd != 1)
...@@ -306,7 +310,9 @@ class GpuSoftmax (Op): ...@@ -306,7 +310,9 @@ class GpuSoftmax (Op):
def c_code_cache_version(self): def c_code_cache_version(self):
#return () #return ()
return (2,) + inline_softmax.code_version return (2,) + inline_softmax.code_version
def c_code(self, node, nodename, (x,), (z,), sub): def c_code(self, node, nodename, inp, out, sub):
x, = inp
z, = out
fail = sub['fail'] fail = sub['fail']
return """ return """
if (%(x)s->nd != 2) if (%(x)s->nd != 2)
...@@ -394,7 +400,9 @@ class GpuSoftmaxWithBias (Op): ...@@ -394,7 +400,9 @@ class GpuSoftmaxWithBias (Op):
#return () #return ()
return (2,) + inline_softmax.code_version return (2,) + inline_softmax.code_version
def c_code(self, node, nodename, (x,b), (z,), sub): def c_code(self, node, nodename, inp, out, sub):
x, b = inp
z, = out
fail = sub['fail'] fail = sub['fail']
return """ return """
if (%(x)s->nd != 2) if (%(x)s->nd != 2)
......
...@@ -67,7 +67,9 @@ class FFT(Op): ...@@ -67,7 +67,9 @@ class FFT(Op):
rval = Apply(self, [_frames, _n, _axis], [spectrogram, buf]) rval = Apply(self, [_frames, _n, _axis], [spectrogram, buf])
return rval return rval
def perform(self, node, (frames, n, axis), (spectrogram, buf)): def perform(self, node, inp, out):
frames, n, axis = inp
spectrogram, buf = out
if self.inverse: if self.inverse:
fft_fn = numpy.fft.ifft fft_fn = numpy.fft.ifft
else: else:
...@@ -88,7 +90,9 @@ class FFT(Op): ...@@ -88,7 +90,9 @@ class FFT(Op):
raise NotImplementedError() raise NotImplementedError()
else: else:
spectrogram[0] = fft spectrogram[0] = fft
def grad(self, (frames, n, axis), (g_spectrogram, g_buf)): def grad(self, inp, out):
frames, n, axis = inp
g_spectrogram, g_buf = out
return [grad_todo(frames), None, None] return [grad_todo(frames), None, None]
fft = FFT(half=False, inverse=False) fft = FFT(half=False, inverse=False)
......
...@@ -28,8 +28,9 @@ class Minimal(gof.Op): ...@@ -28,8 +28,9 @@ class Minimal(gof.Op):
# HERE `args` must be THEANO VARIABLES # HERE `args` must be THEANO VARIABLES
return gof.Apply(op=self, inputs=args, outputs=[tensor.lscalar()]) return gof.Apply(op=self, inputs=args, outputs=[tensor.lscalar()])
def perform(self, node, inputs, (output, )): def perform(self, node, inputs, out_):
# HERE `inputs` are PYTHON OBJECTS output, = out_
# HERE `inputs` are PYTHON OBJECTS
# do what you want here, # do what you want here,
# but do not modify any of the arguments [inplace]. # but do not modify any of the arguments [inplace].
......
...@@ -23,13 +23,17 @@ class Multinomial(Op): ...@@ -23,13 +23,17 @@ class Multinomial(Op):
#assert unis.dtype == 'float32' #assert unis.dtype == 'float32'
return Apply(self, [pvals, unis], [pvals.type()]) return Apply(self, [pvals, unis], [pvals.type()])
def grad(self, (pvals, unis), (gz,)): def grad(self, inp, grads):
pvals, unis = inp
gz, = grads
return [None, None] return [None, None]
def c_code_cache_version(self): def c_code_cache_version(self):
return (3,) return (3,)
def c_code(self, node, name, (pvals, unis), (z,), sub): def c_code(self, node, name, inp, out, sub):
pvals, unis = inp
z, = out
fail = sub['fail'] fail = sub['fail']
return """ return """
...@@ -154,7 +158,9 @@ class GpuMultinomial(Multinomial): ...@@ -154,7 +158,9 @@ class GpuMultinomial(Multinomial):
""" % locals() """ % locals()
def c_code(self, node, name, (pvals, unis), (z,), sub): def c_code(self, node, name, inp, out, sub):
pvals, unis = inp
z, = out
fail = sub['fail'] fail = sub['fail']
return """ return """
......
...@@ -161,7 +161,9 @@ class NeighbourhoodsFromImages(Op): ...@@ -161,7 +161,9 @@ class NeighbourhoodsFromImages(Op):
raise TypeError() raise TypeError()
return gof.Apply(self, [x], [x.type()]) return gof.Apply(self, [x], [x.type()])
def perform(self, node, (x,), (z,)): def perform(self, node, inp, out):
x, = inp
z, = out
if self.inverse: if self.inverse:
# +1 in the inverse case # +1 in the inverse case
if len(x.shape) != (self.n_dims_before + \ if len(x.shape) != (self.n_dims_before + \
......
...@@ -46,7 +46,9 @@ class Images2Neibs(Op): ...@@ -46,7 +46,9 @@ class Images2Neibs(Op):
return Apply(self, [ten4, neib_shape,neib_step], [T.matrix(dtype=ten4.type.dtype)]) return Apply(self, [ten4, neib_shape,neib_step], [T.matrix(dtype=ten4.type.dtype)])
def grad(self, (x, neib_shape, neib_step), (gz,)): def grad(self, inp, grads):
x, neib_shape, neib_step = inp
gz, = grads
if self.mode=='valid': if self.mode=='valid':
return [neibs2images(gz, neib_shape, x.shape), None, None] return [neibs2images(gz, neib_shape, x.shape), None, None]
else: else:
...@@ -54,8 +56,10 @@ class Images2Neibs(Op): ...@@ -54,8 +56,10 @@ class Images2Neibs(Op):
def c_code_cache_version(self): def c_code_cache_version(self):
return (3,) return (3,)
def c_code(self, node, name, (ten4, neib_shape, neib_step), (z,), sub): def c_code(self, node, name, inp, out, sub):
ten4, neib_shape, neib_step = inp
z, = out
fail = sub['fail'] fail = sub['fail']
mode=self.mode mode=self.mode
...@@ -388,7 +392,9 @@ class GpuImages2Neibs(Images2Neibs): ...@@ -388,7 +392,9 @@ class GpuImages2Neibs(Images2Neibs):
""" % locals() """ % locals()
def c_code(self, node, name, (ten4, neib_shape, neib_step), (z,), sub): def c_code(self, node, name, inp, out, sub):
ten4, neib_shape, neib_step = inp
z, = out
fail = sub['fail'] fail = sub['fail']
mode = self.mode mode = self.mode
return """ return """
......
...@@ -173,7 +173,9 @@ class mrg_uniform(mrg_uniform_base): ...@@ -173,7 +173,9 @@ class mrg_uniform(mrg_uniform_base):
op = cls(TensorType(dtype, (False,)*ndim)) op = cls(TensorType(dtype, (False,)*ndim))
return op(rstate, cast(v_size, 'int32')) return op(rstate, cast(v_size, 'int32'))
def perform(self, node, (rstate, size), (o_rstate, o_sample)): def perform(self, node, inp, out):
rstate, size = inp
o_rstate, o_sample = out
numpy_version=numpy.__version__.split('.') numpy_version=numpy.__version__.split('.')
if not self.warned_numpy_version and int(numpy_version[0])<=1 and int(numpy_version[1])<3: if not self.warned_numpy_version and int(numpy_version[0])<=1 and int(numpy_version[1])<3:
print "Warning: you must use numpy version 1.3.0 or higher with the python version of this op. Otherwise numpy leak memory. and numpy" print "Warning: you must use numpy version 1.3.0 or higher with the python version of this op. Otherwise numpy leak memory. and numpy"
...@@ -199,7 +201,9 @@ class mrg_uniform(mrg_uniform_base): ...@@ -199,7 +201,9 @@ class mrg_uniform(mrg_uniform_base):
o_rstate[0] = node.outputs[0].type.filter(rstate) # send to GPU if necessary o_rstate[0] = node.outputs[0].type.filter(rstate) # send to GPU if necessary
o_sample[0] = node.outputs[1].type.filter(rval.reshape(size))# send to GPU if necessary o_sample[0] = node.outputs[1].type.filter(rval.reshape(size))# send to GPU if necessary
def c_code(self, node, name, (rstate, size), (o_rstate, o_sample), sub): def c_code(self, node, name, inp, out, sub):
rstate, size = inp
o_rstate, o_sample = out
if self.inplace: if self.inplace:
o_rstate_requirement = 'NPY_C_CONTIGUOUS|NPY_ALIGNED' o_rstate_requirement = 'NPY_C_CONTIGUOUS|NPY_ALIGNED'
else: else:
...@@ -455,7 +459,9 @@ class GPU_mrg_uniform(mrg_uniform_base): ...@@ -455,7 +459,9 @@ class GPU_mrg_uniform(mrg_uniform_base):
""" %locals() """ %locals()
def c_code(self, node, nodename, (rstate, size), (o_rstate, o_sample), sub): def c_code(self, node, nodename, inp, out, sub):
rstate, size = inp
o_rstate, o_sample = out
inplace = int(self.inplace) inplace = int(self.inplace)
ndim = self.output_type.ndim ndim = self.output_type.ndim
o_type_num = numpy.asarray(0, dtype=self.output_type.dtype).dtype.num o_type_num = numpy.asarray(0, dtype=self.output_type.dtype).dtype.num
......
...@@ -9,13 +9,17 @@ class ScalarSoftsign(theano.scalar.UnaryScalarOp): ...@@ -9,13 +9,17 @@ class ScalarSoftsign(theano.scalar.UnaryScalarOp):
return x / (1.0 + abs(x)) return x / (1.0 + abs(x))
def impl(self, x): def impl(self, x):
return ScalarSoftsign.static_impl(x) return ScalarSoftsign.static_impl(x)
def grad(self, (x, ), (gz, )): def grad(self, inp, grads):
x, = inp
gz, = grads
if 'float' in x.type.dtype: if 'float' in x.type.dtype:
d = (1.0 + abs(x)) d = (1.0 + abs(x))
return [gz / (d * d)] return [gz / (d * d)]
else: else:
return NotImplemented return NotImplemented
def c_code(self, node, name, (x, ), (z, ), sub): def c_code(self, node, name, inp, out, sub):
x, = inp
z, = out
if node.inputs[0].type in [theano.scalar.float32, theano.scalar.float64]: if node.inputs[0].type in [theano.scalar.float32, theano.scalar.float64]:
return "%(z)s = %(x)s / (1.0+fabs(%(x)s));" % locals() return "%(z)s = %(x)s / (1.0+fabs(%(x)s));" % locals()
raise NotImplementedError('only floating point x is implemented') raise NotImplementedError('only floating point x is implemented')
......
...@@ -33,7 +33,9 @@ class Solve(gof.Op): ...@@ -33,7 +33,9 @@ class Solve(gof.Op):
otype = tensor.TensorType(broadcastable=b_.broadcastable, dtype=odtype) otype = tensor.TensorType(broadcastable=b_.broadcastable, dtype=odtype)
return gof.Apply(op=self, inputs=[A, B], outputs=[otype()]) return gof.Apply(op=self, inputs=[A, B], outputs=[otype()])
def perform(self, node, (A, b), (output, )): def perform(self, node, inp, out):
A, b = inp
output, = out
ret=scipy.linalg.solve(A,b) ret=scipy.linalg.solve(A,b)
if ret.dtype != node.outputs[0].dtype: if ret.dtype != node.outputs[0].dtype:
print >> sys.stderr, "WARNING: Solve.perform() required cast." print >> sys.stderr, "WARNING: Solve.perform() required cast."
......
...@@ -18,7 +18,9 @@ class Erf(UnaryScalarOp): ...@@ -18,7 +18,9 @@ class Erf(UnaryScalarOp):
return scipy.special.erf(x) return scipy.special.erf(x)
else: else:
super(Erf,self).impl(x) super(Erf,self).impl(x)
def grad(self, (x, ), (gz, )): def grad(self, inp, grads):
x, = inp
gz, = grads
if x.type in complex_types: if x.type in complex_types:
raise NotImplementedError() raise NotImplementedError()
elif x.type in float_types: elif x.type in float_types:
...@@ -26,7 +28,9 @@ class Erf(UnaryScalarOp): ...@@ -26,7 +28,9 @@ class Erf(UnaryScalarOp):
return gz * cst * exp(-x*x), return gz * cst * exp(-x*x),
else: else:
return None, return None,
def c_code(self, node, name, (x, ), (z, ), sub): def c_code(self, node, name, inp, out, sub):
x, = inp
z, = out
if node.inputs[0].type in complex_types: if node.inputs[0].type in complex_types:
raise NotImplementedError('type not supported', type) raise NotImplementedError('type not supported', type)
return "%(z)s = erf(%(x)s);" % locals() return "%(z)s = erf(%(x)s);" % locals()
...@@ -38,7 +42,9 @@ class Erfc(UnaryScalarOp): ...@@ -38,7 +42,9 @@ class Erfc(UnaryScalarOp):
return scipy.special.erfc(x) return scipy.special.erfc(x)
else: else:
super(Erfc,self).impl(x) super(Erfc,self).impl(x)
def grad(self, (x, ), (gz, )): def grad(self, inp, grads):
x, = inp
gz, = grads
if x.type in complex_types: if x.type in complex_types:
raise NotImplementedError() raise NotImplementedError()
elif x.type in float_types: elif x.type in float_types:
...@@ -46,7 +52,9 @@ class Erfc(UnaryScalarOp): ...@@ -46,7 +52,9 @@ class Erfc(UnaryScalarOp):
return - gz * cst * exp(-x*x), return - gz * cst * exp(-x*x),
else: else:
return None, return None,
def c_code(self, node, name, (x, ), (z, ), sub): def c_code(self, node, name, inp, out, sub):
x, = inp
z, = out
if node.inputs[0].type in complex_types: if node.inputs[0].type in complex_types:
raise NotImplementedError('type not supported', type) raise NotImplementedError('type not supported', type)
return "%(z)s = erfc(%(x)s);" % locals() return "%(z)s = erfc(%(x)s);" % locals()
......
...@@ -45,14 +45,18 @@ class TrueDot(gof.op.Op): ...@@ -45,14 +45,18 @@ class TrueDot(gof.op.Op):
inputs = [x, y] # Need to convert? e.g. assparse inputs = [x, y] # Need to convert? e.g. assparse
outputs = [Sparse(dtype = x.type.dtype, format = myformat).make_variable()] outputs = [Sparse(dtype = x.type.dtype, format = myformat).make_variable()]
return gof.Apply(self, inputs, outputs) return gof.Apply(self, inputs, outputs)
def perform(self, node, (x, y), (out, )): def perform(self, node, inp, out_):
""" """
@todo: Verify that output is sufficiently sparse, and raise a warning if it is not @todo: Verify that output is sufficiently sparse, and raise a warning if it is not
@todo: Also determine that we are storing the output in the best storage format? @todo: Also determine that we are storing the output in the best storage format?
""" """
x, y = inp
out, = out_
rval = x.dot(y) rval = x.dot(y)
out[0] = rval out[0] = rval
def grad(self, (x, y), (gz,)): def grad(self, inp, grads):
x, y = inp
gz, = grads
assert _is_sparse_variable(gz) assert _is_sparse_variable(gz)
assert _is_sparse_variable(x) assert _is_sparse_variable(x)
rval = [true_dot(gz, y.T), true_dot(x.T, gz)] rval = [true_dot(gz, y.T), true_dot(x.T, gz)]
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论