【tensorflow】tf.pad()

tf.pad(tensor,#被填充的张量
       paddings,#填充的格式
       mode='CONSTANT',#填充模式:'CONSTANT''REFLECT''SYMMETRIC'
       name = None,
       constant_values = 0)#用于在'CONSTANT'模式下,设置的填充值

例子:

import tensorflow as tf
import numpy as np

a = tf.constant([[1, 2, 3], [4, 5, 6]])
paddinga = tf.constant([[1, 1], [2, 2]])

b = tf.constant([[[1, 2, 3], [4, 5, 6], [7, 8, 9]]])
paddingb = tf.constant([[1, 1], [1, 1], [2, 2]])


with tf.Session() as sess:
    op1 = tf.pad(a, paddinga)
    print(a.get_shape())
    print(paddinga.get_shape())
    print(sess.run(op1))

    op2 = tf.pad(b, paddingb)
    print(b.get_shape())
    print(paddingb.get_shape())
    print(sess.run(op2))


输出:

(2, 3)
(2, 2)
[[0 0 0 0 0 0 0]
 [0 0 1 2 3 0 0]
 [0 0 4 5 6 0 0]
 [0 0 0 0 0 0 0]]
(1, 3, 3)
(3, 2)
[[[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 0 0]
  [0 0 1 2 3 0 0]
  [0 0 4 5 6 0 0]
  [0 0 7 8 9 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 0 0 0 0]]]

 

你可能感兴趣的:(tensorflow学习)