Tensorflow 张量运算2:填充与复制

1. 填充

以MNIST数据集中的图片数据为例,图片大小为28*28。若网络层所接受的数据高宽为32×32,则必须将28×28大小的图片填充到32×32。

import tensorflow as tf 

x = tf.random.normal([4,28,28,1])

# 填充方案上下左右各填充两个
#tf.pad(x,[[0,0],[2,2],[2,2],[0,0]])
'''<tf.Tensor: shape=(4, 32, 32, 1), dtype=float32, numpy=
array([[[[ 0.        ],
         [ 0.        ],
         [ 0.        ],
         ...,
         ]]], dtype=float32)>
'''

# 填充方案上1下3左1右3
tf.pad(x,[[0,0],[1,3],[1,3],[0,0]])
'''<tf.Tensor: shape=(4, 32, 32, 1), dtype=float32, numpy=
array([[[[ 0.        ],
         [ 0.        ],
         [ 0.        ],
         ...,
         ]]], dtype=float32)>
'''

2. 复制

import tensorflow as tf 

x = tf.random.normal([4,28,28,1])

# tf.tile可以在任意维度将数据复制多份
tf.tile(x,[2,3,3,3])
'''输出
<tf.Tensor: shape=(8, 84, 84, 3), dtype=float32, numpy=
array([[[[ 6.23802304e-01,  6.23802304e-01,  6.23802304e-01],
         [-6.40799105e-01, -6.40799105e-01, -6.40799105e-01],
         [-1.36049956e-01, -1.36049956e-01, -1.36049956e-01],
         ...,]]],
      dtype=float32)>
[
'''
发布了93 篇原创文章 · 获赞 2 · 访问量 3037

猜你喜欢

转载自blog.csdn.net/qq_40041064/article/details/104794945