|
| 1 | +from __future__ import print_function, division |
| 2 | +from builtins import range |
| 3 | +# Note: you may need to update your version of future |
| 4 | +# sudo pip install -U future |
| 5 | + |
| 6 | +import numpy as np |
| 7 | +import matplotlib.pyplot as plt |
| 8 | +from mpl_toolkits.mplot3d import Axes3D |
| 9 | + |
| 10 | +# NOTE: some people using the default Python |
| 11 | +# installation on Mac have had trouble with Axes3D |
| 12 | +# Switching to Python 3 (brew install python3) or |
| 13 | +# using Linux are both viable work-arounds |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +# generate and plot the data |
| 20 | +N = 500 |
| 21 | +X = np.random.random((N, 2))*4 - 2 # in between (-2, +2) |
| 22 | +Y = X[:,0]*X[:,1] # makes a saddle shape |
| 23 | +# note: in this script "Y" will be the target, |
| 24 | +# "Yhat" will be prediction |
| 25 | + |
| 26 | +fig = plt.figure() |
| 27 | +ax = fig.add_subplot(111, projection='3d') |
| 28 | +ax.scatter(X[:,0], X[:,1], Y) |
| 29 | +plt.show() |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | +# make a neural network and train it |
| 36 | +D = 2 |
| 37 | +M = 100 # number of hidden units |
| 38 | + |
| 39 | +# layer 1 |
| 40 | +W = np.random.randn(D, M) / np.sqrt(D) |
| 41 | +b = np.zeros(M) |
| 42 | + |
| 43 | +# layer 2 |
| 44 | +V = np.random.randn(M) / np.sqrt(M) |
| 45 | +c = 0 |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | +# how to get the output |
| 51 | +# consider the params global |
| 52 | +def forward(X): |
| 53 | + Z = X.dot(W) + b |
| 54 | + Z = Z * (Z > 0) # relu |
| 55 | + # Z = np.tanh(Z) |
| 56 | + |
| 57 | + Yhat = Z.dot(V) + c |
| 58 | + return Z, Yhat |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | +# how to train the params |
| 64 | +def derivative_V(Z, Y, Yhat): |
| 65 | + return (Y - Yhat).dot(Z) |
| 66 | + |
| 67 | +def derivative_c(Y, Yhat): |
| 68 | + return (Y - Yhat).sum() |
| 69 | + |
| 70 | +def derivative_W(X, Z, Y, Yhat, V): |
| 71 | + # dZ = np.outer(Y - Yhat, V) * (1 - Z * Z) # this is for tanh activation |
| 72 | + dZ = np.outer(Y - Yhat, V) * (Z > 0) # relu |
| 73 | + return X.T.dot(dZ) |
| 74 | + |
| 75 | +def derivative_b(Z, Y, Yhat, V): |
| 76 | + # dZ = np.outer(Y - Yhat, V) * (1 - Z * Z) # this is for tanh activation |
| 77 | + dZ = np.outer(Y - Yhat, V) * (Z > 0) # this is for relu activation |
| 78 | + return dZ.sum(axis=0) |
| 79 | + |
| 80 | +def update(X, Z, Y, Yhat, W, b, V, c, learning_rate=1e-4): |
| 81 | + gV = derivative_V(Z, Y, Yhat) |
| 82 | + gc = derivative_c(Y, Yhat) |
| 83 | + gW = derivative_W(X, Z, Y, Yhat, V) |
| 84 | + gb = derivative_b(Z, Y, Yhat, V) |
| 85 | + |
| 86 | + V += learning_rate*gV |
| 87 | + c += learning_rate*gc |
| 88 | + W += learning_rate*gW |
| 89 | + b += learning_rate*gb |
| 90 | + |
| 91 | + return W, b, V, c |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | +# so we can plot the costs later |
| 97 | +def get_cost(Y, Yhat): |
| 98 | + return ((Y - Yhat)**2).mean() |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | +# run a training loop |
| 103 | +# plot the costs |
| 104 | +# and plot the final result |
| 105 | +costs = [] |
| 106 | +for i in range(200): |
| 107 | + Z, Yhat = forward(X) |
| 108 | + W, b, V, c = update(X, Z, Y, Yhat, W, b, V, c) |
| 109 | + cost = get_cost(Y, Yhat) |
| 110 | + costs.append(cost) |
| 111 | + if i % 25 == 0: |
| 112 | + print(cost) |
| 113 | + |
| 114 | +# plot the costs |
| 115 | +plt.plot(costs) |
| 116 | +plt.show() |
| 117 | + |
| 118 | +# plot the prediction with the data |
| 119 | +fig = plt.figure() |
| 120 | +ax = fig.add_subplot(111, projection='3d') |
| 121 | +ax.scatter(X[:,0], X[:,1], Y) |
| 122 | + |
| 123 | +# surface plot |
| 124 | +line = np.linspace(-2, 2, 20) |
| 125 | +xx, yy = np.meshgrid(line, line) |
| 126 | +Xgrid = np.vstack((xx.flatten(), yy.flatten())).T |
| 127 | +_, Yhat = forward(Xgrid) |
| 128 | +ax.plot_trisurf(Xgrid[:,0], Xgrid[:,1], Yhat, linewidth=0.2, antialiased=True) |
| 129 | +plt.show() |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | +# plot magnitude of residuals |
| 135 | +Ygrid = Xgrid[:,0]*Xgrid[:,1] |
| 136 | +R = np.abs(Ygrid - Yhat) |
| 137 | + |
| 138 | +plt.scatter(Xgrid[:,0], Xgrid[:,1], c=R) |
| 139 | +plt.show() |
| 140 | + |
| 141 | +fig = plt.figure() |
| 142 | +ax = fig.add_subplot(111, projection='3d') |
| 143 | +ax.plot_trisurf(Xgrid[:,0], Xgrid[:,1], R, linewidth=0.2, antialiased=True) |
| 144 | +plt.show() |
| 145 | + |
| 146 | + |
0 commit comments