gpt4 book ai didi

python - TensorFlow 如何用边缘值填充张量

转载 作者:行者123 更新时间:2023-12-01 01:56:07 25 4
gpt4 key购买 nike

如何使用边缘值填充张量(尺寸为 WxHxC)?

例如:

[1, 2, 3]
[4, 5, 6]
[7, 8, 9]

变成:

[1, 1, 2, 3, 3]
[1, 1, 2, 3, 3]
[4, 4, 5, 6, 6]
[7, 7, 8, 9, 9]
[7, 7, 8, 9, 9]

最佳答案

使用tf.pad()和模式“SYMMETRIC” - 它将反射(reflect)边缘上的值,但如果您只进行 1 深度填充,则相当于重复边缘值。如果需要更多填充,则必须重复该操作,但可以按指数进行(先 1,然后 2,然后 4,等等)。此代码(已测试):

import tensorflow as tf

a = tf.reshape( tf.constant( range( 1, 10 ) ), ( 3, 3 ) )
b = tf.pad( a, [ [ 1, 1 ], [ 1, 1 ] ], "SYMMETRIC" )

with tf.Session() as sess:
print( sess.run( b ) )

输出:

[[1 1 2 3 3]
[1 1 2 3 3]
[4 4 5 6 6]
[7 7 8 9 9]
[7 7 8 9 9]]

根据需要。

关于python - TensorFlow 如何用边缘值填充张量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50174962/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com