简体   繁体   中英

How to optimize a CNN in Keras using precision (instead of accuracy)

This is my first time writing a post. I usually find what I am looking for soon but I didn't have luck this time.

My question is simple, I have a target column with True and False values. Basically, it is a binary classification problem. I would like to know how can I optimize my CNN using Precision (instead of metric: Accuracy)?

Btw, this's doesn't work:

model.compile(loss='binary_crossentropy',  optimizer=optm, metrics=['precision'])

This is my code:

model = Sequential()
model.add(Dense(64,name = 'Primera', input_dim=8, activation='relu'))
model.add(Dense(32 ,name = 'Segunda'))
model.add(Dense(1,name = 'Tercera', activation='sigmoid'))

from tensorflow.keras import optimizers
optm = optimizers.Adam(learning_rate=0.001, beta_1=0.9, beta_2=0.999, amsgrad=False)
model.compile(loss='binary_crossentropy',  optimizer=optm, metrics=['accuracy'])

model.summary()

history = model.fit(trainX, trainY, 
                    epochs=1000, 
                    batch_size=16, 
                    validation_split=0.1, 
                    verbose=1)

Thanks!

You can use tf.keras.metrics.Precision() , see the code below for an example.

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.metrics import Precision
from sklearn.datasets import make_classification

X, y = make_classification(n_classes=2, n_features=8, n_informative=8, n_redundant=0, random_state=42)

model = Sequential()
model.add(Dense(64, input_dim=8, activation='relu'))
model.add(Dense(32))
model.add(Dense(1, activation='sigmoid'))

model.compile(
    loss='binary_crossentropy',
    optimizer=Adam(learning_rate=0.001, beta_1=0.9, beta_2=0.999, amsgrad=False),
    metrics=[Precision()]
)

model.fit(X, y, epochs=5, batch_size=32, validation_split=0.1, verbose=1)
# Epoch 1/5
# 3/3 [==============================] - 1s 83ms/step - loss: 0.8535 - precision: 0.5116 - val_loss: 0.6936 - val_precision: 0.5714
# Epoch 2/5
# 3/3 [==============================] - 0s 7ms/step - loss: 0.6851 - precision: 0.5200 - val_loss: 0.5975 - val_precision: 0.6667
# Epoch 3/5
# 3/3 [==============================] - 0s 7ms/step - loss: 0.6004 - precision: 0.6545 - val_loss: 0.5370 - val_precision: 0.8000
# Epoch 4/5
# 3/3 [==============================] - 0s 7ms/step - loss: 0.5412 - precision: 0.8250 - val_loss: 0.4878 - val_precision: 0.8000
# Epoch 5/5
# 3/3 [==============================] - 0s 8ms/step - loss: 0.5145 - precision: 0.9394 - val_loss: 0.4462 - val_precision: 0.8000

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM