-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart2-2.py
100 lines (85 loc) · 2.82 KB
/
part2-2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# -*- coding: utf-8 -*-
"""chaitanya_part_2_observegradient.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/17qzBPPy81cwF6YVJmRHePbv8OSmcnx3a
"""
pip install tensorflow==2.4
# Commented out IPython magic to ensure Python compatibility.
# This file calculates gradient norm during the training of a DNN
import tensorflow as tf
import numpy as np
import torch
import torchvision as tv
from torchvision import transforms, datasets
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import matplotlib.pyplot as plt
# %matplotlib inline
# Create random data between (-10, 10) and determine groundtruth
# GroundTruth Function: y = arcsinh(5*pi*x)
simulatedInput = 20 * torch.rand((1000, 1)) - 10
groundTruth = np.arcsinh(5*np.pi*simulatedInput)
# Calculate the number of parameters in a neural network
def calcParams(inputModel):
val = sum(params.numel() for params in inputModel.parameters() if params.requires_grad)
return val
# Set up NN for MNIST training - 3 Hidden layer, 209 parameters
class GradientNN(nn.Module):
def __init__(self):
super().__init__()
self.fc1 = nn.Linear(1, 8)
self.fc2 = nn.Linear(8, 12)
self.fc3 = nn.Linear(12, 6)
self.fc4 = nn.Linear(6, 1)
def forward(self, val):
val = F.relu(self.fc1(val))
val = F.relu(self.fc2(val))
val = F.relu(self.fc3(val))
val = self.fc4(val)
return val
model1 = GradientNN()
print(calcParams(model1))
# Set up necessary auxilaries for neural net training
gradNet = GradientNN()
costFunc = nn.MSELoss()
opt = optim.Adam(gradNet.parameters(), lr=0.001)
EPOCHS = 2000
# Train Network
costList = []
gradNormList = []
counterList = []
counter = 1
for index in range(EPOCHS):
counterList.append(counter)
counter += 1
gradNet.zero_grad()
output = gradNet(simulatedInput)
cost = costFunc(output, groundTruth)
costList.append(cost)
cost.backward()
opt.step()
# Get gradient norm (From slides)
gradAll = 0.0
for p in gradNet.parameters():
grad = 0.0
if p.grad is not None:
grad = (p.grad.cpu().data.numpy() ** 2).sum()
gradAll += grad
gradNorm = gradAll ** 0.5
gradNormList.append(gradNorm)
# Visulaize Training process of arcsinh(5*np.pi*x) function
plt.plot(counterList, costList, 'y', label='Model')
plt.title("Learning Progression for arcsinh(5*np.pi*x)")
plt.xlabel("EPOCHS")
plt.ylabel("Mean Squared Error")
plt.legend(loc="upper right")
plt.show()
# Visulaize Gradient Norm of arcsinh(5*np.pi*x) function during training
plt.plot(counterList, gradNormList, 'y', label='Model')
plt.title("Gradient Norm during Training for arcsinh(5*np.pi*x)")
plt.xlabel("EPOCHS")
plt.ylabel("Gradient Norm")
plt.legend(loc="upper right")
plt.show()