Goal: Investigate how DSNN fares in a toy problem.
Compare following models:
In [1]:
%load_ext autoreload
%autoreload 2
In [2]:
# general imports
import os
import numpy as np
# torch imports
import torch
import torch.optim as optim
import torch.optim.lr_scheduler as schedulers
import torch.nn as nn
from torch.utils.data import DataLoader
from torchvision import datasets, transforms
from torchsummary import summary
# nupic research imports
from nupic.research.frameworks.pytorch.image_transforms import RandomNoise
from nupic.torch.modules import KWinners
# local library
from networks_module.base_networks import *
from models_module.base_models import *
# local files
from utils import *
import math
# plotting
import matplotlib.pyplot as plt
from matplotlib import rcParams
%matplotlib inline
%config InlineBackend.figure_format = 'retina'
rcParams['figure.figsize'] = (12,6)
PATH_TO_WHERE_DATASET_WILL_BE_SAVED = PATH = "~/nta/datasets"
In [3]:
# load dataset
config = (dict(
dataset_name="MNIST",
data_dir="~/nta/datasets",
test_noise=True
))
dataset = Dataset(config)
In [4]:
from models_module.base_models import BaseModel
from networks_module.hebbian_networks import MLP
# load a regular network
network = MLP()
# load a regular base model for training
model = BaseModel(network=network)
model.setup()
In [5]:
%%time
large_dense = model.train(dataset, 30, test_noise=False);
In [6]:
from models_module.base_models import SparseModel
# load a regular network
network = MLP()
# load a regular base model for training
config = dict(
debug_sparse=True,
on_perc=0.1,
)
model = SparseModel(network=network, config=config)
model.setup()
In [7]:
%%time
large_sparse = model.train(dataset, 30, test_noise=False);
In [9]:
results = large_sparse
h, w = math.ceil(len(results)/4), 4
combinations = []
for i in range(h):
for j in range(w):
combinations.append((i,j))
fig, axs = plt.subplots(h, w, gridspec_kw={'hspace': 0.5, 'wspace': 0.5})
fig.set_size_inches(16,16)
for (i, j), k in zip(combinations[:len(results)], sorted(results.keys())):
axs[i, j].plot(range(len(results[k])), results[k])
axs[i, j].set_title(k)
In [10]:
from models_module.base_models import DSNNMixedHeb
from networks_module.hebbian_networks import MLPHeb
# load a regular network
network = MLPHeb()
# load a regular base model for training
config = dict(
weight_prune_perc=0.15,
hebbian_prune_perc=0.60,
pruning_es =False,
pruning_active=True,
pruning_interval=1,
debug_sparse=True,
on_perc=0.1,
)
model = DSNNMixedHeb(network=network, config=config)
model.setup()
# debug sparsity
# [b/a for a,b in zip([784*400, 400*400, 400*400, 400*10], model.num_params)]
In [11]:
%%time
dynamic_sparse = model.train(dataset, 30, test_noise=False);
In [12]:
# results.keys(), len(results)
In [13]:
# results
In [14]:
results = dynamic_sparse
h, w = math.ceil(len(results)/4), 4
combinations = []
for i in range(h):
for j in range(w):
combinations.append((i,j))
fig, axs = plt.subplots(h, w, gridspec_kw={'hspace': 0.5, 'wspace': 0.5})
fig.set_size_inches(16,16)
for (i, j), k in zip(combinations[:len(results)], sorted(results.keys())):
axs[i, j].plot(range(len(results[k])), results[k])
axs[i, j].set_title(k)
In [15]:
from models_module.base_models import BaseModel
from networks_module.hebbian_networks import MLP
# load a regular network
config = dict(
hidden_sizes = [12,16,23]
)
network = MLP(config=config)
# load a regular base model for training
model = BaseModel(network=network)
model.setup()
In [16]:
%%time
small_dense = model.train(dataset, 30, test_noise=False);
In [32]:
from matplotlib import rcParams
rcParams['image.cmap'] = 'Accent'
In [37]:
plt.figure(figsize=(14,7))
names = ['large_dense', 'large_sparse', 'dynamic_sparse', 'small_dense']
for name, res in zip(names, [large_dense, large_sparse, dynamic_sparse, small_dense]):
plt.plot(res['train_acc'], label=name)
plt.legend()
plt.ylim((0.85,1.0));
In [38]:
plt.figure(figsize=(14,7))
names = ['large_dense', 'large_sparse', 'dynamic_sparse', 'small_dense']
for name, res in zip(names, [large_dense, large_sparse, dynamic_sparse, small_dense]):
plt.plot(res['val_acc'], label=name)
plt.legend()
plt.ylim((0.85,1.0));
In [53]:
from models_module.base_models import BaseModel, SparseModel, DSNNMixedHeb
from networks_module.hebbian_networks import MLP, MLPHeb
In [54]:
# load dataset
config = (dict(
dataset_name="MNIST",
data_dir="~/nta/datasets",
test_noise=True
))
dataset = Dataset(config)
In [55]:
test_noise = True
use_kwinners = True
epochs = 30
on_perc = 0.1
# large dense
config = dict(hidden_sizes=[100,100,100], use_kwinners=use_kwinners)
network = MLP(config=config)
model = BaseModel(network=network)
model.setup()
print("\nLarge Dense")
large_dense = model.train(dataset, epochs, test_noise=test_noise);
# large sparse
config = dict(hidden_sizes=[100,100,100], use_kwinners=use_kwinners)
network = MLP(config=config)
config = dict(debug_sparse=True, on_perc=on_perc)
model = SparseModel(network=network, config=config)
model.setup()
print("\nLarge Sparse")
large_sparse = model.train(dataset, epochs, test_noise=test_noise);
# dynamic sparse
config = dict(hidden_sizes=[100,100,100], use_kwinners=use_kwinners)
network = MLPHeb(config=config)
config = dict(
weight_prune_perc=0.15,
hebbian_prune_perc=0.60,
pruning_es =False,
pruning_active=True,
pruning_interval=1,
debug_sparse=True,
on_perc=on_perc,
)
model = DSNNMixedHeb(network=network, config=config)
model.setup()
print("\nDynamic Sparse")
dynamic_sparse = model.train(dataset, epochs, test_noise=test_noise);
# small dense
config = dict(hidden_sizes=[12,16,23], use_kwinners=use_kwinners)
network = MLP(config=config)
# load a regular base model for training
model = BaseModel(network=network)
model.setup()
print("\nSmall Dense")
small_dense = model.train(dataset, epochs, test_noise=test_noise);
In [56]:
plt.figure(figsize=(14,7))
names = ['large_dense', 'large_sparse', 'dynamic_sparse', 'small_dense']
for name, res in zip(names, [large_dense, large_sparse, dynamic_sparse, small_dense]):
plt.plot(res['train_acc'], label=name)
plt.legend()
plt.ylim((0.85,1.0));
In [57]:
plt.figure(figsize=(14,7))
names = ['large_dense', 'large_sparse', 'dynamic_sparse', 'small_dense']
for name, res in zip(names, [large_dense, large_sparse, dynamic_sparse, small_dense]):
plt.plot(res['val_acc'], label=name)
plt.legend()
plt.ylim((0.85,1.0));
In [58]:
plt.figure(figsize=(14,7))
names = ['large_dense', 'large_sparse', 'dynamic_sparse', 'small_dense']
for name, res in zip(names, [large_dense, large_sparse, dynamic_sparse, small_dense]):
plt.plot(res['noise_acc'], label=name)
plt.legend()
plt.ylim((0.85,1.0));
In [ ]: