动手学深度学习d2l包M4芯片 gpu加速

conda创建环境

bash 复制代码
CONDA_SUBDIR=osx-arm64 conda create -n ml python=3.9 -c conda-forge
conda env config vars set CONDA_SUBDIR=osx-arm64
conda activate ml

pip安装包

bash 复制代码
pip install --pre torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/nightly/cpu
pip install transformers datasets
pip install matplotlib

下载del源文件

del链接

放到本地项目内

修改del下的torch.py的两个函数内容

python 复制代码
# 修改try gpu函数
def try_gpu(i=0):
    """Return gpu(i) if exists, otherwise return cpu().

    Defined in :numref:`sec_use_gpu`"""
    if torch.cuda.device_count() >= i + 1:
        return torch.device(f'cuda:{i}')
    try:
        return torch.device('mps')
    except:
        return torch.device('cpu')

# 修改try gpu函数
def try_all_gpus():
    """Return all available GPUs, or [cpu(),] if no GPU exists.

    Defined in :numref:`sec_use_gpu`"""
    devices = [torch.device(f'cuda:{i}')
               for i in range(torch.cuda.device_count())]
    try:
        device_macos = torch.device('mps')
    except:
        device_macos = torch.device('cpu')
    return devices if devices else [device_macos]

测试

运行lenet.ipynb测试效果

速度还可以。

还不懂的可以看M1版本的教程

相关推荐
大力财经12 小时前
京东“月黑风高”超级盛典开放预约
人工智能
programhelp_12 小时前
特斯拉 MLE 超详细面经 + 避坑
数据结构·人工智能·算法·面试·职场和发展
躺柒12 小时前
读人工智能全球格局:未来趋势与中国位势06人类的未来(下)
大数据·人工智能·算法·ai·智能
gorgeous(๑>؂<๑)12 小时前
【ICLR26-Oral Paper-Meta】DepthLM:基于视觉语言模型的度量深度
人工智能·计算机视觉·语言模型·自然语言处理
Dev7z13 小时前
当AI学会“听诊”:心肺听诊分析系统,正在悄悄改变医疗
人工智能
池央13 小时前
atvoss:AI 处理器上的智能语音与多媒体解决方案,赋能高效实时交互
人工智能·交互
码云数智-大飞13 小时前
小程序制作平台有哪些?SaaS小程序制作平台对比评测
大数据·人工智能
新缸中之脑14 小时前
Arduino AI手势识别系统
人工智能
码农小韩14 小时前
AIAgent应用开发——DeepSeek分析(二)
人工智能·python·深度学习·agent·强化学习·deepseek
ctrigger14 小时前
家和万事兴
大数据·人工智能·生活