![](/img/trans.png)
[英]AttributeError: 'Model' object has no attribute 'name'
[英]'Model' object has no attribute '_name'
我正在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
定義為輸入張量之后,您正在將其他張量分配給x
。 因此,它不能用作模型的輸入,即inputs=x
。 要用最少的修改來解決這個問題,只需在將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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.