dsean2112 / cardio_ml

1 stars 1 forks source link

Alternative model #4

Open shah-in-boots opened 1 month ago

shah-in-boots commented 1 month ago

https://github.com/dsean2112/cardio_ml/blob/79573df42bca07c30ba98c9052aa92736afb43c8/bilstm_spectro_train.R#L30C1-L34C71

# Input layer
# 5000 steps and 1 channel at a time
input_shape <- c(5000, 1)
n_classes <- 4
inputs <- layer_input(shape = input_shape)

# Convolutional Block
conv_block <- 
    inputs |>
    layer_conv_1d(filters = 64, kernel_size = 5, activation = "relu", padding = "same") |>
    layer_batch_normalization() |>
    layer_max_pooling_1d(pool_size = 2)

# BiLSTM Block
bilstm_block <- 
    conv_block |>
    bidirectional(layer_lstm(units = 64, return_sequences = TRUE))

# Self Attention Block
attn_block <- 
    bilstm_block |>
    layer_dense(units = 1, activation = "tanh") |>
    layer_flatten() |>
    layer_activation("softmax") |>
    layer_repeat_vector(128) |>  
    layer_permute(c(2, 1))

mult_attn_block <- layer_multiply(list(bilstm_block, attn_block))

# Time Distributed Dense Layer
outputs <- 
    mult_attn_block |>
    time_distributed(layer_dense(units = n_classes, activation = "softmax"))

# Create and compile the model
cnn_bilstm_attn_model <- keras_model(inputs = inputs, outputs = outputs)

cnn_bilstm_attn_model |> compile(
    optimizer = optimizer_adam(),
    loss = "categorical_crossentropy",
    metrics = c("accuracy")
)

Could you trial something like this? The only thing that I'm a little fuzzy on is using the 24 channels from the spectrogram data. Could split it into two channels at a time, but also, this may work without spectrograms and may be just fine.

shah-in-boots commented 1 month ago

@dsean2112 As above. Also sending you as a sample R script