tf.pad と torch.nn.function.pad の違い

第一パラメータは入力、第二パラメータは塗りつぶす位置と量です。

例:

ピトーチ:

import torch
t1 = torch.tensor([[ 1,  2,  3],
                      [ 4,  5,  6],
                      [ 7,  8,  9],
                      [10, 11, 12]])


a = torch.nn.functional.pad(t1, [1, 2,3,4])



# tensor([[ 0,  0,  0,  0,  0,  0],
#         [ 0,  0,  0,  0,  0,  0],
#         [ 0,  0,  0,  0,  0,  0],
#         [ 0,  1,  2,  3,  0,  0],
#         [ 0,  4,  5,  6,  0,  0],
#         [ 0,  7,  8,  9,  0,  0],
#         [ 0, 10, 11, 12,  0,  0],
#         [ 0,  0,  0,  0,  0,  0],
#         [ 0,  0,  0,  0,  0,  0],
#         [ 0,  0,  0,  0,  0,  0],
#         [ 0,  0,  0,  0,  0,  0]])

パディングディメンションが内側から外側に追加され、最初に dim=1、次に dim=0 (最初に列、次に行) が追加されていることがわかります。

テンソルフロー:

import tensorflow as tf


input = tf.constant([[ 1,  2,  3],
                      [ 4,  5,  6],
                      [ 7,  8,  9],
                      [10, 11, 12]])



# d =  tf.pad(inputs,[1,2,0,0])
c = tf.pad(input, [(1, 2), (3, 4)])

with tf.Session() as sess:
    print(sess.run(c))
# [[ 0  0  0  0  0  0  0  0  0  0]
#  [ 0  0  0  1  2  3  0  0  0  0]
#  [ 0  0  0  4  5  6  0  0  0  0]
#  [ 0  0  0  7  8  9  0  0  0  0]
#  [ 0  0  0 10 11 12  0  0  0  0]
#  [ 0  0  0  0  0  0  0  0  0  0]
#  [ 0  0  0  0  0  0  0  0  0  0]]

パディングの寸法が外側から内側に追加され、最初の axis=0、次に axis=1 (最初に行、次に列) が追加されていることがわかります。

おすすめ

転載: blog.csdn.net/djdjdhch/article/details/130612467