柚子快报778899分享:填充与复制

http://yzkb.51969.com/

目录OutlinepadImage paddingtiletile VS broadcast_to

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

array([[0, 1, 2],

[3, 4, 5],

[6, 7, 8]], dtype=int32)>

tf.pad(a, [[0, 0], [0, 0]])

array([[0, 1, 2],

[3, 4, 5],

[6, 7, 8]], dtype=int32)>

tf.pad(a, [[

1,

0,

], [0, 0]])

array([[0, 0, 0],

[0, 1, 2],

[3, 4, 5],

[6, 7, 8]], dtype=int32)>

tf.pad(a, [[1, 1], [0, 0]])

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]])

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]])

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

array([[0, 1, 2],

[3, 4, 5],

[6, 7, 8]], dtype=int32)>

# 1表示行不复制,2表示列复制为两倍

tf.tile(a, [1, 2])

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])

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])

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

array([[[0, 1, 2],

[3, 4, 5],

[6, 7, 8]]], dtype=int32)>

tf.tile(aa, [2, 1, 1])

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])

array([[[0, 1, 2],

[3, 4, 5],

[6, 7, 8]],

[[0, 1, 2],

[3, 4, 5],

[6, 7, 8]]], dtype=int32)>

柚子快报778899分享:填充与复制

http://yzkb.51969.com/

推荐阅读

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。