Tensor的分割与合并,维度的阐述 red, green, blue = tf.split(rgb, 3, 3)

tf.split(split_dim, num_split, value, name='split')

Splits value along dimension split_dim into num_split smaller tensors. Requires that num_split evenly divide value.shape[split_dim].

参数:

split_dim:  A 0-D int32 Tensor. The dimension along which to split. Must be in the range [0, rank(value)).

num_split: A 0-D int32 Tensor. The number of ways to split.

value: The Tensor to split.

name: A name for the operation (optional).

返回值:

num_split Tensor objects resulting from splitting value.

举例,分割value,沿着第1维度分割成3个小的tensor。(维度是从0开始的,一个tensor的shape如果是(3,2,4,5,1)那么这个tensor总共是 5维度的,第0维度一直到第四维度对应的位置是从左到右的)分割tensor的时候要注意num_split要能被slipt_dim对应位置上的数整除,在下面这个例子中第一维度是30,可以整除3.

# 'value' is a tensor with shape [5, 30]
# Split 'value' into 3 tensors along dimension 1
split0, split1, split2 = tf.split(1, 3, value)
tf.shape(split0) ==> [5, 10]

 另外一种切割tensor的方式

tf.slice(input_, begin, size, name=None)

作用是从输入的input_这个tensor里面从begin这个位置开始提取出想要size大小的tensor

begin和size都是一个列表

注意:begin和size的元素个数应该和input的shape的维度数目相同。begin里面的元素是从0开始的,size里面的元素是从1开始的,如果size里面的元素出现-1那么代表元素全选

选取的过程的规则是:从外到内

# 'input' is [[[1, 1, 1], [2, 2, 2]],
#             [[3, 3, 3], [4, 4, 4]],
#             [[5, 5, 5], [6, 6, 6]]]
#  'input' shape is (3,2,3)
(1)tf.slice(input, [1, 0, 0], [1, 1, 3]) ==> [[[3, 3, 3]]]
(2)tf.slice(input, [1, 0, 0], [1, 2, 3]) ==> [[[3, 3, 3],
                                            [4, 4, 4]]]
(3)tf.slice(input, [1, 0, 0], [2, 1, 3]) ==> [[[3, 3, 3]],
                                           [[5, 5, 5]]]
在这里我们可以理解分割下来的部分,格式为 位置:大小
(1)对input取[1:1, 0:1, 0:3],对第0维的第1个位置(实际上是第2个,因为位置是从0开始的)开始取1个元素,在第1维的第0个位置开始取1个元素,在第2维的第0个位置开始取3个元素,遵从从外往里一层一层取的原则。最后的到了(1,1,3)这样大小的tensor
(2)对input取(1:1,2:2,0:3).对第0维的第1个位置开始取1个元素,对第1维的第2个位置开始取2个元素,对第2维的第0个位置开始取3个元素,最后取出来的大小就是(1,2,3)的tensor
(3)对input取(1:2,0:1,0:3). 对第0维的第1个位置开始取2个元素,对第1维的第0个位置开始取1个元素,对第2维的第0个位置开始取3个元素

判断一个tensor的shape,遵从规则是从内到外,从后到前 

拓展,从数据上迅速判断一个tensor的shape,遵从规则是从内到外,从后到前。
比如a = [[[1,2,3,4],[1,3,3,3],[2,2,2,2]],[[1,1,1,1],[3,3,3,3],[4,4,4,4]]]
从内到外就是:最里面[1,2,3,4]是4,接着和这个并列的是[1,2,3,4],[1,3,3,3],[2,2,2,2],是3个;接着包括这三个的一个大框有两个[[1,2,3,4],[1,3,3,3],[2,2,2,2]]和[[1,1,1,1],[3,3,3,3],[4,4,4,4]]。所以相应的维数目从后到前是4,3,2.因此总结出来,这个tensor的shape = (2,3,4)

 

 合并

tf.concat(concat_dim, values, name='concat')

Concatenates(连接) tensors along one dimension.

Concatenates the list of tensors values along dimension concat_dim. If values[i].shape = [D0, D1, ... Dconcat_dim(i), ...Dn], the concatenated result has shape

[ D0,D1,......,Reconcat_dim,......,Dn] , where  Reconcat_dim = sum(Dconcat_dim(i))

That is, the data from the input tensors is joined along the concat_dim dimension. The number of dimensions of the input tensors must match, and all dimensions except concat_dim must be equal.

参数:

  • concat_dim: 0-D int32 Tensor. Dimension along which to concatenate.从0开始
  • values: A list of Tensor objects or a single Tensor.
  • name: A name for the operation (optional).

 返回值:A Tensor resulting from concatenation of the input tensors.

t1 = [[1, 2, 3], [4, 5, 6]]
t2 = [[7, 8, 9], [10, 11, 12]]
tf.concat(0, [t1, t2]) ==> [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
tf.concat(1, [t1, t2]) ==> [[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]]

# tensor t3 with shape [2, 3]
# tensor t4 with shape [2, 3]
tf.shape(tf.concat(0, [t3, t4])) ==> [4, 3]
tf.shape(tf.concat(1, [t3, t4])) ==> [2, 6]

如果concat_dim = 0,则在第0维度增加或者说是连接,其他维度的维度大小保持不变,对应到实际就是叠放到列上,相当于axis=0(表述列);如果concat_dim = 1,则在第1维度增加或者说是连接,其他维度的维度大小保持不变,对应到实际就是叠放到行上,相当于axis=1(表述行)

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值