Skip to content

Instantly share code, notes, and snippets.

@ashishrana160796
Last active December 27, 2018 17:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashishrana160796/5e3612ec6d312ebcb9c6db7e27980283 to your computer and use it in GitHub Desktop.
Save ashishrana160796/5e3612ec6d312ebcb9c6db7e27980283 to your computer and use it in GitHub Desktop.
NAC and NALU unit 3-D plot python code with matplotlib library. Can be used as a reference for plotting sigmoid, tanh function also.
'''
NAC 3-D Plot Code
'''
# import statements
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as np
# 3-D axis creation
fig = plt.figure()
ax = fig.gca(projection='3d')
# Two functions for multiplication, and creating transformation matrix W
sigmoid = lambda y: 1 / (1 + np.exp(-y))
tanh = lambda x: ((np.exp(x)-np.exp(-x))/(np.exp(x)+np.exp(-x)))
# Make data
X = np.arange(-10, 10, 0.25)
Y = np.arange(-10, 10, 0.25)
X, Y = np.meshgrid(X, Y)
# Final function for Transformation Matrix W
Z = tanh(X)*sigmoid(Y)
# Axis Labelling
ax.set_xlabel('tanh(x)', fontsize=14)
ax.set_ylabel('sigmoid(y)', fontsize=14)
ax.set_zlabel('W', fontsize=14)
# Plot the surface.
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
linewidth=0, antialiased=False)
# Customize the z axis.
ax.set_zlim(-1.11, 1.11)
ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
# Add a color bar which maps values to colors.
fig.colorbar(surf, shrink=0.5, aspect=5)
# show plot
plt.show()
# save plot in given directory
fig.savefig('NAC.png')
'''
NALU 3-D Plot Code
'''
# import statements
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as np
# 3-D axis creation
fig = plt.figure()
ax = fig.gca(projection='3d')
# Sub-function decleration for NALU units
sigmoid = lambda y: 1 / (1 + np.exp(-y))
tanh = lambda x: ((np.exp(x)-np.exp(-x))/(np.exp(x)+np.exp(-x)))
# learned gate
g_x = lambda x: 1 / (1 + np.exp(-x))
# small epsilon value
eps = 10e-2
# log-exponential space
m_y = lambda y: np.exp(np.log(np.abs(y)+eps))
# Make data
X = np.arange(-10, 10, 0.25)
Y = np.arange(-10, 10, 0.25)
X, Y = np.meshgrid(X, Y)
# Final function for Transformation Matrix W
W_nac = tanh(X)*sigmoid(Y)
# Final function for NALU
Z = ( (g_x(Y)*W_nac) + ((1-g_x(Y))*m_y(Y)) )
# Axis Labelling
ax.set_xlabel('tanh(x)', fontsize=12)
ax.set_ylabel('sig-M/G(y), exp(log(|y|+eps))', fontsize=12)
ax.set_zlabel('Y', fontsize=14)
# Plot the surface.
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
linewidth=0, antialiased=False)
# Customize the z axis.
ax.set_zlim(-1.1, 10.1)
ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
# Add a color bar which maps values to colors.
fig.colorbar(surf, shrink=0.5, aspect=5)
# show plot
plt.show()
# save plot in given directory
fig.savefig('NALU.png')
@ashishrana160796
Copy link
Author

ashishrana160796 commented Dec 27, 2018

NAC Preview

nac


NALU Preview

nalu

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment