“模型”对象没有属性“ _name”

时间:2018-10-20 15:09:34

标签: python tensorflow machine-learning keras conv-neural-network

我正在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'

1 个答案:

答案 0 :(得分:1)

问题在于,您将其他张量定义为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