This package includes an example Recurrent Neural Network. The package is loaded using:
library(rnn)
##
## Attaching package: 'rnn'
## The following object is masked _by_ '.GlobalEnv':
##
## int2bin
We can view the code of the main rnn()
function by
calling it without the parathesis (not printed here).
trainr
As can be seen from the above, the model relies on two other
functions that are available through the sigmoid
package.
The first function is logistic()
, which converts an
integer to its sigmoid value.
<- sigmoid::logistic(3)) (a
## [1] 0.9525741
The code for the sigmoid()
function is:
::logistic sigmoid
## function (x, k = 1, x0 = 0)
## 1/(1 + exp(-k * (x - x0)))
## <bytecode: 0x00000000220b1f08>
## <environment: namespace:sigmoid>
The second function converts the sigmoid value of a number to its derivative.
::sigmoid_output_to_derivative(a) # a was created above using sigmoid() sigmoid
## [1] 0.04517666
Finally, we can inspect this code using:
::sigmoid_output_to_derivative sigmoid
## function (x)
## x * (1 - x)
## <bytecode: 0x0000000021f1fcc0>
## <environment: namespace:sigmoid>
An example is included in the help file.
help('trainr')
Below is a basic function that converts integers to binary format (read left to right)
# basic conversion
<- function(integer, length=8)
i2b as.numeric(intToBits(integer))[1:length]
# apply to entire vectors
<- function(integer, length=8)
int2bin t(sapply(integer, i2b, length=length))
First we generate the data:
# create sample inputs
= sample(0:127, 5000, replace=TRUE)
X1 = sample(0:127, 5000, replace=TRUE)
X2
# create sample output
<- X1 + X2
Y
# convert to binary
<- int2bin(X1)
X1 <- int2bin(X2)
X2 <- int2bin(Y)
Y
# Create 3d array: dim 1: samples; dim 2: time; dim 3: variables.
<- array( c(X1,X2), dim=c(dim(X1),2) )
X <- array( Y, dim=c(dim(Y),1) ) Y
This example is:
# train the model
<- trainr(Y=Y[,dim(Y)[2]:1,,drop=F], # we inverse the time dimension
model X=X[,dim(X)[2]:1,,drop=F], # we inverse the time dimension
learningrate = 0.1,
hidden_dim = 10,
batch_size = 100,
numepochs = 10)
## Trained epoch: 1 - Learning rate: 0.1
## Epoch error: 3.99036933039482
## Trained epoch: 2 - Learning rate: 0.1
## Epoch error: 3.86793697589471
## Trained epoch: 3 - Learning rate: 0.1
## Epoch error: 3.81421449058677
## Trained epoch: 4 - Learning rate: 0.1
## Epoch error: 3.70990306071076
## Trained epoch: 5 - Learning rate: 0.1
## Epoch error: 3.68237145514884
## Trained epoch: 6 - Learning rate: 0.1
## Epoch error: 3.64646678875448
## Trained epoch: 7 - Learning rate: 0.1
## Epoch error: 3.6278389447261
## Trained epoch: 8 - Learning rate: 0.1
## Epoch error: 3.61270171867137
## Trained epoch: 9 - Learning rate: 0.1
## Epoch error: 3.59420980973821
## Trained epoch: 10 - Learning rate: 0.1
## Epoch error: 3.58585568272032
See the evolution of the error over different epochs:
plot(colMeans(model$error),type='l',
xlab='epoch',
ylab='errors' )
Now create testing data
# create test inputs
= int2bin( sample(0:127, 7000, replace=TRUE) )
A1 = int2bin( sample(0:127, 7000, replace=TRUE) )
A2
# create 3d array: dim 1: samples; dim 2: time; dim 3: variables
<- array( c(A1,A2), dim=c(dim(A1),2) ) A
Predict based on testing data.
# predict
<- predictr(model,
B dim(A)[2]:1,,drop=F]
A[,
)= B[,dim(B)[2]:1] B
Define basic functions to convert binary to integer
<- function(binary)
b2i packBits(as.raw(c(binary, rep(0, 32-length(binary) ))), 'integer')
<- function(binary){
bin2int <- round(binary)
binary <- dim(binary)[2] # determine length of binary representation
length apply(binary, 1, b2i) } # apply to full matrix
Test prediction against true values
# convert back to integers
<- bin2int(A1)
A1 <- bin2int(A2)
A2 <- bin2int(B)
B
# plot the difference
hist( B-(A1+A2) )