I am facing similar kind of issue but not while creating the layers rather while calling forward.
Below is the code.
class BasicBlock(nn.Module):
expansion = 1
__constants__ = ['downsample']
def __init__(self, inplanes, planes, stride=1, downsample=None, groups=1,
base_width=64, dilation=1):
super(BasicBlock, self).__init__()
if groups != 1 or base_width != 64:
raise ValueError('BasicBlock only supports groups=1 and base_width=64')
if dilation > 1:
raise NotImplementedError("Dilation > 1 not supported in BasicBlock")
# Both self.conv1 and self.downsample layers downsample the input when stride != 1
self.conv1 = conv3x3(inplanes, planes, stride)
self.relu = nn.ReLU(inplace=True)
self.conv2 = conv3x3(planes, planes)
self.downsample = downsample
self.stride = stride
def forward(self,x,sample=False):
identity = x
out = self.conv1(x,sample)
out = self.relu(out)
out = self.conv2(out,sample)
if self.downsample is not None:
identity = self.downsample(x,sample)
out += identity
out = self.relu(out)
return out
class ResNet(nn.Module):
def __init__(self, block, layers, init='xavierUniform', num_classes=7, zero_init_residual=False,
groups=1, width_per_group=64, replace_stride_with_dilation=None,
norm_layer=None):
super(ResNet, self).__init__()
if norm_layer is None:
norm_layer = nn.BatchNorm2d
self._norm_layer = norm_layer
self.inplanes = 64
self.dilation = 1
if replace_stride_with_dilation is None:
replace_stride_with_dilation = [False, False, False]
if len(replace_stride_with_dilation) != 3:
raise ValueError("replace_stride_with_dilation should be None "
"or a 3-element tuple, got {}".format(replace_stride_with_dilation))
self.groups = groups
self.base_width = width_per_group
self.conv1 = BayesianConv(3,self.inplanes,kernel_size=7, stride=2, padding=3,bias=False)
self.relu = nn.ReLU(inplace=True)
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
self.layer1 = self._make_layer(block, 64, layers[0])
self.layer2 = self._make_layer(block, 128, layers[1], stride=2,
dilate=replace_stride_with_dilation[0])
self.layer3 = self._make_layer(block, 256, layers[2], stride=2,
dilate=replace_stride_with_dilation[1])
self.layer4 = self._make_layer(block, 512, layers[3], stride=2,
dilate=replace_stride_with_dilation[2])
self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
self.fc = BayesianLinear(512*block.expansion,num_classes)
weights_init_xavierUniform(self)
if zero_init_residual:
for m in self.modules():
if isinstance(m, Bottleneck):
nn.init.constant_(m.bn3.weight, 0)
elif isinstance(m, BasicBlock):
nn.init.constant_(m.bn2.weight, 0)
def _make_layer(self, block, planes, blocks, stride=1, dilate=False):
norm_layer = self._norm_layer
downsample = None
previous_dilation = self.dilation
if dilate:
self.dilation *= stride
stride = 1
if stride != 1 or self.inplanes != planes * block.expansion:
downsample = nn.Sequential(
conv1x1(self.inplanes, planes * block.expansion, stride),
)
layers = []
layers.append(block(self.inplanes, planes, stride, downsample, self.groups,
self.base_width, previous_dilation))
self.inplanes = planes * block.expansion
for _ in range(1, blocks):
layers.append(block(self.inplanes, planes, groups=self.groups,
base_width=self.base_width, dilation=self.dilation))
return nn.Sequential(*layers)
def _forward_impl(self, x, sample):
x = self.conv1(x,sample)
x = self.relu(x)
x = self.maxpool(x)
x = self.layer1(x,sample)
x = self.layer2(x,sample)
x = self.layer3(x,sample)
x = self.layer4(x,sample)
x = self.avgpool(x)
x = torch.flatten(x, 1)
x = F.log_softmax(self.fc(x, sample), dim=1)
return x
def forward(self, x, sample=False):
return self._forward_impl(x,sample)
def sample_elbo(self, input, target, NUM_BATCHES, samples=1):
outputs = torch.zeros(samples, input.shape[0], 7).to(DEVICE)
log_priors = torch.zeros(samples).to(DEVICE)
log_variational_posteriors = torch.zeros(samples).to(DEVICE)
for i in range(samples):
outputs[i] = self(input, sample=True)
log_priors[i] = self.log_prior()
log_variational_posteriors[i] = self.log_variational_posterior()
log_prior = log_priors.mean()
log_variational_posterior = log_variational_posteriors.mean()
negative_log_likelihood = F.nll_loss(outputs.mean(0), target, size_average=False)
loss = (log_variational_posterior - log_prior)/NUM_BATCHES + negative_log_likelihood
return loss, log_prior, log_variational_posterior, negative_log_likelihood
File "bayes_backprop.py", line 141, in train
loss, log_prior, log_variational_posterior, negative_log_likelihood = net.sample_elbo(data, target,NUM_BATCHES)
File "/scratch/engn8536/project_data/u6724013/caer/bayes_res.py", line 208, in sample_elbo
outputs[i] = self(input, sample=True)
File "/usr/local/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 547, in __call__
result = self.forward(*input, **kwargs)
File "/scratch/engn8536/project_data/u6724013/caer/bayes_res.py", line 177, in forward
return self._forward_impl(x,sample)
File "/scratch/engn8536/project_data/u6724013/caer/bayes_res.py", line 166, in _forward_impl
x = self.layer1(x,sample)
File "/usr/local/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 547, in __call__
result = self.forward(*input, **kwargs)
TypeError: forward() takes 2 positional arguments but 3 were given