Skip to content

Update VI Bayesian NN notebook to best practices #221

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

Merged
merged 10 commits into from
Oct 24, 2021

Conversation

chiral-carbon
Copy link
Collaborator

Addresses issue #131 and aims to advance it to best practices

@review-notebook-app
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@review-notebook-app
Copy link

review-notebook-app bot commented Sep 1, 2021

View / edit / reply to this conversation on ReviewNB

chiral-carbon commented on 2021-09-01T15:02:46Z
----------------------------------------------------------------

I commented this out for now, but would we require it when we are already setting the seed in the beginning?


OriolAbril commented on 2021-09-08T15:56:09Z
----------------------------------------------------------------

rng = np.random.default_rng(RANDOM_SEED)

only sets the seed of rng. Any other code that doesn't use rng is completely unaffected.

This is one of the main reasons that the np.random.seed was changed and is now legacy. It set a global seed that affected any calls to np.random unless another random state was created manually so it had effects and "produced" bugs because people did not realize their code was being affected by the seed.

chiral-carbon commented on 2021-09-09T08:38:12Z
----------------------------------------------------------------

oh okay. in thats case I do get an error here,

ImportError: cannot import name 'MRG_RandomStreams' from 'pymc3.theanof'

I think it is because of compatibility issues between pymc3 >= 3.10 and theano?

OriolAbril commented on 2021-09-09T11:01:47Z
----------------------------------------------------------------

It looks like it could be fixed with a call to tt_rng before set_tt_rng, but I think we can just remove it and pass a random seed to the sampling methods.

Copy link
Member

rng = np.random.default_rng(RANDOM_SEED)

only sets the seed of rng. Any other code that doesn't use rng is completely unaffected.

This is one of the main reasons that the np.random.seed was changed and is now legacy. It set a global seed that affected any calls to np.random unless another random state was created manually so it had effects and "produced" bugs because people did not realize their code was being affected by the seed.


View entire conversation on ReviewNB

@review-notebook-app
Copy link

review-notebook-app bot commented Sep 8, 2021

View / edit / reply to this conversation on ReviewNB

OriolAbril commented on 2021-09-08T15:59:13Z
----------------------------------------------------------------

Line #3.    ax.scatter(X[Y == 1, 0], X[Y == 1, 1], color="C3", label="Class 1")

I personally prefer c0-c1 as it has more contrast, but both options are fine


@review-notebook-app
Copy link

review-notebook-app bot commented Sep 8, 2021

View / edit / reply to this conversation on ReviewNB

OriolAbril commented on 2021-09-08T15:59:14Z
----------------------------------------------------------------

Line #12.            "train_cols": np.arange(len(X[:][0])),

I would keep the X.shape[1] instead of this len


@review-notebook-app
Copy link

review-notebook-app bot commented Sep 8, 2021

View / edit / reply to this conversation on ReviewNB

OriolAbril commented on 2021-09-08T15:59:14Z
----------------------------------------------------------------

Line #42.            out = pm.Bernoulli(

this should also have the dimensions annotated


chiral-carbon commented on 2021-09-09T08:58:58Z
----------------------------------------------------------------

what dimension would this have though?

OriolAbril commented on 2021-09-09T11:10:17Z
----------------------------------------------------------------

coords = {
    "hidden_layer_1": np.arange(n_hidden),
    "hidden_layer_2": np.arange(n_hidden),
    "train_col": np.arange(X.shape[1]),
    "obs_id": np.arange(X.shape[0])
}

I'd do something like that. Here the number of nodes in each layer are independent, yet coincidentally the same so i'd not use bis. As we don't know much/anything about the observations or their collection process, I just use obs_id as fallback.

@review-notebook-app
Copy link

View / edit / reply to this conversation on ReviewNB

OriolAbril commented on 2021-09-08T15:59:15Z
----------------------------------------------------------------

format with less digits, 3-4 at the very most I'd say. 1-2 already perfectly fine


Copy link
Collaborator Author

oh okay. in thats case I do get an error here,

ImportError: cannot import name 'MRG_RandomStreams' from 'pymc3.theanof'

I think it is because of compatibility issues between pymc3 >= 3.10 and theano?


View entire conversation on ReviewNB

Copy link
Collaborator Author

what dimension would this have though?


View entire conversation on ReviewNB

