Giter VIP home page Giter VIP logo

deep-belief-network's Introduction

deep-belief-network

A simple, clean, fast Python implementation of Deep Belief Networks based on binary Restricted Boltzmann Machines (RBM), built upon NumPy, TensorFlow and scikit-learn:

Hinton, Geoffrey E., Simon Osindero, and Yee-Whye Teh. "A fast learning algorithm for deep belief nets." Neural computation 18.7 (2006): 1527-1554.

Fischer, Asja, and Christian Igel. "Training restricted Boltzmann machines: an introduction." Pattern Recognition 47.1 (2014): 25-39.

Overview

This project works on Python 3.6 and follows the scikit-learn API guidelines. The code includes two implementations: one is built on top of TensorFlow while the other one just uses NumPy. To decide which one to use is as easy as importing the classes from the correct module: dbn.tensorflow for TensorFlow or dbn for NumPy.

import numpy as np

np.random.seed(1337)  # for reproducibility
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
from sklearn.metrics.classification import accuracy_score

from dbn.tensorflow import SupervisedDBNClassification
# use "from dbn import SupervisedDBNClassification" for computations on CPU with numpy

# Loading dataset
digits = load_digits()
X, Y = digits.data, digits.target

# Data scaling
X = (X / 16).astype(np.float32)

# Splitting data
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=0)

# Training
classifier = SupervisedDBNClassification(hidden_layers_structure=[256, 256],
                                         learning_rate_rbm=0.05,
                                         learning_rate=0.1,
                                         n_epochs_rbm=10,
                                         n_iter_backprop=100,
                                         batch_size=32,
                                         activation_function='relu',
                                         dropout_p=0.2)
classifier.fit(X_train, Y_train)

# Save the model
classifier.save('model.pkl')

# Restore it
classifier = SupervisedDBNClassification.load('model.pkl')

# Test
Y_pred = classifier.predict(X_test)
print('Done.\nAccuracy: %f' % accuracy_score(Y_test, Y_pred))

Usage

Clone this repository:

git clone https://github.com/albertbup/deep-belief-network.git

and go to the root folder:

cd deep-belief-network

The docker way:

Build the docker image (you'll need to have docker installed in your system):

docker build --tag albertbup/deep-belief-network:1.0.5 .

Cool, let's go inside the container and run an example:

docker run --rm -it -v ${PWD}:/code albertbup/deep-belief-network:1.0.5 bash
# Now within the container...
python example_classification.py

The virtualenv way:

Create a virtual environment for Python 3.6 and activate it.

Next, install requirements:

pip install -r requirements.txt

Finally, run an example:

# Now within the virtual environment...
python example_classification.py

Citing the code

BibTex reference format:

    @misc{DBNAlbert,
    title={A Python implementation of Deep Belief Networks built upon NumPy and TensorFlow with scikit-learn compatibility},
    url={https://github.com/albertbup/deep-belief-network},
    author={albertbup},
    year={2017}}

deep-belief-network's People

Contributors

albertbup avatar lizeyan avatar mathiasvh avatar matus-tomlein avatar pknam avatar skx300 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

deep-belief-network's Issues

dbn

I've been using your dbn for my project. I'd like to first appreciate your amazing work. I am trying to develop something like the image shown below

dbn

The question I've come across with is

a) I developed separate encoders for two languages and a joint layer using SupervisedDBNClassification()
b) How can I get the weights at each layer
c) How do i do back propagation all together as a single network

Since I am newbie to Tensor flow and Python I dunno how to alter the source code.
Any help would be appreciated.

No module called Tensorflow

When I want to run the sample code, it always tells me

ImportError: No module named tensorflow

Tensorflow is installed and runningh though!

Gaussian Bernoulli RBM

This is the best working library I could find for DBN. I am a newbie, and wondering if I can use it for GBRBM? Any help will be appreciated.

Thanks,

How to enhance the speed of pre-train step

Hi I tried your dbn example and it is quite cool since it is the only successful example with DBN model I have found so far.
However, I find a problem that since the pre-train is done using numpy, it takes quite long time to do the pretrain step when the input is large. Any ideas about how to speed up it?

Is momentum and weight decay implemented? [Short Answer: No]

Hi,
just want to ask if: momentum and weight decay are implemented,
and/or someone knows how to easily integrate it into the code.

If not, is it possible that I’ll try to do it myself and add it to your code? (pull request)

Thanks! (nice package 👍 )

Issue on runing demo examples

Run classification_demo.py and regression_demo. I have the following error:
No module named 'dbn.tensorflow'

