【问题标题】:'Model' object has no attribute '_name'“模型”对象没有属性“_name”
【发布时间】:2018-10-21 00:11:47
【问题描述】:

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

【问题讨论】:

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


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-11
      • 2012-08-17
      • 1970-01-01
      • 1970-01-01
      • 2010-10-02
      • 1970-01-01
      • 2020-04-14
      • 2018-06-12
      相关资源
      最近更新 更多