I want to draw the progress of the gradient descent.
I start with the function "f" and the partial derivation of the function and start the gradient descent with the parameters you see in the code.
When I plot the 3D graph of the function and the points of the gradient descent which i store during the iterations, the markers of the progress aren't good visible.
def f(X,Y):
return (X**2) (2*(Y**2)) (2*X*Y) - (6*X) - (16*Y) (41)
def derivation_x(X, Y):
return (2*X) (2*Y) - (6)
def derivation_y(X,Y):
return (2*X) (4*Y) - (16)
def gradient_step(x, y,lr=0.0000001 ):
new_x = derivation_x(x, y) * lr
new_y = derivation_y(x, y) * lr
return new_x, new_y
def lost_z(x,y):
return f(x,y)
def gradientenDescent(startx, starty):
new_x, new_y = gradient_step(startx, starty, lr=0.005 )
diffx = startx - new_x
diffy= starty - new_y
return diffx, diffy
# I start the folling in a jupyter cell
_____________________________
start_x, start_y = -20 ,-20
points = []
for iteratrion in range(250):
x_step, y_step = gradientenDescent(start_x, start_y)
newlost = lost_z(x_step, y_step )
points.append([start_x, start_y, newlost])
if iteratrion % 10 == 0:
print("iteratrion:" str(iteratrion))
print("x:" str(start_x) " y:" str(start_y))
print("Z:" str(newlost))
print("___________________")
start_x, start_y = x_step, y_step
___________________________________
size = 12
X, Y = np.linspace(-25,25,25), np.linspace(-25,25,25)
X, Y = np.meshgrid(X,Y)
fig = plt.figure(figsize= (size,size))
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface( X ,Y, f(X,Y), cmap="jet", alpha = 0.6)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
for i in range(len(points)):
ax.scatter(points[i,0],points[i,1],points[i,2],color='r', marker='x', alpha = 1)
ax.elev = 270
ax.azim = 270 # xz view
ax.elev = 180
ax.azim = 180 # yz view
ax.elev = 15
ax.azim = 15 # xy view
plt.show()
_________________________
So now i want the markers be more visible as it is now.
here is the Image matplotlib draw so far
CodePudding user response:
The Scatters are hidden by the 3dSurface. To avoid this you can use the following:
ax = fig.add_subplot(111, projection='3d', computed_zorder=False)
...
ax.plot_surface(X, Y, f(X, Y), cmap="jet", alpha=0.6,zorder=1)
...
for i in range(len(points)):
ax.scatter(points[i,0],points[i,1],points[i,2],color='r', marker='x', alpha = 1, zorder=2, s=21)
use s=21
to change the size of xmarks. maybe it would also look nice if a cmap would be used for the scatters instead of an static color