Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Minibatch alignment in Bayesian Neural Network example + Pre-commit hooks #719

Merged
merged 2 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
},
Copy link
Member

@fonnesbeck fonnesbeck Nov 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line #21.            ann_input = pm.Data("ann_input", minibatch_x, mutable=True, dims=("obs_id", "train_cols"))

mutability is True by default


Reply via ReviewNB

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing this would also change a bunch of stuff in the diff -- we'd have to re-execute the notebook, otherwise "Check cells were executed sequentially" would fail during pre-commit. Since this PR is just running the pre-commit hook for someone else's (seemingly-abandoned) PR, I'd prefer to just leave it if it's fine.

"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
Loading