tensorflow 元素重复方法汇总

1. tf.repeat

>>> a = tf.repeat(['a', 'b', 'c'], repeats=[3, 1, 2], axis=0)
[b'a' b'a' b'a' b'b' b'c' b'c']

>>> a = tf.repeat(['a', 'b', 'c'], repeats=[3, 1, 2], axis=1)
[b'a' b'a' b'a' b'c' b'c']

>>> out = tf.repeat([[1, 2], [3, 4]], repeats=[2, 3], axis=0)
[[1 2]
 [1 2]
 [3 4]
 [3 4]
 [3 4]]

>>> out = tf.repeat([[1, 2], [3, 4]], repeats=[2, 3], axis=1)
[[1 1 2 2 2]
 [3 3 4 4 4]]

2. tf.stack

>>> x = tf.constant([1, 4])
>>> y = tf.constant([2, 5])
>>> z = tf.constant([3, 6])
>>> out = tf.stack([x, y, z]) # 默认横坐标拼接
[[1 4]
 [2 5]
 [3 6]]

>>> out = tf.stack([x, y, z], axis=1) # 按纵坐标拼接
[[1 2 3]
 [4 5 6]]

3.tf.tile

>>> a = tf.constant([[1,2,3],[4,5,6]], tf.int32)
>>> b = tf.constant([1,2], tf.int32)
>>> tf.tile(a, b)
[[1,2,3,1,2,3],
 [4,5,6,4,5,6]
]

>>> d = tf.constant([2,2], tf.int32)
>>> tf.tile(a, d)
[[1,2,3,1,2,3],
 [4,5,6,4,5,6],
 [1,2,3,1,2,3],
 [4,5,6,4,5,6]
]

  4. tf.concat

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


>>> tf.concat([t1, t2], 1)
[[1, 2, 3, 7, 8, 9], 
[4, 5, 6, 10, 11, 12]
]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值