OS: windows 7
Python: 3.5.2
tensorflow: 1.0.1

classification_demo.py

Hi! @albertbup
Thanks for the fantastic work. This is very useful to me. But when I run classification_demo.py,it has no result about accuracy_score.

Looking forward to your reply!Thanks.

Time series prediction

Hi albert
Thanks for the great code you've shared
I would like to use the the deep-belief-network for the time series prediction as i read on some papers
Should i use the regression code?
Or do you have some advice to me since im newbie
Thank you
best regards

how to cite your code

Hi Albert, have you published any work using this code. Second, how does one cite your work if this code is used?

ValueError: shapes (256,33626) and (33227,800) not aligned: 33626 (dim 1) != 33227 (dim 0)

I use the trained model through:

classifier = SupervisedDBNClassification(hidden_layers_structure=[256, 256],
    learning_rate_rbm=0.05,
    learning_rate=0.1,
    n_epochs_rbm=10,
    n_iter_backprop=100,
    batch_size=32,
    activation_function='relu',
    optimization_algorithm='sgd',
    dropout_p=0.2)

# 如果只需调用训练好的模型,需要注释下面2行
# classifier.fit(train_data, train_label)
# Save the model
# classifier.save('supervised_model.pkl')

# Restore it
classifier = SupervisedDBNClassification.load('supervised_model.pkl')

I got error as title shows:


  File "e:\applications\wpy64-3741\python-3.7.4.amd64\lib\site-packages\dbn\models.py", line 183, in _compute_hidden_units_matrix
    np.dot(self.W, np.transpose(matrix_visible_units)) + self.c[:, np.newaxis]))

  File "<__array_function__ internals>", line 6, in dot

ValueError: shapes (256,33626) and (33227,800) not aligned: 33626 (dim 1) != 33227 (dim 0)

What's the problem?

cannot import name 'SupervisedDBNClassification'

i tried this code for my accelerometer signal dataset as follow

import numpy as np

np.random.seed(1337)  # for reproducibility
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
from sklearn.metrics.classification import accuracy_score
from dbn import SupervisedDBNClassification

# Loading dataset
train = np.loadtxt("TrainDatasetFinal.txt", delimiter=",")
test = np.loadtxt("testDatasetFinal.txt", delimiter=",")

y_train = train[:,7]
y_test = test[:,7]

train_spec = train[:,6]
test_spec = test[:,6]


# Training
classifier = SupervisedDBNClassification(hidden_layers_structure=[256, 256],
                                         learning_rate_rbm=0.05,
                                         learning_rate=0.1,
                                         n_epochs_rbm=10,
                                         n_iter_backprop=100,
                                         batch_size=32,
                                         activation_function='relu',
                                         dropout_p=0.2)
classifier.fit(train_spec, y_train)

but it gets me the following error

Traceback (most recent call last):
  File "G:\dbn.py", line 35, in <module>
    from dbn import SupervisedDBNClassification
  File "G:\dbn.py", line 35, in <module>
    from dbn import SupervisedDBNClassification
ImportError: cannot import name 'SupervisedDBNClassification'

this is a sample from my dataset

(Patient Number, time in millisecond, accelerometer x-axis,y-axis, z-axis,magnitude, spectrogram,label (0 or 1))

1,15,70,39,-970,947321,596768455815000,0
1,31,70,39,-970,947321,612882670787000,0
1,46,60,49,-960,927601,602179976392000,0
1,62,60,49,-960,927601,808020878060000,0
1,78,50,39,-960,925621,726154800929000,0

in the dataset i am using the only the spectrogram as input feature and the label (0 or 1) as the output the total traing samples is 1,415,684

Diffrent dataset for backpropagation of the “ANN”

Hi, thanks for really good code!

I would like to use the deep-belief-network for regression.

My problem is that I would like to use a different dataset for the last backpropagation of the “ANN” then for initial training of the RBM. Is that possible?

I’m trying to use the deep-belief-network for soft sensor modelling in chemical plants. Found some interesting academic publications where they have used it with great results.

Thanks in advance,

Have a good day,

Best regards
Lukas

How to add new optimizer

First, thanks for creating such a wonderful package!
I am investigating if it is possible to use customized optimizer, such as adam? There are code for adam available, but the question is how to adapt it into the DBN code? Advice and help are very welcome. Thanks.
(I changed my original question, and I think this is what I want to know)

ERROR: Cannot install deep-belief-network and deep-belief-network==1.0.4 because these package versions have conflicting dependencies.

Hello, Just tried to install this code for my study, but suck with below error. Please help.

