字符串标识符和实际类名称的非相同结果,用于激活,损失函数和度量 [英] Non-Identical results from String Identifier and Actual Class names for activations, loss functions, and metrics

查看:32
本文介绍了字符串标识符和实际类名称的非相同结果,用于激活,损失函数和度量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下运行良好的keras模型:

I have the following keras model that is working fine:

model = tf.keras.Sequential(
    [
     #first convolution
     tf.keras.layers.Conv2D(16, (3,3), activation="relu", 
                         input_shape=(IMAGE_SIZE,IMAGE_SIZE,3)),
     tf.keras.layers.MaxPooling2D(2,2),
     #second convolution
     tf.keras.layers.Conv2D(32, (3,3), activation="relu"),
     tf.keras.layers.MaxPooling2D(2,2),
     #third convolution
     tf.keras.layers.Conv2D(64, (3,3), activation="relu"),
     tf.keras.layers.MaxPooling2D(2,2),
     #flatten the results to feed into a DNN
     tf.keras.layers.Flatten(),
     tf.keras.layers.Dense(512, activation="relu"),
     #only 1 neuron, as its a binary classification problem
     tf.keras.layers.Dense(1, activation="sigmoid")
    ]
)
model.compile(optimizer = tf.keras.optimizers.RMSprop(lr=0.001),
              loss="binary_crossentropy", 
              metrics=["acc"])
history = model.fit_generator(train_generator, epochs=15,
             steps_per_epoch=100, validation_data = validation_generator,
             validation_steps=50, verbose=1)

但是,当尝试替换魔术字符串并使用实际的类名进行激活,损失函数和度量时,我具有以下模型,该模型可以很好地编译,但精度始终为0.此模型的行为与一个以上,其他所有都保持不变.这是新模型:

However, when attempting to replace the magic strings, and use actual class names for activations, loss functions, and metrics, I have the following model, which compiles fine, but accuracy is always 0. This model is behaving differently than the one above, with everything else remaining the same. Here is the new model:

model = tf.keras.Sequential(
    [
     #first convolution
     tf.keras.layers.Conv2D(16, (3,3), activation=tf.keras.activations.relu,
                input_shape=(IMAGE_SIZE,IMAGE_SIZE,3)),
     tf.keras.layers.MaxPooling2D(2,2),
     #second convolution
     tf.keras.layers.Conv2D(32, (3,3), activation=tf.keras.activations.relu),
     tf.keras.layers.MaxPooling2D(2,2),
     #third convolution
     tf.keras.layers.Conv2D(64, (3,3), activation=tf.keras.activations.relu),
     tf.keras.layers.MaxPooling2D(2,2),
     #flatten the results to feed into a DNN
     tf.keras.layers.Flatten(),
     tf.keras.layers.Dense(512, activation=tf.keras.activations.relu),
     #only 1 neuron, as its a binary classification problem
     tf.keras.layers.Dense(1, activation=tf.keras.activations.sigmoid)
    ]
)
model.compile(optimizer = tf.keras.optimizers.RMSprop(learning_rate=0.001),
              loss=tf.keras.losses.BinaryCrossentropy(), 
              metrics=[tf.keras.metrics.Accuracy()])
history = model.fit_generator(train_generator, epochs=15,
                steps_per_epoch=100, validation_data = validation_generator,
                validation_steps=50, verbose=1)

我猜我在用类名替换魔术字符串时犯了一个错误,但是我无法发现错误.有什么建议吗?

I am guessing I have made a mistake in replacing the magic strings with class names, but I can't spot the mistake. Any recommendations?

推荐答案

当我们将字符串标识符设置为 ['acc'] ['准确性"] ,程序将为我们的问题选择相关指标,例如二进制或分类类型.但是,当我们设置实际的类名时,我们需要更具体一些.因此,根据您的情况,您需要将指标从

When we set String Identifier for accuracy as ['acc'] or ['accuracy'], the program will choose the relevant metrics for our problem, like whether it's binary or categorical type. But when we set the actual class name, we need to be a bit more specific. So, in your case, you need to change your metrics from

tf.keras.metrics.Accuracy()

tf.keras.metrics.BinaryAccuracy()

.Accuracy()中读取每个内容. .BinaryAccuracy()

这是一个伪造的示例,可以重现该问题和解决方案,以提供完整参考.

Here is one dummy example to reproduce the issue and solution for a complete reference.

# Generate dummy data
np.random.seed(10)
x_train = np.random.random((1000, 20))
y_train = np.random.randint(2, size=(1000, 1))
x_test  = np.random.random((800, 20))
y_test  = np.random.randint(2, size=(800, 1))

# model
model = Sequential()
model.add(Dense(64, input_dim=20, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))

# compile and run
model.compile(loss='binary_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])
model.fit(x_train, y_train,
          epochs=10, verbose=2,
          batch_size=128, validation_data=(x_test, y_test))

使用字符串标识符,它将运行正常.但是,如果将指标更改为 .Accuracy(),则培训和验证部分的得分都将为零.要解决此问题,您需要设置 .BinaryAccuracy(),然后事情将按预期运行.

With String Identifier, it will run OK. But if you change metrics to .Accuracy(), it will give you zero scores for both the train and validation part. To solve it, you need to set .BinaryAccuracy(), then things will run as expected.

这篇关于字符串标识符和实际类名称的非相同结果,用于激活,损失函数和度量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