pytorch forward两个参数实例

yipeiwu_com6年前Python基础

以channel Attention Block为例子

class CAB(nn.Module):
 
  def __init__(self, in_channels, out_channels):
    super(CAB, self).__init__()
    self.global_pooling = nn.AdaptiveAvgPool2d(output_size=1)
    self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=1, padding=0)
    self.relu = nn.ReLU()
    self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=1, stride=1, padding=0)
    self.sigmod = nn.Sigmoid()
 
  def forward(self, x):
    x1, x2 = x # high, low
    x = torch.cat([x1,x2],dim=1)
    x = self.global_pooling(x)
    x = self.conv1(x)
    x = self.relu(x)
    x = self.conv2(x)
    x = self.sigmod(x)
    x2 = x * x2
    res = x2 + x1
    return res

以上这篇pytorch forward两个参数实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Numpy中的mask的使用

Numpy中的mask的使用

numpy中矩阵选取子集或者以条件选取子集,用mask是一种很好的方法 简单来说就是用bool类型的indice矩阵去选择, mask = np.ones(X.shape[0],...

详解python eval函数的妙用

python eval函数功能:将字符串str当成有效的表达式来求值并返回计算结果。 函数定义: eval(expression, globals=None, locals=Non...

python获取当前文件路径以及父文件路径的方法

python获取当前文件路径以及父文件路径的方法

#当前文件的路径 pwd = os.getcwd() #当前文件的父路径 father_path=os.path.abspath(os.path.dirname(pwd)+os.p...

python计算最小优先级队列代码分享

复制代码 代码如下:# -*- coding: utf-8 -*- class Heap(object):     @classmethod &n...

Python标准库sched模块使用指南

事件调度 sched 模块内容很简单,只定义了一个类。它用来最为一个通用的事件调度模块。 class sched.scheduler(timefunc, delayfunc) 这个类定义...