提交 7611ad68 authored 作者: Frédéric Bastien's avatar Frédéric Bastien

Merge pull request #3046 from harlouci/flake8

Flake8 for files in sandbox
"""Provides Ops for FFT and DCT. """Provides Ops for FFT and DCT.
""" """
from theano.gof import Op, Apply, generic
from theano import tensor
import numpy.fft
import numpy import numpy
import numpy.fft
from six.moves import xrange from six.moves import xrange
from theano import tensor
from theano.gof import Op, Apply, generic
class GradTodo(Op): class GradTodo(Op):
def make_node(self, x): def make_node(self, x):
return Apply(self, [x], [x.type()]) return Apply(self, [x], [x.type()])
def perform(self, node, inputs, outputs): def perform(self, node, inputs, outputs):
raise NotImplementedError('TODO') raise NotImplementedError('TODO')
grad_todo = GradTodo() grad_todo = GradTodo()
...@@ -45,8 +46,9 @@ class FFT(Op): ...@@ -45,8 +46,9 @@ class FFT(Op):
self.inverse = inverse self.inverse = inverse
def __eq__(self, other): def __eq__(self, other):
return type(self) == type(other) and (self.half == other.half) and (self.inverse == return (type(self) == type(other) and
other.inverse) self.half == other.half and
self.inverse == other.inverse)
def __hash__(self): def __hash__(self):
return hash(type(self)) ^ hash(self.half) ^ 9828743 ^ (self.inverse) return hash(type(self)) ^ hash(self.half) ^ 9828743 ^ (self.inverse)
...@@ -83,15 +85,16 @@ class FFT(Op): ...@@ -83,15 +85,16 @@ class FFT(Op):
if axis == 0: if axis == 0:
if (M % 2): if (M % 2):
raise ValueError('halfFFT on odd-length vectors is undefined') raise ValueError('halfFFT on odd-length vectors is undefined')
spectrogram[0] = fft[0:M/2, :] spectrogram[0] = fft[0:M / 2, :]
elif axis == 1: elif axis == 1:
if (N % 2): if (N % 2):
raise ValueError('halfFFT on odd-length vectors is undefined') raise ValueError('halfFFT on odd-length vectors is undefined')
spectrogram[0] = fft[:, 0:N/2] spectrogram[0] = fft[:, 0:N / 2]
else: else:
raise NotImplementedError() raise NotImplementedError()
else: else:
spectrogram[0] = fft spectrogram[0] = fft
def grad(self, inp, out): def grad(self, inp, out):
frames, n, axis = inp frames, n, axis = inp
g_spectrogram, g_buf = out g_spectrogram, g_buf = out
...@@ -112,9 +115,9 @@ def dct_matrix(rows, cols, unitary=True): ...@@ -112,9 +115,9 @@ def dct_matrix(rows, cols, unitary=True):
""" """
rval = numpy.zeros((rows, cols)) rval = numpy.zeros((rows, cols))
col_range = numpy.arange(cols) col_range = numpy.arange(cols)
scale = numpy.sqrt(2.0/cols) scale = numpy.sqrt(2.0 / cols)
for i in xrange(rows): for i in xrange(rows):
rval[i] = numpy.cos(i * (col_range*2+1)/(2.0 * cols) * numpy.pi) * scale rval[i] = numpy.cos(i * (col_range * 2 + 1) / (2.0 * cols) * numpy.pi) * scale
if unitary: if unitary:
rval[0] *= numpy.sqrt(0.5) rval[0] *= numpy.sqrt(0.5)
......
from __future__ import print_function from __future__ import print_function
import numpy, scipy.linalg
from theano import gof, tensor, scalar, function
import unittest import unittest
import numpy
from theano import gof, tensor, function
from theano.tests import unittest_tools as utt
class Minimal(gof.Op): class Minimal(gof.Op):
...@@ -49,7 +53,6 @@ minimal = Minimal() ...@@ -49,7 +53,6 @@ minimal = Minimal()
# TODO: test dtype conversion # TODO: test dtype conversion
# TODO: test that invalid types are rejected by make_node # TODO: test that invalid types are rejected by make_node
# TODO: test that each valid type for A and b works correctly # TODO: test that each valid type for A and b works correctly
from theano.tests import unittest_tools as utt
class T_minimal(unittest.TestCase): class T_minimal(unittest.TestCase):
......
...@@ -155,7 +155,6 @@ class MultinomialFromUniform(Op): ...@@ -155,7 +155,6 @@ class MultinomialFromUniform(Op):
unis_n = unis[n] unis_n = unis[n]
for m in range(nb_outcomes): for m in range(nb_outcomes):
z_nm = z[0][n, m]
cummul += pvals[n, m] cummul += pvals[n, m]
if (waiting and (cummul > unis_n)): if (waiting and (cummul > unis_n)):
z[0][n, m] = 1 z[0][n, m] = 1
...@@ -356,8 +355,8 @@ def local_gpu_multinomial(node): ...@@ -356,8 +355,8 @@ def local_gpu_multinomial(node):
return [host_from_gpu(gpu_op(*[gpu_from_host(i) return [host_from_gpu(gpu_op(*[gpu_from_host(i)
for i in node.inputs])).T] 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) node.inputs[0].owner and
is MultinomialFromUniform): 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
......
#!/usr/bin/python
"""WARNING: This code is not recommanded. It is not finished, it is """WARNING: This code is not recommanded. It is not finished, it is
slower then the version in sandbox/neighbours.py, and it do not work slower then the version in sandbox/neighbours.py, and it do not work
on the GPU. on the GPU.
...@@ -8,13 +7,13 @@ it cover more cases. But thoses cases aren't needed frequently, so you ...@@ -8,13 +7,13 @@ it cover more cases. But thoses cases aren't needed frequently, so you
probably don't want to use this version, go see neighbours.py!!!!!!! probably don't want to use this version, go see neighbours.py!!!!!!!
""" """
import theano
from theano import gof, Op, tensor, Variable, Apply
import numpy import numpy
from six.moves import xrange from six.moves import xrange
import six.moves.builtins as builtins import six.moves.builtins as builtins
import theano
from theano import gof, Op
class NeighbourhoodsFromImages(Op): class NeighbourhoodsFromImages(Op):
def __init__(self, n_dims_before, dims_neighbourhoods, def __init__(self, n_dims_before, dims_neighbourhoods,
...@@ -75,7 +74,7 @@ class NeighbourhoodsFromImages(Op): ...@@ -75,7 +74,7 @@ class NeighbourhoodsFromImages(Op):
""" """
self.n_dims_before = n_dims_before self.n_dims_before = n_dims_before
self.dims_neighbourhoods = dims_neighbourhoods self.dims_neighbourhoods = dims_neighbourhoods
if not strides is None: if strides is not None:
self.strides = strides self.strides = strides
else: else:
self.strides = dims_neighbourhoods self.strides = dims_neighbourhoods
...@@ -85,14 +84,6 @@ class NeighbourhoodsFromImages(Op): ...@@ -85,14 +84,6 @@ class NeighbourhoodsFromImages(Op):
self.code_string, self.code = self.make_py_code() self.code_string, self.code = self.make_py_code()
def _compute_neigh_strides(self):
neigh_strides = [1 for i in xrange(len(self.strides))]
cur_stride = 1
for i in xrange(len(self.strides)-1, -1, -1):
neigh_strides[i] = cur_stride
cur_stride *= self.dims_neighbourhoods[i]
return neigh_strides
def __eq__(self, other): def __eq__(self, other):
return type(self) == type(other) and \ return type(self) == type(other) and \
self.n_dims_before == other.n_dims_before and \ self.n_dims_before == other.n_dims_before and \
...@@ -108,8 +99,7 @@ class NeighbourhoodsFromImages(Op): ...@@ -108,8 +99,7 @@ class NeighbourhoodsFromImages(Op):
hash(self.ignore_border) hash(self.ignore_border)
def __str__(self): def __str__(self):
return '%s{%s,%s,%s,%s}' % \ return '%s{%s,%s,%s,%s}' % (self.__class__.__name__,
(self.__class__.__name__,
self.n_dims_before, self.n_dims_before,
self.dims_neighbourhoods, self.dims_neighbourhoods,
self.strides, self.strides,
...@@ -163,11 +153,11 @@ class NeighbourhoodsFromImages(Op): ...@@ -163,11 +153,11 @@ class NeighbourhoodsFromImages(Op):
x = theano.tensor.as_tensor_variable(x) x = theano.tensor.as_tensor_variable(x)
if self.inverse: if self.inverse:
# +1 in the inverse case # +1 in the inverse case
if x.type.ndim != (self.n_dims_before + \ if x.type.ndim != (self.n_dims_before +
len(self.dims_neighbourhoods) + 1): len(self.dims_neighbourhoods) + 1):
raise TypeError() raise TypeError()
else: else:
if x.type.ndim != (self.n_dims_before + \ if x.type.ndim != (self.n_dims_before +
len(self.dims_neighbourhoods)): len(self.dims_neighbourhoods)):
raise TypeError() raise TypeError()
return gof.Apply(self, [x], [x.type()]) return gof.Apply(self, [x], [x.type()])
...@@ -177,22 +167,24 @@ class NeighbourhoodsFromImages(Op): ...@@ -177,22 +167,24 @@ class NeighbourhoodsFromImages(Op):
z, = out 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 +
len(self.dims_neighbourhoods) + 1): len(self.dims_neighbourhoods) + 1):
raise ValueError("Images passed as input don't match the " +\ raise ValueError("Images passed as input don't match the "
"dimensions passed when this (inversed) Apply node was created") "dimensions passed when this (inversed) "
"Apply node was created")
prod = 1 prod = 1
for dim in self.dims_neighbourhoods: for dim in self.dims_neighbourhoods:
prod *= dim prod *= dim
if x.shape[-1] != prod: if x.shape[-1] != prod:
raise ValueError(("Last dimension of neighbourhoods (%s) is not " +\ raise ValueError("Last dimension of neighbourhoods (%s) is not"
"the product of the neighbourhoods dimensions (%s)") % \ " the product of the neighbourhoods dimensions"
(str(x.shape[-1]), str(prod))) " (%s)" % (str(x.shape[-1]), str(prod)))
else: else:
if len(x.shape) != (self.n_dims_before + \ if len(x.shape) != (self.n_dims_before +
len(self.dims_neighbourhoods)): len(self.dims_neighbourhoods)):
raise ValueError("Images passed as input don't match the " +\ raise ValueError("Images passed as input don't match the "
"dimensions passed when this Apply node was created") "dimensions passed when this Apply node "
"was created")
if self.inverse: if self.inverse:
input_shape, num_strides = self.in_shape(x.shape) input_shape, num_strides = self.in_shape(x.shape)
...@@ -201,8 +193,6 @@ class NeighbourhoodsFromImages(Op): ...@@ -201,8 +193,6 @@ class NeighbourhoodsFromImages(Op):
input_shape = x.shape input_shape = x.shape
out_shape, num_strides = self.out_shape(input_shape) out_shape, num_strides = self.out_shape(input_shape)
neigh_strides = self._compute_neigh_strides()
if z[0] is None: if z[0] is None:
if self.inverse: if self.inverse:
z[0] = numpy.zeros(input_shape) z[0] = numpy.zeros(input_shape)
...@@ -228,7 +218,7 @@ class NeighbourhoodsFromImages(Op): ...@@ -228,7 +218,7 @@ class NeighbourhoodsFromImages(Op):
return code_before return code_before
def _py_innerloop(self, inner_dim_no): def _py_innerloop(self, inner_dim_no):
base_indent = ('\t' * (self.n_dims_before + inner_dim_no*2)) base_indent = ('\t' * (self.n_dims_before + inner_dim_no * 2))
code_before = base_indent + \ code_before = base_indent + \
"for stride_idx_%d in xrange(num_strides[%d]):\n" % \ "for stride_idx_%d in xrange(num_strides[%d]):\n" % \
(inner_dim_no, inner_dim_no) (inner_dim_no, inner_dim_no)
...@@ -237,36 +227,33 @@ class NeighbourhoodsFromImages(Op): ...@@ -237,36 +227,33 @@ class NeighbourhoodsFromImages(Op):
"dim_%d_offset = stride_idx_%d * self.strides[%d]\n" %\ "dim_%d_offset = stride_idx_%d * self.strides[%d]\n" %\
(inner_dim_no, inner_dim_no, inner_dim_no) (inner_dim_no, inner_dim_no, inner_dim_no)
code_before += base_indent + \ code_before += base_indent + \
"max_neigh_idx_%d = input_shape[%d] - dim_%d_offset\n" %\ "max_neigh_idx_%d = input_shape[%d] - dim_%d_offset\n" % \
(inner_dim_no, (inner_dim_no, self.n_dims_before + inner_dim_no, inner_dim_no)
self.n_dims_before+inner_dim_no, inner_dim_no)
code_before += base_indent + \ code_before += base_indent + \
("for neigh_idx_%d in xrange(min(max_neigh_idx_%d,"\ ("for neigh_idx_%d in xrange(min(max_neigh_idx_%d,"
+ " self.dims_neighbourhoods[%d])):\n") % \ " self.dims_neighbourhoods[%d])):\n") %\
(inner_dim_no, inner_dim_no, inner_dim_no) (inner_dim_no, inner_dim_no, inner_dim_no)
return code_before return code_before
def _py_flattened_idx(self): def _py_flattened_idx(self):
return "+".join(["neigh_strides[%d]*neigh_idx_%d" % (i, i) \ return "+".join(["neigh_strides[%d]*neigh_idx_%d" % (i, i)
for i in xrange(len(self.strides))]) for i in xrange(len(self.strides))])
def _py_assignment(self): def _py_assignment(self):
input_idx = "".join(["outer_idx_%d," % (i,) \ input_idx = "".join(["outer_idx_%d," % (i,)
for i in xrange(self.n_dims_before)]) for i in xrange(self.n_dims_before)])
input_idx += "".join(["dim_%d_offset+neigh_idx_%d," % \ input_idx += "".join(["dim_%d_offset+neigh_idx_%d," %
(i, i) for i in xrange(len(self.strides))]) (i, i) for i in xrange(len(self.strides))])
out_idx = "".join(\ out_idx = "".join(
["outer_idx_%d," % (i,) for i in \ ["outer_idx_%d," % (i,) for i in xrange(self.n_dims_before)] +
xrange(self.n_dims_before)] + \ ["stride_idx_%d," % (i,) for i in xrange(len(self.strides))])
["stride_idx_%d," % (i,) for i in \
xrange(len(self.strides))])
out_idx += self._py_flattened_idx() out_idx += self._py_flattened_idx()
#return_val = '\t' * (self.n_dims_before + len(self.strides)*2) # return_val = '\t' * (self.n_dims_before + len(self.strides)*2)
#return_val += "print "+input_idx+"'\\n',"+out_idx+"\n" # return_val += "print "+input_idx+"'\\n',"+out_idx+"\n"
return_val = '\t' * (self.n_dims_before + len(self.strides)*2) return_val = '\t' * (self.n_dims_before + len(self.strides) * 2)
if self.inverse: if self.inverse:
# remember z and x are inversed: # remember z and x are inversed:
...@@ -282,8 +269,9 @@ class NeighbourhoodsFromImages(Op): ...@@ -282,8 +269,9 @@ class NeighbourhoodsFromImages(Op):
class ImagesFromNeighbourhoods(NeighbourhoodsFromImages): class ImagesFromNeighbourhoods(NeighbourhoodsFromImages):
def __init__(self, n_dims_before, dims_neighbourhoods, def __init__(self, n_dims_before, dims_neighbourhoods,
strides=None, ignore_border=False): strides=None, ignore_border=False):
NeighbourhoodsFromImages.__init__(self, n_dims_before, dims_neighbourhoods, NeighbourhoodsFromImages.__init__(self, n_dims_before,
strides=strides, ignore_border=ignore_border, dims_neighbourhoods,
strides=strides,
ignore_border=ignore_border,
inverse=True) inverse=True)
# and that's all there is to it # and that's all there is to it
...@@ -4,3 +4,5 @@ This file was created for compatibility. ...@@ -4,3 +4,5 @@ This file was created for compatibility.
""" """
from theano.tensor.nnet.neighbours import (images2neibs, neibs2images, from theano.tensor.nnet.neighbours import (images2neibs, neibs2images,
Images2Neibs) Images2Neibs)
__all__ = ["images2neibs", "neibs2images", "Images2Neibs"]
from __future__ import print_function from __future__ import print_function
import numpy, scipy.linalg
from theano import gof, tensor, scalar
import unittest import unittest
import sys
import numpy
import scipy.linalg
import theano
from theano import gof, tensor, scalar
from theano.tests import unittest_tools as utt
class Solve(gof.Op): class Solve(gof.Op):
...@@ -32,7 +39,7 @@ class Solve(gof.Op): ...@@ -32,7 +39,7 @@ class Solve(gof.Op):
raise TypeError("b must be a matrix or vector", b_.type) raise TypeError("b must be a matrix or vector", b_.type)
odtype = scalar.upcast(A_.dtype, b_.dtype) odtype = scalar.upcast(A_.dtype, b_.dtype)
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, inp, out): def perform(self, node, inp, out):
A, b = inp A, b = inp
...@@ -49,8 +56,6 @@ solve = Solve() ...@@ -49,8 +56,6 @@ solve = Solve()
# TODO: test dtype conversion # TODO: test dtype conversion
# TODO: test that invalid types are rejected by make_node # TODO: test that invalid types are rejected by make_node
# TODO: test that each valid type for A and b works correctly # TODO: test that each valid type for A and b works correctly
from theano.tests import unittest_tools as utt
class T_solve(unittest.TestCase): class T_solve(unittest.TestCase):
def setUp(self): def setUp(self):
......
...@@ -136,20 +136,14 @@ whitelist_flake8 = [ ...@@ -136,20 +136,14 @@ whitelist_flake8 = [
"sandbox/test_theano_object.py", "sandbox/test_theano_object.py",
"sandbox/test_scan.py", "sandbox/test_scan.py",
"sandbox/rng_mrg.py", "sandbox/rng_mrg.py",
"sandbox/solve.py",
"sandbox/theano_object.py", "sandbox/theano_object.py",
"sandbox/scan.py", "sandbox/scan.py",
"sandbox/multinomial.py",
"sandbox/neighbourhoods.py",
"sandbox/fourier.py",
"sandbox/test_multinomial.py", "sandbox/test_multinomial.py",
"sandbox/minimal.py",
"sandbox/test_rng_mrg.py", "sandbox/test_rng_mrg.py",
"sandbox/test_neighbourhoods.py", "sandbox/test_neighbourhoods.py",
"sandbox/symbolic_module.py", "sandbox/symbolic_module.py",
"sandbox/conv.py", "sandbox/conv.py",
"sandbox/debug.py", "sandbox/debug.py",
"sandbox/neighbours.py",
"sandbox/cuda/dnn.py", "sandbox/cuda/dnn.py",
"sandbox/cuda/var.py", "sandbox/cuda/var.py",
"sandbox/cuda/GpuConvGrad3D.py", "sandbox/cuda/GpuConvGrad3D.py",
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论