下面两个类有什么区别吗?我知道inplace是什么(如果inplace是True
,则不需要执行x = function(x)
,而只需执行function(x)
来修改x
)。但是这里因为我们return self.conv(x)
,应该没关系吧?
class ConvBlock(nn.Module):
def __init__(
self,
in_channels,
out_channels,
down=True,
use_act=True,
**kwargs
):
super().__init__()
self.conv = nn.Sequential((nn.Conv2d(in_channels, out_channels,
padding_mode='reflect',
**kwargs) if down else nn.ConvTranspose2d(in_channels,
out_channels, **kwargs)),
nn.InstanceNorm2d(out_channels),
(nn.ReLU() if use_act else nn.Identity()))
def forward(self, x):
return self.conv(x)
class ConvBlockInplace(nn.Module):
def __init__(
self,
in_channels,
out_channels,
down=True,
use_act=True,
**kwargs
):
super().__init__()
self.conv = nn.Sequential((nn.Conv2d(in_channels, out_channels,
padding_mode='reflect',
**kwargs) if down else nn.ConvTranspose2d(in_channels,
out_channels, **kwargs)),
nn.InstanceNorm2d(out_channels),
(nn.ReLU(inplace=True) if use_act else nn.Identity()))
def forward(self, x):
return self.conv(x)
字符串
1条答案
按热度按时间lfapxunr1#
inplace
运算完成了精确的计算量。但是,如果任务是内存受限的,则内存访问较少。那就有关系了我使用the
ptflops
flops counter生成以下统计信息字符串