'Dense'类型的对象没有len() [英] Object of Type 'Dense' has no len()
问题描述
我尝试创建一个CNN模型,但总是收到此错误消息。
I try to create a CNN model, but always get this error message.
错误:TypeError跟踪(最近一次调用)在()----> 1模型= simple_conv_model()5帧/usr/local/lib/python3.6/dist-packages/keras/engine/network.py在build_map中(张量,finished_nodes,nodes_in_progress,图层,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:密集类型的对象没有len()
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()
这是模型:
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()
推荐答案
我之前也遇到过这个问题,因为我是从tensorflow.python.ke导入库的ras。只是改为使用keras
I also face this problem before because I import library from tensorflow.python.keras. Just change to use keras instead
这篇关于'Dense'类型的对象没有len()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!