如何用keras功能API描述模型?

时间:2020-06-21 13:54:18

标签: r keras

我正在学习keras Functional API,并试图描述“一种”残留神经网络。 这是我建立模型的方式:

  1. 具有INPUT的策略,例如“ K_input”,
  2. 使用5 * 5内核对它进行卷积,并以max_pooling的形式将其作为“ CV1”,
  3. 使用原始的INPUT做更多常规的污染,例如“ CV2”,
  4. 连接CV1和CV2,并应用于密集层和分类器。
  5. 使用“ keras_model”制作模型。

当移动到#5时,它给出错误消息,如下所示:

 Error in py_call_impl(callable, dots$args, dots$keywords) : 
  ValueError: Graph disconnected: cannot obtain value for tensor Tensor("input_2:0", shape=(?, 28, 28, 1), dtype=float32) at layer "input_2". The following previous layers were accessed without issue: [] 

这是什么意思? 我试图通过在CV1上应用一维转换来将CV1和CV2的形状调整为c(14、14、16),但是它不起作用。 感谢您的建议。

    K_input <- layer_input(c(28, 28,1), name = "INPUT")
    
    CV1 <- 
      K_input %>% 
      layer_conv_2d(filters = 4, c(5,5), activation = "relu", padding = "same", name = "CV1_CV1") %>% 
      layer_max_pooling_2d(pool_size = c(2,2), name = "CV1_MX1") 
    
    summary(CV1) 
    #Tensor("CV1_MX1_1/MaxPool:0", shape=(?, 14, 14, 4), dtype=float32)
    
    CV2 <- 
      K_input %>% 
      layer_conv_2d(filters = 4, c(3,3), activation = "relu", padding = "same", name = "CV2_CV1") %>% 
      layer_conv_2d(filters = 8, c(3,3), activation = "relu", padding = "same", name = "CV2_CV2") %>% 
      layer_conv_2d(filters = 16, c(3,3), activation = "relu", padding = "same", name = "CV2_CV3") %>% 
      layer_max_pooling_2d(c(2,2), name = "CV2_MX1") 
    
    summary(CV2)
    #Tensor("CV2_MX1_1/MaxPool:0", shape=(?, 14, 14, 16), dtype=float32)
    
    
    K _output <- 
      layer_concatenate(c(CV1, CV2) , name = "OUT_C") %>% 
      layer_flatten(name = "OUT_FLAT") %>% 
      layer_dense(units = 32, activation = "relu", name = "OUT_D1") %>% 
      layer_dense(units = 10, activation = "softmax", name = "OUT_D2")  
    
    summary(K_output) 
    
    model_API2 <- keras_model(inputs = K_input, outputs = K_output)
    # When error msg comes
    # Error in py_call_impl(callable, dots$args, dots$keywords) : 
    # ValueError: Graph disconnected: cannot obtain value for 
    # tensor Tensor("input_2:0", shape=(?, 28, 28, 1), dtype=float32) at layer "input_2". 
    # The following previous layers were accessed without issue: [] 

0 个答案:

没有答案