Object of Type 'Dense' has no len()

你说的曾经没有我的故事 提交于 2020-05-17 08:46:17

问题


I try to create a CNN model, but always get this error message.

Error: TypeError Traceback (most recent call last) 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 # Propagate to all previous tensors connected to this node. -> 1347 for i in range(len(node.inbound_layers)): 1348 x = node.input_tensors[i] 1349 layer = node.inbound_layers[i] TypeError: object of type 'Dense' has no len()

This is the model:

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

回答1:


I also face this problem before because I import library from tensorflow.python.keras. Just change to use keras instead




回答2:


You are probably mistakenly using input_layer as the input of all the three Conv2D layers, whereas it is very likely that you intended to write something like this:

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


来源:https://stackoverflow.com/questions/57585900/object-of-type-dense-has-no-len

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!