下面的以下代碼給出了形狀(1,1,3)的形狀輸出xoddis (1,1,2)。給定的內核形狀是(112, 1, 1)。
from torch.nn import functional as F
output = F.conv1d(xodd, kernel, padding=zeros)
效果如何padding=zeros?
還有,如何在 tensorflow 中撰寫等效代碼,以使輸出與上述相同output?
uj5u.com熱心網友回復:
是什么padding=zeros?
如果我們設定paddin=zeros,我們不需要在張量的左右添加數字。
填充=0:
from torch.nn import functional as F
import torch
inputs = torch.randn(33, 16, 6) # (minibatch,in_channels,features)
filters = torch.randn(20, 16, 5) # (out_channels, in_channels, kernel_size)
out_tns = F.conv1d(inputs, filters, stride=1, padding=0)
print(out_tns.shape)
# torch.Size([33, 20, 2]) # (minibatch,out_channels,(features-kernel_size 1))

padding=2:(我們要在張量的左右兩邊加兩個數)
inputs = torch.randn(33, 16, 6) # (minibatch,in_channels,features)
filters = torch.randn(20, 16, 5) # (out_channels, in_channels, kernel_size)
out_tns = F.conv1d(inputs, filters, stride=1, padding=2)
print(out_tns.shape)
# torch.Size([33, 20, 6]) # (minibatch,out_channels,(features-kernel_size 1 2 2))

如何在 tensorflow 中撰寫等效代碼:
import tensorflow as tf
input_shape = (33, 6, 16)
x = tf.random.normal(input_shape)
out_tf = tf.keras.layers.Conv1D(filters = 20,
kernel_size = 5,
strides = 1,
input_shape=input_shape[1:])(x)
print(out_tf.shape)
# TensorShape([33, 2, 20])
# If you want that tensor have shape exactly like pytorch you can transpose
tf.transpose(out_tf, [0, 2, 1]).shape
# TensorShape([33, 20, 2])
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/487681.html
