How to add a layer in an architecture like resnet18

How can i add a layer (lets say a linear layer) at the end of resnet18 ?
thanks in advance :slight_smile:

# Get resnet18 model
model = models.resnet18()

# Create your own layers
my_layer = nn.Sequential(
    nn.ReLU(),
    nn.ReLU6(),
)

# Create a new model
my_model = nn.Sequential(
    model,
    my_layer,
)

1 Like

that was very helpful. Thanks. I have one more doubt. I am trying to train a multi-label image classification on a highly unbalanced dataset. So Binary cross entropy is what i am using. I want to pass the weight argument which can scale the losses of the different classes loss. I want the losses belong to the classes with larger number of examples, to be penalized less and vice versa. Any idea how can this be done ?
Thanks in advance :slight_smile:

Have you tried Focal Loss.

no i havent. So it does the same thing which i was trying to achieve? if yes? how is this different from the thing i was talking about?

You can use weight argument in some loss functions like CrossEntropy, to specify the weight for each class. I mentioned Focal Loss as it is mostly used when we have unbalanced classes. If is just an extension of CrossEntropy in a way.

1 Like

thanks for the clarification.
can u answer this the following questio pls
once i make a databunch, i want to iterate over it. is it possible to do so?

The dataloaders are stored as train_dl, val_dl in the databunch.

for i, data in enumerate(data_bunch.train_dl):
    x = data[0]
    y = data[1]