Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Graphs from overparameterized broadness script #16

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import argparse
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import matplotlib as mpl
import matplotlib.pyplot as plt

from toumei.misc.model_broadness import BroadnessMeasurer

Expand Down Expand Up @@ -60,19 +63,49 @@ def train(net, criterion, optimizer):
last_loss = val

def main():
torch.manual_seed(0)
parser = argparse.ArgumentParser()
parser.add_argument('--seed', type=int, default=0)
parser.add_argument('--graph', action='store_true')

args = parser.parse_args()

# Would "generate and print random seed" be useful instead of defaulting to
# 0?
torch.manual_seed(args.seed)

net = Net_9()
criterion = nn.MSELoss()
optimizer = optim.SGD(net.parameters(), lr=0.01)

train(net, criterion, optimizer)

measurer = BroadnessMeasurer(net,
list(zip(data, labels)),
torch.nn.MSELoss())
_, deltas = measurer.run([x * 0.0001 for x in range(10)], num_itrs=10000)
print(deltas)
if args.graph:
steps = [x/100 for x in range(0, 101, 1)]
points = [[[x, y] for x in steps] for y in steps[::-1]]
results = net(torch.tensor(points)).detach().numpy()

fix, ax = plt.subplots()
im = ax.imshow(results)

# The range of values will be roughly (epsilon, 1-epsilon), so that's
# what gets assigned colors by default. It looks fine, but means the
# colorbar doesn't get values at 0 and 1. set_clim fixes that.
im.set_clim(0, 1)
cbar = ax.figure.colorbar(im, ax=ax)

tick_points = list(range(0, 101, 10))
tick_vals = [x/100 for x in tick_points]
ax.set_xticks(tick_points, tick_vals)
ax.set_yticks(tick_points, tick_vals[::-1])

plt.show()
else:
measurer = BroadnessMeasurer(net,
list(zip(data, labels)),
torch.nn.MSELoss())
_, deltas = measurer.run([x * 0.0001 for x in range(10)],
num_itrs=10000)
print(deltas)

if __name__ == '__main__':
main()