小白在呐喊 2024-03-25 18:28 采纳率: 0%
浏览 85

微调中文bert训练时采用CPU跑?

微调中文bert,训练它采用CPU跑为啥啊?
可是我的电脑上有GPU啊,不知道怎么改这个,跑得真的好慢啊。看了好几个博客,都没怎么提,都是在GPU上的跑的,到俺这咋就在CPU跑啊?哭了
我是tensorflow1.13.0,有人可以帮忙解决问题嘛

  • 写回答

3条回答 默认 最新

  • 码农阿豪@新空间 新星创作者: 前端开发技术领域 2024-03-25 18:28
    关注
    让阿豪来帮你解答,本回答参考chatgpt3.5编写提供,如果还有疑问可以评论或留言
    可能是因为在训练过程中需要的batch size比较大,导致GPU显存不足。或者是因为没有设置GPU运行参数,导致程序默认在CPU上运行。 建议先检查一下自己的GPU显存大小,如果发现显存不足,可以尝试减小batch size或者使用更高端的GPU。如果显存大小足够,可以尝试设置tensorflow的GPU运行参数,例如:
    import tensorflow as tf
    import os
    os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
    os.environ["CUDA_VISIBLE_DEVICES"] = "0"  # 只使用第0个GPU
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True  # 设置GPU按需使用显存
    session = tf.Session(config=config)
    

    以上代码中,os.environ["CUDA_VISIBLE_DEVICES"]设置只使用第0个GPU,config.gpu_options.allow_growth表示显存按需使用,避免浪费。 另外,如果你使用的是tensorflow 2.0以上版本,可以使用以下方式开启GPU加速:

    import tensorflow as tf
    physical_devices = tf.config.experimental.list_physical_devices('GPU')
    tf.config.experimental.set_memory_growth(physical_devices[0], True)
    

    案例: 例如,你想在自己的GPU上训练BERT模型,但是在运行过程中发现速度较慢,可以先检查一下显存大小:

    import tensorflow as tf
    gpus = tf.config.experimental.list_physical_devices('GPU')
    if gpus:
        # 设置CUDA的库路径
        tf.config.set_soft_device_placement(True)
        tf.config.experimental.set_visible_devices(gpus[0], 'GPU')
        for gpu in gpus:
            # 查看显存大小
            print("GPU Name: {}, Memory: {:.3f} GB".format(gpu.name, tf.config.experimental.get_memory_info(gpu).used/1024/1024/1024))
    else:
        print('No GPU available.')
    

    如果发现显存大小不足,可以尝试减小batch size或者更换更高端的GPU。 如果显存大小足够,可以尝试设置tensorflow的GPU运行参数:

    import os
    import tensorflow as tf
    os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
    os.environ["CUDA_VISIBLE_DEVICES"] = "0"  # 只使用第0个GPU
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True  # 设置GPU按需使用显存
    session = tf.Session(config=config)
    

    以上代码中,os.environ["CUDA_VISIBLE_DEVICES"]设置只使用第0个GPU,config.gpu_options.allow_growth表示显存按需使用,避免浪费。 另外,如果你使用的是tensorflow 2.0以上版本,可以使用以下方式开启GPU加速:

    import tensorflow as tf
    physical_devices = tf.config.experimental.list_physical_devices('GPU')
    tf.config.experimental.set_memory_growth(physical_devices[0], True)
    # 或者使用以下代码,指定特定的GPU设备(第0号GPU)
    # gpu_devices = tf.config.experimental.list_physical_devices('GPU')
    # tf.config.experimental.set_visible_devices(gpu_devices[0], 'GPU')
    # tf.config.experimental.set_memory_growth(gpu_devices[0], True)
    
    评论

报告相同问题?

问题事件

  • 创建了问题 3月25日