'Issue Rewriting Backpropagation Network to Use Keras
I'm trying to rewrite my code from neuralnet to use Keras, but I'm having issues getting the same results (R/Rstudio) :(.
My code first creates an array of dimensions c(n, nvar, ndata) where n = # observations, nvar = # inputs+1, and ndata = # simulations. e.g., for simplicity, c(500,3,500) creates 500 matrices of dim 3x500, filled with 0. next, i fill each matrix in the array with a random binomial distribution of 0 and 1. for example, the matrix looks like this, with the columns labeled x1, x2, y where x1 and x2 are the inputs and y is the output/label.
The details of this part aren't really important -- the result is a matrix that looks like the following, which is fed into my neural network.

The neural network is a backpropagation with a structure of (for example) inputs x1,x2 1 hidden layer of 5 hidden units, and then an output layer. the way I write this using neuralnet is as follows:
nnet <- neuralnet(y~x1+x2, currentmatrix, hidden=5, stepmax = 10^7,err.fct = "ce",linear.output = F,likelihood = T)
predictions <- predict(nnet,currentmatrix[,1:nvar-1])
Where currentmatrix is the selected matrix in the array. I enclose the network in a for loop of ndata=500 iterations -- so each matrix in the array is fitted once and the predictions (and resulting calculations) stored.
Now I'm trying to write this in Keras/tf and my code is as follows:
model <- keras_model_sequential() %>%
layer_dense(units = 5, activation = "sigmoid", input_shape = c(2)) %>% #logistic, input
layer_dense(units = 1, activation = "sigmoid") #output
model %>% compile(
optimizer = "rmsprop",
loss = "binary_crossentropy",
metrics = c("accuracy")
)
for(i in 1:ndata){
... #select matrix
currentmatrix <- datas[,,i]
trainee <- currentmatrix[,-3]
trainee <- as.matrix(array_reshape(trainee, c(500,2)))
y1 <- y
dim(y1) <- c(500,1)
history <- model %>% fit(
trainee,
y1,
epochs = 2,
batch_size = 50,
)
predictions <- model %>% predict(currentmatrix[,1:nvar-1])
}
But for some reason this isn't returning the same results as my neuralnet code and I'm not sure why. I think it has to do with either how I'm creating/fitting the model, or how I'm inputting the data into the Keras model. Can anyone help? I'd be happy to post my full code :)).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