ERROR: Cannot install deep-belief-network and deep-belief-network==1.0.4 because these package versions have conflicting dependencies.

The conflict is caused by:
deep-belief-network 1.0.4 depends on numpy==1.12.0
scipy 0.18.1 depends on numpy>=1.7.1
tensorflow 1.5.0 depends on numpy>=1.12.1

AttributeError: module 'tensorflow' has no attribute 'Session'

When I import the library I got this error:
AttributeError: module 'tensorflow' has no attribute 'Session'

after searching about it I found the problem in the model file:
import tensorflow as tf
sess = tf.Session()

tensorflow 2 and above remove the session() from tf.Session() to tf.compat.v1.Session()

Face Recognition on Video

Hi, thanks for really good code!
I would like to use the deep-belief-network for classification on face recognition. I've already train my own datasets (face image), on 'pickle' format (datasets and train model).
Then I want to run the model that I made on a video (realtime face recognition), do you have any suggestions for me, or do you have a code / site link that can help me please?

Thank you
Best regards

Run in GPU

For me, when I import from dbn.tensorflow, everything runs in CPU. And when I import from dbn, the pre-training part runs in GPU while the fine-tuning part still runs in CPU. I do not know why. Is there anything wrong with my environment settings?

RuntimeWarning - Result in NaN - Python 3.5

/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/dbn/models.py:127: RuntimeWarning: invalid value encountered in subtract
delta_W = np.outer(h_0, v_0) - np.outer(h_k, v_k)
/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/dbn/models.py:129: RuntimeWarning: invalid value encountered in subtract
delta_c = h_0 - h_k

Not sure if it's Python 3's issue, I've fixed the errors that prevents the programs from running in python3, like prints and maps, but not sure what these are about. Are they not the same in 2.7 and 3.5?

Call `.get_params()` method return empty dict

When I call .get_params() method, but return empty dict.

from dbn import SupervisedDBNClassification

classifier = SupervisedDBNClassification(hidden_layers_structure=[256, 256],
                                         learning_rate_rbm=0.05,
                                         learning_rate=0.1,
                                         n_epochs_rbm=10,
                                         n_iter_backprop=100,
                                         batch_size=32,
                                         activation_function='relu',
                                         dropout_p=0.2)

print(classifier.get_params())
# Output: {}

How can I save your RBM network?

How can I save your network using tf.train.Saver?
I'm using SupervisedDBNClassification classifier.

And if possible, can you add a Save method?
thanks

Hidden Layer Structure

Hello sir,
This is a very good tf module i found for RBM and DBN, thanks for the same but I have a small doubt

In the below code snippet could you please tell me how to interpret 'hidden_layer_structure' parameter.

dbn = UnsupervisedDBN(hidden_layers_structure=[30,2],
batch_size=10,
learning_rate_rbm=0.06,
n_epochs_rbm=20,
activation_function='sigmoid')

Hidden units number_ deep-belief-network for regression

Hi, thanks for really good code!
I would like to use the deep-belief-network for regression. But, when I see your code, I can see that the number of hidden layers (hidden_layers_structure), the learning rates (learning_rate_rbm, learning_rate), epochs'number (n_epochs_rbm, n_iter_backprop), batch size( batch_size) and activation function are settable. However, this is not the case of the number of hidden units.
How is it set please?
Thank you
Best regards

failed to solve with frontend dockerfile.v0: failed to read dockerfile: open /var/lib/docker/tmp/buildkit-mount080660389/Dockerfile: no such file or directory

New issue popped up when tried to install through docker:

C:\Users\xxxx>docker build --tag albertbup/deep-belief-network:1.0.5 .
[+] Building 0.1s (2/2) FINISHED
=> [internal] load build definition from Dockerfile 0.1s
=> => transferring dockerfile: 2B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
failed to solve with frontend dockerfile.v0: failed to read dockerfile: open /var/lib/docker/tmp/buildkit-mount080660389/Dockerfile: no such file or directory

Update to Tensorflow 2

On Tensorflow 2 I get some errors, e.g.:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-1-2516a398efe0> in <module>
----> 1 from dbn.tensorflow import SupervisedDBNClassification
      2 import numpy as np
      3 import pandas as pd
      4 from sklearn.model_selection import train_test_split
      5 from sklearn.metrics.classification import accuracy_score

~/RBMpro/jupyter/tf-dbn/dbn/tensorflow/__init__.py in <module>
----> 1 from .models import BinaryRBM, UnsupervisedDBN, SupervisedDBNRegression, SupervisedDBNClassification

~/RBMpro/jupyter/tf-dbn/dbn/tensorflow/models.py in <module>
     17 
     18 
---> 19 sess = tf.Session()
     20 atexit.register(close_session)
     21 

AttributeError: module 'tensorflow' has no attribute 'Session'

Please update

accuracy not improving

I have data that contains the probability values. It is a 4 class classification problem. The accuracy is not improving with your classification example. The data format is given below
untitled

DBN for Univariate series with a dicision label

Greetings sir,

My code for univariate classification for 5 classes is

X=df['value'].values;y = df['class']
dbn = UnsupervisedDBN(hidden_layers_structure=[1,5],
batch_size=10,
learning_rate_rbm=0.06,
n_epochs_rbm=20,
activation_function='sigmoid')

The error i got is
Please help me to rectify the same!
issue

Cannot run the example code (or any) on cluster

Hey,

I am trying to use this great library on a cluster (computecanada) but It gives me this error when i try the example codes or any other.
Here is the list of libraries and their versions I have available:

numpy 1.16.0
scikit-learn 0.20.2
scipy 1.2.0
tensorflow-cpu 1.5.0+computecanada


terminate called after throwing an instance of 'std::system_error' what(): Resource temporarily unavailable Aborted

the error above is about a memory leak, I tried to find a leak in the library but could not really get anywhere. Hope to hear from you soon!

Cheers,

what is the shape of an accelerometer dataset?

what i want is to modify this code in way to accept my accelerometer dataset, below is the code with my modification to add the new dataset

import numpy as np

np.random.seed(1337)  # for reproducibility
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
from sklearn.metrics.classification import accuracy_score
from dbn import SupervisedDBNClassification

# Loading dataset
train = np.loadtxt("TrainDatasetFinal.txt", delimiter=",")
test = np.loadtxt("testDatasetFinal.txt", delimiter=",")

y_train = train[:,7]
y_test = test[:,7]

train_spec = train[:,6]
test_spec = test[:,6]


# Training
classifier = SupervisedDBNClassification(hidden_layers_structure=[256, 256],
                                         learning_rate_rbm=0.05,
                                         learning_rate=0.1,
                                         n_epochs_rbm=10,
                                         n_iter_backprop=100,
                                         batch_size=32,
                                         activation_function='relu',
                                         dropout_p=0.2)
classifier.fit(train_spec, y_train)

And this is a sample from my dataset

(Patient Number, time in millisecond, accelerometer x-axis,y-axis, z-axis,magnitude, spectrogram,label (0 or 1))

1,15,70,39,-970,947321,596768455815000,0
1,31,70,39,-970,947321,612882670787000,0
1,46,60,49,-960,927601,602179976392000,0
1,62,60,49,-960,927601,808020878060000,0
1,78,50,39,-960,925621,726154800929000,0

in the dataset i am using the only the spectrogram as input feature and the label (0 or 1) as the output the total traing samples is 1,415,684

So how to modify this code to be appropriate the signal dataset instead of the image dataset

Doesn't work with GPU

Hi - the code isn't working with GPU. I see that tensor flow gets initialized, but the GPU usage stays lower than 5%. I am running unsupervised_demo.py

Can't install due to requirement for old version of tensorflow

There is reference to tensorflow 1.0.0 as a requirement preventing installation:

Collecting tensorflow==1.0.0 (from deep-belief-network==1.0.1)
Could not find a version that satisfies the requirement tensorflow==1.0.0 (from deep-belief-network==1.0.1) (from versions: 1.2.0rc2, 1.2.0, 1.2.1, 1.3.0rc0)
No matching distribution found for tensorflow==1.0.0 (from deep-belief-network==1.0.1)

please add a redme file

I have seen your code. It's nice but the classification example what you had given ...is that works for any dimensionality data set...which means i have data which is of size 1000 * 20 - 19 features and 1 class label(10 classes) and test data set has 100*20 - 19 features and 1 class label (15 classes). What are all the changes i need to be done to make it work..

How To Define new Activation Function?

how to define new activation function. in library use class az activation function. please write sample :)
for example:

def double_activation(x):
return x * 2

Question regarding hidden_layers_structure

Dear albertbup,

First of all, thank you so much for implementing DBN into Python with lots of example from logistic regression, linear regression. Also, I have a question with your sample code, which is: hidden_layers_structure = [256,256], does that mean this DBN has 2 hidden layers with 256 neurons in each hidden layer ? Because I believe a standard DBN usually has 3 hidden layers (with hidden layer 1 as RBM). Hope you can help with my confusion.

Thank you for your time,

Sincerely,

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.