knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
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:
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
.
library(brain) brain()
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.
# training data train <- dplyr::tibble( input1 = c(0, 0, 1, 1), input2 = c(0, 1, 0, 1), output = c(0, 1, 1, 0) )
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.
# 2 input layers # 3 hidden layers # 1 output layer brain() %>% perceptron(c(2, 3, 1))
Since the output is either 0
or 1
we should use either a Tanh or a sigmoid activation function.
brain
lets you explore those squash functions.
tanh <- run_squash("tanh") knitr::kable(tanh)
brain() %>% perceptron(c(2, 3, 1)) %>% squash_output( squash = squash_function("tanh") )
Now we need to pass our training dataset (train
) to our model.
brain() %>% perceptron(c(2, 3, 1)) %>% squash_input( squash = squash_function("tanh") ) %>% squash_hidden( squash = squash_function("tanh") ) %>% squash_output( squash = squash_function("tanh") ) %>% train_data(train) %>% train_input(input1, input2) %>% train_output(output)
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.
br <- brain() %>% perceptron(c(2, 3, 1)) %>% squash_input( squash = squash_function("tanh") ) %>% squash_hidden( squash = squash_function("tanh") ) %>% squash_output( squash = squash_function("tanh") ) %>% train_data(train) %>% train_input(input1, input2) %>% train_output(output) %>% train( cost = cost_function("cross_entropy") )
That is our model build, let's define a test set to see how it fares with the activate
function.
test <- dplyr::tibble( input1 = c(0, 0, 1), input2 = c(0, 1, 0), expected = c(0, 1, 1) ) br <- br %>% activate_data(test) %>% activate(input1, input2)
We can then get the activations with get_activations
.
get_activations(br)
Our activation function (tanh
) returns probabilities between -1 and 1 so we need to round them in order to get our "real" output.
get_activations(br) %>% round()
To clarify the output, let's compare it to the expected output.
output <- get_activations(br) %>% round() %>% unlist() cbind(test, output)
100% correct!
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.