Contact Us!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Avatar for stephanie's main branch.

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

| Download

"Guiding Future STEM Leaders through Innovative Research Training" ~ thinkingbeyond.education

Views: 1138
Image: ubuntu2204
Kernel: Python 3

Open In Colab

Summary of the Code

In this code, we use matplotlib to visualise the construction of simplices from ReLU functions

import matplotlib.pyplot as plt import numpy as np import matplotlib as mpl from matplotlib import cm from matplotlib.colors import LinearSegmentedColormap, ListedColormap from matplotlib.ticker import LinearLocator #customising colormap YlGn_r_big = mpl.colormaps['YlGn_r'] newcmp = ListedColormap(YlGn_r_big(np.linspace(0.05, 0.75, 256))) ax = plt.figure(figsize=(10,10)).add_subplot(projection='3d') # Make data. X = np.arange(-1, 1, 0.025) xlen = len(X) Y = np.arange(-1, 1, 0.025) ylen = len(Y) X, Y = np.meshgrid(X, Y) r= np.zeros(xlen) R = np.fmax(r,X) #plotting ax.plot_surface(X,Y,R, alpha = 1, cmap=newcmp) plt.xlabel('x') plt.ylabel('y') ax.set_zlabel('z') ax.set_zlim(-1, 1) ax.zaxis.set_major_locator(LinearLocator(6)) ax.set_facecolor("#93c280") ax.view_init(30, 250) plt.show()
Image in a Jupyter notebook
import matplotlib.pyplot as plt import numpy as np import matplotlib as mpl from matplotlib import cm from matplotlib.colors import LinearSegmentedColormap, ListedColormap from matplotlib.ticker import LinearLocator #customising colormap YlGn_r_big = mpl.colormaps['YlGn_r'] newcmp = ListedColormap(YlGn_r_big(np.linspace(0.05, 0.75, 256))) ax = plt.figure(figsize=(10,10)).add_subplot(projection='3d') # Make data. X = np.arange(-5, 5, 0.025) xlen = len(X) Y = np.arange(-5, 5, 0.025) ylen = len(Y) X, Y = np.meshgrid(X, Y) r= np.zeros(xlen) Z = np.fmax(r,(1-np.fmax(r,0.3*X)-np.fmax(r,-0.3*X)-np.fmax(r,-0.3*Y)-np.fmax(r,0.3*Y))) #plotting ax.plot_surface(X,Y,Z, alpha = 1, cmap=newcmp) plt.xlabel('x') plt.ylabel('y') ax.set_zlabel('z') ax.set_zlim(-1, 1) ax.zaxis.set_major_locator(LinearLocator(6)) ax.set_facecolor("#93c280") ax.view_init(30, 250) plt.show()
Image in a Jupyter notebook
import matplotlib.pyplot as plt import numpy as np import matplotlib as mpl from matplotlib import cm from matplotlib.colors import LinearSegmentedColormap, ListedColormap from matplotlib.ticker import LinearLocator #colormap customisation YlGn_r_big = mpl.colormaps['YlGn_r'] newcmp = ListedColormap(YlGn_r_big(np.linspace(0.05, 0.75, 256))) ax = plt.figure(figsize=(10,10)).add_subplot(projection='3d') def a(n,x,y): return np.fmax(r,(1-np.fmax(r,n*(x))-np.fmax(r,-n*(x))-np.fmax(r,-n*(y))-np.fmax(r,n*(y)))) # Make data. X = np.arange(-3.5, 3.5, 0.005) xlen = len(X) Y = np.arange(-3.5, 3.5, 0.005) ylen = len(Y) X, Y = np.meshgrid(X, Y) r= np.zeros(xlen) R = np.fmax(r,X) #relu Z= a(0.3,X,Y) #pyramid D= 0.35*(a(0.5,1.5+X,Y)+a(0.5,1.5 -X,Y)+a(0.5,X,1.5-Y)+a(0.5,X,1.5+Y))+0.35*(a(0.5,1-X,1-Y)+a(0.5,1+X,1+Y)+a(0.5,1-X,1+Y)+a(0.5,1+X,1-Y)) #random function #plotting ax.plot_surface(X,Y,D, alpha = 1, cmap=newcmp) plt.xlabel('x') plt.ylabel('y') ax.set_zlabel('z') ax.set_zlim(-1,1) ax.set_ylim(-3.5, 3.5) ax.set_xlim(-3.5, 3.5) ax.zaxis.set_major_locator(LinearLocator(6)) ax.set_facecolor("#93c280") ax.view_init(30, 60) plt.show()
Image in a Jupyter notebook