Deep learning和tensorflow学习记录(二十四):tf.stack

tf.stack(
    values,
    axis=0,
    name='stack'

)

定义于:tensorflow/python/ops/array_ops.py。

将秩为R的tensors集合合并为一个秩为R+1的tensor。

将values中的tensor集合打包成一个比values中的每个tensor秩大一的tensor,在axis维度上进行打包。给出一个长度为N的tensor集合,形状是(A, B, C);

如果axis == 0,那么output的tensor将具有形状(N, A, B, C)。

如果axis == 1,那么output的tensor将具有形状(A, N, B, C)。

例如:

x = tf.constant([1, 4])
y = tf.constant([2, 5])
z = tf.constant([3, 6])
tf.stack([x, y, z])  # [[1, 4], [2, 5], [3, 6]] (Pack along first dim.)
tf.stack([x, y, z], axis=1)  # [[1, 2, 3], [4, 5, 6]]

这与unstack相反。numpy等价物是

tf.stack([x, y, z]) = np.stack([x, y, z])

参数:
values:具有相同形状和类型的Tensor对象列表。

axis:一个nt。沿着axis打包。默认为第一个维度。有效范围是[-(R+1), R+1)。

name:此操作的名称(可选)。

返回:
output:打包后的Tensor,与values相同的类型。

猜你喜欢

转载自blog.csdn.net/heiheiya/article/details/81064545
今日推荐