## if keras package has not been installed, install it.                         
if(!require(keras)) {install.packages("keras"); library(keras)}
#[1]-----
tmp <- read.csv("DatafingerprintSub.csv", header=TRUE)
## choose target                                                                
target1 <- scale(tmp[,"pKa"])
## remove the target variables             
data1_org <- as.matrix(tmp[, -1])
## remove variables with no variation                                           
is_nv <- apply(data1_org, 2, function(xxx){
  return(var(xxx) == 0)
})
data1_v <- data1_org[, !is_nv]
## scale each variable                                                          
data1 <- apply(data1_v, 2, scale)
nc <- ncol(data1); nr <- nrow(data1)
#[2]-----
model1 <- keras_model_sequential()
model1 %>%
  layer_dense(units=20, input_shape=nc) %>% 
  layer_activation_relu() %>%
  layer_dense(units=1)
pred1 <- predict(model1, data1)
model1 %>% compile(loss="mean_squared_error", optimizer=optimizer_adagrad(learning_rate=0.01))
#[3}-----
hist1 <- fit(model1, data1, target1, epochs=20, validation_split=0.1, verbose=1)
#[4]-----
matplot(cbind(hist1$metrics$loss, hist1$metrics$val_loss), type="l", ylab="loss")
legend("topright", legend=c("Train", "Test"), lty=1:2, col=1:2)

