How to calculate prediction uncertainty using Keras?

前端 未结 4 926
再見小時候
再見小時候 2020-12-13 00:08

I would like to calculate NN model certainty/confidence (see What my deep model doesn\'t know) - when NN tells me an image represents \"8\", I would like to know how certain

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-13 00:58

    If you want to implement dropout approach to measure uncertainty you should do the following:

    1. Implement function which applies dropout also during the test time:

      import keras.backend as K
      f = K.function([model.layers[0].input, K.learning_phase()],
                     [model.layers[-1].output])
      
    2. Use this function as uncertainty predictor e.g. in a following manner:

      def predict_with_uncertainty(f, x, n_iter=10):
          result = numpy.zeros((n_iter,) + x.shape)
      
          for iter in range(n_iter):
              result[iter] = f(x, 1)
      
          prediction = result.mean(axis=0)
          uncertainty = result.var(axis=0)
          return prediction, uncertainty
      

    Of course you may use any different function to compute uncertainty.

提交回复
热议问题