tile 的含義:
n. 瓦 / 瓷磚 / 軟木片 / 亞麻油地氈片
v. 用瓦蓋 / 貼磚于 / 鋪以瓦 / 鋪以瓷磚
函數(shù)定義:
def tile(input, multiples, name=None):
函數(shù)功能:
Constructs a tensor by tiling a given tensor.
即:通過“平鋪”一個給定的 tensor 來構(gòu)造一個新的 tensor旺入。用人話講就是:把輸入的 tensor烫止,在指定的維度上復制N遍(就像鋪瓷磚一樣)更耻,來創(chuàng)建出一個新的 tensor嫁审。
3個參數(shù):
input:輸入的tensor
multiples:在指定的維度上復制原tensor的次數(shù)
name:operation的名字
下面舉例:
import tensorflow as tf
with tf.Session() as sess:
a = tf.constant([[15, 16], [17, 18]])
b = tf.tile(a, [1, 3])
c = tf.tile(a, [3, 2])
print('------------------------------------')
print(sess.run(a))
print('------------------------------------')
print(sess.run(b))
print('------------------------------------')
print(sess.run(c))
輸出:
[[15 16]
[17 18]]
[[15 16 15 16 15 16]
[17 18 17 18 17 18]]
[[15 16 15 16]
[17 18 17 18]
[15 16 15 16]
[17 18 17 18]
[15 16 15 16]
[17 18 17 18]]
解釋一下:
輸入的 a 是一個 2x2 的矩陣,tf.tile(a, [1, 3]) 里的 [1, 3] 表示在第一個維度上把輸入的tensor重復1遍借浊,在第二個維度上把輸入的tensor重復3遍挑胸。在本例中揽思,第一個維度就是行悉抵,第二個維度就是列肩狂,因此 b 就變成了 2x6 的矩陣。
注意:tf.tile() 里的第2個參數(shù)姥饰,例如 [1, 3]傻谁,里面有兩個元素,它必須與輸入的 tensor 的維度一樣(2維)列粪,如果輸入的 tensor 是3維的审磁,那么 tf.tile() 的第2個參數(shù)里也必須有3個元素,例如 [2, 3, 5]岂座,否則會報類似于下面的錯:
ValueError: Shape must be rank 3 but is rank 1 for 'Tile_1' (op: 'Tile') with input shapes
?? 版權聲明 ??
轉(zhuǎn)載需注明出處:codelast.com