gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 09:07:15 25 4
gpt4 key购买 nike

我正在使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com