您好,登錄后才能下訂單哦!
如下所示:
import numpy as np from torchvision.transforms import Compose, ToTensor from torch import nn import torch.nn.init as init def transform(): return Compose([ ToTensor(), # Normalize((12,12,12),std = (1,1,1)), ]) arr = range(1,26) arr = np.reshape(arr,[5,5]) arr = np.expand_dims(arr,2) arr = arr.astype(np.float32) # arr = arr.repeat(3,2) print(arr.shape) arr = transform()(arr) arr = arr.unsqueeze(0) print(arr) conv1 = nn.Conv2d(1, 1, 3, stride=1, bias=False, dilation=1) # 普通卷積 conv2 = nn.Conv2d(1, 1, 3, stride=1, bias=False, dilation=2) # dilation就是空洞率,即間隔 init.constant_(conv1.weight, 1) init.constant_(conv2.weight, 1) out1 = conv1(arr) out2 = conv2(arr) print('standare conv:\n', out1.detach().numpy()) print('dilated conv:\n', out2.detach().numpy())
輸出:
(5, 5, 1) tensor([[[[ 1., 2., 3., 4., 5.], [ 6., 7., 8., 9., 10.], [11., 12., 13., 14., 15.], [16., 17., 18., 19., 20.], [21., 22., 23., 24., 25.]]]]) standare conv: [[[[ 63. 72. 81.] [108. 117. 126.] [153. 162. 171.]]]] dilated conv: [[[[117.]]]]
以上這篇PyTorch 普通卷積和空洞卷積實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。