簡體   English   中英

為什么 1x1 pytorch 卷積會改變數據?

[英]Why is a 1x1 pytorch convolution changing the data?

我正在調試我使用 torch::nn:Conv2d 遇到的問題。 這是一個演示意外行為的簡單腳本

import torch

use_cuda = torch.cuda.is_available()
device = torch.device("cuda:0" if use_cuda else "cpu")

t = torch.ones([1,1,3,3]).to(device)

print(t)

kernel_size=[1,1]
t2 = torch.nn.Conv2d(in_channels=1,out_channels=1,kernel_size=kernel_size, stride=kernel_size, padding=0).to(device)(t)

print(t2)

運行此結果:

tensor([[[[1., 1., 1.],
          [1., 1., 1.],
          [1., 1., 1.]]]], device='cuda:0')
tensor([[[[-1.7190, -1.7190, -1.7190],
          [-1.7190, -1.7190, -1.7190],
          [-1.7190, -1.7190, -1.7190]]]], device='cuda:0',
       grad_fn=<AddBackward0>)

為什么 t2 與 t1 不同? 我希望 1x1 卷積使輸入保持不變。

感謝 Michael Szczesny 的評論,我將 conv2d 替換為;

t2 = torch.nn.AvgPool2d(1, stride=1)(t)

一切都很好:

tensor([[[[1., 1., 1.],
          [1., 1., 1.],
          [1., 1., 1.]]]], device='cuda:0')
tensor([[[[1., 1., 1.],
          [1., 1., 1.],
          [1., 1., 1.]]]], device='cuda:0')

基本上,我使用了錯誤的 function。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM