How to build a convolutional neural network with existing parameters?

I want to build a convolutional neural network to recognize digits. And I already have the structure and the parameters of weight and bias stored in the format of “txt”. How should I use them to implement a network?

You could create the model first, load the parameters from the .txt file, transform them to floats and then tensors, and load each parameter manually via:

with torch.no_grad():
    model.layerX.weight.copy_(layerX_weight)
    model.layerX.bias.copy_(layerX_bias)

Note that his approach is a bit tedious, as it seems you are loading the parameters from a raw text file, which is not a supported via utility methods (such as model.load_state_dict).

1 Like