8.3 How to use keras3
keras3 is a high-level neural networks API, written in Python and capable of running on top of TensorFlow, CNTK, or Theano. It allows for easy and fast prototyping, supports both convolutional networks and recurrent networks, and runs seamlessly on CPU and GPU.
8.3.1 General Infection
In this example, we will use the keras3
package to build a simple neural network model
to predict the number of cases of a general infection based on various features.
We have seen how to simulate infection with the SEIR
model. We will use the same model function and update the parameters obtained by training a neural network model.
The function we will be using are:
keras_model_sequential()
layer_dense()
layer_activation()
compile()
fit()
The scenario is as follows:
We have a dataset with the number of cases of a general infection and various features
We build a neural network model to update the SEIR parameters based on certain social interaction features with
keras3
:adjusted_parameters["beta"] * (1 + mean(predicted_infections))
Apply the new model to predict the number of infections based on social interaction features
8.3.2 Neural Network Model
Given an input vector x=[x1,x2,...,xp]
- First Dense Layer Transformation:
z(1)=p∑i=1W(1)ixi+b(1)
Activation Function: a(1)=ReLu(z(1))=max(0,z(1))
Second Dense Layer Transformation: z(2)=p∑i=1W(2)ixi+b(2)
Output Activation (Sigmoid): a(2)=Sigmoid(z(2))=11+e−z(2)
8.3.3 Example Code
model <- keras_model_sequential(input_shape = c(p))
# simple model
model %>%
layer_dense(units = 1) %>%
layer_activation("relu") %>%
layer_dense(units = 1, activation = "sigmoid")
Compile the model with a binary crossentropy loss function and an Adam optimizer to match the difference between original data and the model output, and apply model adjustments.
model %>% compile(loss = "binary_crossentropy",
optimizer = optimizer_adam(),
metrics = c("accuracy"))
To fit the model to the data, we will use the fit()
function, this is usually called history
:
history <- model %>% fit(x = as.matrix(social_data[, 1:p]),
y = social_data$infection,
epochs = 30,
batch_size = 128,
validation_split = 0.2
)
History object contains the training and validation loss and accuracy for each epoch.
Finally, we adjust the output parameters of the SEIR model using the predicted values from the neural network model: