简体   繁体   English

为什么我会收到 best_precision_threshold 的 IndexError?

[英]Why am I getting an IndexError for best_precision_threshold?

I've ran the following code without an issue on a different machine, however, when I try running it on another machine I run into the following error:我在另一台机器上运行以下代码没有问题,但是,当我尝试在另一台机器上运行它时,我遇到以下错误:

class_names = ['Fish', 'Flower', 'Sugar', 'Gravel']

def get_threshold_for_recall(y_true, y_pred, class_i, recall_threshold=0.94, precision_threshold=0.90, plot=False):

    precision, recall, thresholds = precision_recall_curve(y_true[:, class_i], y_pred[:, class_i])
    i = len(thresholds) - 1
    best_recall_threshold = None
    while best_recall_threshold is None:
        next_threshold = thresholds[i]
        next_recall = recall[i]
        if next_recall >= recall_threshold:
            best_recall_threshold = next_threshold
        i -= 1

    # consice, even though unnecessary passing through all the values
    best_precision_threshold = [thres for prec, thres in zip(precision, thresholds) if prec >= precision_threshold][0]

    if plot:
        plt.figure(figsize=(10, 7))
        plt.step(recall, precision, color='r', alpha=0.3, where='post')
        plt.fill_between(recall, precision, alpha=0.3, color='r')
        plt.axhline(y=precision[i + 1])
        recall_for_prec_thres = [rec for rec, thres in zip(recall, thresholds) 
                                 if thres == best_precision_threshold][0]
        plt.axvline(x=recall_for_prec_thres, color='g')
        plt.xlabel('Recall')
        plt.ylabel('Precision')
        plt.ylim([0.0, 1.05])
        plt.xlim([0.0, 1.0])
        plt.legend(['PR curve', 
                    f'Precision {precision[i + 1]: .2f} corresponding to selected recall threshold',
                    f'Recall {recall_for_prec_thres: .2f} corresponding to selected precision threshold'])
        plt.title(f'Precision-Recall curve for Class {class_names[class_i]}')
    return best_recall_threshold, best_precision_threshold


y_pred = model.predict_generator(data_generator_val, workers=num_cores)
y_true = data_generator_val.get_labels()
recall_thresholds = dict()
precision_thresholds = dict()

for i, class_name in tqdm(enumerate(class_names)):
    recall_thresholds[class_name], precision_thresholds[class_name] = get_threshold_for_recall(y_true, y_pred, i, plot=True)

I expect four precision-recall curves for the four classes, however, instead I get the following error message:我希望这四个类有四个精确召回曲线,但是,我收到以下错误消息:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-79-422044a4f5da> in <module>
     37 precision_thresholds = dict()
     38 for i, class_name in tqdm(enumerate(class_names)):
---> 39     recall_thresholds[class_name], precision_thresholds[class_name] = get_threshold_for_recall(y_true, y_pred, i, plot=True)

<ipython-input-79-422044a4f5da> in get_threshold_for_recall(y_true, y_pred, class_i, recall_threshold, precision_threshold, plot)
     12 
     13     # consice, even though unnecessary passing through all the values
---> 14     best_precision_threshold = [thres for prec, thres in zip(precision, thresholds) if prec > precision_threshold][0]
     15 
     16     if plot:

IndexError: list index out of range

Figured out the issue that I was having with the code snippet above.找出我在上面的代码片段中遇到的问题。 Please make sure you have the right versions: tensorflow==1.14.0 and keras=2.3.0请确保您拥有正确的版本:tensorflow==1.14.0 和 keras=2.3.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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