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

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

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