提交 81841795 authored 作者: Arnaud Bergeron's avatar Arnaud Bergeron

Fix fugly blocksparse doc formatting.

上级 d451fc3b
...@@ -10,7 +10,8 @@ from theano.gradient import grad_undefined ...@@ -10,7 +10,8 @@ from theano.gradient import grad_undefined
class SparseBlockGemv(Op): class SparseBlockGemv(Op):
""" """
This op computes the dot product of specified pieces of vectors This op computes the dot product of specified pieces of vectors
and matrices, returning pieces of vectors: and matrices, returning pieces of vectors::
for b in range(batch_size): for b in range(batch_size):
for j in range(o.shape[1]): for j in range(o.shape[1]):
for i in range(h.shape[1]): for i in range(h.shape[1]):
...@@ -20,6 +21,7 @@ class SparseBlockGemv(Op): ...@@ -20,6 +21,7 @@ class SparseBlockGemv(Op):
.. image:: ../../images/blocksparse.png .. image:: ../../images/blocksparse.png
:scale: 50 % :scale: 50 %
""" """
registered_opts = [] registered_opts = []
...@@ -34,29 +36,40 @@ class SparseBlockGemv(Op): ...@@ -34,29 +36,40 @@ class SparseBlockGemv(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.
The parameter types are actually their expected shapes
relative to each other.
Parameters Parameters
---------- ----------
var: shape, comment o : batch, oWin, oSize
o: (batch, oWin, oSize) output vector output vector
W: (iBlocks, oBlocks, iSize, oSize), weight matrix W : iBlocks, oBlocks, iSize, oSize
h: (batch, iWin, iSize), input from lower layer (sparse) weight matrix
inputIdx: (batch, iWin), indexes of the input blocks h : batch, iWin, iSize
outputIdx: (batch, oWin), indexes of the output blocks input from lower layer (sparse)
returns (batch, oWin, oSize), dot(W[i, j], h[i]) + o[j] inputIdx : batch, iWin
indexes of the input blocks
Notation outputIdx : batch, oWin
-------- indexes of the output blocks
Returns
-------
(batch, oWin, oSize)
dot(W[i, j], h[i]) + o[j]
Notes
-----
- `batch` is the number of examples in a minibatch (batch size). - `batch` is the number of examples in a minibatch (batch size).
- `iBlocks` is the total number of blocks in the input (from lower - `iBlocks` is the total number of blocks in the input (from lower
layer). layer).
- `iSize` is the size of each of these input blocks. - `iSize` is the size of each of these input blocks.
- `iWin` is the number of blocks that will be used as inputs. Which - `iWin` is the number of blocks that will be used as inputs. Which
blocks blocks will be used is specified in `inputIdx`.
will be used is specified in `inputIdx`.
- `oBlocks` is the number or possible output blocks. - `oBlocks` is the number or possible output blocks.
- `oSize` is the size of each of these output blocks. - `oSize` is the size of each of these output blocks.
- `oWin` is the number of output blocks that will actually be computed. - `oWin` is the number of output blocks that will actually be computed.
Which blocks will be computed is specified in `outputIdx`. Which blocks will be computed is specified in `outputIdx`.
""" """
o = theano.tensor.as_tensor_variable(o) o = theano.tensor.as_tensor_variable(o)
W = theano.tensor.as_tensor_variable(W) W = theano.tensor.as_tensor_variable(W)
...@@ -118,10 +131,13 @@ class SparseBlockGemv(Op): ...@@ -118,10 +131,13 @@ class SparseBlockGemv(Op):
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
updating a full matrix with the results: updating a full matrix with the results::
for b in range(batch_size): for b in range(batch_size):
o[xIdx[b, i], yIdx[b, j]] += (alpha * outer(x[b, i], y[b, j])) o[xIdx[b, i], yIdx[b, j]] += (alpha * outer(x[b, i], y[b, j]))
This op is involved in the gradient of SparseBlockGemv. This op is involved in the gradient of SparseBlockGemv.
""" """
registered_opts = [] registered_opts = []
...@@ -136,18 +152,26 @@ class SparseBlockOuter(Op): ...@@ -136,18 +152,26 @@ 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.
The parameter types are actually their expected shapes
relative to each other.
Parameters Parameters
---------- ----------
var: shape, comment o : xBlocks, yBlocks, xSize, ySize
o: (xBlocks, yBlocks, xSize, ySize) x : batch, xWin, xSize
x: (batch, xWin, xSize) y : batch, yWin, ySize
y: (batch, yWin, ySize) xIdx : batch, iWin
xIdx: (batch, iWin), indexes of the x blocks indexes of the x blocks
yIdx: (batch, oWin), indexes of the y blocks yIdx : batch, oWin
returns (xBlocks, yBlocks, xSize, ySize), outer(x[i], y[j]) + o[i, j] indexes of the y blocks
Notation Returns
-------- -------
(xBlocks, yBlocks, xSize, ySize)
outer(x[i], y[j]) + o[i, j]
Notes
-----
- `batch` is the number of examples in a minibatch (batch size). - `batch` is the number of examples in a minibatch (batch size).
- `xBlocks` is the total number of blocks in x. - `xBlocks` is the total number of blocks in x.
- `xSize` is the size of each of these x blocks. - `xSize` is the size of each of these x blocks.
...@@ -157,6 +181,7 @@ class SparseBlockOuter(Op): ...@@ -157,6 +181,7 @@ class SparseBlockOuter(Op):
- `ySize` is the size of each of these y blocks. - `ySize` is the size of each of these y blocks.
- `yWin` is the number of y blocks that will actually be computed. - `yWin` is the number of y blocks that will actually be computed.
Which blocks will be computed is specified in `yIdx`. Which blocks will be computed is specified in `yIdx`.
""" """
one = tensor.constant(numpy.asarray(1.0, dtype='float32')) one = tensor.constant(numpy.asarray(1.0, dtype='float32'))
o = theano.tensor.as_tensor_variable(o) o = theano.tensor.as_tensor_variable(o)
...@@ -197,27 +222,38 @@ def sparse_block_dot(W, h, inputIdx, b, outputIdx): ...@@ -197,27 +222,38 @@ def sparse_block_dot(W, h, inputIdx, b, outputIdx):
Compute the dot product (plus bias) of the specified pieces of vectors Compute the dot product (plus bias) of the specified pieces of vectors
and matrices. See SparseBlockGemv to get more information. and matrices. See SparseBlockGemv to get more information.
The parameter types are actually their expected shapes relative to
each other.
Parameters Parameters
---------- ----------
var: shape, comment W : iBlocks, oBlocks, iSize, oSize
W: (iBlocks, oBlocks, iSize, oSize), weight matrix weight matrix
h: (batch, iWin, iSize), input from lower layer (sparse) h : batch, iWin, iSize
inputIdx: (batch, iWin), indexes of the input blocks input from lower layer (sparse)
b: (oBlocks, oSize), bias vector inputIdx : batch, iWin
outputIdx: (batch, oWin), indexes of the output blocks indexes of the input blocks
returns (batch, oWin, oSize), dot(W[i, j], h[i]) + b[j] b : oBlocks, oSize
but b[j] is only added once bias vector
Notation outputIdx : batch, oWin
-------- indexes of the output blocks
Returns
-------
(batch, oWin, oSize)
dot(W[i, j], h[i]) + b[j] but b[j] is only added once
Notes
-----
- `batch` is the number of examples in a minibatch (batch size). - `batch` is the number of examples in a minibatch (batch size).
- `iBlocks` is the total number of blocks in the input (from lower layer). - `iBlocks` is the total number of blocks in the input (from lower layer).
- `iSize` is the size of each of these input blocks. - `iSize` is the size of each of these input blocks.
- `iWin` is the number of blocks that will be used as inputs. Which blocks - `iWin` is the number of blocks that will be used as inputs. Which blocks
will be used is specified in `inputIdx`. will be used is specified in `inputIdx`.
- `oBlocks` is the number or possible output blocks. - `oBlocks` is the number or possible output blocks.
- `oSize` is the size of each of these output blocks. - `oSize` is the size of each of these output blocks.
- `oWin` is the number of output blocks that will actually be computed. - `oWin` is the number of output blocks that will actually be computed.
Which blocks will be computed is specified in `outputIdx`. Which blocks will be computed is specified in `outputIdx`.
""" """
assert inputIdx.ndim == h.ndim - 1 assert inputIdx.ndim == h.ndim - 1
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论