python - 喀拉斯 : TypeError: 'AddNL' object has no attribute '__getitem__'

标签 python tensorflow machine-learning keras neural-network

我正在使用 Keras 功能 API 和 TensorFlow 后端来定义卷积层,并且我必须实现自定义合并层。

假设:

# All necessary libraries imported.
# image_shape = (28, 28, 3)
# 16 and (3,3) are kernel_count and kernel_size respectively.
def MyModel(image_shape):
    input = Input(shape=image_shape)
    conv1 = Conv2D(16, (3,3), kernel_initializer='he_normal')(input)
    conv2 = Conv2D(16, (3,3), kernel_initializer='he_normal')(input)
    conv3 = AddNL()[conv1, conv2]

我的自定义合并层AddNLkeras.layers.merge中实现,如下所示:

class AddNL(_Merge):
    def _merge_function(self, inputs):
        nK1 = inputs[0].shape[-1]
        nK2 = inputs[1].shape[-1]
        # <?, image,image, channel> Channel = Number of Kernels.
        k = nK1 if nK1 < nK2 else nK2
        output = input[0][...,0:k] + input[1][...,0:k] 
        return output

当我的代码到达该行时,抛出以下TypeError:

类型错误:“AddNL”对象没有属性“__getitem__”

最佳答案

有一个拼写错误。代码应如下所示:

conv3 = AddNL([conv1, conv2])

关于python - 喀拉斯 : TypeError: 'AddNL' object has no attribute '__getitem__' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49118388/

相关文章:

python - 如何解释奇异值分解结果(Python 3)?

machine-learning - 关于马尔可夫链的困惑

Python unittest 成功断言 None 为 False

python - Pandas - 提取行直到满足条件

python - 为什么 CPU 上的 Keras LSTM 比 GPU 快三倍?

tensorflow - 堆叠 LSTM 的初始状态结构

solr - 使用 Solr ltr(学习排名)模块时如何使用用户点击来训练模型

python - 如何确定发生了什么类型的异常?

python索引错误: index 3 is out of bounds for axis 0 with size 3

python - Tensorflow ValueError : logits and labels must have the same shape ((None, 2) vs (None, 1))