提交 5b604a49 authored 作者: AdeB's avatar AdeB 提交者: Xavier Bouthillier

Fix a few pep8 errors

上级 1e0da364
......@@ -16,6 +16,7 @@ class SparseBlockGemv(Op):
for i in range(h.shape[1]):
o[b, j, :] += numpy.dot(h[b, i], W[iIdx[b, i], oIdx[b, j]])
where b, h, W, o iIdx, oIdx are defined in the docstring of make_node.
.. image:: ../../images/blocksparse.png
"""
......@@ -29,7 +30,7 @@ class SparseBlockGemv(Op):
def make_node(self, o, W, h, inputIdx, outputIdx):
"""
Compute the dot product of the specified pieces of vectors
and matrices.
and matrices.
Parameters
----------
......@@ -76,7 +77,7 @@ class SparseBlockGemv(Op):
assert outputIdx.type.dtype in discrete_dtypes
output = o.type.__class__(dtype=o.type.dtype,
broadcastable=(False,)*o.ndim)()
broadcastable=(False,) * o.ndim)()
return Apply(self, [o, W, h, inputIdx, outputIdx], [output])
......@@ -100,7 +101,6 @@ class SparseBlockGemv(Op):
"grad of outputIdx makes no sense")]
class SparseBlockOuter(Op):
"""
This computes the outer product of two sets of pieces of vectors
......@@ -121,7 +121,7 @@ class SparseBlockOuter(Op):
"""
Compute the dot product of the specified pieces of vectors
and matrices.
and matrices.
Parameters
----------
......@@ -154,7 +154,7 @@ class SparseBlockOuter(Op):
alpha = one
output = o.type.__class__(dtype=o.type.dtype,
broadcastable=(False,)*o.ndim)()
broadcastable=(False,) * o.ndim)()
return Apply(self, [o, x, y, xIdx, yIdx, alpha],
[output])
......
......@@ -220,7 +220,8 @@ class GpuCrossentropySoftmaxArgmax1HotWithBias(GpuOp):
# return ()
return (4,)
gpu_crossentropy_softmax_argmax_1hot_with_bias = GpuCrossentropySoftmaxArgmax1HotWithBias()
gpu_crossentropy_softmax_argmax_1hot_with_bias = \
GpuCrossentropySoftmaxArgmax1HotWithBias()
class GpuCrossentropySoftmax1HotWithBiasDx(GpuOp):
......@@ -391,7 +392,8 @@ class GpuCrossentropySoftmax1HotWithBiasDx(GpuOp):
}
""" % locals()
gpu_crossentropy_softmax_1hot_with_bias_dx = GpuCrossentropySoftmax1HotWithBiasDx()
gpu_crossentropy_softmax_1hot_with_bias_dx = \
GpuCrossentropySoftmax1HotWithBiasDx()
class GpuSoftmax(GpuOp):
......
......@@ -7,13 +7,13 @@ import theano.tests.unittest_tools as utt
import theano.sandbox.tests.test_blocksparse
import theano.sandbox.cuda as cuda_ndarray
if not cuda_ndarray.cuda_available:
raise SkipTest('Optional package cuda disabled')
from theano.sandbox.cuda.blocksparse import (GpuSparseBlockOuter,
gpu_sparse_block_gemv,
gpu_sparse_block_outer)
from theano.sandbox.cuda.var import float32_shared_constructor
if not cuda_ndarray.cuda_available:
raise SkipTest('Optional package cuda disabled')
if theano.config.mode == 'FAST_COMPILE':
mode_with_gpu = theano.compile.mode.get_mode('FAST_RUN').including('gpu')
......
......@@ -766,11 +766,10 @@ def test_blocksparse_gpu_outer_opt():
o = sparse_block_dot(W, h, iIdx, b, oIdx)
theano.printing.debugprint(tensor.grad(o.sum(),wrt=W))
f = theano.function([W, h, iIdx, b, oIdx], [o, tensor.grad(o.sum(),wrt=W)],
f = theano.function([W, h, iIdx, b, oIdx], [o, tensor.grad(o.sum(),
wrt=W)],
mode=mode_with_gpu)
assert isinstance(f.maker.fgraph.toposort()[-2].op, GpuSparseBlockOuter)
......@@ -785,10 +784,8 @@ class test_diag(theano.tensor.tests.test_nlinalg.test_diag):
self).__init__(name)
if __name__ == '__main__':
test_gpualloc()
test_opt_gpujoin_onlyajoin()
test_opt_gpujoin_joinvectors_elemwise_then_minusone()
test_opt_gpujoin_joinvectors_negativeaxes()
......@@ -126,7 +126,7 @@ def register_meta_opt(op_class, db_name, position, *args):
idx = bisect.bisect_left((positions, local_meta_opt),
op_class.registered_opts)
op_class.registered_opts.insert(idx,
(positions, local_meta_opt.__name__))
(positions, local_meta_opt.__name__))
return local_meta_opt
......
......@@ -2,7 +2,6 @@
Tests for block sparse dot
"""
import unittest
import time
import numpy
from numpy.random import randn
......@@ -11,8 +10,8 @@ import theano
from theano import tensor
import theano.tests.unittest_tools as utt
from theano.sandbox.blocksparse import sparse_block_dot, cpu_sparse_block_gemv, \
cpu_sparse_block_outer
from theano.sandbox.blocksparse import sparse_block_dot, \
cpu_sparse_block_gemv, cpu_sparse_block_outer
class BlockSparse_Gemv_and_Outer(unittest.TestCase):
......@@ -94,6 +93,9 @@ class BlockSparse_Gemv_and_Outer(unittest.TestCase):
@staticmethod
def gemv_numpy2(o, W, h, iIdx, oIdx):
"""
Other implementation
"""
from numpy import ix_
for b in range(o.shape[0]):
w = W[ix_(iIdx[b], oIdx[b])].swapaxes(1, 2)
......@@ -103,9 +105,14 @@ class BlockSparse_Gemv_and_Outer(unittest.TestCase):
@staticmethod
def gemv_numpy3(o, W, h, iIdx, oIdx):
"""
Other implementation
"""
from numpy import ix_
for b in range(o.shape[0]):
w = W[ix_(iIdx[b], oIdx[b])]
# The next three lines do the same operation. The last one is the
# fastest
# o[b] += (h[b][:, None, :, None] * w).sum(axis=(0, 2))
# o[b] += numpy.tensordot(h[b], w, [(0,1),(0,2)])
o[b] += numpy.einsum('ik,ijkl', h[b], w)
......@@ -135,27 +142,6 @@ class BlockSparse_Gemv_and_Outer(unittest.TestCase):
return weight, input, inputIndice, bias, outputIndice
@staticmethod
def compare():
W_val, h_val, iIdx_val, b_val, oIdx_val = \
BlockSparse_Gemv_and_Outer.gemv_data2()
start = time.clock()
ref_out = BlockSparse_Gemv_and_Outer.gemv_numpy(
b_val.take(oIdx_val, axis=0), W_val, h_val, iIdx_val, oIdx_val)
v1 = time.clock()
ref_out_2 = BlockSparse_Gemv_and_Outer.gemv_numpy2(
b_val.take(oIdx_val, axis=0), W_val, h_val, iIdx_val, oIdx_val)
v2 = time.clock()
ref_out_3 = BlockSparse_Gemv_and_Outer.gemv_numpy3(
b_val.take(oIdx_val, axis=0), W_val, h_val, iIdx_val, oIdx_val)
v3 = time.clock()
print v1 - start
print v2 - v1
print v3 - v2
# utt.assert_allclose(ref_out, ref_out_2)
@staticmethod
def outer_numpy(o, x, y, xIdx, yIdx):
for b in range(x.shape[0]):
......@@ -185,7 +171,7 @@ class BlockSparse_Gemv_and_Outer(unittest.TestCase):
th_out = f(W_val, h_val, iIdx_val, b_val, oIdx_val)
ref_out = BlockSparse_Gemv_and_Outer.gemv_numpy(
b_val.take(oIdx_val, axis=0), W_val, h_val, iIdx_val, oIdx_val)
b_val.take(oIdx_val, axis=0), W_val, h_val, iIdx_val, oIdx_val)
utt.assert_allclose(ref_out, th_out)
......@@ -208,7 +194,7 @@ class BlockSparse_Gemv_and_Outer(unittest.TestCase):
th_out = f(W_val, h_val, iIdx_val, b_val, oIdx_val)
ref_out = BlockSparse_Gemv_and_Outer.gemv_numpy(
b_val.take(oIdx_val, axis=0), W_val, h_val, iIdx_val, oIdx_val)
b_val.take(oIdx_val, axis=0), W_val, h_val, iIdx_val, oIdx_val)
utt.assert_allclose(ref_out, th_out)
......@@ -224,9 +210,10 @@ class BlockSparse_Gemv_and_Outer(unittest.TestCase):
oIdx = tensor.imatrix()
o = self.gemv_op(b.take(oIdx, axis=0),
tensor.DimShuffle((False, False, False, False),
(0, 1, 3, 2))(tensor.as_tensor_variable(W)),
h, iIdx, oIdx)
tensor.DimShuffle((False, False, False, False),
(0, 1, 3, 2))
(tensor.as_tensor_variable(W)),
h, iIdx, oIdx)
f = theano.function([W, h, iIdx, b, oIdx], o, mode=self.mode)
......@@ -236,7 +223,7 @@ class BlockSparse_Gemv_and_Outer(unittest.TestCase):
th_out = f(numpy.swapaxes(W_val, 2, 3), h_val, iIdx_val, b_val,
oIdx_val)
ref_out = BlockSparse_Gemv_and_Outer.gemv_numpy(
b_val.take(oIdx_val, axis=0), W_val, h_val, iIdx_val, oIdx_val)
b_val.take(oIdx_val, axis=0), W_val, h_val, iIdx_val, oIdx_val)
utt.assert_allclose(ref_out, th_out)
......
......@@ -4,7 +4,7 @@ import numpy
import theano
from theano import config, function, tensor
from . import multinomial
from theano.sandbox import multinomial
from theano.compile.mode import get_default_mode, predefined_linkers
import theano.sandbox.cuda as cuda
......
import theano
from theano import tensor
from theano.sandbox.blocksparse import CpuSparseBlockGemv, CpuSparseBlockOuter, sparse_block_dot
from theano.sandbox.blocksparse import CpuSparseBlockGemv, \
CpuSparseBlockOuter, sparse_block_dot
def test_blocksparse_cpu_gemv_opt():
......@@ -26,8 +27,9 @@ def test_blocksparse_cpu_outer_opt():
o = sparse_block_dot(W, h, iIdx, b, oIdx)
theano.printing.debugprint(tensor.grad(o.sum(),wrt=W))
theano.printing.debugprint(tensor.grad(o.sum(), wrt=W))
f = theano.function([W, h, iIdx, b, oIdx],
[o, tensor.grad(o.sum(), wrt=W)])
f = theano.function([W, h, iIdx, b, oIdx], [o, tensor.grad(o.sum(),wrt=W)])
assert isinstance(f.maker.fgraph.toposort()[-1].op, CpuSparseBlockOuter)
import theano
import numpy
from . import scan
from theano.sandbox import scan
def test_001():
......
from __future__ import print_function
from .theano_object import *
from theano.sandbox.theano_object import *
RUN_TESTS = False
......
......@@ -98,14 +98,15 @@ whitelist_flake8 = [
"tensor/nnet/tests/test_sigm.py",
"scalar/__init__.py",
"scalar/tests/test_basic.py",
"sandbox/test_theano_object.py",
"sandbox/test_scan.py",
"sandbox/tests/test_theano_object.py",
"sandbox/tests/test_scan.py",
"sandbox/tests/test_rng_mrg.py",
"sandbox/tests/test_neighbourhoods.py",
"sandbox/tests/test_multinomial.py",
"sandbox/tests/__init__.py"
"sandbox/rng_mrg.py",
"sandbox/theano_object.py",
"sandbox/scan.py",
"sandbox/test_multinomial.py",
"sandbox/test_rng_mrg.py",
"sandbox/test_neighbourhoods.py",
"sandbox/symbolic_module.py",
"sandbox/conv.py",
"sandbox/debug.py",
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论