[英]How padding=zeros works in pytorch in functional.conv1d
This following code below giving a output of shape (1,1,3)
for the shape of xodd
is (1,1,2)
.下面的代码为 xodd 的形状给出形状为
(1,1,3)
的xodd
是(1,1,2)
。 The given kernel shape is (112, 1, 1)
.给定的 kernel 形状是
(112, 1, 1)
。
from torch.nn import functional as F
output = F.conv1d(xodd, kernel, padding=zeros)
How the padding=zeros
works? padding=zeros
是如何工作的?
And also, How can I write an equivalent code in tensorflow so that the output is as same as the above output
?而且,我如何在 tensorflow 中编写等效代码,以便 output 与上面的
output
相同?
What is padding=zeros
?什么是
padding=zeros
? If we set paddin=zeros
, we don't need to add numbers at the right and the left of the tensor.如果我们设置
paddin=zeros
,我们不需要在张量的左右添加数字。
Padding=0 :填充=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: (We want to add two numbers at the right and the left of the tensor) padding=2:(我们要在tensor的左右两边相加)
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))
How can I write an equivalent code in tensorflow:如何在 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])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.