填充与复制

目录

  • Outline
  • pad
    • Image padding
  • tile
  • tile VS broadcast_to

Outline

  • pad

  • tile

  • broadcast_to

pad

  • [3]
  • [[1,2]]
  • [6]

  • [2,2]
  • [[0,1][1,1]] # [行,列]
  • [3,4]

import tensorflow as tf
a = tf.reshape(tf.range(9), [3, 3])
a
tf.pad(a, [[0, 0], [0, 0]])
tf.pad(a, [[
    1,
    0,
], [0, 0]])
tf.pad(a, [[1, 1], [0, 0]])
tf.pad(a, [[1, 1], [1, 0]])
tf.pad(a, [[1, 1], [1, 1]])

Image padding

a = tf.random.normal([4, 28, 28, 3])
a.shape
TensorShape([4, 28, 28, 3])
# 对图片的行和列padding两行
b = tf.pad(a, [[0, 0], [2, 2], [2, 2], [0, 0]])
b.shape
TensorShape([4, 32, 32, 3])
  • [1,5,5,1]
  • [[0,0],[2,2],[2,2],[0,0]]
  • [1,9,9,1]

tile

  • repeat data along dim n times
  • [a,b,c],2
  • --> [a,b,c,a,b,c]
a = tf.reshape(tf.range(9), [3, 3])
a
# 1表示行不复制,2表示列复制为两倍
tf.tile(a, [1, 2])
tf.tile(a, [2, 1])
tf.tile(a, [2, 2])

tile VS broadcast_to

aa = tf.expand_dims(a, axis=0)
aa
tf.tile(aa, [2, 1, 1])
# 不占用内存,性能更优
tf.broadcast_to(aa, [2, 3, 3])

你可能感兴趣的:(填充与复制)