keras 连接多个层导致 AttributeError: 'NoneType' 对象没有属性 '_inbound_nodes'

佩里

我正在尝试在我的 CNN 中添加一些固定内核,请参阅下面的代码。

这就是我创建内核的方式:

# Kernels
def create_kernel(x):
    t = pipe(
        x, 
        lambda x: tf.constant(x, dtype=tf.float32), 
        lambda x: tf.reshape(x, [3, 3, 1, 1]))
    return t

k_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 layer
l_input = Input(shape=(28**2, ))
# Reshape layer
l_reshape = Reshape(target_shape=(28, 28, 1))(l_input)
# Convolution layers
l_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])  # <- The error should be caused by this line.
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 layer
l_flat = Flatten()(l_pool1)
# Fully connected layers
l_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)

# Model
cnn_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)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

具有连接 AttributeError 的 Keras:'NoneType' 对象没有属性 '_inbound_nodes'

来自分类Dev

keras 'NoneType' 对象没有属性 '_inbound_nodes'

来自分类Dev

错误:“NoneType”对象在连接层时没有属性“_inbound_nodes”(多输入模型)

来自分类Dev

如何修复 AttributeError: 'NoneType' 对象没有使用曼哈顿距离创建 lstm 模型时出现的属性 '_inbound_nodes'?

来自分类Dev

'NoneType'对象没有属性'_inbound_nodes'错误

来自分类Dev

Keras导入模型AttributeError的:“NoneType”对象有没有属性“运”

来自分类Dev

AttributeError:'NoneType'对象没有属性'ravel'

来自分类Dev

AttributeError:'NoneType'对象没有属性'endswith'

来自分类Dev

AttributeError:'NoneType'对象没有属性'split'

来自分类Dev

AttributeError:'NoneType'对象没有属性'replace'

来自分类Dev

AttributeError:'NoneType'对象没有属性'iterrows'

来自分类Dev

“ AttributeError:'NoneType'对象没有属性'insert'”

来自分类Dev

AttributeError:'NoneType'对象没有属性'tbody'

来自分类Dev

AttributeError:'NoneType'对象没有属性'pencolor'

来自分类Dev

AttributeError:“ NoneType”对象没有属性“ head”

来自分类Dev

AttributeError:'NoneType'对象没有属性'roles'

来自分类Dev

AttributeError:'NoneType'对象没有属性'nrows'

来自分类Dev

AttributeError:'NoneType'对象没有属性'upper'

来自分类Dev

AttributeError:'NoneType'对象没有属性'endswith'

来自分类Dev

AttributeError:'NoneType'对象没有属性'get'

来自分类Dev

AttributeError:'NoneType'对象没有属性'current'

来自分类Dev

AttributeError:'NoneType'对象没有属性'browse'

来自分类Dev

AttributeError: 'NoneType' 对象没有属性 'group'

来自分类Dev

AttributeError: 'NoneType' 对象没有属性 'foo'

来自分类Dev

AttributeError: 'NoneType' 对象没有属性 'send'

来自分类Dev

AttributeError: 'NoneType' 对象没有属性 'setText'

来自分类Dev

AttributeError: 'NoneType' 对象没有属性 'scenePos'

来自分类Dev

AttributeError: 'NoneType' 对象没有属性 'persist'

来自分类Dev

model.fit()Keras分类多输入-单输出给出错误:AttributeError:'NoneType'对象没有属性'fit'

Related 相关文章

  1. 1

    具有连接 AttributeError 的 Keras:'NoneType' 对象没有属性 '_inbound_nodes'

  2. 2

    keras 'NoneType' 对象没有属性 '_inbound_nodes'

  3. 3

    错误:“NoneType”对象在连接层时没有属性“_inbound_nodes”(多输入模型)

  4. 4

    如何修复 AttributeError: 'NoneType' 对象没有使用曼哈顿距离创建 lstm 模型时出现的属性 '_inbound_nodes'?

  5. 5

    'NoneType'对象没有属性'_inbound_nodes'错误

  6. 6

    Keras导入模型AttributeError的:“NoneType”对象有没有属性“运”

  7. 7

    AttributeError:'NoneType'对象没有属性'ravel'

  8. 8

    AttributeError:'NoneType'对象没有属性'endswith'

  9. 9

    AttributeError:'NoneType'对象没有属性'split'

  10. 10

    AttributeError:'NoneType'对象没有属性'replace'

  11. 11

    AttributeError:'NoneType'对象没有属性'iterrows'

  12. 12

    “ AttributeError:'NoneType'对象没有属性'insert'”

  13. 13

    AttributeError:'NoneType'对象没有属性'tbody'

  14. 14

    AttributeError:'NoneType'对象没有属性'pencolor'

  15. 15

    AttributeError:“ NoneType”对象没有属性“ head”

  16. 16

    AttributeError:'NoneType'对象没有属性'roles'

  17. 17

    AttributeError:'NoneType'对象没有属性'nrows'

  18. 18

    AttributeError:'NoneType'对象没有属性'upper'

  19. 19

    AttributeError:'NoneType'对象没有属性'endswith'

  20. 20

    AttributeError:'NoneType'对象没有属性'get'

  21. 21

    AttributeError:'NoneType'对象没有属性'current'

  22. 22

    AttributeError:'NoneType'对象没有属性'browse'

  23. 23

    AttributeError: 'NoneType' 对象没有属性 'group'

  24. 24

    AttributeError: 'NoneType' 对象没有属性 'foo'

  25. 25

    AttributeError: 'NoneType' 对象没有属性 'send'

  26. 26

    AttributeError: 'NoneType' 对象没有属性 'setText'

  27. 27

    AttributeError: 'NoneType' 对象没有属性 'scenePos'

  28. 28

    AttributeError: 'NoneType' 对象没有属性 'persist'

  29. 29

    model.fit()Keras分类多输入-单输出给出错误:AttributeError:'NoneType'对象没有属性'fit'

热门标签

归档