Skip to content

Instantly share code, notes, and snippets.

@BundleOfKent
Created December 17, 2020 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BundleOfKent/dbc60bb7c6ef0b8ae3f8ceab8c334b57 to your computer and use it in GitHub Desktop.
Save BundleOfKent/dbc60bb7c6ef0b8ae3f8ceab8c334b57 to your computer and use it in GitHub Desktop.
from matplotlib import animation
N=500 # new sample size (N=500)
# Define new cost function w.r.t. new weights of second hidden layer:
def costs_3(x,y,w_a,w_b,seed_):
np.random.seed(seed_)
w0=np.random.randn(hidden_0,784)
w1= np.random.randn(hidden_1,hidden_0)
w2=np.random.randn(10,hidden_1)
w1[200][30] = w_a # w200–30(1)
w1[200][31] = w_b # w200–31(1)
a0 = expit(w0 @ x.T)
a1= expit(w1@a0)
pred= expit(w2 @ a1)
return np.mean(np.sum((y.T-pred)**2,axis=0))
# Calculate z-values w.r.t. random seed with new cost function:
zs_3 = np.array([costs_3(X_train[0:N],y_train_oh[0:N]
,np.array([[mp1]]), np.array([[mp2]]),3)
for mp1, mp2 in zip(np.ravel(M1), np.ravel(M2))])
Z_3 = zs_3.reshape(M1.shape)
zs_16 = np.array([costs_3(X_train[0:N],y_train_oh[0:N]
,np.array([[mp1]]), np.array([[mp2]]),16)
for mp1, mp2 in zip(np.ravel(M1), np.ravel(M2))])
Z_16 = zs_16.reshape(M1.shape)
zs_60 = np.array([costs_3(X_train[0:N],y_train_oh[0:N]
,np.array([[mp1]]), np.array([[mp2]]),60)
for mp1, mp2 in zip(np.ravel(M1), np.ravel(M2))])
Z_60 = zs_60.reshape(M1.shape)
zs_140 = np.array([costs_3(X_train[0:N],y_train_oh[0:N]
,np.array([[mp1]]), np.array([[mp2]]),140)
for mp1, mp2 in zip(np.ravel(M1), np.ravel(M2))])
Z_140 = zs_140.reshape(M1.shape)
titlefontsize_=16
fontsize_=19
# Add subplots to figure:
fig = plt.figure(figsize=(8,8))
ax0 = fig.add_subplot(2, 2, 1,projection='3d' )
ax1=fig.add_subplot(2, 2, 2,projection='3d')
ax2=fig.add_subplot(2, 2, 3,projection='3d')
ax3=fig.add_subplot(2, 2, 4,projection='3d')
# Customize subplots:
ax0.set_title('Seed:3', fontsize=titlefontsize_)
ax0.set_xlabel(r'$w_a$', fontsize=fontsize_, labelpad=-4)
ax0.set_ylabel(r'$w_b$', fontsize=fontsize_, labelpad=-9)
ax0.set_zlabel("costs", fontsize=fontsize_, labelpad=-7)
ax0.set_xticklabels([])
ax0.set_yticklabels([])
ax0.set_zticklabels([])
ax1.set_title('Seed:16', fontsize=titlefontsize_)
ax1.set_xlabel(r'$w_a$', fontsize=fontsize_, labelpad=-4)
ax1.set_ylabel(r'$w_b$', fontsize=fontsize_, labelpad=-9)
ax1.set_zlabel("costs", fontsize=fontsize_, labelpad=-7)
ax1.set_xticklabels([])
ax1.set_yticklabels([])
ax1.set_zticklabels([])
ax2.set_title('Seed:60', fontsize=titlefontsize_)
ax2.set_xlabel(r'$w_a$', fontsize=fontsize_, labelpad=-4)
ax2.set_ylabel(r'$w_b$', fontsize=fontsize_, labelpad=-9)
ax2.set_zlabel("costs", fontsize=fontsize_, labelpad=-7)
ax2.set_xticklabels([])
ax2.set_yticklabels([])
ax2.set_zticklabels([])
ax3.set_title('Seed:140', fontsize=titlefontsize_)
ax3.set_xlabel(r'$w_a$', fontsize=fontsize_, labelpad=-4)
ax3.set_ylabel(r'$w_b$', fontsize=fontsize_, labelpad=-9)
ax3.set_zlabel("costs", fontsize=fontsize_, labelpad=-7)
ax3.set_xticklabels([])
ax3.set_yticklabels([])
ax3.set_zticklabels([])
# Rotate plots around the z-axis:
def rotate(angle):
ax0.view_init(elev=50,azim=angle)
ax1.view_init(elev=50,azim=angle)
ax2.view_init(elev=50,azim=angle)
ax3.view_init(elev=50,azim=angle)
# Create loss landscapes w.r.t. seed:
ax0.plot_surface(M1, M2, Z_3, cmap='terrain',
antialiased=True,cstride=1,rstride=1, alpha=0.99)
ax1.plot_surface(M1, M2, Z_16, cmap='terrain',
antialiased=True,cstride=1,rstride=1, alpha=0.99)
ax2.plot_surface(M1, M2, Z_60, cmap='terrain',
antialiased=True,cstride=1,rstride=1, alpha=0.99)
ax3.plot_surface(M1, M2, Z_140, cmap='terrain',
antialiased=True,cstride=1,rstride=1, alpha=0.99)
plt.tight_layout()
rot_animation = animation.FuncAnimation(fig, rotate, frames=np.arange(0,362,2),interval=100)
rot_animation.save('RotLoss_500.gif', dpi=80, writer='imagemagick')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment