I have constructed an example of a DNN-based model using the following code in R.
library(keras)
library(caret)
data(iris)
iris_df <- iris
rm(iris)
iris_df$Species <- as.factor(iris_df$Species)
set.seed(42)
splitIndex <- createDataPartition(iris_df$Species, p = 0.8, list = FALSE)
train_data <- iris_df[splitIndex, ]
test_data <- iris_df[-splitIndex, ]
train_labels <- keras::to_categorical(as.integer(train_data$Species) - 1, num_classes = 3)
test_labels <- to_categorical(as.integer(test_data$Species) - 1, num_classes = 3)
model <- keras_model_sequential()
# Input Layer
model %>%
layer_dense(units = 3, activation = 'sigmoid', input_shape = ncol(train_data) - 1)
# Hidden Layers
for (i in 1:8) {
units <- sample(20:60, 1) # Select one random number from 20 to 60
model %>%
layer_dense(units = units, activation = 'tanh') %>%
layer_dropout(rate = 0.5) # Dropout layer to prevent overfitting
}
# Output Layer
model %>%
layer_dense(units = 3, activation = 'softmax')
# Model Compile
model %>% compile(
optimizer = optimizer_adam(), # Adam optimizer
loss = 'categorical_crossentropy', # Categorical cross-entropy loss
metrics = c('accuracy')
)
However, I keep encountering errors when attempting to plot the framework of the model. What could be the issue?
I would like to draw the diagram as follows:
