No Inbound Nodes - Keras CNN Model

ⅰ亾dé卋堺 提交于 2020-06-28 05:28:08

问题


I had trained a CNN model in keras with the following structure

model_11 = Sequential()

#Convolutional Layers
model_11.add(Reshape((55, 1)))
model_11.add(Conv1D(50, kernel_size=5, strides=1, padding="same", activation = 'relu'))
model_11.add(Conv1D(24, kernel_size=4, strides=5, padding="same", activation = 'relu'))
model_11.add(Conv1D(23, kernel_size=2, strides=1, padding="same", activation = 'relu'))

#Dense Layers
model_11.add(Flatten())
model_11.add(Dense(units=30, activation='relu'))
model_11.add(Dense(units=15, activation='relu'))

model_11.add(Dense(units=1, activation='sigmoid'))

#Compile model
model_11.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

#Fit the model
model_11.fit(X_train, y_train, epochs=20, batch_size=20)



Now, I tried the following

model_11.layers[-3].output



Which gives me the following error

AttributeError: Layer dense_40 has no inbound nodes.



There are many solutions regarding multiple inbound nodes, but I haven't seen anything so far for no inbound nodes. And despite that, the model is working well (binary classification).


回答1:


This is because when you define a Sequential without specifying the input shape for the first layer, the computation graph is only created during the fit function, and thus layers' input and output tensors (and thus nodes) are not computed.

If you need to access output tensor of a layer, specify the input shape for the first layer in the sequential model. Thus the first layer is defined as this:

model_11.add(Reshape((55, 1), input_shape=(55,))

Now model_11.layers[-3].output will return a tensor.



来源:https://stackoverflow.com/questions/53789858/no-inbound-nodes-keras-cnn-model

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