gpt4 book ai didi

python - “模型”对象没有属性 '_name'

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

我正在 Keras 中制作 CNN。但我在制作 Keras 模型时遇到问题。这是我的代码:

x = Input(shape=(256,256,1))
for i in range(16):
u = int(16 * 2 ** (i//4))
x = BatchNormalization()(x)
x1 = Conv2D(u, kernel_size=(1,1), strides=(1,1), activation='relu')(x)
x1 = MaxPooling2D(pool_size=(3,3), strides=(1,1))(x1)
x2 = Conv2D(u, kernel_size=(2,2), strides=(1,1), activation='relu')(x)
x2 = MaxPooling2D(pool_size=(2,2), strides=(1,1))(x2)
x3 = Conv2D(u, kernel_size=(3,3), strides=(1,1), activation='relu')(x)
x3 = MaxPooling2D(pool_size=(1,1), strides=(1,1))(x3)
x = multiply([x1, x2, x3])
#x = Dropout(0.45)(x)
x = MaxPooling2D(pool_size=(3,3), strides=(1,1))(x)
out = BatchNormalization()(x)
model = tf.keras.models.Model(inputs=x, outputs=out)

我收到以下错误:

AttributeError                            Traceback (most recent call last)
<ipython-input-99-630b3ef0b15f> in <module>()
13 x = MaxPooling2D(pool_size=(3,3), strides=(1,1))(x)
14 out = BatchNormalization()(x)
---> 15 model = tf.keras.models.Model(inputs=x, outputs=out)
...

AttributeError: 'Model' object has no attribute '_name'

最佳答案

问题是您在将 x 定义为输入张量后将其他张量分配给它。因此,它不能用作模型的输入,即inputs=x。要通过最少的修改解决此问题,只需将 x 定义为输入张量后将其存储在另一个变量中即可:

x = Input(shape=(256,256,1))
inp = x

# the rest is the same...

model = tf.keras.models.Model(inputs=inp, outputs=out) # pass `inp` as inputs

关于python - “模型”对象没有属性 '_name',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52907059/

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