r - Setting an S4 slot to function and representing a neural network layer -


i attempting write classes in r. here start of neural network layer class. generating warnings , errors don't know how correct.

# slot definitions setclass(  class="neuralnetworklayer",  representation=representation(    input = "vector",    linearoutput = "vector",    squashedoutput = "vector",    hasbias = "logical",    bias = "vector",    weights = "vector",    gains = "matrix",    squashfcn = "closure",    squashfcnderivative = "closure"  ) )  # constructors neuralnetworklayer <- function(ninput,noutput,hasbias=true,squashfcn,squashfcnderivative) {   nc = list(     input = c(rep(na,ninput)),     linearoutput = c(rep(na,noutput)),     squashedoutput = c(rep(na,noutput)),     hasbias = hasbias,     bias = c(rep(na,noutput)),     weights = c(rep(na,noutput)),     gain = matrix(data=weights, nrow = ninput, ncol = noutput),     squashfcn = squashfcn,   # source of warning / error     squashfcnderivative = squashfcnderivative,      = function(x) nc[[x]],     set = function(x, value) nc[[x]] <<- value,     props = list()   )   #add few more functions   nc$addprop = function(name, value) {     p <- nc$props     p[[name]] <- value     assign('props', p, envir=nc)   }   nc <- list2env(nc)   class(nc) <- "neuralnetwork"   return(nc) }    tanhderivative <- function(x) {     d = 1 - tan(x)^2       return(d)   }    test <- neuralnetworklayer(ninput=4,noutput=5,hasbias=true,                              squashfcn=tanh,squashfcnderivative=tanhderivative) 

the messages generated are

warning message: undefined slot classes in definition of "neuralnetworklayer": squashfcn(class "closure"), squashfcnderivative(class "closure")  error in as.vector(x, mode) :    cannot coerce type 'closure' vector of type 'any'  

both messages indicate base class closure can not used slot. how pass function?

taking advice 2 answers, following code can generated. addresses original question of passing function slot, , using function. completeness, revised neural network layer class present.

setclass(   class="neuralnetworklayer",   representation=representation(     ninput = "numeric",     noutput = "numeric",     squashfcn = "function",     derivsquashfcn = "function",     gains = "matrix",     hasbias = "logical",     bias = "matrix",     linoutput = "matrix",     squashoutput = "matrix"   ) ) getclass("neuralnetworklayer") getslots("neuralnetworklayer")  sf <- function(x){   f = tanh(x)   return(f) } dsf <- function(x) {   d = 1 - tan(x)^2     return(d) }  # create object of class  hh = new("neuralnetworklayer",squashfcn=sf,ninput=5,noutput=5,hasbias=true,                         derivsquashfcn = dsf)   hh@squashfcn(3) hh@derivsquashfcn(3) 

the error/warning :

 undefined slot classes in definition of "neuralnetworklayer": squashfcn(class "closure") 

means slot not defined beacuse type "closure" not defined.

you try define slot(attribute) generic function 1 idea use any ( default slot think) type:

neuralnetworklayer <-  setclass(   class="neuralnetworklayer",   representation=representation(     squashfcn = "any"   ) ) 

then ,for example instantiate class :

# constructors hh = neuralnetworklayer(squashfcn=function(x)print(x)) ## dummy function here  hh@squashfcn(10) [1] 10 

that's said , think should consider define functions slots real method(see setmethod) . methods have typed (safer) objects otheriwse there no reason reason use s4 system , easier use s3 method.


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -