提交 1923c879 authored 作者: Frederic's avatar Frederic

pep8

上级 27f6c59e
...@@ -12,22 +12,28 @@ if cuda_available: ...@@ -12,22 +12,28 @@ if cuda_available:
from theano.sandbox.cuda.basic_ops import host_from_gpu, gpu_from_host from theano.sandbox.cuda.basic_ops import host_from_gpu, gpu_from_host
from theano.sandbox.cuda.opt import register_opt from theano.sandbox.cuda.opt import register_opt
class MultinomialFromUniform(Op): class MultinomialFromUniform(Op):
'''Converts samples from a uniform into sample from a multinomial.''' '''Converts samples from a uniform into sample from a multinomial.'''
def __init__(self, odtype): def __init__(self, odtype):
self.odtype=odtype self.odtype = odtype
def __eq__(self, other): def __eq__(self, other):
return type(self) == type(other) and self.odtype==other.odtype return type(self) == type(other) and self.odtype == other.odtype
def __hash__(self): def __hash__(self):
return hash((type(self), self.odtype)) return hash((type(self), self.odtype))
def __str__(self): def __str__(self):
return '%s{%s}'%(self.__class__.__name__, self.odtype) return '%s{%s}' % (self.__class__.__name__, self.odtype)
def __setstate__(self, dct): def __setstate__(self, dct):
self.__dict__.update(dct) self.__dict__.update(dct)
try: try:
self.odtype self.odtype
except AttributeError: except AttributeError:
self.odtype='auto' self.odtype = 'auto'
def make_node(self, pvals, unis): def make_node(self, pvals, unis):
pvals = T.as_tensor_variable(pvals) pvals = T.as_tensor_variable(pvals)
unis = T.as_tensor_variable(unis) unis = T.as_tensor_variable(unis)
...@@ -35,7 +41,7 @@ class MultinomialFromUniform(Op): ...@@ -35,7 +41,7 @@ class MultinomialFromUniform(Op):
raise NotImplementedError('pvals ndim should be 2', pvals.ndim) raise NotImplementedError('pvals ndim should be 2', pvals.ndim)
if unis.ndim != 1: if unis.ndim != 1:
raise NotImplementedError('unis ndim should be 1', unis.ndim) raise NotImplementedError('unis ndim should be 1', unis.ndim)
if self.odtype=='auto': if self.odtype == 'auto':
odtype = pvals.dtype odtype = pvals.dtype
else: else:
odtype = self.odtype odtype = self.odtype
...@@ -121,6 +127,7 @@ class MultinomialFromUniform(Op): ...@@ -121,6 +127,7 @@ class MultinomialFromUniform(Op):
} }
} // END NESTED SCOPE } // END NESTED SCOPE
""" % locals() """ % locals()
def perform(self, node, ins, outs): def perform(self, node, ins, outs):
(pvals, unis) = ins (pvals, unis) = ins
(z,) = outs (z,) = outs
...@@ -165,7 +172,7 @@ class GpuMultinomialFromUniform(MultinomialFromUniform, GpuOp): ...@@ -165,7 +172,7 @@ class GpuMultinomialFromUniform(MultinomialFromUniform, GpuOp):
raise TypeError('pvals must be cudandarray', pvals) raise TypeError('pvals must be cudandarray', pvals)
if not isinstance(unis.type, CudaNdarrayType): if not isinstance(unis.type, CudaNdarrayType):
raise TypeError('unis must be cudandarray', unis) raise TypeError('unis must be cudandarray', unis)
if self.odtype=='auto': if self.odtype == 'auto':
odtype = pvals.dtype odtype = pvals.dtype
else: else:
odtype = self.odtype odtype = self.odtype
...@@ -226,7 +233,6 @@ class GpuMultinomialFromUniform(MultinomialFromUniform, GpuOp): ...@@ -226,7 +233,6 @@ class GpuMultinomialFromUniform(MultinomialFromUniform, GpuOp):
""" % locals() """ % locals()
def c_code(self, node, name, ins, outs, sub): def c_code(self, node, name, ins, outs, sub):
(pvals, unis) = ins (pvals, unis) = ins
(z,) = outs (z,) = outs
...@@ -327,25 +333,30 @@ class GpuMultinomialFromUniform(MultinomialFromUniform, GpuOp): ...@@ -327,25 +333,30 @@ class GpuMultinomialFromUniform(MultinomialFromUniform, GpuOp):
} // END NESTED SCOPE } // END NESTED SCOPE
""" % locals() """ % locals()
@local_optimizer() @local_optimizer()
def local_gpu_multinomial(node): def local_gpu_multinomial(node):
if type(node.op) is MultinomialFromUniform: if type(node.op) is MultinomialFromUniform:
p, u = node.inputs p, u = node.inputs
m, = node.outputs m, = node.outputs
if (p.dtype == u.dtype == m.dtype == 'float32' and if (p.dtype == u.dtype == m.dtype == 'float32' and
any([i.owner and isinstance(i.owner.op, theano.sandbox.cuda.HostFromGpu) any([i.owner and isinstance(i.owner.op,
theano.sandbox.cuda.HostFromGpu)
for i in node.inputs])): for i in node.inputs])):
gpu_op = GpuMultinomialFromUniform(node.op.odtype) gpu_op = GpuMultinomialFromUniform(node.op.odtype)
return [host_from_gpu(gpu_op(*[gpu_from_host(i) for i in node.inputs])).T] return [host_from_gpu(gpu_op(*[gpu_from_host(i)
for i in node.inputs])).T]
if (isinstance(node.op, theano.sandbox.cuda.GpuFromHost) and if (isinstance(node.op, theano.sandbox.cuda.GpuFromHost) and
node.inputs[0].owner and type(node.inputs[0].owner.op) is MultinomialFromUniform): node.inputs[0].owner and type(node.inputs[0].owner.op)
is MultinomialFromUniform):
multi = node.inputs[0].owner multi = node.inputs[0].owner
p, u = multi.inputs p, u = multi.inputs
m, = multi.outputs m, = multi.outputs
if (p.dtype == u.dtype == m.dtype == 'float32'): if (p.dtype == u.dtype == m.dtype == 'float32'):
gpu_op = GpuMultinomialFromUniform(multi.op.odtype) gpu_op = GpuMultinomialFromUniform(multi.op.odtype)
ret = gpu_op(*[gpu_from_host(i) for i in multi.inputs]).T ret = gpu_op(*[gpu_from_host(i) for i in multi.inputs]).T
# The dimshuffle is on the cpu, but will be moved to the gpu by an opt. # The dimshuffle is on the cpu, but will be moved to the
# gpu by an opt.
return [gpu_from_host(ret)] return [gpu_from_host(ret)]
if cuda_available: if cuda_available:
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论