Keras自定义决策阈值,用于精确和召回

时间:2017-03-05 08:35:47

标签: python machine-learning tensorflow classification keras

我正在使用package de.freakyonline.ucone; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.Socket; import javafx.application.Application; import static javafx.application.Application.launch; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage; public class MainApp extends Application { String ver = "v0.1-SNAPSHOT"; ObjectOutputStream out; ObjectInputStream in; @Override public void start(Stage stage) throws Exception { Parent root = FXMLLoader.load(getClass().getResource("/fxml/Scene.fxml")); // Connect to Server try { Socket sock = new Socket("unitycraft.de", 2009); out = new ObjectOutputStream(sock.getOutputStream()); in = new ObjectInputStream(sock.getInputStream()); // Listen for remote stuff comming in ... Thread remote = new Thread(new RemoteReader(in,out,sock)); remote.start(); } catch (Exception ex) { ex.printStackTrace(); } Scene scene = new Scene(root); scene.getStylesheets().add("/styles/Styles.css"); stage.setTitle("UCOne - The UnityCraft Staff Tool " + ver); stage.setScene(scene); stage.show(); } /** * The main() method is ignored in correctly deployed JavaFX application. * main() serves only as fallback in case the application can not be * launched through deployment artifacts, e.g., in IDEs with limited FX * support. NetBeans ignores main(). * * @param args the command line arguments */ public static void main(String[] args) { launch(args); } } package de.freakyonline.ucone; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.Socket; /** * * @author uwe */ public class RemoteReader implements Runnable { Object obj = null; ObjectInputStream in; ObjectOutputStream out; Socket sock; public RemoteReader (ObjectInputStream in, ObjectOutputStream out, Socket sock) { this.in = in; this.out = out; this.sock = sock; } public void run() { try { while((obj=in.readObject()) != null) System.out.println("Got object from server ..."); } catch (Exception ex) { ex.printStackTrace(); } } } 后端)进行二进制分类,并且我有大约76%的精确度和70%的召回率。现在我想尝试使用决策阈值。据我所知Keras使用决策阈值0.5。 Tensorflow是否有办法使用自定义阈值来确定决策精度和召回?

感谢您的时间!

1 个答案:

答案 0 :(得分:18)

创建这样的自定义指标:

编辑感谢@Marcin :创建以threshold_value为参数返回所需指标的函数

def precision_threshold(threshold=0.5):
    def precision(y_true, y_pred):
        """Precision metric.
        Computes the precision over the whole batch using threshold_value.
        """
        threshold_value = threshold
        # Adaptation of the "round()" used before to get the predictions. Clipping to make sure that the predicted raw values are between 0 and 1.
        y_pred = K.cast(K.greater(K.clip(y_pred, 0, 1), threshold_value), K.floatx())
        # Compute the number of true positives. Rounding in prevention to make sure we have an integer.
        true_positives = K.round(K.sum(K.clip(y_true * y_pred, 0, 1)))
        # count the predicted positives
        predicted_positives = K.sum(y_pred)
        # Get the precision ratio
        precision_ratio = true_positives / (predicted_positives + K.epsilon())
        return precision_ratio
    return precision

def recall_threshold(threshold = 0.5):
    def recall(y_true, y_pred):
        """Recall metric.
        Computes the recall over the whole batch using threshold_value.
        """
        threshold_value = threshold
        # Adaptation of the "round()" used before to get the predictions. Clipping to make sure that the predicted raw values are between 0 and 1.
        y_pred = K.cast(K.greater(K.clip(y_pred, 0, 1), threshold_value), K.floatx())
        # Compute the number of true positives. Rounding in prevention to make sure we have an integer.
        true_positives = K.round(K.sum(K.clip(y_true * y_pred, 0, 1)))
        # Compute the number of positive targets.
        possible_positives = K.sum(K.clip(y_true, 0, 1))
        recall_ratio = true_positives / (possible_positives + K.epsilon())
        return recall_ratio
    return recall

现在您可以在

中使用它们
model.compile(..., metrics = [precision_threshold(0.1), precision_threshold(0.2),precision_threshold(0.8), recall_threshold(0.2,...)])

我希望这会有所帮助:)