-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
69 lines (61 loc) · 1.87 KB
/
util.py
File metadata and controls
69 lines (61 loc) · 1.87 KB
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
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
def showCostPlot(J, testJ):
plt.plot(J)
plt.plot(testJ)
plt.grid(1)
plt.ylabel('Cost')
plt.xlabel('Iterations')
plt.show()
def showContourPlot(xx, yy, outputs):
# Make Contour Plot
CS = plt.contour(xx, yy, 100*outputs.reshape(100,100))
plt.clabel(CS, inline=1, fontsize=10)
plt.xlabel('Hours Sleep')
plt.ylabel('Hours Study')
plt.show()
def show3DPlot(xx, yy, outputs):
# Make 3d plot
fig = plt.figure()
ax = fig.gca(projection='3d')
surf = ax.plot_surface(xx, yy, 100*outputs.reshape(100,100), cmap=cm.jet)
ax.set_xlabel('Hours Sleep')
ax.set_ylabel('Hours Study')
ax.set_zlabel('Test Scores')
plt.show()
def showProjectionsPlot(x, y):
# Make projections plot
fig = plt.figure(0,(8,3))
plt.subplot(1,2,1)
plt.scatter(x[:,0],y)
plt.grid(1)
plt.xlabel('Hours Sleeping')
plt.ylabel('Test Score')
plt.subplot(1,2,2)
plt.scatter(x[:,1], y)
plt.grid(1)
plt.xlabel('Hours Studying')
plt.ylabel('Test Score')
plt.show()
def computeNumericalGradient(nn, x, y):
paramsInitial = nn.getParams()
numgrad = np.zeros(paramsInitial.shape)
perturb = np.zeros(paramsInitial.shape)
e = 1e-4 #variation
for p in range(len(paramsInitial)):
perturb[p] = e # Set perturbation vector
# Calc loss2
nn.setParams(paramsInitial + perturb)
loss2 = nn.costFunction(x, y)
# Calc loss1
nn.setParams(paramsInitial - perturb)
loss1 = nn.costFunction(x, y)
# Compute Numerical Gradient between
numgrad[p] = (loss2 - loss1) / (2*e)
# Return the valuye we changed back to zero
perturb[p] = 0
# Return params to original value
nn.setParams(paramsInitial)
return numgrad