Friday, July 31, 2015

Invoking SWI prolog from PHP

Php File invoking Swi Prolog by passing an argument

php file (index.php)

$arg = 'Parameter';
        $cmd = 'C:\progra~1\swipl\bin\swipl.exe C:\xampp\htdocs\prolog\ '.$arg;
        $output = exec($cmd);
        echo $output;

swi prolog file (

:- initialization main.

main :-
  current_prolog_flag(argv, Argv),
  format('Testing Prolog File. Argument is: ~w\n', Argv),  halt(0).

Friday, June 12, 2015

Creating Neural Networks with Python PyBrain

Let's learn how to create a simple artificial neural network (ann) with PyBrain tool kit. Before jumping on to the coding let's give ourselves a heads up about the inside creation of the network. I assume you have done your research about what a neural network is and what its capabilities are. As we know ANN's communicate similar to neurons . Let me explain to you through a diagram. 

The circles in the diagram denote nodes. As per this example we have three input nodes, 4 middle layer nodes and 2 output nodes. When it comes to the ANN development we do not deal with the middle layer node series, a point to note would be, higher the number of hidden nodes, higher will be the complexity thus resulting in accurate results. 

A single node from the input layer will communicate with every single node in the hidden layer therefore when it comes to writing code we initialize the sizes of all three layers respectively.

Lets get started with the code

Open up your Python shell and import the following libraries.

Now we have the supporting libraries imported to define a neural network, so lets define a neural network by passing in three parameters to denote the input nodes, hidden nodes and output nodes. We shall pass 3,5,2 respectively as per the above diagram

The ANN has been defined, now the network has to be activated with our set of inputs. We have defined 3 input nodes therefore let's activate the ANN with 3 inputs. (XOR function)

We have activated the network with three inputs, what's next is to define our dataset of which we will be using to train. we refer to it as the training data set, so lets quickly have it defined. The dataset takes in 2 parameters. First parameter denotes the width of the dataset, in our case its 3 and the second parameter is the output which is 2 in our case.

The dataset is now defined, lets add sample values to our dataset to train it. Lets add in sample inputs
to the dataset. 2 parameters are passed to the sample data set as well. 1st param is set of inputs and the 2nd is the expected output.

The dataset is now ready to be trained. Lets train the dataset against the input.

let's execute the script in command prompt to see the value.

Installing PyBrain on Windows

Hello people,

Before installing PyBrain on windows it is important that you understand the requisites that is to be installed prior to the PyBrain Library. This tutorial will guide you through the simple steps as it is quite a challenging task to install the libraries on windows. Make sure no 64 bit versions of the libraries are installed as they're bound to pop errors. I have attached an archived folder below that contains all the needed files. (Note - 32 bit version). If you're using a 64 bit version of windows it still wouldn't be an issue as it supports 32 bit.

Link to Download the PyBrain Resources

What you need,

  1. Python 2.7 or Greater
  3. numpy 
  4. scipy 
Install Python 2.7 or a higher version on your windows computer. You will find the setup on the archived folder. 

Once it is installed, open up a command prompt terminal on the folder where you copied and type the following command python install and hit enter. You will now see the needed pip packages and libraries being installed. If it is installed properly you will find a Scripts folder inside your Python27 directory which you will find on your C drive. 

Then edit the path environmental variable by appending the following paths, 

Next is to install numpy, you will find a .whl file in the attached folder. Copy the file to your Scripts folder. Then change directory through a command prompt terminal to the Scripts folder and type 
pip install numpy-1.9.2+mkl-cp27-none-win32.whl 

Final step is to install scipy, let's install scipy through a windows executable file. The setup is in the attached folder as the others. Double click and install the file like any other installation.

Scipy and numpy has been installed, you can now install any other python library that requires scipy and numpy as prerequisites. In our case it's PyBrain. So let us install PyBrain now.   

You will find a folder in the name of pybrain-master in the attached folder, CD inside the folder through command prompt and type the command python 
The need packages will be downloaded via the command prompt. If no error is prompt you have installed PyBrain successfully.

To make sure your python libraries are working, go to the python command line and type the following commands,

import scipy
import numpy
import pybrain

If the libraries are imported without an error as shown below, you are good to go.