<jupyter_start><jupyter_text>Install Dependencies<jupyter_code>!pip install biotorch --upgrade
!git clone https://github.com/jsalbert/biotorch.git<jupyter_output>Collecting biotorch
  Downloading biotorch-0.0.6.tar.gz (35.3 MB)
[K     |████████████████████████████████| 35.3 MB 59 kB/s 
[?25hCollecting jsonschema>=3.0
  Downloading jsonschema-3.2.0-py2.py3-none-any.whl (56 kB)
[K     |████████████████████████████████| 56 kB 3.8 MB/s 
[?25hRequirement already satisfied: torch>=1.0 in /usr/local/lib/python3.7/dist-packages (from biotorch) (1.9.0+cu102)
Requirement already satisfied: torchvision in /usr/local/lib/python3.7/dist-packages (from biotorch) (0.10.0+cu102)
Requirement already satisfied: numpy in /usr/local/lib/python3.7/dist-packages (from biotorch) (1.19.5)
Requirement already satisfied: matplotlib in /usr/local/lib/python3.7/dist-packages (from biotorch) (3.2.2)
Requirement already satisfied: pandas in /usr/local/lib/python3.7/dist-packages (from biotorch) (1.1.5)
Requirement already satisfied: ipykernel in /usr/local/lib/python3.7/dist-packages (from biotorch) (4.10.1)
Requirement already satisfied: jupyter in /usr/local/lib/pytho[...]<jupyter_text>Imports<jupyter_code>import yaml
import torch
import torch.nn as nn
import torch.nn.functional as F

from biotorch.benchmark.run import Benchmark
from biotorch.module.biomodule import BioModule<jupyter_output><empty_output><jupyter_text>Create a PyTorch model and convert it to use Feedback Alignment<jupyter_code>class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        self.conv1 = nn.Conv2d(in_channels=3, out_channels=32, kernel_size=3)
        self.relu1 = nn.ReLU()
        self.pool1 = nn.MaxPool2d(kernel_size=2)
        self.conv2 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3)
        self.relu2 = nn.ReLU()
        self.pool2 = nn.MaxPool2d(kernel_size=2)
        self.conv3 = nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3)
        self.relu3 = nn.ReLU()
        self.fc = nn.Linear(in_features=128, out_features=10)

    def forward(self, x):
        out = self.pool1(self.relu1(self.conv1(x)))
        out = self.pool2(self.relu2(self.conv2(out)))
        out = self.relu3(self.conv3(out))
        out = F.avg_pool2d(out, out.size()[3])
        out = out.view(out.size(0), -1)
        return self.fc(out)
# Create pytorch model
model = Model()
# Convert model to use Feedback Alignment `fa`
biomodel = BioModule(model, mode='fa')
# Print model
print(biomodel)
# Save model
torch.save(biomodel, 'biotorch/biomodel.pth')<jupyter_output>Module has been converted to fa mode:

The layer configuration was:  {'type': 'fa', 'options': {'constrain_weights': False, 'gradient_clip': False, 'init': 'xavier'}}
- All the 3 <class 'torch.nn.modules.conv.Conv2d'> layers were converted successfully.
- All the 1 <class 'torch.nn.modules.linear.Linear'> layers were converted successfully.
BioModule(
  (module): Model(
    (conv1): Conv2d(3, 32, kernel_size=(3, 3), stride=(1, 1))
    (relu1): ReLU()
    (pool1): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (conv2): Conv2d(32, 64, kernel_size=(3, 3), stride=(1, 1))
    (relu2): ReLU()
    (pool2): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (conv3): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1))
    (relu3): ReLU()
    (fc): Linear(in_features=128, out_features=10, bias=True)
  )
)<jupyter_text>Modify Configuration File to Append the Model Path<jupyter_code># Open an example configuration file
with open("biotorch/benchmark_configs/cifar10/le_net/sgd/fa.yaml", 'r') as stream:
    try:
        file = yaml.safe_load(stream)
    except yaml.YAMLError as exc:
        print(exc)

# Re write to put our custom model path
file['model'] = {
    'architecture': None,
    'loss_function': {'name': 'cross_entropy'},
    'mode': {'options': {'constrain_weights': False,
    'gradient_clip': False,
    'init': 'xavier'},
    'type': 'fa'},
    'pretrained': False,
    'checkpoint': 'biomodel.pth'
    }

file['experiment']['name'] = 'custom_model_fa'
# Deterministic won't work from Colab because we have to set CUDA environments
file['experiment']['deterministic'] = False

with open("biotorch/custom_model.yaml", 'w') as outfile:
    yaml.dump(file, outfile)<jupyter_output><empty_output><jupyter_text>Run Benchmark<jupyter_code>!ls
%cd biotorch
benchmark = Benchmark('custom_model.yaml')
benchmark.run()<jupyter_output>Preparing CIFAR-10 Benchmark Dataset and storing data in ./datasets/cifar10
Downloading https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz to ./datasets/cifar10/cifar-10-python.tar.gz
{'fraction_non_alphanumeric': 0.15555555555555556, 'fraction_numerical': 0.03867521367521368, 'mean_word_length': 4.559382422802851, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 22, 'https://': 2, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '586238', 'license': 'Apache-2.0', 'n_tokens_mistral': 1889, 'n_tokens_neox': 1727, 'n_words': 365, 'path': 'notebooks/accuracy_benchmark/benchmark_custom_model.ipynb', 'repo_name': 'jsalbert/biotorch', 'size': '4680'}