'ListWrapper'对象没有属性'minimize'



我正在编写一个具有多输出鉴别器的GAN。尝试批量训练鉴别器,但得到错误- AttributeError: 'ListWrapper'对象没有属性'最小化'。下面是鉴别器代码,这里c_model是具有多个输出的鉴别器-

# custom activation function
def custom_activation(output):
logexpsum = backend.sum(backend.exp(output), axis=-1, keepdims=True)
result = logexpsum / (logexpsum + 1.0)
return result
# define the standalone supervised and unsupervised discriminator models
def define_discriminator(in_shape=input, n_classes=n_class):
# image input
in_image = Input(shape=in_shape)
# downsample
fe = Conv2D(128, (3,3), strides=(2,2), padding='same')(in_image)
fe = LeakyReLU(alpha=0.2)(fe)
# downsample
fe = Conv2D(128, (3,3), strides=(2,2), padding='same')(fe)
fe = LeakyReLU(alpha=0.2)(fe)
# downsample
fe = Conv2D(128, (3,3), strides=(2,2), padding='same')(fe)
fe = LeakyReLU(alpha=0.2)(fe)
# flatten feature maps
fe = Flatten()(fe)
# dropout
fe = Dropout(0.4)(fe)
# output layer nodes
fe = Dense(n_classes)(fe)
# supervised output
c_out_layer = Activation('softmax')(fe)
# unsupervised output
d_out_layer = Lambda(custom_activation)(fe)

# The part of discriminator that is giving the error
# define and compile supervised discriminator model
c_model = Model(inputs = in_image, outputs = [c_out_layer, d_out_layer])
opt = tf.keras.optimizers.SGD(learning_rate=0.0002)
c_model.compile(loss=['sparse_categorical_crossentropy', 'binary_crossentropy'], optimizer=[opt, opt], metrics=['accuracy', 'accuracy'])

# define and compile unsupervised discriminator model
d_model = Model(in_image, d_out_layer)
d_model.compile(loss='binary_crossentropy', optimizer = opt)
return d_model, c_model
下面是训练模型的代码片段-
c_loss, c_acc = c_model.train_on_batch(Xsup_real, [ysup_real, label_real])

3个输入的输入形状为-

Xsup_real = (60, 64, 64, 1)
ysup_real = (60, 1)
label_real = (60, 1)

tensorflow版本是2.6.0 2.6.0 keras版本谢谢!

问题来自以下调用

c_model.compile(
loss=['sparse_categorical_crossentropy', 'binary_crossentropy'],
optimizer=[opt, opt],
metrics=['accuracy', 'accuracy']
)

不能将多个优化器作为列表传递给compile函数。如果确实需要,请使用tensorflow-addons中的tfa.optimizer . multioptimizer。

multiOpt = tfa.optimizers.MultiOptimizer(
[(opt, c_out_layer), (opt, d_out_layer)]
)
c_model.compile(
loss=['sparse_categorical_crossentropy', 'binary_crossentropy'],
optimizer=multiOpt,
metrics=['accuracy', 'accuracy']
)

但是,仔细看看你的代码,你可能甚至不需要多个优化器。只做

c_model.compile(
loss=['sparse_categorical_crossentropy', 'binary_crossentropy'],
optimizer=opt,
metrics=['accuracy', 'accuracy']
)

最新更新