space_to_depth
该OP对成块的空间数据进行重组,输出一个输入张量的拷贝,其高度和宽度维度上的值移至通道维度。
重组时,依据 blocksize
指明的数据块大小, 对形为 [batch, channel, height, width]
的输入张量进行space_to_depth(广度至深度)运算,生成形为 [batch, channel * blocksize * blocksize, height/blocksize, width/blocksize]
的输出:
该OP适用于在卷积间重放缩激活函数,并保持所有的数据。
范例如下:
参数:
blocksize (int) – 在每个特征图上选择元素时采用的块大小,应该 >= 2
name (str,可选) - 具体用法请参见 ,一般无需设置,默认值为None。
返回:输出,形状为 [batch, channel * blocksize * blocksize, height/blocksize, width/blocksize]
的4维Tensor或LoD Tensor。数据类型与输入 x
一致。
返回类型:Variable
TypeError
-blocksize
必须是int64类型
代码示例
import numpy as np
data = fluid.data(
name='data', shape=[1, 4, 2, 2], dtype='float32')
space_to_depthed = fluid.layers.space_to_depth(
x=data, blocksize=2)
exe = fluid.Executor(fluid.CPUPlace())
data_np = np.arange(0,16).reshape((1,4,2,2)).astype('float32')
print(data_np)
#array([[[[ 0., 1.], [ 2., 3.]],
# [[ 8., 9.], [10., 11.]],
# [[12., 13.], [14., 15.]]]], dtype=float32)
out_main = exe.run(fluid.default_main_program(),
feed={'data': data_np},
fetch_list=[space_to_depthed])
print(out_main)
#[array([[[[ 0.]], [[ 4.]], [[ 1.]], [[ 5.]],
# [[ 8.]], [[12.]], [[ 9.]], [[13.]],
# [[10.]], [[14.]], [[11.]], [[15.]]]], dtype=float32)]