-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
134 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from torchvision import datasets, transforms | ||
default_datapath = "tmp" | ||
|
||
def get_mnist(): | ||
return datasets.MNIST( | ||
root=default_datapath, | ||
train=True, | ||
download=True, | ||
transform=transforms.ToTensor(), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import torch | ||
|
||
if torch.cuda.is_available(): | ||
device = "cuda" | ||
|
||
def to_device(tensor): | ||
return tensor.to(device) | ||
|
||
def to_device_model(model): | ||
model.to("cuda") | ||
|
||
else: | ||
device = "cpu" | ||
|
||
# on cpu we need to use double as otherwise ill-conditioning in sums | ||
# causes numerical instability | ||
def to_device(tensor): | ||
return tensor.double() | ||
|
||
def to_device_model(model): | ||
model.double() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import torch.nn as nn | ||
from .datasets import get_mnist | ||
from .device import to_device_model,to_device | ||
from torch.utils.data import DataLoader, Subset | ||
from nngeometry.layercollection import LayerCollection | ||
|
||
class LayerNormNet(nn.Module): | ||
def __init__(self, out_size): | ||
super(LayerNormNet, self).__init__() | ||
|
||
self.linear1 = nn.Linear(18*18, out_size) | ||
self.layer_norm1 = nn.LayerNorm((out_size,)) | ||
|
||
self.net = nn.Sequential(self.linear1, self.layer_norm1) | ||
|
||
def forward(self, x): | ||
x = x[:, :, 5:-5, 5:-5].contiguous() | ||
x = x.view(x.size(0), -1) | ||
return self.net(x) | ||
|
||
def get_layernorm_task(normalization="none"): | ||
train_set = get_mnist() | ||
train_set = Subset(train_set, range(70)) | ||
train_loader = DataLoader(dataset=train_set, batch_size=30, shuffle=False) | ||
net = LayerNormNet(out_size=3) | ||
to_device_model(net) | ||
net.eval() | ||
|
||
def output_fn(input, target): | ||
return net(to_device(input)) | ||
|
||
layer_collection = LayerCollection.from_model(net) | ||
return (train_loader, layer_collection, net.parameters(), net, output_fn, 3) |