# TensorFlow张量变换：tf.space_to_batch_nd函数

## tf.space_to_batch_nd 函数

``````space_to_batch_nd(
input,
block_shape,
name=None
)``````

### 函数参数

• input：一个Tensor；是N维的并且具有形状input_shape = [batch] + spatial_shape + remaining_shape,其中spatial_shape有M维度.
• block_shape：一个Tensor；必须是以下类型之一：int32,int64；1-D,并且具有形状[M],所有值必须>=1.
``[batch] + [padded_shape[1] / block_shape[0], block_shape[0], ..., padded_shape[M] / block_shape[M-1], block_shape[M-1]] + remaining_shape            ``
``block_shape + [batch] + [padded_shape[1] / block_shape[0], ..., padded_shape[M] / block_shape[M-1]] + remaining_shape``
``[batch * prod(block_shape)] + [padded_shape[1] / block_shape[0], ..., padded_shape[M] / block_shape[M-1]] + remaining_shape                ``
一些例子：
(1)以下形状为[1, 2, 2, 1]的输入中,block_shape = [2, 2],并且paddings = [[0, 0], [0, 0]]：
``x = [[[[1], [2]], [[3], [4]]]]``

输出张量具有形状[4, 1, 1, 1]和值：

``[[[[1]]], [[[2]]], [[[3]]], [[[4]]]]``

(2)以下形状为[1, 2, 2, 3]的输入中,block_shape = [2, 2],并且paddings = [[0, 0], [0, 0]]：

``x = [[[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]]``

输出张量具有形状[4, 1, 1, 3]和值：

``[[[1, 2, 3]], [[4, 5, 6]], [[7, 8, 9]], [[10, 11, 12]]]``

(3)以下形状为[1, 4, 4, 1]的输入中,block_shape = [2, 2],并且paddings = [[0, 0], [0, 0]]：

``x = [[[[1], [2], [3], [4]], [[5], [6], [7], [8]], [[9], [10], [11], [12]], [[13], [14], [15], [16]]]]``

输出张量具有形状[4, 2, 2, 1]和值：

``x = [[[[1], [3]], [[9], [11]]], [[[2], [4]], [[10], [12]]], [[[5], [7]], [[13], [15]]], [[[6], [8]], [[14], [16]]]]``

(4)以下形状为[2, 2, 4, 1]的输入中,block_shape = [2, 2],并且paddings = [[0, 0], [2, 0]]：

``x = [[[[1], [2], [3], [4]], [[5], [6], [7], [8]]], [[[9], [10], [11], [12]], [[13], [14], [15], [16]]]]``

输出张量具有形状[8, 1, 3, 1]和值：

``x = [[[[0], [1], [3]]], [[[0], [9], [11]]], [[[0], [2], [4]]], [[[0], [10], [12]]], [[[0], [5], [7]]], [[[0], [13], [15]]], [[[0], [6], [8]]], [[[0], [14], [16]]]]``

除此之外,这个操作对于减少卷积成正则卷积非常有用,其中,name：操作的名称(可选).

### 函数返回

tf.space_to_batch_nd函数返回一个Tensor,它与input具有相同的类型.

App下载