Copy link
Member

It looks like it could be fixed with a call to tt_rng before set_tt_rng, but I think we can just remove it and pass a random seed to the sampling methods.


View entire conversation on ReviewNB

Copy link
Member

coords = {
    "hidden_layer_1": np.arange(n_hidden),
    "hidden_layer_2": np.arange(n_hidden),
    "train_col": np.arange(X.shape[1]),
    "obs_id": np.arange(X.shape[0])
}

I'd do something like that. Here the number of nodes in each layer are independent, yet coincidentally the same so i'd not use bis. As we don't know much/anything about the observations or their collection process, I just use obs_id as fallback.


View entire conversation on ReviewNB

@review-notebook-app
Copy link

review-notebook-app bot commented Sep 10, 2021

View / edit / reply to this conversation on ReviewNB

Sayam753 commented on 2021-09-10T20:44:03Z
----------------------------------------------------------------

I think the problem lies here -

coords = {
    "hidden_layer_1": np.arange(n_hidden),
    "hidden_layer_2": np.arange(n_hidden),
    "train_col": np.arange(X.shape[1]),
    "obs_id": np.arange(X.shape[0])
}

X has shape (1000, 2), but X_train has shape (500, 2). It is X_train that we use for training.

So, the right coords will be -

coords = {
    "hidden_layer_1": np.arange(n_hidden),
    "hidden_layer_2": np.arange(n_hidden),
    "train_col": np.arange(X_train.shape[1]),
    "obs_id": np.arange(X_train.shape[0])
}

I wonder what will happen if we pass X_test of shape not of shape (500, 2), but something else, like (400, 2) :thinking_face:


Sayam753 commented on 2021-09-10T20:56:31Z
----------------------------------------------------------------

Minor nitpick -

Looking at the warning, @OriolAbril do we promote plotting trace in PyMC3's model context?

OriolAbril commented on 2021-09-10T22:37:42Z
----------------------------------------------------------------

Right, because the original code uses X but should use X_train. There wasn't an error because the columns are the same. Now that we take cols and rows we do get an error.

No, plotting should not happen inside the model context. The warning is because we are giving ArviZ a non InferenceData input, so it first has to convert to idata. It's the conversion what should happen inside the model context, and it should be explicit. After that, plotting will work without warnings outside the model context.

Copy link
Member

Minor nitpick -

Looking at the warning, @OriolAbril do we promote plotting trace in PyMC3's model context?


View entire conversation on ReviewNB

Copy link
Member

Right, because the original code uses X but should use X_train. There wasn't an error because the columns are the same. Now that we take cols and rows we do get an error.

No, plotting should not happen inside the model context. The warning is because we are giving ArviZ a non InferenceData input, so it first has to convert to idata. It's the conversion what should happen inside the model context, and it should be explicit. After that, plotting will work without warnings outside the model context.


View entire conversation on ReviewNB

@review-notebook-app
Copy link

review-notebook-app bot commented Sep 19, 2021

View / edit / reply to this conversation on ReviewNB

Sayam753 commented on 2021-09-19T03:22:58Z
----------------------------------------------------------------

How do I interpret the output of this cell?


OriolAbril commented on 2021-10-13T05:04:40Z
----------------------------------------------------------------

this is a forgotten debug print that made it to the arviz release, rerunning with 0.11.4 will get rid of this message

Copy link
Member

this is a forgotten debug print that made it to the arviz release, rerunning with 0.11.4 will get rid of this message


View entire conversation on ReviewNB

Copy link
Member

@Sayam753 Sayam753 left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Member

@OriolAbril OriolAbril left a comment

Choose a reason for hiding this comment

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

I'd also suggest rerunning with latest arviz to get rid of those debug prints. But at the very least it needs to include the post directive. It should not be merged without it

@chiral-carbon
Copy link
Collaborator Author

@OriolAbril I have fixed all the broken links, I think this could be merged now

@OriolAbril
Copy link
Member

There is still a broken ADVI link in the summary section.

@chiral-carbon
Copy link
Collaborator Author

In the next steps section, should I add a line where Theano is mentioned about it being continued as Aesara?

@OriolAbril OriolAbril merged commit ff2af9f into pymc-devs:main Oct 24, 2021
@OriolAbril
Copy link
Member

we can directly update theano to aesara when running with v4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants