Installing Haskell plugin for Eclipse

I use Eclipse for all sorts of development in C, Java and Haskell. It gives me uniform look and feel on every project. This is updated post with instructions for installing Haskell plugin for Eclipse.

This time I am installing on Windows XP (and Ubuntu 9.10 after) machine.

Windows XP

We need the following software installed (and versions i am installing):

  • Eclipse (3.5.1) +Java (1.6)
  • GHC (6.10)+Cabal
  • EclipseFP plugin (1.108.0)
  • Scion IDE library

Ubuntu 9.10

I have no success to run Wink on Ubuntu 9.10 (it seems to work on 8.10) so no screencast just instructions for now.

GHC

  • First if you don’t have GHC installed install it “sudo apt-get install ghc”. There is still no GHC 6.10 version for Ubuntu so i am installing it manually.
  • go to “http://haskell.org/ghc/”    Binary Packages->Linux (x86)
  • get “ghc-6.10.4-i386-unknown-linux-n.tar.bz2″
  • “tar jxvf ghc-6.10.4-i386-unknown-linux-n.tar.bz2″
  • “cd ghc-6.10.4/”
  • “./configure –prefix=/home/ross/ghc” – here i put installation in custom location in my HOME directory. If you want this to be installed on standard location skip “–prefix” and run as “sudo”. Also skip next steps.
  • make install
  • Now extend PATH environment variable to this custom location. “vi ~/.bashrc” add “PATH=$PATH:/home/ross/ghc/bin” and “export PATH”
  • If you want GHC 6.8 just skip previous steps and “sudo apt-get install ghc”

Cabal

  • get Cabal “http://www.haskell.org/cabal/download.html” “cabal-install-0.6.2.tar.gz”
  • “tar zxvf cabal-install-0.6.2.tar.gz” – extract archive
  • “cd cabal-install-0.6.2/”
  • “./bootstrap.sh” – here installation stops with some missing library like “/usr/bin/ld: cannot find -lgmp”. The reason is that you need not only library (in this case Multiprecision Arithmetic Library) but also development version of it. Install development version “sudo apt-get install libgmp3-dev” and run “./bootstrap.sh” again. If all dependencies are resolved “cabal” is installed in you HOME directory for example “/home/ross/.cabal/config”.
  • Run cabal to update and to install Scion “~/.cabal/bin/cabal update” “~/.cabal/bin/cabal install scion”. There are some warnings during the installation. Lets hope they aren’t important ones.

Eclipse

  • Install eclipse if not already installed “sudo apt-get install eclipse”
  • Run and choose “workspace” location.
  • Go to “Help->install new software”
  • “add” and enter name “GHC 6.10″ and location “http://eclipsefp.sf.net/updates”
  • Expand list of plugins and select new version of the plugin “1.108.0″ (this version uses Scion). Install it.
  • go to “Window->Preferences->Haskell->Haskell Implementations”. Add name “GHC 6.10″ and executable folder “/home/ross/ghc/bin”. In this case my custom installation location.
  • Go to Scion section “autodetect”. It detects the scion server at “/home/ross/.cabal/bin/scion-server”. “apply” options.
  • Change default Java to “Haskell” perspective at the right top corner of Eclipse. “click->other..->Haskell”

Haskell programming

Now lets create simple Haskell project to test the installation.

  • “file->new->Project->Haskell Project”
  • project name “Hello”
  • Expand created project and right click on “src” directory “new->haskell module”. Source folder is set to “/Hello/src”. Enter module name “Main”.
  • in “Main.hs” file enter “module Main where” next “mian = putStrLn “Hello World!”. Save and click on green button “Run As->Run GHCi session”. Next time you run it there should be “Main” item in “Run” button list.
  • Type “main” in console tab. Now you should see “Hello World!” written to console.

Haskell Eclipse Plugin

Enjoy happy programming.

How to set CUDA in Eclipse

Here I describe steps to get Eclipse working with CUDA. I am working on Ubuntu 8.04 and Eclipse: 3.2.2.

  1. Install latest CUDA – www.nvidia.com/cuda
  2. Install Eclipse if not already installed - www.eclipse.org
  3. set all necessary paths in /etc/ld.conf  and execute ldconfig as root (sudo)  during the CUDA installation.
  4. In Eclipse to enable code highlighting:  Window -> Preferences -> in C/C++ -> File Types -> New -> enter “*.cu” and select “C++ Source File”
  5. Make a copy of NVidia’s template project and set this project in eclipse
    • cd NVIDIA_CUDA_SDK/projects/
    • cp -r template/   my project/
    • New -> C++ Project ->
      • uncheck “Use default location” and set project name
      • next, on Make builder tab removed “all” target.
  6. after the project creation right click on project and open properties menu and in “Include paths and symbols” add external include path /usr/local/cuda/bin
  7. To set up Eclipse to run it:
    Run -> Run Configurations -> select “C/C++ Local Application” -> “New launch configuration” (click the leftmost icon on the top)

    • set name
    • set project
    • set path to binary e.g /…./bin/linux/release/name
  8. Edit Makefile to reflect the name of executable and other names.

Restricted Boltzmann Machine – Short Tutorial

I have read a lot of papers on RBM and it seems to be difficult to grasp all the implementation details.
So, I want to share my experience with people facing the same problems. My tutorial is based on variant of RBM-s named Continuous Restricted Boltzmann Machine or CRBM for short. CRBM have very close implementation to original RBM with binomial neurons (0,1) as possible values of activation. At the end of the article I provide some code in Python. No guarantee is given that this implementation is correct so let me know of any bugs found.
First you may have a look at the original papers that describe theory behind RBM neural networks:

RBM application to Netflix challenge

Boltzmann Machine – Scholarpedia

Continuous RBM

What is a Restricted Boltzmann Machine

Restricted Boltzmann Machine is a stochastic neural network (that is a network of neurons where each neuron have some random behavior when activated). It consist of one layer of visible units (neurons) and one layer of hidden units. Units in each layer have no connections between them and are connected to all other units in other layer (fig.1). Connections between neurons are bidirectional and symmetric . This means that information flows in both directions during the training and during the usage of the network and that weights are the same in both directions.

Restricted Boltzmann Machine

fig.1 Restricted Boltzmann Machine

RBM Network works in the following way:
First the network is trained by using some data set and setting the neurons on visible layer to match data points in this data set.
After the network is trained we can use it on new unknown data to make classification of the data (this is known as unsupervised learning)

Data set

For purpose of this tutorial I will use artificially generated data set. It is 2D data that form a pattern shown in fig.2. I choose to use 500 data points for training. Because each data points is formed by to numbers between 0 and 1 neurons have to accept continuous values and I use Continuous RBM. I have tried different 2D patterns and this one seems to be relative difficult to be learned by CRBM.

Training Data

fig. 2 Training data

Learning Algorithm

Training a RBM is performed by algorithm known as “Contrastive Divergence Learning”.

More info on Contrastive Divergence
Let W be the matrix of IxJ (I – number of visible neurons, J – number of hidden neurons) that represents
weights between neurons. Each neuron input is provided by connections from all neurons in other layer.
Current neuron state S is formed by multiplication of each input by weight, summation over all inputs and application of this sum as a argument of nonlinear sigmoidal function:
Sj = F( Sum( Si x Wij + N(0,sigma)) ) – here Si are all neurons in given layer plus one bias neuron that stays constantly set at 1.0
N(0,1) is random number from normal distribution with mean 0.0 and standard deviation sigma (I use sigma=0.2).
This nonlinear function in my case is:
F = lo + (hi – lo)/(1.0 + exp(-A*Sj))

Where lo and hi are the lower and higher bound of input values (in my case 0,1), so it
becomes: F = 1.0/(1.0 + exp(-A*Sj))
A – is some parameter that is determined during the learning process.

Contrastive divergence is a value that is computed (actually matrix of values) and that is used to adjust values of W matrix. Changing W incrementally lead to training of W values.

Let W0 be the initial matrix of weights that are set to some random small values. I use N(0, 0.1) for this.
Let CD = <Si.Sj>0 – <Si.Sj>n  – contrastive divergence
Then on each step (epoch) W is updated to new value W’:
W’ = W + alpha*CD
Here alpha is some small step – “learning rate”. There exist more complex ways for W update that involve
some “momentum” and “cost” of update to avoid W values to become very large.

Contrastive Divergence explanation

There seems to be big confusion what exactly Contrastive Divergence means and how to implement it.
I have spend a lot of time to understand it.
First of all CD as shown in the formula above is a matrix of size IxJ. So this formula have to be
computed for each combination of I and J.
<…> is a average over each data point in the data set.

Si.Sj is just a multiplication of current activation (state) of neuron I and neuron J (obviously :) ). Where Si is the state of a visible neuron, and Sj is the state of a hidden neuron.
Indexes after <…> mean that average is taken after 0 or N-th reconstruction step performed.

fig. 2 Training Restricted Boltzmann Machine

fig. 2 Training Restricted Boltzmann Machine

How is the reconstruction performed?

  1. get one data point from data set.
  2. use values of this data point to set state of visible neurons Si
  3. compute Sj for each hidden neuron based on formula above and states of visible neurons Si
  4. now Si and Sj values can be used to compute (Si.Sj)0 – here (…) means just values not average
  5. on visible neurons compute Si using the Sj computed in step3. This is known as “reconstruction”
  6. compute state of hidden neurons Sj again using Si from 5 step.
  7. now use Si and Sj to compute (Si.Sj)1 (fig.3)
  8. repeating multiple times steps 5,6 and 7 compute (Si.Sj)n. Where n is small number and can increase with learning steps to achieve better accuracy.

The algorithm as a whole is:

  • For each epoch do:
    • For each data point in data set do:
      • CDpos =0, CDneg=0 (matrices)
      • perform steps 1…8
      • accumulate CDpos = CDpos + (Si.Sj)0
      • accumulate CDneg = CDneg + (Si.Sj)n
    • At the end compute average of CDpos and CDneg by dividing them by number of data points.
    • Compute CD = < Si.Sj >0 – < Si.Sj >n = CDpos – CDneg
    • Update weights and biases W’ = W + alpha*CD (biases are just weights to neurons that stay always 1.0)
    • Compute some “error function” like sum of squared difference between Si in 1) and Si in 5)
      e.g between data and reconstruction.
  • repeat for the next epoch until error is small or some fixed number of epoch.

The value of parameter A for visible units stay constant and for hidden unit is adjusted
by the same CD calculation but instead of formula above the following formula is used:
CD = (<Sj.Sj>0 – <Sj.Sj>n)/(Aj.Aj)

In my code i use n=20 initially and gradually increase it to 50.
Most of the steps in the algorithm can be performed by some simple matrix multiplication.

RBM usage

After the RBM learning process finishes it can be shown how well it performs on new data points.
I use set of 500 data points that are random and uniformly distributed in the 2D interval (0..1, 0..1).
Visible layer neurons states are set with values of each data point and steps 1) 2) 3) 5) are repeated
multiple times. Number of repetitions is the max number of “n” used in the learning process.

At the end of n-th step, neurons states in visible layer represent a data reconstruction. This is repeated for each data point. All the reconstruction points form a 2D image pattern that can be compared with initial image.

Python implementation of CRBM

At the end of this article I provide some implementation of Continuous Restricted Boltzmann Machine in Python. Most of the steps are performed by matrix operations using NumPy library. I used PyLab and SciPy to make some nice visualizations. Images of data that are shown (fig.4) are interpolated with Gaussian kernel in order to have some “feeling” of the density of data points and reconstructed data points.

RBM reconstruction

fig. 4 Training data and reconstruction.

Next image shows initial data set and reconstructed data superimposed. Initial data is in blue, reconstructed in red and green line connects each data point with reconstructed one.

Fig. 5 Training data (blue) and reconstruction (red) superimposed.

Fig. 5 Training data (blue) and reconstruction (red) superimposed.

Main difficulty I observed is to fine tune the set of learning parameters.

I used the following parameters and if somebody is able to find better values – let me know.
sigma=0.2
A=0.1 (on visible neurons)
Learning Rate W = 0.5
Learning Rate A = 0.5
Learning Cost = 0.00001
Learning Moment = 0.9

RBM architecture is 2 visible neurons for 2D data points and 8 hidden neurons. On some experiments with simple patterns 4 neurons are enough to reconstruct pattern successfully.

Python code for Restricted Boltzmann Machine