sklearn-docs/MLP-Regularization
10
1import numpy as np2import plotly.graph_objects as go3 4from sklearn.preprocessing import StandardScaler5from sklearn.datasets import make_moons, make_circles, make_classification, make_blobs6from sklearn.neural_network import MLPClassifier7 8import gradio as gr9 10# =========================================================================11 12GRANULARITY = 0.213MARGIN = 0.514N_SAMPLES = 15015SEED = 116 17datasets = {}18X, y = make_moons(n_samples=N_SAMPLES, noise=0.2, random_state=SEED)19X = StandardScaler().fit_transform(X)20datasets["Moons"] = (X.copy(), y.copy())21 22X, y = make_circles(n_samples=N_SAMPLES, noise=0.2, factor=0.5, random_state=SEED)23X = StandardScaler().fit_transform(X)24datasets["Circles"] = (X.copy(), y.copy())25 26X, y = make_blobs(n_samples=N_SAMPLES, n_features=2, centers=4, cluster_std=2, random_state=SEED)27X = StandardScaler().fit_transform(X)28y[y==2] = 029y[y==3] = 130datasets["Blobs"] = (X.copy(), y.copy())31 32X, y = make_classification(n_samples=N_SAMPLES, n_features=2, n_redundant=0, n_informative=2, n_clusters_per_class=1, random_state=SEED)33X += 2 * np.random.uniform(size=X.shape)34X = StandardScaler().fit_transform(X)35datasets["Linear"] = (X.copy(), y.copy())36 37# =========================================================================38 39def get_figure_dict():40 figure_dict = dict(data=[], layout={}, frames=[])41 42 play_button = dict(args=[None, {"mode": "immediate", "fromcurrent": False, "frame": {"duration": 50}, "transition": {"duration": 50}}],43 label="Play",44 method="animate")45 46 pause_button = dict(args=[[None], {"mode": "immediate"}],47 label="Stop",48 method="animate")49 50 slider = dict(steps=[], active=0, currentvalue={"prefix": "Iteration: "})51 52 figure_dict["layout"] = dict(width=600, height=600, hovermode=False, margin=dict(l=40, r=40, t=40, b=40), 53 title=dict(text="Decision Surface", x=0.5),54 sliders=[slider],55 updatemenus=[dict(buttons=[play_button, pause_button], direction="left", pad={"t": 85}, type="buttons", x=0.6, y=-0.05)]56 )57 58 return figure_dict59 60def get_decision_surface(X, model):61 x_min, x_max = X[:, 0].min() - MARGIN, X[:, 0].max() + MARGIN62 y_min, y_max = X[:, 1].min() - MARGIN, X[:, 1].max() + MARGIN63 xrange = np.arange(x_min, x_max, GRANULARITY)64 yrange = np.arange(y_min, y_max, GRANULARITY)65 x, y = np.meshgrid(xrange, yrange)66 x = x.ravel(); y = y.ravel()67 z = model.predict_proba(np.column_stack([x, y]))[:, 1]68 return x, y, z69# =========================================================================70 71def create_plot(dataset, alpha, h1, h2, seed):72 X, y = datasets[dataset]73 74 model = MLPClassifier(alpha=alpha, max_iter=2000, learning_rate_init=0.01, hidden_layer_sizes=[h1, h2], random_state=seed)75 76 figure_dict = get_figure_dict()77 78 model.partial_fit(X, y, classes=[0, 1])79 xx, yy, zz = get_decision_surface(X, model)80 figure_dict["data"] = [go.Contour(x=xx, y=yy, z=zz, opacity=0.6, showscale=False,),81 go.Scatter(x=X[:, 0], y=X[:, 1], mode="markers", marker_color=y, marker={"colorscale": "jet", "size": 8})]82 83 prev_loss = np.inf84 tol = 3e-485 for i in range(100):86 for _ in range(3):87 model.partial_fit(X, y, classes=[0, 1])88 89 if prev_loss - model.loss_ <= tol: break90 prev_loss = model.loss_91 92 xx, yy, zz = get_decision_surface(X, model)93 figure_dict["frames"].append({"data": [go.Contour(x=xx, y=yy, z=zz, opacity=0.6, showscale=False)], "name": i})94 95 slider_step = {"args": [[i], {"mode": "immediate"}], "method": "animate", "label": i}96 figure_dict["layout"]["sliders"][0]["steps"].append(slider_step)97 98 fig = go.Figure(figure_dict)99 return fig100 101info = '''102# Effect of Regularization Parameter of Multilayer Perceptron103 104This example demonstrates the effect of varying the regularization parameter (alpha) of a multilayer perceptron on the binary classification of toy datasets, as represented by the decision surface of the classifier.105 106Higher values of alpha encourages smaller weights, thus making the model less prone to overfitting, while lower values may help against underfitting. Use the slider below to control the amount of regularization and observe how the decision surface changes with higher values.107 108The neural network is trained until the loss stops decreasing below a specific tolerance. The color of the decision surface represents the probability of observing the corresponding class.109 110Created by [@huabdul](https://huggingface.co/huabdul) based on [scikit-learn docs](https://scikit-learn.org/stable/auto_examples/neural_networks/plot_mlp_alpha.html).111'''112with gr.Blocks(analytics_enabled=False) as demo:113 with gr.Row():114 with gr.Column():115 gr.Markdown(info)116 dd_dataset = gr.Dropdown(list(datasets.keys()), value="Moons", label="Dataset", interactive=True)117 with gr.Row():118 with gr.Column(min_width=100):119 s_alpha = gr.Slider(0, 4, value=0.1, step=0.05, label="α (regularization parameter)")120 s_seed = gr.Slider(1, 1000, value=1, step=1, label="Seed")121 with gr.Column(min_width=100):122 s_h1 = gr.Slider(2, 20, value=10, step=1, label="Hidden layer 1 size")123 s_h2 = gr.Slider(2, 20, value=10, step=1, label="Hidden layer 2 size")124 submit = gr.Button("Submit")125 with gr.Column():126 plot = gr.Plot(show_label=False)127 128 submit.click(create_plot, inputs=[dd_dataset, s_alpha, s_h1, s_h2, s_seed], outputs=[plot])129 demo.load(create_plot, inputs=[dd_dataset, s_alpha, s_h1, s_h2, s_seed], outputs=[plot])130 131demo.launch()