Sorry I just copy-pasted the example code.
What i want to know is how the blocks are connected.
Lets say i make a class containing two blocks each containing 3 convolutional layers:
class Two_Blocks(nn.Module):
def __init__(self):
self.block1 = nn.Sequential(
nn.Conv1d(
in_channels=128,
out_channels=128,
kernel_size=5,
stride=1,
padding=2,
),
nn.BatchNorm1d(128),
nn.ReLU(),
nn.Conv1d(
in_channels=128,
out_channels=128,
kernel_size=5,
stride=1,
padding=2,
),
nn.Conv1d(
in_channels=128,
out_channels=128,
kernel_size=5,
stride=1,
padding=2,
),
nn.BatchNorm1d(128),
nn.ReLU(),
)
self.block2 = nn.Sequential(
nn.Conv1d(
in_channels=128,
out_channels=128,
kernel_size=3,
stride=1,
padding=1,
),
nn.BatchNorm1d(128),
nn.ReLU(),
nn.Conv1d(
in_channels=128,
out_channels=128,
kernel_size=3,
stride=1,
padding=1,
),
nn.Conv1d(
in_channels=128,
out_channels=128,
kernel_size=3,
stride=1,
padding=1,
),
nn.BatchNorm1d(128),
nn.ReLU(),
)
### END ###
self.Global_Avg_Pool = nn.AvgPool1d(kernel_size = 2)
self.end = nn.Sequential(
nn.Dropout(p=0.3),
nn.Linear(128*1000, 3),
)
def forward(self, x):
residual1 = x #Save input as residual
x = self.block1(x)
x += residual1 #add input to output of block1
residual2 = x #save output of block1 as residual
x = self.block2(x)
x += residual2 #add output of block1 to output of block2
x = self.Global_Avg_Pool(x) #Global average pooling instead of fully connected.
x = x.view(-1, 128*1000)
x = F.softmax(self.end(x),dim=1)
return x
Does this make it more clear?
The other way would be to do the following instead:
def forward(self, x):
residual = x #Save input as residual
x = self.block1(x)
x += residual #add input to output of block1
x = self.block2(x)
#The same input is added for block 2 as for block 1:
x += residual #add input to output of block2
x = self.Global_Avg_Pool(x) #Global average pooling instead of fully connected.
x = x.view(-1, 128*1000)
x = F.softmax(self.end(x),dim=1)
return x
In my head the first example is the correct one. But just wanted to be sure that the residual saved in block one is not the same used for all blocks throughout the model. Sorry if i overcomplicated my question.