填充与复制

TensorFlow2教程完整教程目录(更有python、go、pytorch、tensorflow、爬虫、人工智能教学等着你):https://www.cnblogs.com/nickchen121/p/10840284.html

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.Tensor: id=17, shape=(3, 3), dtype=int32, numpy=
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]], dtype=int32)>
tf.pad(a, [[0, 0], [0, 0]])
<tf.Tensor: id=20, shape=(3, 3), dtype=int32, numpy=
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]], dtype=int32)>
tf.pad(a, [[
    1,
    0,
], [0, 0]])
<tf.Tensor: id=23, shape=(4, 3), dtype=int32, numpy=
array([[0, 0, 0],
       [0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]], dtype=int32)>
tf.pad(a, [[1, 1], [0, 0]])
<tf.Tensor: id=26, shape=(5, 3), dtype=int32, numpy=
array([[0, 0, 0],
       [0, 1, 2],
       [3, 4, 5],
       [6, 7, 8],
       [0, 0, 0]], dtype=int32)>
tf.pad(a, [[1, 1], [1, 0]])
<tf.Tensor: id=29, shape=(5, 4), dtype=int32, numpy=
array([[0, 0, 0, 0],
       [0, 0, 1, 2],
       [0, 3, 4, 5],
       [0, 6, 7, 8],
       [0, 0, 0, 0]], dtype=int32)>
tf.pad(a, [[1, 1], [1, 1]])
<tf.Tensor: id=32, shape=(5, 5), dtype=int32, numpy=
array([[0, 0, 0, 0, 0],
       [0, 0, 1, 2, 0],
       [0, 3, 4, 5, 0],
       [0, 6, 7, 8, 0],
       [0, 0, 0, 0, 0]], dtype=int32)>

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
<tf.Tensor: id=76, shape=(3, 3), dtype=int32, numpy=
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]], dtype=int32)>
# 1表示行不复制,2表示列复制为两倍
tf.tile(a, [1, 2])
<tf.Tensor: id=79, shape=(3, 6), dtype=int32, numpy=
array([[0, 1, 2, 0, 1, 2],
       [3, 4, 5, 3, 4, 5],
       [6, 7, 8, 6, 7, 8]], dtype=int32)>
tf.tile(a, [2, 1])
<tf.Tensor: id=82, shape=(6, 3), dtype=int32, numpy=
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8],
       [0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]], dtype=int32)>
tf.tile(a, [2, 2])
<tf.Tensor: id=85, shape=(6, 6), dtype=int32, numpy=
array([[0, 1, 2, 0, 1, 2],
       [3, 4, 5, 3, 4, 5],
       [6, 7, 8, 6, 7, 8],
       [0, 1, 2, 0, 1, 2],
       [3, 4, 5, 3, 4, 5],
       [6, 7, 8, 6, 7, 8]], dtype=int32)>

tile VS broadcast_to

aa = tf.expand_dims(a, axis=0)
aa
<tf.Tensor: id=90, shape=(1, 3, 3), dtype=int32, numpy=
array([[[0, 1, 2],
        [3, 4, 5],
        [6, 7, 8]]], dtype=int32)>
tf.tile(aa, [2, 1, 1])
<tf.Tensor: id=93, shape=(2, 3, 3), dtype=int32, numpy=
array([[[0, 1, 2],
        [3, 4, 5],
        [6, 7, 8]],

       [[0, 1, 2],
        [3, 4, 5],
        [6, 7, 8]]], dtype=int32)>
# 不占用内存,性能更优
tf.broadcast_to(aa, [2, 3, 3])
<tf.Tensor: id=96, shape=(2, 3, 3), dtype=int32, numpy=
array([[[0, 1, 2],
        [3, 4, 5],
        [6, 7, 8]],

       [[0, 1, 2],
        [3, 4, 5],
        [6, 7, 8]]], dtype=int32)>
posted @ 2019-05-12 16:40  B站-水论文的程序猿  阅读(964)  评论(0编辑  收藏  举报