File size: 2,162 Bytes
f08f8ee
 
 
 
 
19f8b3a
 
 
 
 
 
 
 
 
a24c6bc
 
 
19f8b3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84a759a
 
 
 
 
 
 
19f8b3a
 
 
a24c6bc
19f8b3a
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import gradio as gr
import numpy as np
import matplotlib.pyplot as plt


def plot_penalties():




    # Plot the results
    plt.clf()


    l1_color = 'r'  # hard coded as color picker not working
    l2_color = 'g'    # hard coded as color picker not working
    elastic_net_color = 'b'  # hard coded as color picker not working

    line = np.linspace(-1.5, 1.5, 1001)
    xx, yy = np.meshgrid(line, line)

    l2 = xx**2 + yy**2
    l1 = np.abs(xx) + np.abs(yy)
    rho = 0.5
    elastic_net = rho * l1 + (1 - rho) * l2
    fig = plt.figure(figsize=(10, 10), dpi=100)

    ax = plt.gca()

    elastic_net_contour = plt.contour(
    xx, yy, elastic_net, levels=[1], colors=elastic_net_color
    )
    l2_contour = plt.contour(xx, yy, l2, levels=[1], colors=l2_color)
    l1_contour = plt.contour(xx, yy, l1, levels=[1], colors=l1_color)
    ax.set_aspect("equal")
    ax.spines["left"].set_position("center")
    ax.spines["right"].set_color("none")
    ax.spines["bottom"].set_position("center")
    ax.spines["top"].set_color("none")

    plt.clabel(
    elastic_net_contour,
    inline=1,
    fontsize=18,
    fmt={1.0: "elastic-net"},
    manual=[(-1, -1)],)
    plt.clabel(l2_contour, inline=1, fontsize=18, fmt={1.0: "L2"}, manual=[(-1, -1)])
    plt.clabel(l1_contour, inline=1, fontsize=18, fmt={1.0: "L1"}, manual=[(-1, -1)])

    plt.tight_layout()
    # plt.show()
    return fig




title = "SGD Penalties"


with gr.Blocks(title=title) as demo:
    gr.Markdown(f"# {title}")
    gr.Markdown(
    """
    ### The plot shows the contours of L1, L2 and Elastic Net regularizers.
    ### The value of penalties is equal to 1 in all of them.
    ### L2 regularizer is used for linear SVM models, L1 and elastic net brings sparsity in the models
    ### SGDClassifier and SGDRegressor support all of the above.  
    """)

    gr.Markdown(" **[Demo is based on sklearn docs](https://scikit-learn.org/stable/auto_examples/linear_model/plot_sgd_penalties.html#sphx-glr-auto-examples-linear-model-plot-sgd-penalties-py)**")



    btn = gr.Button(value="Visualize SGD penalties")
    btn.click(plot_penalties, outputs= gr.Plot() ) # 

    

demo.launch()