Skip to content

Commit 7557086

Browse files
committed
minor edits to the linear regression notebook
1 parent 1bf0ae8 commit 7557086

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

examples/Linear_Regression_Starter.ipynb

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@
415415
"id": "PmSWcEVB86pr"
416416
},
417417
"source": [
418-
"Let me elaborate on a few adapter steps:\n",
418+
"Let us discuss a few adapter steps in detail:\n",
419419
"\n",
420420
"The `.broadcast(\"N\", to=\"x\")` transform will copy the value of `N` batch-size times to ensure that it will also have a `batch_size` dimension even though it was actually just a single value, constant over all datasets within a batch. The batch dimension will be inferred from `x` (this needs to be present during inference).\n",
421421
"\n",
@@ -469,7 +469,7 @@
469469
"source": [
470470
"Those shapes are as we expect them to be. The first dimenstion is always the batch size which was 500 for our example data. All variables adhere to this rule since the first dimension is indeed 500.\n",
471471
"\n",
472-
"For `summary_variables`, the second dimension is equal to `N`, which happend to be sampled as `14` for these example data. It's third dimension is `2`, since we have combined `x` and `y` into summary variables, each of which are vectors of length `N` within each simulated dataset.\n",
472+
"For `summary_variables`, the second dimension is equal to the sampled valued of `N`. It's third dimension is `2`, since we have combined `x` and `y` into summary variables, each of which are vectors of length `N` within each simulated dataset.\n",
473473
"\n",
474474
"For `inference_conditions`, the second dimension is just `1` because we have passed only the scalar variable `N` there.\n",
475475
"\n",
@@ -620,7 +620,7 @@
620620
"source": [
621621
"Let's check out the resulting inference. Say we want to obtain 1000 posterior samples from our approximated posterior of a simulated dataset where we know the ground truth values.\n",
622622
"\n",
623-
"You can also explore the automated diagnostics avilable through the methods:\n",
623+
"You can also explore the automated diagnostics available through the methods:\n",
624624
"\n",
625625
"- `compute_default_diagnostics()`\n",
626626
"- `plot_default_diagnostics()`"
@@ -793,7 +793,7 @@
793793
"cell_type": "markdown",
794794
"metadata": {},
795795
"source": [
796-
"Accuracy looks good for most datasets There is some more variation especially for $\\beta_2$ but this is not necessarily a reason for concern. Keep in mind that perfect accuracy is not the goal of bayesflow inference. Rather, the goal is to estimate the correct posterior as close as possible. And this correct posterior might very well be far away from the true value for some datasets. In fact, we would fully expect the true value to sometimes be at the tail of the posterior. \n",
796+
"Accuracy looks good for most datasets There is some more variation especially for $\\beta_1$ and $\\sigma$ but this is not necessarily a reason for concern. Keep in mind that perfect accuracy is not the goal of bayesflow inference. Rather, the goal is to estimate the correct posterior as close as possible. And this correct posterior might very well be far away from the true value for some datasets. In fact, we would fully expect the true value to sometimes be at the tail of the posterior. \n",
797797
"\n",
798798
"If this was not the case, than our posterior approximation may be too wide. Unfortunately, in many cases we don't have access to the correct posterior, so we need a method that provides us with an indication of the posterior approximations' accuracy without. This is where simulation-based calibration (SBC) comes into play. In short, if the true values are simulated from the prior used during inference (as is the case for our validatian data above), We would expect the rank of the true parameter value to be uniformly distributed from 1 to `num_samples`."
799799
]
@@ -845,7 +845,7 @@
845845
"cell_type": "markdown",
846846
"metadata": {},
847847
"source": [
848-
"The histograms look quite good overall, but could be a bit more uniform especially for $\\beta_0$. That said, the SBC histograms have some drawbacks on how the confidence bands are computed, so we recommend using another kind of plot that is based on the empirical cumulative distribution function (ECDF). For the ECDF, we can compute better confidence bands than for histograms, so the SBC ECDF plot is usually preferable. [This SBC interpretation guide by Martin Modrák](https://hyunjimoon.github.io/SBC/articles/rank_visualizations.html) gives further background information and also practical examples of how to interpret the SBC plots."
848+
"The histograms look quite good overall, but could be a bit more uniform. That said, the SBC histograms have some drawbacks on how the confidence bands are computed, so we recommend using another kind of plot that is based on the empirical cumulative distribution function (ECDF). For the ECDF, we can compute better confidence bands than for histograms, so the SBC ECDF plot is usually preferable. [This SBC interpretation guide by Martin Modrák](https://hyunjimoon.github.io/SBC/articles/rank_visualizations.html) gives further background information and also practical examples of how to interpret the SBC plots."
849849
]
850850
},
851851
{
@@ -885,7 +885,7 @@
885885
"id": "nv6ipPY6EyBv"
886886
},
887887
"source": [
888-
"The plot confirms that the approximate posteriors are well calibrated, except for the small issues in the posteriors of $\\beta_0$ that we had already seen in the histograms. Likely, for fully well calibrated inference, we would have to train the approximator a little longer, but that's okay. After all, we can effort a little more training time since afterwards, inference on any number of new (real or simulated) datasets is very fast due to amortization."
888+
"The plot confirms that the approximate posteriors are well calibrated, except for the small issues that we had already seen in the histograms. Likely, for fully well calibrated inference, we would have to train the approximator a little longer, but that's okay. After all, we can effort a little more training time since afterwards, inference on any number of new (real or simulated) datasets is very fast due to amortization."
889889
]
890890
},
891891
{
@@ -941,6 +941,13 @@
941941
"### Saving and Loading the Trained Networks"
942942
]
943943
},
944+
{
945+
"cell_type": "markdown",
946+
"metadata": {},
947+
"source": [
948+
"For saving and later on reloading bayesflow approximators, we provide some convenient functionalities. For saving, we use:"
949+
]
950+
},
944951
{
945952
"cell_type": "code",
946953
"execution_count": 32,
@@ -954,6 +961,13 @@
954961
"# approximator.save_weights(filepath=\"checkpoints/regression.h5\")"
955962
]
956963
},
964+
{
965+
"cell_type": "markdown",
966+
"metadata": {},
967+
"source": [
968+
"For loading, we then use:"
969+
]
970+
},
957971
{
958972
"cell_type": "code",
959973
"execution_count": 33,
@@ -973,6 +987,13 @@
973987
"approximator = keras.saving.load_model(\"checkpoints/regression.keras\")"
974988
]
975989
},
990+
{
991+
"cell_type": "markdown",
992+
"metadata": {},
993+
"source": [
994+
"All the usual methods continue to work on the loaded approximator. For example:"
995+
]
996+
},
976997
{
977998
"cell_type": "code",
978999
"execution_count": 34,

0 commit comments

Comments
 (0)