PyTorch 普通卷积和空洞卷积实例

yipeiwu_com6年前Python基础

如下所示:

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 普通卷积和空洞卷积实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python concurrent.futures模块使用实例

这篇文章主要介绍了Python concurrent.futures模块使用实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 con...

TensorFlow Session会话控制&Variable变量详解

TensorFlow Session会话控制&Variable变量详解

这篇文章主要讲TensorFlow中的Session的用法以及Variable。 Session会话控制 Session是TensorFlow为了控制和输出文件的执行语句,运行sessi...

python实现微信定时每天和女友发送消息

python实现微信定时每天和女友发送消息

但凡有些事情重复时,我就在想怎么可以用程序来自动化。这里想分享如何每天给女友定时微信发送”晚安“,如果只是晚安,就略显单调,于是爬取金山词霸每日一句,英文和翻译,借此设定定时器进行发送。...

python获取程序执行文件路径的方法(推荐)

python获取程序执行文件路径的方法(推荐)

1.获取当前执行主脚本方法:sys.argv[0]和_ file _ (1)sys.argv 一个传给Python脚本的指令参数列表。sys.argv[0]是脚本的名字。一般得到的是...

python导出hive数据表的schema实例代码

本文研究的主要问题是python语言导出hive数据表的schema,分享了实现代码,具体如下。 为了避免运营提出无穷无尽的查询需求,我们决定将有查询价值的数据从mysql导入hive中...