【问题标题】:Building a multi input and multi output model: giving AttributeError: 'dict' object has no attribute 'shape'构建多输入多输出模型:给出 AttributeError: 'dict' object has no attribute 'shape'
【发布时间】:2020-05-30 03:25:18
【问题描述】:

我是深度学习的新手,并尝试在 R 中使用 keras 功能 API 构建多输入和多输出模型并收到此错误。

我的代码

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)
)

这会导致错误 py_call_impl(callable, dots$args, dots$keywords) : AttributeError: 'dict' 对象没有属性 'shape'

谁能帮我理解错误。谢谢。

【问题讨论】:

    标签: python r tensorflow keras deep-learning


    【解决方案1】:

    此错误是由于输入数据格式造成的。我的输入数据之一是数据框,当转换为矩阵类型时它工作正常。

    希望对他人有所帮助。

    【讨论】:

      猜你喜欢
      • 2021-01-08
      • 2021-07-25
      • 2015-09-26
      • 1970-01-01
      • 2022-11-18
      • 2017-03-08
      • 1970-01-01
      • 2022-11-09
      • 2019-09-23
      相关资源
      最近更新 更多