Skip to content

pytorch_model.bin

James Clow edited this page Feb 26, 2023 · 4 revisions

pytorch_model.bin is a binary file that contains the serialized state_dict of a PyTorch model. The state_dict is a dictionary object that maps the names of model parameters to their corresponding tensors, and it is the primary way that PyTorch models are stored and loaded.

The state_dict is a nested dictionary structure where the keys are the names of the model parameters and the values are PyTorch tensors that contain the values of those parameters. The state_dict dictionary is hierarchical, with the top-level keys being the names of the model layers or modules, and the values being nested dictionaries that contain the parameter names and tensor values for each layer/module.

For example, suppose we have a simple PyTorch model with two layers, named fc1 and fc2, each with a weight tensor and a bias tensor. The state_dict dictionary for this model might look like this:

{
'fc1.weight': tensor([...]),
'fc1.bias': tensor([...]),
'fc2.weight': tensor([...]),
'fc2.bias': tensor([...])
}

In this example, the top-level keys are the names of the layers, and the nested dictionaries contain the parameter names and tensor values for each layer.

When the PyTorch model is saved to a pytorch_model.bin file, the state_dict is serialized and written to the file as binary data. When the model is loaded from the file, the binary data is deserialized and the state_dict is reconstructed as a dictionary object in memory.

Clone this wiki locally