chainer.functions.convolution_nd¶
-
chainer.functions.
convolution_nd
(x, W, b=None, stride=1, pad=0, cover_all=False, dilate=1, groups=1)[source]¶ N-dimensional convolution function.
This is an implementation of N-dimensional convolution which is generalized two-dimensional convolution in ConvNets. It takes three variables: the input
x
, the filter weightW
and the bias vectorb
.Notation: here is a notation for dimensionalities.
N is the number of spatial dimensions.
n is the batch size.
cI and cO are the number of the input and output channels, respectively.
d1,d2,...,dN are the size of each axis of the input’s spatial dimensions, respectively.
k1,k2,...,kN are the size of each axis of the filters, respectively.
l1,l2,...,lN are the size of each axis of the output’s spatial dimensions, respectively.
p1,p2,...,pN are the size of each axis of the spatial padding size, respectively.
Then the
convolution_nd
function computes correlations between filters and patches of size (k1,k2,...,kN) inx
. Note that correlation here is equivalent to the inner product between expanded tensors. Patches are extracted at positions shifted by multiples ofstride
from the first position(-p_1, -p_2, ..., -p_N)
for each spatial axis.Let (s1,s2,...,sN) be the stride of filter application. Then, the output size (l1,l2,...,lN) is determined by the following equations:
ln=(dn+2pn−kn)/sn+1 (n=1,...,N)If
cover_all
option isTrue
, the filter will cover the all spatial locations. So, if the last stride of filter does not cover the end of spatial locations, an addtional stride will be applied to the end part of spatial locations. In this case, the output size is determined by the following equations:ln=(dn+2pn−kn+sn−1)/sn+1 (n=1,...,N)- Parameters
x (
Variable
or N-dimensional array) – Input variable of shape (n,cI,d1,d2,...,dN).W (
Variable
or N-dimensional array) – Weight variable of shape (cO,cI,k1,k2,...,kN).b (None or
Variable
or N-dimensional array) – One-dimensional bias variable with length cO (optional).stride (
int
ortuple
ofint
s) – Stride of filter applications (s1,s2,...,sN).stride=s
is equivalent to(s, s, ..., s)
.pad (
int
ortuple
ofint
s) – Spatial padding width for input arrays (p1,p2,...,pN).pad=p
is equivalent to(p, p, ..., p)
.cover_all (bool) – If
True
, all spatial locations are convoluted into some output pixels. It may make the output size larger. cover_all needs to beFalse
if you want to use cuDNN.dilate (
int
ortuple
ofint
s) – Dilation factor of filter applications.dilate=d
anddilate=(d, d, ..., d)
are equivalent.groups (
int
) – The number of groups to use grouped convolution. The default is one, where grouped convolution is not used.
- Returns
Output variable of shape (n,cO,l1,l2,...,lN).
- Return type
Note
This function uses cuDNN implementation for its forward and backward computation if ALL of the following conditions are satisfied:
cuda.cudnn_enabled
isTrue
chainer.config.use_cudnn
is'always'
or'auto'
The number of spatial dimensions is more than one.
cover_all
isFalse
The input’s
dtype
is equal to the filter weight’s.The
dtype
is FP16, FP32 or FP64. (FP16 is only available when cuDNN version ≥ v3.)
Convolution links can use a feature of cuDNN called autotuning, which selects the most efficient CNN algorithm for images of fixed-size, can provide a significant performance boost for fixed neural nets. To enable, set chainer.using_config(‘autotune’, True)
See also
Example
>>> n = 10 >>> c_i, c_o = 3, 1 >>> d1, d2, d3 = 30, 40, 50 >>> k1, k2, k3 = 10, 10, 10 >>> p1, p2, p3 = 5, 5, 5 >>> x = np.random.uniform(0, 1, (n, c_i, d1, d2, d3)).astype(np.float32) >>> x.shape (10, 3, 30, 40, 50) >>> W = np.random.uniform(0, 1, (c_o, c_i, k1, k2, k3)).astype(np.float32) >>> W.shape (1, 3, 10, 10, 10) >>> b = np.random.uniform(0, 1, (c_o)).astype(np.float32) >>> b.shape (1,) >>> s1, s2, s3 = 2, 4, 6 >>> y = F.convolution_nd(x, W, b, stride=(s1, s2, s3), pad=(p1, p2, p3)) >>> y.shape (10, 1, 16, 11, 9) >>> l1 = int((d1 + 2 * p1 - k1) / s1 + 1) >>> l2 = int((d2 + 2 * p2 - k2) / s2 + 1) >>> l3 = int((d3 + 2 * p3 - k3) / s3 + 1) >>> y.shape == (n, c_o, l1, l2, l3) True >>> y = F.convolution_nd(x, W, b, stride=(s1, s2, s3), pad=(p1, p2, p3), cover_all=True) >>> y.shape == (n, c_o, l1, l2, l3 + 1) True