Spaces:
Sleeping
Sleeping
Jayabalambika
commited on
Commit
•
19f8b3a
1
Parent(s):
39579a9
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
def plot_penalties():
|
2 |
+
|
3 |
+
|
4 |
+
|
5 |
+
|
6 |
+
# Plot the results
|
7 |
+
plt.clf()
|
8 |
+
|
9 |
+
|
10 |
+
l1_color = 'r'
|
11 |
+
l2_color = 'g'
|
12 |
+
elastic_net_color = 'b'
|
13 |
+
|
14 |
+
line = np.linspace(-1.5, 1.5, 1001)
|
15 |
+
xx, yy = np.meshgrid(line, line)
|
16 |
+
|
17 |
+
l2 = xx**2 + yy**2
|
18 |
+
l1 = np.abs(xx) + np.abs(yy)
|
19 |
+
rho = 0.5
|
20 |
+
elastic_net = rho * l1 + (1 - rho) * l2
|
21 |
+
fig = plt.figure(figsize=(10, 10), dpi=100)
|
22 |
+
|
23 |
+
ax = plt.gca()
|
24 |
+
|
25 |
+
elastic_net_contour = plt.contour(
|
26 |
+
xx, yy, elastic_net, levels=[1], colors=elastic_net_color
|
27 |
+
)
|
28 |
+
l2_contour = plt.contour(xx, yy, l2, levels=[1], colors=l2_color)
|
29 |
+
l1_contour = plt.contour(xx, yy, l1, levels=[1], colors=l1_color)
|
30 |
+
ax.set_aspect("equal")
|
31 |
+
ax.spines["left"].set_position("center")
|
32 |
+
ax.spines["right"].set_color("none")
|
33 |
+
ax.spines["bottom"].set_position("center")
|
34 |
+
ax.spines["top"].set_color("none")
|
35 |
+
|
36 |
+
plt.clabel(
|
37 |
+
elastic_net_contour,
|
38 |
+
inline=1,
|
39 |
+
fontsize=18,
|
40 |
+
fmt={1.0: "elastic-net"},
|
41 |
+
manual=[(-1, -1)],)
|
42 |
+
plt.clabel(l2_contour, inline=1, fontsize=18, fmt={1.0: "L2"}, manual=[(-1, -1)])
|
43 |
+
plt.clabel(l1_contour, inline=1, fontsize=18, fmt={1.0: "L1"}, manual=[(-1, -1)])
|
44 |
+
|
45 |
+
plt.tight_layout()
|
46 |
+
# plt.show()
|
47 |
+
return fig
|
48 |
+
|
49 |
+
|
50 |
+
|
51 |
+
|
52 |
+
title = "SGD Penalties"
|
53 |
+
|
54 |
+
|
55 |
+
with gr.Blocks(title=title) as demo:
|
56 |
+
gr.Markdown(f"# {title}")
|
57 |
+
|
58 |
+
|
59 |
+
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)**")
|
60 |
+
|
61 |
+
# greet_btn.click(fn=greet, inputs=name, outputs=output)
|
62 |
+
x = gr.Dropdown(["red", "blue", "green"], label="l1_color", type="index"),
|
63 |
+
y = gr.Dropdown(["red", "blue", "green"], label="l2_color", type="index"),
|
64 |
+
z = gr.Dropdown(["red", "blue", "green"], label="elastic_net_color", type="index"),
|
65 |
+
|
66 |
+
btn = gr.Button(value="Visualize SGD penalties")
|
67 |
+
btn.click(plot_penalties, outputs= gr.Plot() ) #
|
68 |
+
|
69 |
+
|
70 |
+
|
71 |
+
demo.launch()
|