tf.pad用法

tf.pad(
    tensor,
    paddings,
    mode='CONSTANT',
    name=None,
    constant_values=0
)

Args:

  • tensor: A Tensor.
  • paddings: A Tensor of type int32.
  • mode: One of "CONSTANT", "REFLECT", or "SYMMETRIC" (case-insensitive)
  • name: A name for the operation (optional).
  • constant_values: In "CONSTANT" mode, the scalar pad value to use. Must be same type as tensor.

Returns:

A Tensor. Has the same type as tensor.

Raises:

ValueError: When mode is not one of "CONSTANT", "REFLECT", or "SYMMETRIC".

作用

Pads a tensor.
This operation pads a tensor according to the paddings you specify. paddings is an integer tensor with shape [n, 2], where n is the rank of tensor. For each dimension D of input, paddings[D, 0] indicates how many values to add before the contents of tensor in that dimension, and paddings[D, 1] indicates how many values to add after the contents of tensor in that dimension. If mode is "REFLECT" then both paddings[D, 0] and paddings[D, 1] must be no greater than tensor.dim_size(D) - 1. If mode is "SYMMETRIC" then both paddings[D, 0] and paddings[D, 1] must be no greater than tensor.dim_size(D).
The padded size of each dimension D of the output is:
paddings[D, 0] + tensor.dim_size(D) + paddings[D, 1]

例子

import tensorflow as tf

# 可以看到張量有四個維度。
x=tf.Variable([[[[1,1],
                 [2,2]],
               [[3,3],
                [4,4]]]],tf.float32)

# tf.pad的每一個[a,b]的矩陣中,a表示在相應(yīng)維前面加a層值,b表示在相應(yīng)維后面加b層值。
x=tf.pad(x,[[1,0],[1,0],[1,0],[1,0]])
'''
即,第一個[1,0]表示在張量x的第一維前面添1個值,得到:
[[[[0,1,1],
   [0,2,2]],
  [[0,3,3],
   [0,4,4]]]]
第二個[1,0]表示在第二維前面添1層值,得到:
[[[[0,0,0],
   [0,1,1],
   [0,2,2]],
  [[0,0,0],
   [0,3,3],
   [0,4,4]]]]
第三個[1,0]表示在第三維前面添1層值,得到:
[[[[0,0,0],
   [0,0,0],
   [0,0,0]]
   [[0,0,0],
   [0,1,1],
   [0,2,2]],
  [[0,0,0],
   [0,3,3],
   [0,4,4]]]]
第四個[1,0]表示在第四維前面添1層值,得到:
[[[[0,0,0],
   [0,0,0],
   [0,0,0]]
   [[0,0,0],
   [0,0,0],
   [0,0,0]],
  [[0,0,0],
   [0,0,0],
   [0,0,0]]]
 [[[0,0,0],
   [0,0,0],
   [0,0,0]]
   [[0,0,0],
   [0,1,1],
   [0,2,2]],
  [[0,0,0],
   [0,3,3],
   [0,4,4]]]]
'''

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(sess.run(x))
# 輸出
[[[[0 0 0]
   [0 0 0]
   [0 0 0]]
  [[0 0 0]
   [0 0 0]
   [0 0 0]]
  [[0 0 0]
   [0 0 0]
   [0 0 0]]]
 [[[0 0 0]
   [0 0 0]
   [0 0 0]]
  [[0 0 0]
   [0 1 1]
   [0 2 2]]
  [[0 0 0]
   [0 3 3]
   [0 4 4]]]]
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 酒閣醉醉醉何人, 龍涎紫煙,縈盞無回時。 紅爐梅酒相思處,院深不見來時路。 月白風(fēng)起仙人暮, 墨染河星,難掩相思怒...
    手握瓷杯閱讀 468評論 6 3
  • 你看空中飄落的雪花在風(fēng)雨的陪伴下若隱若現(xiàn),像不像我對你的思念? 愿你一切安好,晚安!
    何時再出發(fā)閱讀 169評論 1 0
  • 早就想去南通水上樂園玩了,好不容易等到暑假,好不容易等到爸爸有空,真的很不容易哦 上火車前來個留影,預(yù)祝旅途愉快 ...
    永泰水產(chǎn)閱讀 811評論 0 0

友情鏈接更多精彩內(nèi)容