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

Fix a few pep8 errors

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