没有入站节点-Keras CNN模型 [英] No Inbound Nodes - Keras CNN Model
问题描述
我在喀拉拉邦训练了CNN模型,结构如下:
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:层density_40没有入站节点.
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).
推荐答案
这是因为当您定义Sequential
而不指定第一层的输入形状时,仅在fit
函数期间创建计算图,因此,不计算图层的输入和输出张量(以及节点).
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,))
现在model_11.layers[-3].output
将返回张量.
这篇关于没有入站节点-Keras CNN模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!