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]
时间: 2024-04-04 18:29:06 浏览: 179
这段代码的作用是对测试集数据进行批量预测,并计算模型在测试集上的精度。具体来说,代码首先定义了一个空列表 classified_ids,以及初始化精度 acc 为零。然后,通过 for 循环依次遍历测试集数据,每次处理一个批次的数据。对于第 i 个批次,使用切片方式将测试集数据和标签分成大小为 batch_size 的若干个子集,依次传入网络进行预测,并使用 np.argmax 函数获取预测结果中概率最大的类别。将预测结果保存到 classified_ids 列表中,并将预测正确的样本数量累加到 acc 变量中。最终,将 acc 除以测试集总样本数,得到模型在测试集上的精度。需要注意的是,代码中使用了 train_flg=False 参数来指示网络在测试模式下运行。
相关问题
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函数展示错误预测结果。
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
这段代码的作用是将分类错误的样本可视化,并将其真实标签和预测标签保存到 mis_pairs 字典中。具体来说,代码首先将 classified_ids 列表转换为形状为 (x_test.shape[0],) 的一维数组,并将其展平为一维,方便后续处理。然后,定义了一个画布 fig,并调用 fig.subplots_adjust 函数设置画布边距和子图之间的间距。接着,使用 for 循环依次遍历测试集数据,并判断当前样本的预测结果是否与真实标签相同。如果当前样本被错误分类,则将其在画布上绘制出来,并将其真实标签和预测标签保存到 mis_pairs 字典中。具体来说,使用 fig.add_subplot 函数在画布上添加一个子图,并设置 xticks 和 yticks 参数为空,以去除子图的坐标轴。然后,使用 imshow 函数在子图上显示当前样本的图像,并使用 cmap=plt.cm.gray_r 参数指定颜色映射为灰度图。最后,将当前子图的编号和对应的真实标签和预测标签保存到 mis_pairs 字典中,并将 current_view 加 1。如果当前绘制的子图数量已经达到了 max_view 的限制,则跳出循环。
阅读全文
相关推荐











