Skip to content

Commit

Permalink
Fix Minibatch alignment in Bayesian Neural Network example + Pre-comm…
Browse files Browse the repository at this point in the history
…it hooks (pymc-devs#719)

* Fix Minibatch alignment in Bayesian Neural Network example

* Run: pre-commit run all-files

---------

Co-authored-by: Deepak CH <[email protected]>
  • Loading branch information
2 people authored and fonnesbeck committed Dec 20, 2024
1 parent 5b02ca4 commit 25aafe4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
16 changes: 11 additions & 5 deletions examples/variational_inference/bayesian_neural_network_advi.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@
},
"outputs": [],
"source": [
"def construct_nn(ann_input, ann_output):\n",
"def construct_nn():\n",
" n_hidden = 5\n",
"\n",
" # Initialize random weights between each layer\n",
Expand All @@ -204,9 +204,14 @@
" \"train_cols\": np.arange(X_train.shape[1]),\n",
" \"obs_id\": np.arange(X_train.shape[0]),\n",
" }\n",
"\n",
" with pm.Model(coords=coords) as neural_network:\n",
" ann_input = pm.Data(\"ann_input\", X_train, dims=(\"obs_id\", \"train_cols\"))\n",
" ann_output = pm.Data(\"ann_output\", Y_train, dims=\"obs_id\")\n",
" # Define minibatch variables\n",
" minibatch_x, minibatch_y = pm.Minibatch(X_train, Y_train, batch_size=50)\n",
"\n",
" # Define data variables using minibatches\n",
" ann_input = pm.Data(\"ann_input\", minibatch_x, mutable=True, dims=(\"obs_id\", \"train_cols\"))\n",
" ann_output = pm.Data(\"ann_output\", minibatch_y, mutable=True, dims=\"obs_id\")\n",
"\n",
" # Weights from input to hidden layer\n",
" weights_in_1 = pm.Normal(\n",
Expand All @@ -231,13 +236,14 @@
" \"out\",\n",
" act_out,\n",
" observed=ann_output,\n",
" total_size=Y_train.shape[0], # IMPORTANT for minibatches\n",
" total_size=X_train.shape[0], # IMPORTANT for minibatches\n",
" dims=\"obs_id\",\n",
" )\n",
" return neural_network\n",
"\n",
"\n",
"neural_network = construct_nn(X_train, Y_train)"
"# Create the neural network model\n",
"neural_network = construct_nn()"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ A neural network is quite simple. The basic unit is a [perceptron](https://en.wi
jupyter:
outputs_hidden: true
---
def construct_nn(ann_input, ann_output):
def construct_nn():
n_hidden = 5
# Initialize random weights between each layer
Expand All @@ -128,9 +128,14 @@ def construct_nn(ann_input, ann_output):
"train_cols": np.arange(X_train.shape[1]),
"obs_id": np.arange(X_train.shape[0]),
}
with pm.Model(coords=coords) as neural_network:
ann_input = pm.Data("ann_input", X_train, dims=("obs_id", "train_cols"))
ann_output = pm.Data("ann_output", Y_train, dims="obs_id")
# Define minibatch variables
minibatch_x, minibatch_y = pm.Minibatch(X_train, Y_train, batch_size=50)
# Define data variables using minibatches
ann_input = pm.Data("ann_input", minibatch_x, mutable=True, dims=("obs_id", "train_cols"))
ann_output = pm.Data("ann_output", minibatch_y, mutable=True, dims="obs_id")
# Weights from input to hidden layer
weights_in_1 = pm.Normal(
Expand All @@ -155,13 +160,14 @@ def construct_nn(ann_input, ann_output):
"out",
act_out,
observed=ann_output,
total_size=Y_train.shape[0], # IMPORTANT for minibatches
total_size=X_train.shape[0], # IMPORTANT for minibatches
dims="obs_id",
)
return neural_network
neural_network = construct_nn(X_train, Y_train)
# Create the neural network model
neural_network = construct_nn()
```

That's not so bad. The `Normal` priors help regularize the weights. Usually we would add a constant `b` to the inputs but I omitted it here to keep the code cleaner.
Expand Down

0 comments on commit 25aafe4

Please sign in to comment.