-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Remove Theano from Week 5 #348
Conversation
Check out this pull request on You'll be able to see Jupyter notebook diff and discuss changes. Powered by ReviewNB. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Cleanup notebook metadata
- Prettify Python code
- Remove redundant outputs
- Fix Colab initialization
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a superficial review. I haven't launched the code or even opened the notebook, I've only looked at the diff. The whole thing looks close to finished. I'll test whether it actually works later.
""" | ||
A single-file module that makes your lasagne network into a bayesian neural net. | ||
Originally created by github.com/ferrine , rewritten by github.com/justheuristic for simplicity | ||
import torch |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this file is converted from someone's module, I believe credit should be given to the original author.
self.bias.data.zero_() | ||
|
||
def forward(self, x): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
redundant new line
# Local reparameterization trick | ||
out = mean + std * epsilon | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
redundant new line
@@ -2,7 +2,7 @@ | |||
"cells": [ | |||
{ | |||
"cell_type": "code", | |||
"execution_count": null, | |||
"execution_count": 1, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please clean up these. Use my script or Jupyter's native feature (cells -> execution counts -> clean all
I believe)
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def calc_kl(model):\n", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is already implemented in ModuleWrapper.forward
, no?
" for module in model.modules():\n", | ||
" if hasattr(module, 'kl_loss'):\n", | ||
" kl = kl + module.kl_loss()\n", | ||
" return kl/10000" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/10000
?
" updates=updates,\n", | ||
" allow_input_downcast=True)\n", | ||
" self.n_actions = n_actions\n", | ||
" self.model = < Your network here> # Use BBBLinear instead of Linear layers\n", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
<YOUR CODE: network. Use BBLinear instead of linear layers>
@@ -1423,9 +1365,22 @@ | |||
} | |||
], | |||
"metadata": { | |||
"kernelspec": { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please clean up this
"\n", | ||
" def predict_sample_rewards(self, states):\n", | ||
" return self.model(torch.Tensor(states))\n", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tensor
is lowercase nowadays
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left some comments on codestyle. Will review how it works later.
You should fix environment setup in notebook since it does not require theano installation now.
week05_explore/bayes.py
Outdated
|
||
__all__ = ['NormalApproximation', 'get_var_cost', 'bbpwrap'] | ||
class GaussianVariational(nn.Module): | ||
#Samples weights for variational inference as in Weights Uncertainity on Neural Networks (Bayes by backprop paper) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs space after #
Add link to referred paper.
week05_explore/bayes.py
Outdated
|
||
|
||
class ScaleMixturePrior(nn.Module): | ||
#Calculates a Scale Mixture Prior distribution for the prior part of the complexity cost on Bayes by Backprop paper |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use ''' doc strings. Add link to paper.
week05_explore/bayes.py
Outdated
log q(weights|learned mu and rho) aka log q(theta|x) | ||
Calculates the log_likelihood for each of the weights sampled relative to a prior distribution as a part of the complexity cost | ||
returns: | ||
torch.tensor with shape [] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what shape?
week05_explore/bayes.py
Outdated
# if layer is bayesian or pretends so | ||
cost += layer.get_var_cost() | ||
return cost | ||
Bayesian Linear layer, implements the linear layer proposed on Weight Uncertainity on Neural Networks |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
link to paper
week05_explore/bayes.py
Outdated
freeze = False): | ||
super().__init__() | ||
|
||
#our main parameters |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spaces after #. Comment is redundatn.
week05_explore/bayes.py
Outdated
self.bias = bias | ||
self.freeze = freeze | ||
|
||
#parameters for the scale mixture prior |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Space
week05_explore/bayes.py
Outdated
def forward(self, x): | ||
# Sample the weights and forward it | ||
|
||
#if the model is frozen, return frozen |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comment repeats code. Remove it
week05_explore/bayes.py
Outdated
# Sample the weights and forward it | ||
|
||
#if the model is frozen, return frozen | ||
if self.freeze: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is boolean variable. Name it is_frozen.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Supply doc string to your layer implementation. It will help students to understand it.
- In the notebook
sample_prediction
method does not work without .detach().numpy() - Is it possible to make 2-layer network no? If not, than "your code here" section in
__init__
of BNNAgent is redundant. Can you fill it with model or provide possibility of multilayer agent? - Why Bayesian layer is called BBBLayer?
super().__init__() | ||
|
||
#our main parameters | ||
def __init__(self, in_features, out_features, alpha_shape=(1, 1), bias=True, name='BBBLinear'): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you write a detailed docstrig here?
@@ -2,7 +2,7 @@ | |||
"cells": [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove installation of Theano/Lasagne from the Colab initialization cell.
Fixes #174. Part of #258.