brain is an open-source neural network framework for R which is easy to use but nonetheless provides a lot of depth, this document focuses on the former, ease of use.

Install

brain depends on Chrome V8 which thus needs to be installed.

Binary packages for OS-X or Windows can be installed directly from CRAN:

Installation from source on Linux requires libv8 3.14 or 3.15 (no newer!). On Debian or Ubuntu use libv8-3.14-dev:

sudo apt-get install -y libv8-3.14-dev

On Fedora we need v8-314-devel:

sudo yum install v8-314-devel

On CentOS / RHEL we install v8-devel via EPEL:

sudo yum install epel-release
sudo yum install v8-devel

On OS-X use v8@3.15 (not regular v8) from Homebrew:

brew install v8@3.15

On other systems you might need to install libv8 from source. A compatible version of V8 3.14 is available from https://github.com/v8-314/v8. Build instructions are in the build directory.

Sequential API

brain follows the usual steps involved in building any model, define the model, train it, run it, with brain you can pipe those along. On sequential models, the package provides four distinct network types:

XOR

To demonstrate the basic building blocks of the package we shall build a simple perceptron to sovle a XOR. First, all networks are initialised with the function brain.

## -- Brain ----------------------------------------------------------------------- 
##  x Architecture: undefined
##  ( ) Untrained

This initialises an empty brain, with no architecture, we want to build a perceptron. We can do so with the perceptron function which takes an argument layers. This layers argument can be a single int or a vector of int the length of the layers where each int represents the size of the layers. In order to define the latter we need to know the number of inputs out network is going to take.

Our network will take two inputs input1 and input2, we therefore need to input layers. The output of the network (train$output) is a single number (0 or 1), so we need one output layer.

## -- Brain ----------------------------------------------------------------------- 
##  v Architecture: perceptron
##  = Layers:
##       > input:  logistic - 2 
##       = hidden: logistic - [3] 
##       < output: logistic - 1
##  ( ) Untrained

Since the output is either 0 or 1 we should use either a Tanh or a sigmoid (logistic) activation function, which is actually the default in brain, we’ll state tanh to demonstarte how it works.

## -- Brain ----------------------------------------------------------------------- 
##  v Architecture: perceptron
##  = Layers:
##       > input:  tanh - 2 
##       = hidden: tanh - [3] 
##       < output: tanh - 1
##  ( ) Untrained

Now we need to pass our training dataset (train) to our model.

## -- Brain ----------------------------------------------------------------------- 
##  v Architecture: perceptron
##  = Layers:
##       > input:  tanh - 2 
##       = hidden: tanh - [3] 
##       < output: tanh - 1
##  ( ) Untrained

Finally we can pass training options with train_opts, it’d be good to increase the number of iterations.

## -- Brain ----------------------------------------------------------------------- 
##  v Architecture: perceptron
##  = Layers:
##       > input:  tanh - 2 
##       = hidden: tanh - [3] 
##       < output: tanh - 1
##  ( ) Untrained

Note that the above merely passes the data but does not actually train the model. Once we have specified inputs and outputs we can train the model with the function train. However the latter takes one required argument cost which is the cost function to use. The cost function is returned by cost_function. In our case we want to use cross entropy.

## -- Training -------------------------------------------------------------------- 
##  x Error: 
##  i Iterations: 1 
##  > Time: 6 ms

That is our model build, let’s define a test set to see how it fares with the activate function.

We can then get the activations with get_activations.

##           [,1]
## [1,] 0.2796627
## [2,] 0.2768219
## [3,] 0.2914401

Our activation function (tanh) returns probabilities between -1 and 1 so we need to round them in order to get our “real” output.

##      [,1]
## [1,]    0
## [2,]    0
## [3,]    0

To clarify the output, let’s compare it to the expected output.

##   input1 input2 expected output
## 1      0      0        0      0
## 2      0      1        1      0
## 3      1      0        1      0

100% correct!

Multiclass

Let’s use the iris dataset and predict the specie of a flower given measurement of it’s petals. This is really just for demonstration purposes as we do not have enough data to make an accurate prediction using brain

brain, like all neural networks, expects inputs to range between 0 and 1. Therefore we need to rescale it.

## -- Training -------------------------------------------------------------------- 
##  x Error: 0.2565802 
##  i Iterations: 10000 
##  > Time: 11461 ms
## # A tibble: 6 x 2
##   predicted actual
##       <dbl>  <dbl>
## 1       0      0  
## 2       0      0  
## 3       0.9    0.5
## 4       0.9    0.5
## 5       1      1  
## 6       1      1

Continuous

## -- Training -------------------------------------------------------------------- 
##  x Error: 0.4857029 
##  i Iterations: 200 
##  > Time: 580 ms