

The three important things to remember when writing you own configuration are the following:
- you have to inherit from PretrainedConfig,
- the __init__ of your PretrainedConfig must accept any kwargs,
- those kwargs need to be passed to the superclass __init__.
The inheritance is to make sure you get all the functionality from the 🤗 Transformers library, while the two other
constraints come from the fact a PretrainedConfig has more fields than the ones you are setting. When reloading a
config with the from_pretrained method, those fields need to be accepted by your config and then sent to the
superclass.
Defining a model_type for your configuration (here model_type="resnet") is not mandatory, unless you want to
register your model with the auto classes (see last section).
With this done, you can easily create and save your configuration like you would do with any other model config of the
library. Here is how we can create a resnet50d config and save it:
py
resnet50d_config = ResnetConfig(block_type="bottleneck", stem_width=32, stem_type="deep", avg_down=True)
resnet50d_config.save_pretrained("custom-resnet")
This will save a file named config.json inside the folder custom-resnet. You can then reload your config with the
from_pretrained method:
py
resnet50d_config = ResnetConfig.from_pretrained("custom-resnet")
You can also use any other method of the [PretrainedConfig] class, like [~PretrainedConfig.push_to_hub] to
directly upload your config to the Hub.
Writing a custom model
Now that we have our ResNet configuration, we can go on writing the model. We will actually write two: one that
extracts the hidden features from a batch of images (like [BertModel]) and one that is suitable for image
classification (like [BertForSequenceClassification]).
As we mentioned before, we'll only write a loose wrapper of the model to keep it simple for this example. The only
thing we need to do before writing this class is a map between the block types and actual block classes. Then the
model is defined from the configuration by passing everything to the ResNet class: