visualize.multi_animation#

gen_simple_gradient(function, history, cnt_dots=200, title='<b>Contour plot with optimization steps</b>', showlegend=True, font_size=18)[source]#

Return go.Figure with gradient steps under contour plot. Not animated

Parameters:
  • function (Callable[[Tensor], Tensor]) – callable that depends on the first positional argument

  • history (HistoryGD) – History after some gradient method

  • cnt_dots (int) – the numbers of point per each axis

  • title (str) – title of chart

  • showlegend (bool) – flag of showing legend

  • font_size (int) – font size

Returns:

go.Figure with contour and line of gradient steps

Return type:

Figure

>>> def f(x): return x[0] ** 2 + x[1] ** 2 / 2
>>> x_opt, hist = gd_optimal(f, torch.tensor([8, 5]), keep_history=True)
>>> gen_simple_gradient(f, hist).show()

gen_animated_surface(function, history, cnt_dots=100, title='<b>Surface with optimization steps</b>')[source]#

Return go.Figure with animation per each step of descent

Parameters:
  • function (Callable[[Tensor], float]) – callable that depends on the first positional argument

  • history (HistoryGD) – History after some gradient method

  • cnt_dots (int) – the numbers of point per each axis

  • title (str) – how many frames will drawing. ~300 frames will be drawn for ~5-10 seconds

Returns:

go.Figure with animation steps on surface

Return type:

Figure

>>> def f(x): return x[0] ** 2 + x[1] ** 2 / 9
>>> _, h = bfgs(f, torch.tensor([10, 10]), keep_history=True)

make_contour(function, bounds, cnt_dots=100, colorscale='teal', showlegend=False)[source]#

Return go.Contour for draw by go.Figure. Evaluate function per each point in the 2d grid

Parameters:
  • function (Callable[[Tensor], float | torch.Tensor]) – callable that depends on the first positional argument

  • bounds (tuple[tuple[float, float], tuple[float, float]]) – two tuples with constraints for x- and y-axis

  • cnt_dots (int) – number of point per each axis

  • colorscale – plotly colorscale for go.Contour

  • showlegend (bool) – show legend flag

Returns:

go.Contour

Return type:

Contour

>>> def f(x): return x[0] ** 2 + x[1] ** 2 / 2
>>> make_contour(f, ((0, 1), (0, 1)), cnt_dots=4)
Contour({
    'colorscale': [[0.0, 'rgb(209, 238, 234)'], [0.16666666666666666, 'rgb(168,
                   219, 217)'], [0.3333333333333333, 'rgb(133, 196, 201)'], [0.5,
                   'rgb(104, 171, 184)'], [0.6666666666666666, 'rgb(79, 144,
                   166)'], [0.8333333333333334, 'rgb(59, 115, 143)'], [1.0,
                   'rgb(42, 86, 116)']],
    'name': 'f(x, y)',
    'showlegend': False,
    'showscale': False,
    'x': array([0.        , 0.33333334, 0.6666666 , 1.        ], dtype=float32),
    'y': array([0.        , 0.33333334, 0.6666666 , 1.        ], dtype=float32),
    'z': array([[0.        , 0.11111112, 0.4444444 , 1.        ],
                [0.05555556, 0.16666669, 0.49999994, 1.0555556 ],
                [0.2222222 , 0.3333333 , 0.66666657, 1.2222222 ],
                [0.5       , 0.6111111 , 0.9444444 , 1.5       ]], dtype=float32)
})
make_surface(function, bounds, cnt_dots=100, colorscale='teal', showlegend=False)[source]#

Return go.Surface for draw by go.Figure. Evaluate function per each point in the 2d grid

Parameters:
  • function (Callable[[Tensor], float | torch.Tensor]) – callable that depends on the first positional argument

  • bounds (tuple[tuple[float, float], tuple[float, float]]) – two tuples with constraints for x- and y-axis

  • cnt_dots (int) – number of point per each axis

  • colorscale – plotly colorscale for go.Contour

  • showlegend (bool) – showlegend flag

Returns:

go.Surface

Return type:

Surface

>>> def f(x): return x[0] ** 2 + x[1] ** 2 / 2
>>> make_surface(f, ((0, 1), (0, 1)), cnt_dots=4)
Surface({
    'colorscale': [[0.0, 'rgb(209, 238, 234)'], [0.16666666666666666, 'rgb(168,
                   219, 217)'], [0.3333333333333333, 'rgb(133, 196, 201)'], [0.5,
                   'rgb(104, 171, 184)'], [0.6666666666666666, 'rgb(79, 144,
                   166)'], [0.8333333333333334, 'rgb(59, 115, 143)'], [1.0,
                   'rgb(42, 86, 116)']],
    'name': 'f(x, y)',
    'opacity': 0.75,
    'showlegend': False,
    'x': array([0.        , 0.33333334, 0.6666666 , 1.        ], dtype=float32),
    'y': array([0.        , 0.33333334, 0.6666666 , 1.        ], dtype=float32),
    'z': array([[0.        , 0.11111112, 0.4444444 , 1.        ],
                [0.05555556, 0.16666669, 0.49999994, 1.0555556 ],
                [0.2222222 , 0.3333333 , 0.66666657, 1.2222222 ],
                [0.5       , 0.6111111 , 0.9444444 , 1.5       ]], dtype=float32)
})