动手学深度学习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版本的教程

相关推荐
心.c16 小时前
AI Agent 的新战场:从会动手,到被允许动手
人工智能·ai
救救孩子把16 小时前
89-机器学习与大模型开发数学教程-8-7 本书总结与展望
人工智能·机器学习
X54先生(人文科技)16 小时前
ELR-SELLM 碳硅光阴协同演进系统架构文档
人工智能·深度学习·系统架构·开源协议
云烟成雨TD16 小时前
Spring AI 1.x 系列【39】MCP Java SDK 与 Spring AI 集成
java·人工智能·spring
继续商行16 小时前
性能优化的工程美学与极致追求
人工智能
超梦dasgg16 小时前
详细讲解 AI 上下文(Context)
人工智能·状态模式
救救孩子把16 小时前
87-机器学习与大模型开发数学教程-8-5 微分方程与神经微分方程(Neural ODEs)
人工智能·机器学习
完成大叔16 小时前
模块二,Agent个性化模式的价值呈现
人工智能
Shan120516 小时前
机器学习之平均精确率均值(Average Precision)
人工智能·机器学习·均值算法
共享家952716 小时前
Skill的概述与使用
人工智能·学习·openclaw