TypeError:“ InputLayer”类型的对象没有len()

时间:2019-09-11 20:17:34

标签: python tensorflow keras neural-network

我想从另一个CNN构建一个CNN,以提取图像的特征向量。这个想法只是采用第一个CNN的前13层,并使用这些层构建第二个。

我正在使用带有GPU的Google Colab Notebook

from keras.models import Model

layer_input_f_nmist = model_f_mnist.input
layer_outputs = [layer.output for layer in model_f_mnist.layers[:13]] 

model_mnist_featured = Model(inputs = layer_input_f_nmist, outputs = layer_outputs)

featured_f_mnist_train = model_mnist_featured.predict(X_f_mnist_train)
TypeError                                 Traceback (most recent call last)

<ipython-input-74-ceb627fe9c83> in <module>()
      4 layer_outputs = [layer.output for layer in model_f_mnist.layers[:13]]
      5 
----> 6 model_mnist_featured = Model(inputs = layer_input_f_nmist, outputs = layer_outputs)
      7 
      8 featured_f_mnist_train = model_mnist_featured.predict(X_f_mnist_train)

4 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)
   1400 
   1401         # Propagate to all previous tensors connected to this node.
-> 1402         for i in range(len(node.inbound_layers)):
   1403             x = node.input_tensors[i]
   1404             layer = node.inbound_layers[i]

TypeError: object of type 'InputLayer' has no len()

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。我已使用for(int i=0;i<10;++i) { sum += grades[i]; avg = sum / (i+1); printf ("After adding lecture %u --> sum: %.1f + average: %.1f\n", i+1,sum , avg); } 在google colab中实现该模型。 keras.layers的输出和输入与tf.keras.layers的输出和输入不匹配。我没看过模特的背景。当我使用tf.keras.layerstf.keras.models.Modelinputs = classifier.input而不是Model(outputs = layer_outputsinputs = classifier.input)时,问题解决了。我建议您在使用Google colab时使用outputs = layer_outputs属性而不是tf.keras.*