【问题标题】:Object of Type 'Dense' has no len()“密集”类型的对象没有 len()
【发布时间】:2019-12-26 09:28:18
【问题描述】:

我尝试创建 CNN 模型,但总是收到此错误消息。

错误:TypeError Traceback (最近一次调用最后一次) in () ----> 1 model = simple_conv_model() 5 frames /usr/local/lib/python3.6/dist-packages/keras/engine/network. py in build_map(tensor, finished_nodes, nodes_in_progress, layer, node_index, tensor_index) 1345 1346 # 传播到连接到该节点的所有先前张量。 -> 1347 for i in range(len(node.inbound_layers)): 1348 x = node.input_tensors[i] 1349 layer = node.inbound_layers[i] TypeError: 'Dense' 类型的对象没有 len()

这是模型:

def simple_conv_model():
        input_layer=layers.Input(shape=(64,64,3), name="input_layer")    
        model=layers.Conv2D(16,3, activation="relu", padding='same', name="first_block_conv", strides=(1,1)) (input_layer)
        model=layers.MaxPooling2D((2,2), name="first_block_pooling") (model)
        model=layers.BatchNormalization(name="first_block_bn") (model)

        model=layers.Conv2D(32,3, activation="relu", padding='same', name="second_block_conv", strides=(1,1)) (input_layer)
        model=layers.MaxPooling2D((2,2), name="second_block_pooling") (model)
        model=layers.BatchNormalization(name="second_block_bn") (model)

        model=layers.Conv2D(64,3, activation="relu", padding='same', name="third_block_conv", strides=(1,1)) (input_layer)
        model=layers.MaxPooling2D((2,2), name="third_block_pooling") (model)
        model=layers.BatchNormalization(name="third_block_bn") (model)

        model=layers.Flatten() (model)
        model=layers.Dense(16, activation="relu", name="dense_1") (model)
        model=layers.BatchNormalization() (model)
        model=layers.Dropout(0.5, name="drop_out_dense_1") (model)

        model=layers.Dense(4, activation="relu", name="dense_2") (model)

        model=layers.Dense(1, activation="linear") (model)

        model_cnn = Model(input_layer, model)
        model_cnn.compile(loss="mean_absolute_percentage_error", optimizer="adam")

        return model_cnn

    model = simple_conv_model()

【问题讨论】:

  • 错误:TypeError Traceback(最近一次调用最后一次) in () ----> 1 model = simple_conv_model() 5 frames /usr/local /lib/python3.6/dist-packages/keras/engine/network.py in build_map(tensor, finished_nodes, nodes_in_progress, layer, node_index, tensor_index) 1345 1346 # 传播到连接到该节点的所有先前张量。 -> 1347 for i in range(len(node.inbound_layers)): 1348 x = node.input_tensors[i] 1349 layer = node.inbound_layers[i] TypeError: 'Dense' 类型的对象没有 len()
  • 请在问题本身中包含回溯,您可以随时编辑自己的问题。确保回溯是完整的。

标签: python tensorflow keras conv-neural-network


【解决方案1】:

我之前也遇到过这个问题,因为我从 tensorflow.python.keras 导入库。改用keras就行了

【讨论】:

    【解决方案2】:

    您可能错误地将input_layer 用作所有三个Conv2D 层的输入,而您很可能打算编写如下内容:

    model = layers.Conv2D(16,3,...)(input_layer) # this is correct
    # ...
    model = layers.Conv2D(32, 3, ...)(model)  # pass `model` here as input
    # ...
    model = layers.Conv2D(64, 3, ...)(model)  # pass `model` here as input
    

    【讨论】:

    • 不,我很确定他是故意这样做的:他正在做一个初始网络(就像我一样,我也遇到了这个问题)
    【解决方案3】:

    即使使用要素层和功能 API 制作简单的网络,我也使用了相同的错误。在@patacharapon 的对面,

    我换了

    import keras
    

    这里的keras版本是2.3.1

    from tensorflow import keras
    

    这里的 keras 版本是2.3.0-tf

    【讨论】:

      【解决方案4】:

      我将 keras.models 替换为 tensorflow.keras.models,它对我有用。

      之前:

      from keras.models import Model
      

      更换后:

      from tensorflow.keras.models import Model
      

      【讨论】:

        【解决方案5】:

        这里提供了正确的解决方案:https://www.programmersought.com/article/14464784093/ 即从tenorflow.keras.models而不是keras.models导入模型

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-08-21
          • 2013-03-14
          • 2022-01-08
          • 1970-01-01
          • 1970-01-01
          • 2015-08-22
          • 2019-05-23
          • 2018-07-20
          相关资源
          最近更新 更多