Skip to content

Commit

Permalink
Refactor examples
Browse files Browse the repository at this point in the history
  • Loading branch information
kulbachcedric committed Jun 16, 2024
1 parent 2cd9194 commit 14e9c6a
Show file tree
Hide file tree
Showing 9 changed files with 3,291 additions and 203 deletions.
219 changes: 188 additions & 31 deletions docs/examples/anomaly/example_autoencoder.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,161 @@
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-06-16T08:14:05.055354Z",
"iopub.status.busy": "2024-06-16T08:14:05.054747Z",
"iopub.status.idle": "2024-06-16T08:14:09.531336Z",
"shell.execute_reply": "2024-06-16T08:14:09.530358Z"
}
},
"outputs": [],
"source": [
"from river import compose, preprocessing, metrics, datasets\n",
"\n",
"from deep_river.anomaly import Autoencoder\n",
"from torch import nn, manual_seed"
],
"metadata": {
"collapsed": false
}
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-06-16T08:14:09.535105Z",
"iopub.status.busy": "2024-06-16T08:14:09.534748Z",
"iopub.status.idle": "2024-06-16T08:14:09.550878Z",
"shell.execute_reply": "2024-06-16T08:14:09.550249Z"
}
},
"outputs": [
{
"data": {
"text/plain": "Pipeline (\n MinMaxScaler (),\n Autoencoder (\n module=None\n loss_fn=\"mse_loss\"\n optimizer_fn=<class 'torch.optim.sgd.SGD'>\n lr=0.005\n device=\"cpu\"\n seed=42\n )\n)",
"text/html": "<div><div class=\"river-component river-pipeline\"><details class=\"river-component river-estimator\"><summary class=\"river-summary\"><pre class=\"river-estimator-name\">MinMaxScaler</pre></summary><code class=\"river-estimator-params\">()\n\n</code></details><details class=\"river-component river-estimator\"><summary class=\"river-summary\"><pre class=\"river-estimator-name\">Autoencoder</pre></summary><code class=\"river-estimator-params\">(\n module=None\n loss_fn=\"mse_loss\"\n optimizer_fn=&lt;class 'torch.optim.sgd.SGD'&gt;\n lr=0.005\n device=\"cpu\"\n seed=42\n)\n\n</code></details></div><style scoped>\n.river-estimator {\n padding: 1em;\n border-style: solid;\n background: white;\n}\n\n.river-pipeline {\n display: flex;\n flex-direction: column;\n align-items: center;\n background: linear-gradient(#000, #000) no-repeat center / 3px 100%;\n}\n\n.river-union {\n display: flex;\n flex-direction: row;\n align-items: center;\n justify-content: center;\n padding: 1em;\n border-style: solid;\n background: white;\n}\n\n.river-wrapper {\n display: flex;\n flex-direction: column;\n align-items: center;\n justify-content: center;\n padding: 1em;\n border-style: solid;\n background: white;\n}\n\n.river-wrapper > .river-estimator {\n margin-top: 1em;\n}\n\n/* Vertical spacing between steps */\n\n.river-component + .river-component {\n margin-top: 2em;\n}\n\n.river-union > .river-estimator {\n margin-top: 0;\n}\n\n.river-union > .pipeline {\n margin-top: 0;\n}\n\n/* Spacing within a union of estimators */\n\n.river-union > .river-component + .river-component {\n margin-left: 1em;\n}\n\n/* Typography */\n\n.river-estimator-params {\n display: block;\n white-space: pre-wrap;\n font-size: 120%;\n margin-bottom: -1em;\n}\n\n.river-estimator > .river-estimator-params,\n.river-wrapper > .river-details > river-estimator-params {\n background-color: white !important;\n}\n\n.river-estimator-name {\n display: inline;\n margin: 0;\n font-size: 130%;\n}\n\n/* Toggle */\n\n.river-summary {\n display: flex;\n align-items:center;\n cursor: pointer;\n}\n\n.river-summary > div {\n width: 100%;\n}\n</style></div>"
"text/html": [
"<div><div class=\"river-component river-pipeline\"><details class=\"river-component river-estimator\"><summary class=\"river-summary\"><pre class=\"river-estimator-name\">MinMaxScaler</pre></summary><code class=\"river-estimator-params\">MinMaxScaler ()\n",
"</code></details><details class=\"river-component river-estimator\"><summary class=\"river-summary\"><pre class=\"river-estimator-name\">Autoencoder</pre></summary><code class=\"river-estimator-params\">Autoencoder (\n",
" module=None\n",
" loss_fn=\"mse_loss\"\n",
" optimizer_fn=&lt;class 'torch.optim.sgd.SGD'&gt;\n",
" lr=0.005\n",
" device=\"cpu\"\n",
" seed=42\n",
")\n",
"</code></details></div><style scoped>\n",
".river-estimator {\n",
" padding: 1em;\n",
" border-style: solid;\n",
" background: white;\n",
" max-width: max-content;\n",
"}\n",
"\n",
".river-pipeline {\n",
" display: flex;\n",
" flex-direction: column;\n",
" align-items: center;\n",
" background: linear-gradient(#000, #000) no-repeat center / 1.5px 100%;\n",
"}\n",
"\n",
".river-union {\n",
" display: flex;\n",
" flex-direction: row;\n",
" align-items: center;\n",
" justify-content: center;\n",
" padding: 1em;\n",
" border-style: solid;\n",
" background: white;\n",
"}\n",
"\n",
".river-wrapper {\n",
" display: flex;\n",
" flex-direction: column;\n",
" align-items: center;\n",
" justify-content: center;\n",
" padding: 1em;\n",
" border-style: solid;\n",
" background: white;\n",
"}\n",
"\n",
".river-wrapper > .river-estimator {\n",
" margin-top: 1em;\n",
"}\n",
"\n",
"/* Vertical spacing between steps */\n",
"\n",
".river-component + .river-component {\n",
" margin-top: 2em;\n",
"}\n",
"\n",
".river-union > .river-estimator {\n",
" margin-top: 0;\n",
"}\n",
"\n",
".river-union > .river-component {\n",
" margin-top: 0;\n",
"}\n",
"\n",
".river-union > .pipeline {\n",
" margin-top: 0;\n",
"}\n",
"\n",
"/* Spacing within a union of estimators */\n",
"\n",
".river-union > .river-component + .river-component {\n",
" margin-left: 1em;\n",
"}\n",
"\n",
"/* Typography */\n",
"\n",
".river-estimator-params {\n",
" display: block;\n",
" white-space: pre-wrap;\n",
" font-size: 110%;\n",
" margin-top: 1em;\n",
"}\n",
"\n",
".river-estimator > .river-estimator-params,\n",
".river-wrapper > .river-details > river-estimator-params {\n",
" background-color: white !important;\n",
"}\n",
"\n",
".river-wrapper > .river-details {\n",
" margin-bottom: 1em;\n",
"}\n",
"\n",
".river-estimator-name {\n",
" display: inline;\n",
" margin: 0;\n",
" font-size: 110%;\n",
"}\n",
"\n",
"/* Toggle */\n",
"\n",
".river-summary {\n",
" display: flex;\n",
" align-items:center;\n",
" cursor: pointer;\n",
"}\n",
"\n",
".river-summary > div {\n",
" width: 100%;\n",
"}\n",
"</style></div>"
],
"text/plain": [
"Pipeline (\n",
" MinMaxScaler (),\n",
" Autoencoder (\n",
" module=None\n",
" loss_fn=\"mse_loss\"\n",
" optimizer_fn=<class 'torch.optim.sgd.SGD'>\n",
" lr=0.005\n",
" device=\"cpu\"\n",
" seed=42\n",
" )\n",
")"
]
},
"execution_count": 2,
"metadata": {},
Expand Down Expand Up @@ -61,20 +197,26 @@
" Autoencoder(module=MyAutoEncoder, lr=0.005)\n",
")\n",
"model_pipeline"
],
"metadata": {
"collapsed": false
}
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-06-16T08:14:09.588291Z",
"iopub.status.busy": "2024-06-16T08:14:09.588025Z",
"iopub.status.idle": "2024-06-16T08:14:13.445007Z",
"shell.execute_reply": "2024-06-16T08:14:13.444562Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ROCAUC: 0.7447\n"
"ROCAUC: 0.9017\n"
]
}
],
Expand All @@ -84,20 +226,26 @@
" metric.update(y_true=y, y_pred=score)\n",
" model_pipeline.learn_one(x=x)\n",
"print(f\"ROCAUC: {metric.get():.4f}\")"
],
"metadata": {
"collapsed": false
}
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-06-16T08:14:13.447545Z",
"iopub.status.busy": "2024-06-16T08:14:13.447295Z",
"iopub.status.idle": "2024-06-16T08:14:13.450916Z",
"shell.execute_reply": "2024-06-16T08:14:13.450376Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ROCAUC: 0.7447\n"
"ROCAUC: 0.9017\n"
]
}
],
Expand All @@ -107,20 +255,26 @@
" metric.update(y_true=y, y_pred=score)\n",
" model_pipeline.learn_one(x=x)\n",
"print(f\"ROCAUC: {metric.get():.4f}\")"
],
"metadata": {
"collapsed": false
}
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-06-16T08:14:13.454438Z",
"iopub.status.busy": "2024-06-16T08:14:13.454179Z",
"iopub.status.idle": "2024-06-16T08:14:13.458105Z",
"shell.execute_reply": "2024-06-16T08:14:13.457591Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ROCAUC: 0.7447\n"
"ROCAUC: 0.9017\n"
]
}
],
Expand All @@ -130,20 +284,26 @@
" metric.update(y_true=y, y_pred=score)\n",
" model_pipeline.learn_one(x=x)\n",
"print(f\"ROCAUC: {metric.get():.4f}\")"
],
"metadata": {
"collapsed": false
}
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-06-16T08:14:13.460717Z",
"iopub.status.busy": "2024-06-16T08:14:13.460519Z",
"iopub.status.idle": "2024-06-16T08:14:13.464046Z",
"shell.execute_reply": "2024-06-16T08:14:13.463614Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ROCAUC: 0.7447\n"
"ROCAUC: 0.9017\n"
]
}
],
Expand All @@ -153,10 +313,7 @@
" metric.update(y_true=y, y_pred=score)\n",
" model_pipeline.learn_one(x=x)\n",
"print(f\"ROCAUC: {metric.get():.4f}\")"
],
"metadata": {
"collapsed": false
}
]
}
],
"metadata": {
Expand All @@ -175,7 +332,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.4"
"version": "3.11.9"
},
"vscode": {
"interpreter": {
Expand Down
Loading

0 comments on commit 14e9c6a

Please sign in to comment.