import needed modules
In [1]:
#numerical library
import numpy as np
#plot library
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import matplotlib.pyplot as plt
from pprint import pprint
inizialization of the variable
In [2]:
x,y = np.indices([1024,1024])
Guessing the speed gain if this work
In [3]:
%timeit (x**2+y**2)**0.5
In [4]:
%timeit 0.7531854654594905*(x+y)
So if this approximation work we can stimate the norm over x7 time faster
hyper parameters initialization
In [5]:
epoch_of_training = 1000
learning_rate = 1e-8
gamma = 1
Initial Error
In [6]:
init_sq_err = np.sum(0.5*((gamma*(x+y) - (x**2+y**2)**0.5)**2))
init_sq_err
Out[6]:
the partial derivative of the error
In [7]:
for i in range(epoch_of_training):
gamma -= learning_rate * np.mean((gamma*(x+y)-(x**2+y**2)**0.5)*(x+y))
In [8]:
gamma
Out[8]:
In [9]:
fin_sq_err = np.sum(0.5*((gamma*(x+y) - (x**2+y**2)**0.5)**2))
fin_sq_err
Out[9]:
In [10]:
delta_sq_err = init_sq_err - fin_sq_err
delta_sq_err
Out[10]:
In [11]:
Error = abs(gamma*(x+y) - (x**2+y**2)**0.5)
print(np.max(Error))
print(np.mean(Error))
print(np.min(Error))
In [12]:
fig = plt.figure(figsize=[12,12])
ax = fig.gca(projection='3d')
X = np.arange(1, 1024, 8)
Y = np.arange(1, 1024, 8)
X, Y = np.meshgrid(X, Y)
Z = np.sqrt(X**2 + Y**2)
F = abs(gamma*(X+Y)-Z)/(Z)
surf = ax.plot_surface(X, Y, F, rstride=2, cstride=2, cmap=cm.jet,linewidth=1)
ax.set_xlabel('X')
ax.set_xlim(-10, 1034)
ax.set_ylabel('Y')
ax.set_ylim(-10, 1034)
ax.set_zlabel('Z')
ax.set_zlim(0, 0.30)
ax.invert_yaxis()
ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
fig.colorbar(surf)
plt.show()
In [ ]: