acc_number = 0 err_number = 0 for i in range(len(prediect_result)): if prediect_result[i] == t_test[i]: acc_number += 1 else: err_number += 1 print("预测正确数:", acc_number) print("预测错误数:", err_number) print("预测总数:", x_test.shape[0]) print("预测正确率:", acc_number / x_test.shape[0])
时间: 2024-04-04 22:29:05 浏览: 75
这段代码的作用是根据预测结果计算模型在测试集上的精度。具体来说,代码首先初始化预测正确数和预测错误数为零。然后,通过 for 循环依次遍历 prediect_result 列表中的每个预测结果,并将其与对应的真实标签 t_test[i] 进行比较,如果二者相等,则将预测正确数 acc_number 加 1,否则将预测错误数 err_number 加 1。最终,输出预测正确数、预测错误数、预测总数和预测正确率。其中,预测正确率等于预测正确数除以预测总数。
相关问题
x_train, t_train, x_test, t_test = load_data('F:\\2023\\archive\\train') network = DeepConvNet() network.load_params("deep_convnet_params.pkl") print("calculating test accuracy ... ") sampled = 1000 x_test = x_test[:sampled] t_test = t_test[:sampled] prediect_result = [] for i in x_test: i = np.expand_dims(i, 0) y = network.predict(i) _result = network.predict(i) _result = softmax(_result) result = np.argmax(_result) prediect_result.append(int(result)) acc_number = 0 err_number = 0 for i in range(len(prediect_result)): if prediect_result[i] == t_test[i]: acc_number += 1 else: err_number += 1 print("预测正确数:", acc_number) print("预测错误数:", err_number) print("预测总数:", x_test.shape[0]) print("预测正确率:", acc_number / x_test.shape[0]) classified_ids = [] acc = 0.0 batch_size = 100 for i in range(int(x_test.shape[0] / batch_size)): tx = x_test[i * batch_size:(i + 1) * batch_size] tt = t_test[i * batch_size:(i + 1) * batch_size] y = network.predict(tx, train_flg=False) y = np.argmax(y, axis=1) classified_ids.append(y) acc += np.sum(y == tt) acc = acc / x_test.shape[0] classified_ids = np.array(classified_ids) classified_ids = classified_ids.flatten() max_view = 20 current_view = 1 fig = plt.figure() fig.subplots_adjust(left=0, right=1, bottom=0, top=1, hspace=0.2, wspace=0.2) mis_pairs = {} for i, val in enumerate(classified_ids == t_test): if not val: ax = fig.add_subplot(4, 5, current_view, xticks=[], yticks=[]) ax.imshow(x_test[i].reshape(28, 28), cmap=plt.cm.gray_r, interpolation='nearest') mis_pairs[current_view] = (t_test[i], classified_ids[i]) current_view += 1 if current_view > max_view: break print("======= 错误预测结果展示 =======") print("{view index: (label, inference), ...}") print(mis_pairs) plt.show()
这段代码是一个深度卷积神经网络用于对手写数字图像进行分类的代码。首先,通过load_data函数加载训练数据和测试数据集,然后使用DeepConvNet()创建了一个深度卷积神经网络,并通过load_params函数加载了预训练的参数。接着,从测试集中选取了1000个样本进行测试,并用predict函数对每个样本进行预测,将预测结果存储在predict_result列表中。然后,计算预测的正确率,并将错误预测结果展示出来。最后,调用show函数展示错误预测结果。
阅读全文