keras 合并多个层导致 AttributeError: ‘NoneType’ object has no attribute ‘_inbound_nodes’

我在我的CNN中尝试添加一些固定的,请看我下面的代码。

这是我创建核的方式:

# Kernelsdef create_kernel(x):    t = pipe(        x,         lambda x: tf.constant(x, dtype=tf.float32),         lambda x: tf.reshape(x, [3, 3, 1, 1]))    return tk_edge1 = create_kernel([1, 0, -1, 0, 0, 0, -1, 0, 1])k_edge2 = create_kernel([0, 1, 0, 1, -4, 1, 0, 1, 0])k_edge3 = create_kernel([-1, -1, -1, -1, 8, -1, -1, -1, -1])

我的卷积网络如下:

# Convolution network# Input layerl_input = Input(shape=(28**2, ))# Reshape layerl_reshape = Reshape(target_shape=(28, 28, 1))(l_input)# Convolution layersl_conv1 = Conv2D(filters=20, kernel_size=(3, 3), padding='valid')(l_reshape)l_edge1 = tf.nn.conv2d(l_reshape, k_edge1, strides=[1, 1, 1, 1], padding='VALID')l_edge2 = tf.nn.conv2d(l_reshape, k_edge2, strides=[1, 1, 1, 1], padding='VALID')l_edge3 = tf.nn.conv2d(l_reshape, k_edge3, strides=[1, 1, 1, 1], padding='VALID')l_conv1a = Concatenate(axis=3)([l_conv1, l_edge1, l_edge2, l_edge3])  # <- 错误应该由这一行引起。l_conv2 = Conv2D(filters=20, kernel_size=(3, 3), padding='valid')(l_conv1a)l_pool1 = MaxPooling2D(pool_size=(2, 2), border_mode='valid')(l_conv2)# Flatten layerl_flat = Flatten()(l_pool1)# Fully connected layersl_fc1 = Dense(50, kernel_initializer='he_normal')(l_flat)l_act1 = PReLU()(l_fc1)l_fc3 = Dense(10, kernel_initializer='he_normal')(l_act1)l_output = Activation('softmax')(l_fc1)# Modelcnn_model = Model(l_input, l_output)

然而,我得到了以下错误:

Traceback (most recent call last):File "<stdin>", line 2, in <module>File "C:\Users\Perry Cheng\AppData\Local\conda\conda\envs\ml_py_3_6\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper    return func(*args, **kwargs)File "C:\Users\Perry Cheng\AppData\Local\conda\conda\envs\ml_py_3_6\lib\site-packages\keras\engine\network.py", line 93, in __init__    self._init_graph_network(*args, **kwargs)File "C:\Users\Perry Cheng\AppData\Local\conda\conda\envs\ml_py_3_6\lib\site-packages\keras\engine\network.py", line 237, in _init_graph_network    self.inputs, self.outputs)File "C:\Users\Perry Cheng\AppData\Local\conda\conda\envs\ml_py_3_6\lib\site-packages\keras\engine\network.py", line 1353, in _map_graph_network    tensor_index=tensor_index)File "C:\Users\Perry Cheng\AppData\Local\conda\conda\envs\ml_py_3_6\lib\site-packages\keras\engine\network.py", line 1340, in build_map    node_index, tensor_index)File "C:\Users\Perry Cheng\AppData\Local\conda\conda\envs\ml_py_3_6\lib\site-packages\keras\engine\network.py", line 1340, in build_map    node_index, tensor_index)File "C:\Users\Perry Cheng\AppData\Local\conda\conda\envs\ml_py_3_6\lib\site-packages\keras\engine\network.py", line 1340, in build_map    node_index, tensor_index)[Previous line repeated 2 more times]File "C:\Users\Perry Cheng\AppData\Local\conda\conda\envs\ml_py_3_6\lib\site-packages\keras\engine\network.py", line 1312, in build_map    node = layer._inbound_nodes[node_index]AttributeError: 'NoneType' object has no attribute '_inbound_nodes'

经过一些测试,我认为错误来自于:

l_conv1a = Concatenate(axis=3)([l_conv1, l_edge1, l_edge2, l_edge3])

有什么方法可以解决这个问题吗?


回答:

Keras层接受Keras张量而不是普通的张量作为输入。因此,如果你想在Keras中使用tf.nn.conv2d而不是Conv2D层,你需要将它们包装在Lambda层中:

l_edge1 = Lambda(lambda x: tf.nn.conv2d(x, k_edge1, strides=[1, 1, 1, 1], padding='VALID'))(l_reshape)l_edge2 = Lambda(lambda x: tf.nn.conv2d(x, k_edge2, strides=[1, 1, 1, 1], padding='VALID'))(l_reshape)l_edge3 = Lambda(lambda x: tf.nn.conv2d(x, k_edge3, strides=[1, 1, 1, 1], padding='VALID'))(l_reshape)

Related Posts

在使用k近邻算法时,有没有办法获取被使用的“邻居”?

我想找到一种方法来确定在我的knn算法中实际使用了哪些…

Theano在Google Colab上无法启用GPU支持

我在尝试使用Theano库训练一个模型。由于我的电脑内…

准确性评分似乎有误

这里是代码: from sklearn.metrics…

Keras Functional API: “错误检查输入时:期望input_1具有4个维度,但得到形状为(X, Y)的数组”

我在尝试使用Keras的fit_generator来训…

如何使用sklearn.datasets.make_classification在指定范围内生成合成数据?

我想为分类问题创建合成数据。我使用了sklearn.d…

如何处理预测时不在训练集中的标签

已关闭。 此问题与编程或软件开发无关。目前不接受回答。…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注