tf.pad()的用法

https://blog.csdn.net/sinat_39372048/article/details/80976722

  • 例一
In [26]: import tensorflow as tf

In [27]:

In [27]: a=[[1,1],[2,2]]

In [28]: padding=[[1,1]]

In [29]: print(sess.run(tf.pad(input,padding)))
InvalidArgumentError: Shape must be rank 1 but is rank 2 for 'Pad_1' (op: 'Pad') with input shapes: [2,2], [1,2].

  • 例二

In [33]: a=[[1,1],[2,2]]

In [34]: padding=[[1,1],[1,2]]

In [35]: sess=tf.Session()

In [36]: print(sess.run(tf.pad(a,padding)))
[[0 0 0 0 0]
 [0 1 1 0 0]
 [0 2 2 0 0]
 [0 0 0 0 0]]

In [37]: padding=[[1,2],[1,2]]

In [38]: print(sess.run(tf.pad(a,padding)))
[[0 0 0 0 0]
 [0 1 1 0 0]
 [0 2 2 0 0]
 [0 0 0 0 0]
 [0 0 0 0 0]]
  • 例三

In [43]: a=[[[1,1],[2,2]],[[3,3],[4,4]]]

In [44]: padding=[[1,2],[1,2]]

In [45]: print(sess.run(tf.pad(a,padding)))

ValueError: Shape must be rank 2 but is rank 3 for 'Pad_5' (op: 'Pad') with input shapes: [2,2,2], [2,2].
  • 例四
In [43]: a=[[[1,1],[2,2]],[[3,3],[4,4]]]
In [46]: padding=[[1,1],[1,2],[2,1]]

In [47]: print(sess.run(tf.pad(a,padding)))
[ [[0 0 0 0 0]
  [0 0 1 1 0]
  [0 0 2 2 0]
  [0 0 0 0 0]
  [0 0 0 0 0]]

 [[0 0 0 0 0]
  [0 0 3 3 0]
  [0 0 4 4 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.padding 行数和 rank of input 一致
"[[[[[]]]]]"从左到右依次为第一、二、三维度(从左到右维度递增)

上一篇:MS CRM商机产品等Mapping


下一篇:Pyhton核心编程-Chap2习题-DIY