gugarosa / learnergy Goto Github PK
View Code? Open in Web Editor NEW๐ก Learnergy is a Python library for energy-based machine learning models.
Home Page: https://learnergy.readthedocs.io
License: Apache License 2.0
๐ก Learnergy is a Python library for energy-based machine learning models.
Home Page: https://learnergy.readthedocs.io
License: Apache License 2.0
[x ] Check that you are up-to-date with the master branch of Learnergy. You can update with:
pip install git+git://github.com/gugarosa/learnergy.git --upgrade --no-deps
[x ] Check that you have read all of our README.
I look and could not find existing methods for sampling visible units from a trained Gaussian RBM model. The procedure should be starting with a random input dataset to feed into the visible layer, do hidden and visible sampling back and forth until convergence, then take the final visible unit values as the synthetic data.
Since I could not find such a method, I wrote my own and tested on a particular class of the MNIST dataset. The results are quite awful. you could hardly make out a 4, but very distorted and noisy. The training process does appear to have reached convergence after about 100 epochs. and I tried a number of values for iterations in the synthetic sampling run.
My questions:
Hello,
Thank you very much for developing this superb library!!
I believe that I found an issue, but I am not able to find the root cause.
From time to time I receive this error:
Traceback (most recent call last):
File "/Users/supermario/LNIssue/run_rbm.py", line 26, in
epochs=1000000)
File "/Users/supermario/LNIssue/venv/lib/python3.7/site-packages/learnergy/models/bernoulli/rbm.py", line 495, in fit
_, _, _, _, visible_states = self.gibbs_sampling(samples)
File "/Users/supermario/LNIssue/venv/lib/python3.7/site-packages/learnergy/models/bernoulli/rbm.py", line 379, in gibbs_sampling
neg_hidden_states, True)
File "/Users/supermario/LNIssue/venv/lib/python3.7/site-packages/learnergy/models/bernoulli/rbm.py", line 352, in visible_sampling
states = torch.bernoulli(probs)
RuntimeError: Expected p_in >= 0 && p_in <= 1 to be true, but got false. (Could this error message be improved? If so, please report an enhancement request to PyTorch.)
Here is the code to reproduce the error:
import numpy as np
import torch
from torch.utils.data import Dataset
from learnergy.models.bernoulli.rbm import RBM
np.random.seed(16)
torch.manual_seed(16)
class CustomDataset(Dataset):
def __init__(self, custom_data):
self.custom_data = torch.from_numpy(custom_data)
self.dummy_y = torch.from_numpy(np.array([0.0]))
def __getitem__(self, index):
return self.custom_data[index], self.dummy_y[0]
def __len__(self):
return self.custom_data.shape[0]
rbm = RBM(n_visible=32, n_hidden=64, steps=1, learning_rate=0.0001, momentum=0.9, decay=0.00001, temperature=0, use_gpu=False)
data = np.random.choice(a=[False, True], size=(5755, 32)).astype(np.float32)
data_train = CustomDataset(data)
rbm.fit(dataset=data_train, batch_size=20, epochs=1000000)
And requirements:
astroid==2.8.4
attrs==21.2.0
coverage==6.1.1
cycler==0.11.0
imageio==2.10.3
importlib-metadata==4.8.2
iniconfig==1.1.1
isort==5.10.1
kiwisolver==1.3.2
lazy-object-proxy==1.6.0
learnergy==1.1.1
matplotlib==3.4.3
mccabe==0.6.1
networkx==2.6.3
numpy==1.21.4
packaging==21.2
Pillow==8.4.0
platformdirs==2.4.0
pluggy==1.0.0
py==1.11.0
pylint==2.11.1
pyparsing==2.4.7
pytest==6.2.5
python-dateutil==2.8.2
PyWavelets==1.2.0
scikit-image==0.18.3
scipy==1.7.2
six==1.16.0
tifffile==2021.11.2
toml==0.10.2
torch==1.9.0
tqdm==4.62.3
typed-ast==1.4.3
typing-extensions==3.10.0.2
wrapt==1.13.3
zipp==3.6.0
The error should appear at epoch 221/1000000.
If I add the following torch.nan_to_num in visible_sampling() method, then the error is not present anymore.
probs = torch.nan_to_num(probs, nan=0.0)
Sampling current states
states = torch.bernoulli(probs)
macosx, python 3.7.0
Thank you! Please let me know if you need additional info.
Regarding the basic Bernoulli RBM for classification, could I check what is the basis of your implementation?
From what I can tell, the training is broken up in two sections:
So, it seems like the RBM is just used as an initializer for the neural network.
In addition, I noticed that RBM::fit's epoch defaults to 1. Increasing the epoch number does not seem to improve performance. Does this mean that there is no use in training the RBM?
Lastly, was there any prior work that implemented RBM in this way, or is this your original idea? Are there any published results or study into this architecture? Thank you!
Please, make sure that the following boxes are checked before submitting a new issue. There is a small chance that you can solve it by your own or even that it was already addressed by someone.
Thank you!
[ x] Check that you are up-to-date with the master branch of Learnergy. You can update with:
pip install git+git://github.com/gugarosa/learnergy.git --upgrade --no-deps
[ x] Check that you have read all of our README.
[Description of the issue]
I ran through the example code in gaussian_rbm_training.py.
after 5 epochs, the mse is at 253.189896
then I followed up with reconstruction, and the mse jumps to 253349.031250, which is of course useless.
I can increase the epoch counts, but for now, I just want to make sure I am repeating what you see on your side.
Expected behavior: [It should be what you expect to happen]
mse value a lot closer to the training loop values
Actual behavior: [What actually happens]
mse is orders of magnitude worse on reconstruction
Reproduces how often: [How much does it occur? Show us your percentage]
Any additional information, configuration or data that might be necessary to reproduce the issue.
I found in the comments of file learnergy/learnergy/models/bernoulli/rbm.py (line 401), you said
Creating a Softplus function for numerical stability
The form of Softplus function is
f(x) = log(1+exp(x)),
which fits the form of the log term of marginal energy of RBM
E(v)= \sum_i a_i v_i + \sum_j ( log( 1 + exp( \sum_i v_i \lambda_{ij} +b_j ) ) ).
So I suppose the Softplus function is not used to ensure the numerical stability, but to calculate the marginal energy. I am a beginner to RBM, if I have any wrong perceptions, please point it out.
Thank you!
Hi Gustavo!
I hope you are doing well!
I was wondering if you had looked into / planned to add probabilistic max-pooling, as descibed in:
H. Lee, et al.,
Convolutional Deep Belief Networksfor Scalable Unsupervised Learning of Hierarchical Representations
Proceedings of the 26th annual international conference on machine learning (2009).
I am happy to help, but figured it would be best to consult you first.
Thanks!
Nick
The variable names used in the getters and setters of each model class are different than what is initialized in the constructor.
Note that the name 'W' is used in init, but '_W' is used as the variable name in the getters and setters, both of which have the same name as the variable initialized in init 'W'. This issue is the same for all member variables. I have tested the DBN and GaussianVarianceRBM subclasses as well, and it looks like the issue persists across all other subclasses.
This causes issues when trying to make copies of classes for things like multi-GPU training via torch.nn.DistributedDataParallel
[ x] Check that you are up-to-date with the master branch of Learnergy. You can update with:
pip install git+git://github.com/gugarosa/learnergy.git --upgrade --no-deps
[ x] Check that you have read all of our README.
I see the convergence.plot sample code, where you input a static dictionary of the values to be plotted. They are mse, pl and times.
In the gaussian_rbm.fit() method, you did a self.dump() for each epoch, but that only updates an internal structure. And then there is the logging. The fit() call would only return the cumulative mse and pl at the end of all epoch runs. So if you would like to see the convergence rate from epoch to epoch, I can't figure out how you intend to feed the training parameters to the plot routine.
please explain or give an example how to create such a hook.
When I use GaussianRBM to run rbm_classification.py on GPU, these errors appear.
11%|โ | 51/469 [00:01<00:07, 58.24it/s]C:/cb/pytorch_1000000000000/work/aten/src\ATen/native/cuda/DistributionTemplates.h:591: block: [6,0,0], thread: [416,0,0] Assertion 0 <= p4 && p4 <= 1
failed.
C:/cb/pytorch_1000000000000/work/aten/src\ATen/native/cuda/DistributionTemplates.h:591: block: [6,0,0], thread: [417,0,0] Assertion 0 <= p4 && p4 <= 1
failed.
C:/cb/pytorch_1000000000000/work/aten/src\ATen/native/cuda/DistributionTemplates.h:591: block: [6,0,0], thread: [418,0,0] Assertion 0 <= p4 && p4 <= 1
failed.
C:/cb/pytorch_1000000000000/work/aten/src\ATen/native/cuda/DistributionTemplates.h:591: block: [6,0,0], thread: [419,0,0] Assertion 0 <= p4 && p4 <= 1
failed.
C:/cb/pytorch_1000000000000/work/aten/src\ATen/native/cuda/DistributionTemplates.h:591: block: [6,0,0], thread: [420,0,0] Assertion 0 <= p4 && p4 <= 1
failed.
C:/cb/pytorch_1000000000000/work/aten/src\ATen/native/cuda/DistributionTemplates.h:591: block: [6,0,0], thread: [421,0,0] Assertion 0 <= p4 && p4 <= 1
failed.
C:/cb/pytorch_1000000000000/work/aten/src\ATen/native/cuda/DistributionTemplates.h:591: block: [6,0,0], thread: [422,0,0] Assertion 0 <= p4 && p4 <= 1
failed.
C:/cb/pytorch_1000000000000/work/aten/src\ATen/native/cuda/DistributionTemplates.h:591: block: [6,0,0], thread: [423,0,0] Assertion 0 <= p4 && p4 <= 1
failed.
C:/cb/pytorch_1000000000000/work/aten/src\ATen/native/cuda/DistributionTemplates.h:591: block: [6,0,0], thread: [424,0,0] Assertion 0 <= p4 && p4 <= 1
failed.
C:/cb/pytorch_1000000000000/work/aten/src\ATen/native/cuda/DistributionTemplates.h:591: block: [6,0,0], thread: [425,0,0] Assertion 0 <= p4 && p4 <= 1
failed.
C:/cb/pytorch_1000000000000/work/aten/src\ATen/native/cuda/DistributionTemplates.h:591: block: [6,0,0], thread: [426,0,0] Assertion 0 <= p4 && p4 <= 1
failed.
C:/cb/pytorch_1000000000000/work/aten/src\ATen/native/cuda/DistributionTemplates.h:591: block: [6,0,0], thread: [427,0,0] Assertion 0 <= p4 && p4 <= 1
failed.
C:/cb/pytorch_1000000000000/work/aten/src\ATen/native/cuda/DistributionTemplates.h:591: block: [6,0,0], thread: [428,0,0] Assertion 0 <= p4 && p4 <= 1
failed.
C:/cb/pytorch_1000000000000/work/aten/src\ATen/native/cuda/DistributionTemplates.h:591: block: [6,0,0], thread: [429,0,0] Assertion 0 <= p4 && p4 <= 1
failed.
C:/cb/pytorch_1000000000000/work/aten/src\ATen/native/cuda/DistributionTemplates.h:591: block: [6,0,0], thread: [430,0,0] Assertion 0 <= p4 && p4 <= 1
failed.
C:/cb/pytorch_1000000000000/work/aten/src\ATen/native/cuda/DistributionTemplates.h:591: block: [6,0,0], thread: [431,0,0] Assertion 0 <= p4 && p4 <= 1
failed.
C:/cb/pytorch_1000000000000/work/aten/src\ATen/native/cuda/DistributionTemplates.h:591: block: [6,0,0], thread: [432,0,0] Assertion 0 <= p4 && p4 <= 1
failed.
C:/cb/pytorch_1000000000000/work/aten/src\ATen/native/cuda/DistributionTemplates.h:591: block: [6,0,0], thread: [433,0,0] Assertion 0 <= p4 && p4 <= 1
failed.
C:/cb/pytorch_1000000000000/work/aten/src\ATen/native/cuda/DistributionTemplates.h:591: block: [6,0,0], thread: [434,0,0] Assertion 0 <= p4 && p4 <= 1
failed.
C:/cb/pytorch_1000000000000/work/aten/src\ATen/native/cuda/DistributionTemplates.h:591: block: [6,0,0], thread: [435,0,0] Assertion 0 <= p4 && p4 <= 1
failed.
C:/cb/pytorch_1000000000000/work/aten/src\ATen/native/cuda/DistributionTemplates.h:591: block: [6,0,0], thread: [436,0,0] Assertion 0 <= p4 && p4 <= 1
failed.
C:/cb/pytorch_1000000000000/work/aten/src\ATen/native/cuda/DistributionTemplates.h:591: block: [6,0,0], thread: [437,0,0] Assertion 0 <= p4 && p4 <= 1
failed.
C:/cb/pytorch_1000000000000/work/aten/src\ATen/native/cuda/DistributionTemplates.h:591: block: [6,0,0], thread: [438,0,0] Assertion 0 <= p4 && p4 <= 1
failed.
C:/cb/pytorch_1000000000000/work/aten/src\ATen/native/cuda/DistributionTemplates.h:591: block: [6,0,0], thread: [439,0,0] Assertion 0 <= p4 && p4 <= 1
failed.
C:/cb/pytorch_1000000000000/work/aten/src\ATen/native/cuda/DistributionTemplates.h:591: block: [6,0,0], thread: [440,0,0] Assertion 0 <= p4 && p4 <= 1
failed.
C:/cb/pytorch_1000000000000/work/aten/src\ATen/native/cuda/DistributionTemplates.h:591: block: [6,0,0], thread: [441,0,0] Assertion 0 <= p4 && p4 <= 1
failed.
C:/cb/pytorch_1000000000000/work/aten/src\ATen/native/cuda/DistributionTemplates.h:591: block: [6,0,0], thread: [442,0,0] Assertion 0 <= p4 && p4 <= 1
failed.
C:/cb/pytorch_1000000000000/work/aten/src\ATen/native/cuda/DistributionTemplates.h:591: block: [6,0,0], thread: [443,0,0] Assertion 0 <= p4 && p4 <= 1
failed.
C:/cb/pytorch_1000000000000/work/aten/src\ATen/native/cuda/DistributionTemplates.h:591: block: [6,0,0], thread: [444,0,0] Assertion 0 <= p4 && p4 <= 1
failed.
C:/cb/pytorch_1000000000000/work/aten/src\ATen/native/cuda/DistributionTemplates.h:591: block: [6,0,0], thread: [445,0,0] Assertion 0 <= p4 && p4 <= 1
failed.
C:/cb/pytorch_1000000000000/work/aten/src\ATen/native/cuda/DistributionTemplates.h:591: block: [6,0,0], thread: [446,0,0] Assertion 0 <= p4 && p4 <= 1
failed.
C:/cb/pytorch_1000000000000/work/aten/src\ATen/native/cuda/DistributionTemplates.h:591: block: [6,0,0], thread: [447,0,0] Assertion 0 <= p4 && p4 <= 1
failed.
RuntimeError: CUDA error: device-side assert triggered
But if I run it on cpu, the error while like this:
RuntimeError: Expected p_in >= 0 && p_in <= 1 to be true, but got false.
It seems like the function hidden_sampling dosen't run correctly.
Please, make sure that the following boxes are checked before submitting a new issue. There is a small chance that you can solve it by your own or even that it was already addressed by someone.
Thank you!
[x ] Check that you are up-to-date with the master branch of Learnergy. You can update with:
pip install git+git://github.com/gugarosa/learnergy.git --upgrade --no-deps
[ x] Check that you have read all of our README.
I fed a train Torch Dataset into GaussianRBM, which is derived from a standard DataFrame of real numbers. each dataset.getitem() returns a instance with shape torch.Size([1, 165])
The model.fit() call blows up with
KeyError: 0
I checked your example gaussian_rbm_training.py. There is one notable difference which is you used MNIST sample dataset, which has a label. However, in theory, RBM does not require a label to train.
Can you tell what is wrong with my data, and is the label field really what's causing the error?
KeyError: 0
Expected behavior: [It should be what you expect to happen]
expect it to just train through all the epochs
Actual behavior: [What actually happens]
throws a key error: 0
Reproduces how often: [How much does it occur? Show us your percentage]
everytime
suspect a label field is needed for the input dataset. But don't understand why if that is the case.
Check that you are up-to-date with the master branch of Learnergy. You can update with:
pip install git+git://github.com/gugarosa/learnergy.git --upgrade --no-deps
Check that you have read all of our README.
I am studying through gaussian_rbm.py, but I don't gaussian formulations anywhere.
https://github.com/gugarosa/learnergy/blob/master/learnergy/models/gaussian/gaussian_rbm.py
https://www.jmlr.org/papers/volume12/taylor11a/taylor11a.pdf
Expected behavior: [It should be what you expect to happen]
gaussian formulation both in energy and sampling of visible nodes
Actual behavior: [What actually happens]
seems everything is treated as binary
is gaussian_rbm.py meant for gaussian visible layer and binary hidden layer?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.