Movatterモバイル変換


[0]ホーム

URL:


Recurrent Neural Network

Bastiaan Quast

2023-04-21

Package

This package includes an example Recurrent Neural Network. Thepackage is loaded using:

library(rnn)
## ## Attaching package: 'rnn'
## The following object is masked _by_ '.GlobalEnv':## ##     int2bin

Code

We can view the code of the mainrnn() function bycalling it without the parathesis (not printed here).

trainr

As can be seen from the above, the model relies on two otherfunctions that are available through thesigmoidpackage.

The first function islogistic(), which converts aninteger to its sigmoid value.

(a<- sigmoid::logistic(3))
## [1] 0.9525741

The code for thesigmoid() function is:

sigmoid::logistic
## function(x, k=1, x0=0)##   1 / (1+exp( -k*(x-x0) ))## <bytecode: 0x557bf97ce9e8>## <environment: namespace:sigmoid>

The second function converts the sigmoid value of a number to itsderivative.

sigmoid::sigmoid_output_to_derivative(a)# a was created above using sigmoid()
## [1] 0.04517666

Finally, we can inspect this code using:

sigmoid::sigmoid_output_to_derivative
## function(x)##   x*(1-x)## <bytecode: 0x557bf7d57f38>## <environment: namespace:sigmoid>

Application

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 conversioni2b<-function(integer,length=8)as.numeric(intToBits(integer))[1:length]# apply to entire vectorsint2bin<-function(integer,length=8)t(sapply(integer, i2b,length=length))

First we generate the data:

# create sample inputsX1=sample(0:127,5000,replace=TRUE)X2=sample(0:127,5000,replace=TRUE)# create sample outputY<- X1+ X2# convert to binaryX1<-int2bin(X1)X2<-int2bin(X2)Y<-int2bin(Y)# Create 3d array: dim 1: samples; dim 2: time; dim 3: variables.X<-array(c(X1,X2),dim=c(dim(X1),2) )Y<-array( Y,dim=c(dim(Y),1) )

This example is:

# train the modelmodel<-trainr(Y=Y[,dim(Y)[2]:1,,drop=F],# we inverse the time dimensionX=X[,dim(X)[2]:1,,drop=F],# we inverse the time dimensionlearningrate   =0.1,hidden_dim     =10,batch_size =100,numepochs =10)
## Trained epoch: 1 - Learning rate: 0.1
## Epoch error: 3.99023495317551
## Trained epoch: 2 - Learning rate: 0.1
## Epoch error: 3.86768251716534
## Trained epoch: 3 - Learning rate: 0.1
## Epoch error: 3.74153792043825
## Trained epoch: 4 - Learning rate: 0.1
## Epoch error: 3.69369796139338
## Trained epoch: 5 - Learning rate: 0.1
## Epoch error: 3.67351064550201
## Trained epoch: 6 - Learning rate: 0.1
## Epoch error: 3.6481805596686
## Trained epoch: 7 - Learning rate: 0.1
## Epoch error: 3.62138148801879
## Trained epoch: 8 - Learning rate: 0.1
## Epoch error: 3.61217609782397
## Trained epoch: 9 - Learning rate: 0.1
## Epoch error: 3.60556692236638
## Trained epoch: 10 - Learning rate: 0.1
## Epoch error: 3.6011780462736

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 inputsA1=int2bin(sample(0:127,7000,replace=TRUE) )A2=int2bin(sample(0:127,7000,replace=TRUE) )# create 3d array: dim 1: samples; dim 2: time; dim 3: variablesA<-array(c(A1,A2),dim=c(dim(A1),2) )

Predict based on testing data.

# predictB<-predictr(model,               A[,dim(A)[2]:1,,drop=F]               )B= B[,dim(B)[2]:1]

Define basic functions to convert binary to integer

b2i<-function(binary)packBits(as.raw(c(binary,rep(0,32-length(binary) ))),'integer')bin2int<-function(binary){  binary<-round(binary)  length<-dim(binary)[2]# determine length of binary representationapply(binary,1, b2i)     }# apply to full matrix

Test prediction against true values

# convert back to integersA1<-bin2int(A1)A2<-bin2int(A2)B<-bin2int(B)# plot the differencehist( B-(A1+A2) )


[8]ページ先頭

©2009-2025 Movatter.jp