在使用 Python 的 Matplotlib 库绘制训练准确率曲线时,可能会遇到以下错误:

ValueError: x and y must have same first dimension, but have shapes (1,) and (10,)

这个错误是因为 epochsacc 的维度不匹配,导致无法绘制曲线。epochsacc 应该具有相同的维度,以便正确绘制训练准确率曲线。

你可以通过确保 epochsacc 具有相同的维度来解决这个问题。以下是一个修复这个错误的示例代码:

import matplotlib.pyplot as plt

假设你的训练周期范围是从1到10

epochs = range(1, 11)

plt.figure(figsize=(16, 8)) plt.subplot(1, 2, 1) plt.plot(epochs, acc, label='Training Accuracy') # 绘制训练准确率曲线 plt.plot(epochs, val_acc, label='Validation Accuracy') # 绘制验证准确率曲线 plt.legend(loc='lower right') # 添加图例,位置在右下角 plt.title('Training and Validation Accuracy') plt.xlabel('Epochs') plt.ylabel('Accuracy')

plt.subplot(1, 2, 2) plt.plot(epochs, loss, label='Training Loss') # 绘制训练损失曲线 plt.plot(epochs, val_loss, label='Validation Loss') # 绘制验证损失曲线 plt.legend(loc='upper right') # 添加图例,位置在右上角 plt.title('Training and Validation Loss') plt.xlabel('Epochs') plt.ylabel('Loss')

plt.show()

请确保你已经正确导入了 matplotlib.pyplot 库,并且 epochsacc 具有相同的维度。在这个示例代码中,我们假设 epochs 的范围是从1到10,你需要根据你的实际情况来定义 epochs 和其他相关变量。

解决 Python Matplotlib 中“x and y must have same first dimension”错误

原文地址: https://www.cveoy.top/t/topic/hHc 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录