激活、丢失函数和度量的字符串标识符和实际类名的结果不相同

2024-05-16 06:17:07 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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。此模型的行为与上述模型不同,其他一切保持不变。以下是新型号:

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)

我猜我在用类名替换神奇字符串时犯了一个错误,但我看不出这个错误。有什么建议吗


Tags: 模型imagesizemodellayerstfgeneratoractivation
1条回答
网友
1楼 · 发布于 2024-05-16 06:17:07

当我们将字符串标识符的准确度设置为['acc']['accuracy']时,程序将为我们的问题选择相关的度量,比如它是二进制还是分类类型。但是当我们设置实际的类名时,我们需要更具体一点。因此,在您的情况下,您需要从

tf.keras.metrics.Accuracy()

tf.keras.metrics.BinaryAccuracy()

^{}^{}中读取每个的内容


下面是一个虚拟示例,用于重现问题和解决方案,以供完整参考

# 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(),然后事情将按预期运行

相关问题 更多 >