Skip to main content

Analysis of Pytorch Models

Project description

BBDecoder

Main Image

How to use

Initialization

Start by wrapping the entire model. Each layer in the model is assigned a name and a unique layer ID.

from BBDecoder import Master_analyzer

model = resnet50().to(device)
wrapped_model = Master_analyzer(model, save_path, input_size)

model is the model to be analyzed. save_path is the main path where all the analysis data will be saved. input_size = [1, 3, 32, 32] this shape is used to create the architecture graph using torchview.

Gradient analysis

wrapped_model.backward_propagation() can be used to get gradients of each layer and also the L1 and L2 norms of the gradient for each itteration.

for data in tqdm(testloader):
    inputs, labels = data
    inputs, labels = inputs.to(device), labels.to(device)
    
    outputs = wrapped_model.forward_propagation(inputs)
    loss = F.cross_entropy(outputs, labels)

    optimizer.zero_grad()
    wrapped_model.backward_propagation(loss, collect_grads = True, layer_inds = [0, 1, 2, 3, 4, 5, 6])
    optimizer.step()
    losses.append(loss.item())
    
print('Epoch: ', epoch)
print('Average Loss: ', sum(losses)/len(losses))
wrapped_model.save_collected_grads(ep = epoch, save_folder)

The graphs will be saved directly in the save_folder path, by default save_folder = None if a new path is not specified then the graphs will be saved on a separate folder in the path specified during Master_analyzer initialization.

collect_grads = True is grads need to be collected other wise False. layer_inds is the list of layers that need to be analyzed. Setting collect_grads = True needs to be accompanied by wrapped_model.save_collected_grads() to actually save the collected data.

Grad

Mean and Max gradients through layers accross the entire dataset.

L1 and L2 norms of each selected layer in the model can highlight the stability of the training process.

Grad

L1 and L2 norms differentiating between stable and unstable training process.

Weight histograms

wrapped_model.visualize_weight_hist(layer_inds, save_folder) can be used to save weight histograms for layers with unique ID specified in layer_inds. save_folder = None by default, results will be saved in the path specified during initialization if no path specified.

Weight histograms can be used to identify saturation, divergence and clustering in layer weights. Weights clustering at extreme ends can saturate activation functions. Clustering of weights around certain values in convolution operation can point towards kernels with similar values, this would prevent the network to distinguish between subtle features.

Grad

Weight histograms of multi-layer perceptrons and convolutional layers.

Divergence calculation

get_sim(x, layer_inds, dim) function can be used to calculate how similar the features are accross a certain dimension, this can be used to perform studies similar to vision transformers with patch diversificattion.

forward propagation on the input x will generate features, the features similarity accross dimension dim will be calculated on the intermediate features of layer_inds.

wrapped_model.get_sim(torch.unsqueeze(inputs.cuda()[0, :, :, :], dim=0), layer_inds = [0, 1, 2, 3, 4, 5, 6], dim = 1)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bbdecoder-1.1.0.tar.gz (17.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bbdecoder-1.1.0-py3-none-any.whl (21.1 kB view details)

Uploaded Python 3

File details

Details for the file bbdecoder-1.1.0.tar.gz.

File metadata

  • Download URL: bbdecoder-1.1.0.tar.gz
  • Upload date:
  • Size: 17.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.21

File hashes

Hashes for bbdecoder-1.1.0.tar.gz
Algorithm Hash digest
SHA256 3c71e99932242646dfe6ffcac3eaaab87a06f837b54e21af655daa8d8b8be0a1
MD5 db781cadbe94636823da8878c1bbc729
BLAKE2b-256 1c0b45bf709b673e86a6670705ac7f599b4bb4d97f80c54f27c81639cdf497d6

See more details on using hashes here.

File details

Details for the file bbdecoder-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: bbdecoder-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 21.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.21

File hashes

Hashes for bbdecoder-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 152e463864dbf73a14e51d0def62b3a62ca9f63f6d8b7ba0323f4cd6d4c98f18
MD5 d0488fa6f6980087352485b6e5769c93
BLAKE2b-256 1e67a2855e25db53638605406f321e8062da1c54116e6fed0227b2f988f798ee

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page