Main Content


Energy distribution discriminator

Since R2023a


    The EnergyDistributionDiscriminator object is a distribution discriminator that uses the energy method to compute distribution confidence scores. The object contains a threshold that you can use to separate observations into in-distribution (ID) and out-of-distribution (OOD) data sets.


    Create an EnergyDistributionDiscriminator object using the networkDistributionDiscriminator function and setting the method input argument to "energy".


    The object type depends on the method you specify when you use the networkDistributionDiscriminator function. The method determines how the software computes the distribution confidence scores. You can set the method argument to either "baseline", "odin", "energy", or "hbos". For more information, see Distribution Confidence Scores.


    expand all

    This property is read-only.

    Method for computing the distribution confidence scores, returned as "energy". For more information, see Distribution Confidence Scores.

    This property is read-only.

    Deep learning network, returned as a dlnetwork object.

    This property is read-only.

    Temperature scaling, returned as a positive scalar. The temperature controls the scaling of the softmax scores when the software computes the distribution confidence scores. For more information, see Distribution Confidence Scores.

    Example: 1000

    This property is read-only.

    Distribution threshold, returned as a positive scalar. The threshold separates the ID and OOD data using the distribution confidence scores.

    Example: 1.2

    Object Functions

    isInNetworkDistributionDetermine whether data is within the distribution of the network
    distributionScoresDistribution confidence scores


    collapse all

    Load a pretrained classification network.


    Load ID data. Convert the data to a dlarray object.

    XID = digitTrain4DArrayData;
    XID = dlarray(XID,"SSCB");

    Modify the ID training data to create an OOD set.

    XOOD = XID.*0.3 + 0.1;

    Create a discriminator using the energy method. Set the temperature scaling to 0.1.

    method = "energy";
    discriminator = networkDistributionDiscriminator(net,XID,XOOD,method, ...
    discriminator = 
      EnergyDistributionDiscriminator with properties:
             Method: "energy"
            Network: [1x1 dlnetwork]
        Temperature: 0.1000
          Threshold: 8.7246

    Assess the performance of the discriminator on the OOD data.

    tf = isInNetworkDistribution(discriminator,XOOD);
    accuracy = sum(tf == 0)/numel(tf)
    accuracy = 0.9066

    More About

    expand all


    [1] Shalev, Gal, Gabi Shalev, and Joseph Keshet. “A Baseline for Detecting Out-of-Distribution Examples in Image Captioning.” In Proceedings of the 30th ACM International Conference on Multimedia, 4175–84. Lisboa Portugal: ACM, 2022.

    [2] Shiyu Liang, Yixuan Li, and R. Srikant, “Enhancing The Reliability of Out-of-distribution Image Detection in Neural Networks” arXiv:1706.02690 [cs.LG], August 30, 2020,

    [3] Weitang Liu, Xiaoyun Wang, John D. Owens, and Yixuan Li, “Energy-based Out-of-distribution Detection” arXiv:2010.03759 [cs.LG], April 26, 2021,

    [4] Markus Goldstein and Andreas Dengel. "Histogram-based outlier score (hbos): A fast unsupervised anomaly detection algorithm." KI-2012: poster and demo track 9 (2012).

    [5] Jingkang Yang, Kaiyang Zhou, Yixuan Li, and Ziwei Liu, “Generalized Out-of-Distribution Detection: A Survey” August 3, 2022,

    [6] Lee, Kimin, Kibok Lee, Honglak Lee, and Jinwoo Shin. “A Simple Unified Framework for Detecting Out-of-Distribution Samples and Adversarial Attacks.” arXiv, October 27, 2018.

    Version History

    Introduced in R2023a