```python
from pythreejs import *
```


```python
import numpy as np
from IPython.display import display
from traittypes import Array
from ipywidgets import HTML, Text, Widget
from traitlets import link, dlink, Any, HasTraits
```


```python
class Surf(HasTraits):
    
    function = Any()
    points = Array()
    
    def __init__(self, **kwargs):
        self.xmin = kwargs.pop('xmin', -1)
        self.xmax = kwargs.pop('xmax', 1)
        self.ymin = kwargs.pop('ymin', -1)
        self.ymax = kwargs.pop('ymax', 1)
        
        self.grid_size = kwargs.pop('grid_size', 20)
        
        self.points = kwargs.pop('points', np.zeros((1, 3)))
        self.observe(self.update_points, 'points')
            
        self.point_size = kwargs.pop('size', 0.05)
        
        self.function = kwargs.pop('function', lambda xx, yy: xx**2 - yy**2)
        self.observe(self.update_surf, 'function')
        
        self.surf_color = kwargs.pop('surf_color', 'viridis')
        self.line_color = kwargs.pop('line_color', 'black')
        self.point_colors = kwargs.pop('point_colors', ['red'])
        self.cam_position = [0, 3, 3]
        self.cam_up = [0, 0, 1]
        
        self.plot(**kwargs)
        self.draw(**kwargs)
        
    def plot(self, **kwargs):
        x = np.linspace(self.xmin, self.xmax, self.grid_size)
        y = np.linspace(self.xmin, self.xmax, self.grid_size)
        xx, yy = np.meshgrid(x, y)
        self.z = self.function(xx, yy)
        
    def update_surf(self, new):
        self.plot()
        self.surface_geom.z = list(self.z[::-1].flat)
        self.surface.material = LambertMaterial(map=height_texture(self.z[::-1], self.surf_color))
        
    def update_points(self, new):
        self.draw_points()
        self.marks = [self.surface, self.surfgrid] + self.dots + [AmbientLight(color='#777777')]
        self.scene.children = self.marks
        
    def draw_surf(self, **kwargs):
        self.surface_geom = SurfaceGeometry(z=list(self.z[::-1].flat), 
                         width=2 * self.xmax,
                         height=2 * self.xmax,
                         width_segments=self.grid_size - 1,
                         height_segments=self.grid_size - 1)
        self.surface = Mesh(geometry=self.surface_geom, 
                         material=LambertMaterial(map=height_texture(self.z[::-1], self.surf_color)))
        self.surfgrid = SurfaceGrid(geometry=self.surface_geom, material=LineBasicMaterial(color=self.line_color))
        
    def draw_points(self, **kwargs):
        self.dots = []
        for i in range(self.points.shape[0]):
            self.dots.append(Mesh(geometry=SphereGeometry(radius=self.point_size), 
                                    material=LambertMaterial(color=self.point_colors[i % len(self.point_colors
                                                                                            )]),
                                    position=self.points[i, :].tolist()))
    
    def draw(self, **kwargs):
        self.draw_surf(**kwargs)
        self.draw_points(**kwargs)
        
        self.marks = [self.surface, self.surfgrid] + self.dots + [AmbientLight(color='#777777')]
        
        self.scene = Scene(children=self.marks)
        self.cam = PerspectiveCamera(position=self.cam_position, up=self.cam_up, 
                              children=[DirectionalLight(color='white', position=[3, 5, 1], intensity=0.6)])

        self.figure = Renderer(camera=self.cam,
                                 scene = self.scene, controls=[OrbitControls(controlling=self.cam)])
        return
```


```python
a = Surf()
a.figure
```


```python
a.function = lambda x, y: x**3 - 3*x*(y**2)
```


```python
a.points = np.array([[0, 0, 1], [0, 0, -1]])
```


```python
from sympy import *
```


```python
def evaluate_gradient(func, point, variables=['x', 'y']):
    var = []
    grad = []
    for i in variables:
        var.append(symbols(i))
        
    if len(point) != len(variables):
        raise ValueError('Length of points doesnt match the number of variables')

    for i in range(len(var)):
        diff_func = lambdify(variables, diff(sympify(func), var[i]))
        grad.append(diff_func(*point))
    return np.array(grad)
```


```python
func = 'x**3 - 3*x*(y**2)'
```


```python
evaluate_gradient(func, [2, 1])
```




    array([  9, -12])




```python
loc = [.2,.2]
```


```python
z=a.function(loc[0],loc[1])
```


```python
a.points = np.array([[loc[0],loc[1],z]])
```


```python
g = evaluate_gradient(func, loc)
```


```python
step_size = .1
```


```python
loc = loc - step_size * g
```


```python
a.points.append([0,0,1])
```


```python
a.points.shape
```




    (1, 3)



71f7e0f6e4708bf425604e4bedf9f7b53d57c1f2
ipynb
Jupyter Notebook
Archive/2018/Code/SGD/sgd-animation.ipynb
LBJ-Wade/mlcourse
f5af0db001bf5e2fb153d381c10b35d34a491ebf
CC-BY-4.0
2016-01-29T18:44:38.000Z
2022-03-28T21:31:34.000Z
Archive/2018/Code/SGD/sgd-animation.ipynb
LBJ-Wade/mlcourse
f5af0db001bf5e2fb153d381c10b35d34a491ebf
CC-BY-4.0
2016-12-25T19:14:21.000Z
2020-06-20T19:52:59.000Z
Archive/2018/Code/SGD/sgd-animation.ipynb
LBJ-Wade/mlcourse
f5af0db001bf5e2fb153d381c10b35d34a491ebf
CC-BY-4.0
2016-01-31T04:11:57.000Z
2022-03-12T00:45:41.000Z
Qwen/Qwen-72B
1. YES
2. YES