Categories
Development

Building a multi input and multi output model: giving AttributeError: ‘dict’ object has no attribute ‘shape’

I am newbie to deep learning and trying to build the multi input and multi output model using keras functional API in R and getting this error.

My code

build_model <- function() {
    ### motif module

    # motif module input
    motif_module_input <- layer_input(shape = ncol(x_train_motif_module), name = 'motif_module_input')

    motif_module <- motif_module_input %>%
        layer_dense(units = 16, activation = 'relu') %>%
        layer_dropout(rate = 0.5) %>%
        layer_dense(units = 16, activation = 'relu') %>%
        layer_dropout(rate = 0.5) %>%
        layer_dense(units = 16, activation = 'relu') %>%
        layer_dropout(rate = 0.5) %>%
        layer_dense(units = 4, activation = 'relu')

    motif_module_output <- motif_module %>% 
        layer_dense(units = 2, activation = 'softmax', name = 'motif_module_output')

    #SVM_input <- layer_input(shape = c(2), name = 'SVM_input')

    ### DNA module
    # DNA module input
    DNA_module_input <- layer_input(shape = c(user_img_rows, user_img_cols), name = 'DNA_module_input')

    DNA_module <- DNA_module_input %>%
        layer_conv_1d(filters = 128, kernel_size = 7, activation = 'relu', input_shape = c(user_img_rows, user_img_cols)) %>%
        layer_max_pooling_1d(pool_size = 4, strides = 2) %>%
        layer_dropout(rate = 0.3) %>%

        layer_conv_1d(filters = 128, kernel_size = 5, activation = 'relu', input_shape = c(user_img_rows, user_img_cols)) %>%
        layer_max_pooling_1d(pool_size = 8, strides = 4) %>%
        layer_dropout(rate = 0.3) %>%

        layer_flatten() %>%
        layer_dense(units  = 128, activation  = 'relu') 

    DNA_module_output <- DNA_module %>% 
        layer_dense(units = 2, activation = 'softmax', name = 'DNA_module_output')


    main_output <- layer_concatenate(c(motif_module, DNA_module)) %>%  
        layer_dense(units = 16, activation = 'relu') %>%
        layer_dense(units = 4, activation = 'relu') %>% 
        layer_dense(units = 2, activation = 'softmax', name = 'main_output')


    model <- keras_model(
        inputs = c(motif_module_input, DNA_module_input), 
        outputs = c(motif_module_output, DNA_module_output, main_output)
    )

    model %>% compile(
        loss = list(motif_module_output = 'binary_crossentropy', DNA_module_output = 'binary_crossentropy', main_output = 'binary_crossentropy'),
        loss_weights = list(motif_module_output = 0.5, DNA_module_output = 0.5, main_output = 1),
        optimizer = optimizer_adam(lr = 0.001, beta_1 = 0.9, beta_2 = 0.999, epsilon = 1e-08, decay = 1e-06),
        metrics   = c('accuracy')
    )

    model

}

model <- build_model()
# training the model
# Display training progress by printing a single dot for each completed epoch.
print_dot_callback <- callback_lambda(
    on_epoch_end = function(epoch, logs) {
        if (epoch %% 80 == 0) cat("\n")
        cat(".")
    }
) 

# The patience parameter is the amount of epochs to check for improvement.
early_stop <- callback_early_stopping(monitor = "val_loss", patience = 5, restore_best_weights = FALSE)

# model <- build_model()
history <- model %>% fit(
    x = list(motif_module_input = x_train_motif_module, DNA_module_input = x_train_DNA_module),
    y = list(motif_module_output = y_train_DNA_module, DNA_module_output = y_train_DNA_module, main_output = y_train_DNA_module),
    epochs = user_epochs,
    validation_split = user_validation_split,
    batch_size = user_batch_size, 
    use_multiprocessing = TRUE,
    verbose = 2,
    callbacks = list(early_stop, print_dot_callback)
)

This results in error py_call_impl(callable, dots$args, dots$keywords) :
AttributeError: ‘dict’ object has no attribute ‘shape’

Can some one help me understand the error. Thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *