画图时使用的函数和一些错误处理

1. 关于 画图时后的数据量匹配错误(应该是)

然后这块还有个问题:

python 复制代码
import pynvml
import matplotlib.pyplot as plt
import matplotlib.animation as animation

def get_gpu_memory_usage(handle):
    gpu_mem = pynvml.nvmlDeviceGetMemoryInfo(handle)
    return gpu_mem.used / 1024 ** 2  # Convert memory usage to MB

def animate(i, ys, handle):
    ys.append(get_gpu_memory_usage(handle))
    ys = ys[-100:]
    line.set_ydata(ys)
    return line,

if __name__ == "__main__":
    pynvml.nvmlInit()
    handle = pynvml.nvmlDeviceGetHandleByIndex(0)  # Assuming you have only one GPU

    fig, ax = plt.subplots()
    line, = ax.plot([], [])
    ax.set_xlim(0, 100)
    ax.set_ylim(0, 10000)  # Assuming the maximum GPU memory is 10000 MB
    ax.set_title('Real-time GPU Memory Usage')
    ax.set_xlabel('Time')
    ax.set_ylabel('Memory Usage (MB)')

    ys = [0] * 100

    ani = animation.FuncAnimation(fig, animate, fargs=(ys, handle), interval=1000)

    plt.show()

    pynvml.nvmlShutdown()

出现这个错:"ValueError: shape mismatch: objects cannot be broadcast to a single shape. Mismatch is between arg 0 with shape (0,) and arg 1 with shape (100,)."

把:

python 复制代码
def animate(i, ys, handle):
    # update GPU memory usage
    ys.append(get_gpu_memory_usage(handle))
    ys = ys[-100:]
    line.set_ydata(ys)
    return line,

改为:

python 复制代码
def animate(i, ys, handle):
    ys.append(get_gpu_memory_usage(handle))
    ys = ys[-100:]
    line.set_data(range(len(ys)), ys)  # Set both x and y data
    ax.set_xlim(0, len(ys))  # Adjust x-axis limit based on the length of ys
    return line,    # 用于指示函数应返回包含 line 值的元组。
                    # 在 Python 中,单项元组必须有一个尾随逗号,以将其与括号内的普通表达式区分开。

就不报错

相关推荐
计算机源码社5 分钟前
分享一个餐饮连锁店点餐系统 餐馆食材采购系统Java、python、php三个版本(源码、调试、LW、开题、PPT)
java·python·php·毕业设计项目·计算机课程设计·计算机毕业设计源码·计算机毕业设计选题
汤兰月10 分钟前
Python中的观察者模式:从基础到实战
开发语言·python·观察者模式
西柚与蓝莓2 小时前
【开源开放体系总结】
python
belldeep5 小时前
python:reportlab 将多个图片合并成一个PDF文件
python·pdf·reportlab
FreakStudio8 小时前
全网最适合入门的面向对象编程教程:56 Python字符串与序列化-正则表达式和re模块应用
python·单片机·嵌入式·面向对象·电子diy
丶21368 小时前
【CUDA】【PyTorch】安装 PyTorch 与 CUDA 11.7 的详细步骤
人工智能·pytorch·python
_.Switch9 小时前
Python Web 应用中的 API 网关集成与优化
开发语言·前端·后端·python·架构·log4j
一个闪现必杀技9 小时前
Python入门--函数
开发语言·python·青少年编程·pycharm
小鹿( ﹡ˆoˆ﹡ )9 小时前
探索IP协议的神秘面纱:Python中的网络通信
python·tcp/ip·php
卷心菜小温10 小时前
【BUG】P-tuningv2微调ChatGLM2-6B时所踩的坑
python·深度学习·语言模型·nlp·bug