TensorFlow —— expand_dim()函数

来源:互联网 发布:银龙裁决多少钱淘宝价 编辑:程序博客网 时间:2024/06/05 20:45

该函数用于增加tensor(张量)的维数
官方的例子和说明:

# 't' is a tensor of shape [2]shape(expand_dims(t, 0)) ==> [1, 2]shape(expand_dims(t, 1)) ==> [2, 1]shape(expand_dims(t, -1)) ==> [2, 1]# 't2' is a tensor of shape [2, 3, 5]shape(expand_dims(t2, 0)) ==> [1, 2, 3, 5]shape(expand_dims(t2, 2)) ==> [2, 3, 1, 5]shape(expand_dims(t2, 3)) ==> [2, 3, 5, 1]

Args:
input: A Tensor.
dim: A Tensor. Must be one of the following types: int32, int64. 0-D (scalar). Specifies the dimension index at which to expand the shape of input.
name: A name for the operation (optional).
Returns:
A Tensor. Has the same type as input. Contains the same data as input, but its shape has an additional dimension of size 1 added.

使用时需要注意几个点:
1. 一次只能增加一个维度
2. 数据不变,所以增加的维度的数值都是1。比如官方第一个例子里,t 的shape由[2]变成[1,2]或者[2,1],其实[2]和[1,2]除了维度数之外没有区别,即都是含两个元素,这样就保证数据是不变的。增加的维度的数值必须是1,不可能出现由[2]变成[3,2]或者[2,2]这种情况
3. 注意插入维度的位置,从0开始,-1代表结尾。[(0) a1 (1) a2 (2) a3 (3 or -1)],输入是几,就是特定的位置插入一个 “,1,”

原创粉丝点击