Skip to content

Commit

Permalink
Register ResNet1d layers in a way compatible with DataParallel
Browse files Browse the repository at this point in the history
The `self.add_module` is a technique to register parameters, but it does
not work with DataParallel because it does not put the parameters into
the __dict__ in the normal way.
https://discuss.pytorch.org/t/discrepancy-between-manual-parameter-registration-vs-using-nn-modulelist-when-parallelizing/181055
pytorch/pytorch#3174
  • Loading branch information
timgianitsos committed Aug 7, 2023
1 parent 6b98c98 commit d25029a
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions model.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,14 @@ def __init__(self, input_dim, blocks_dim, n_classes, kernel_size=17, dropout_rat
self.bn1 = nn.BatchNorm1d(n_filters_out)

# Residual block layers
self.res_blocks = []
for i, (n_filters, n_samples) in enumerate(blocks_dim):
n_filters_in, n_filters_out = n_filters_out, n_filters
n_samples_in, n_samples_out = n_samples_out, n_samples
downsample = _downsample(n_samples_in, n_samples_out)
resblk1d = ResBlock1d(n_filters_in, n_filters_out, downsample, kernel_size, dropout_rate)
self.add_module('resblock1d_{0}'.format(i), resblk1d)
self.res_blocks += [resblk1d]
setattr(self, f'resblock1d_{i}', ResBlock1d(
n_filters_in, n_filters_out, downsample,
kernel_size, dropout_rate
))

# Linear layer
n_filters_last, n_samples_last = blocks_dim[-1]
Expand All @@ -198,8 +198,8 @@ def forward(self, x):

# Residual blocks
y = x
for blk in self.res_blocks:
x, y = blk(x, y)
for i in range(self.n_blk):
x, y = getattr(self, f'resblock1d_{i}')(x, y)

# Flatten array
x = x.view(x.size(0), -1)
Expand Down

0 comments on commit d25029a

Please sign in to comment.