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

相关推荐
赢乐几秒前
大模型学习笔记:检索增强生成(RAG)架构
人工智能·python·深度学习·机器学习·智能体·幻觉·检索增强生成(rag)
飞哥数智坊8 分钟前
OPC 需要的不是一个个AI工具,而是一支数字团队
人工智能
小橙讲编程20 分钟前
200+ 模型、零内容过滤、完全免费 — Open Generative AI 全面解析与实战指南
人工智能
happyprince1 小时前
Hugging Face Transformers 源码全景解读
人工智能
春风LiuK1 小时前
远程服务器安装 Claude Code 并配置 DeepSeek v4
人工智能
冬奇Lab2 小时前
RAG 系列(二十):企业级 RAG 架构设计
人工智能·llm
冬奇Lab2 小时前
一天一个开源项目(第104篇):CLI-Anything - 让所有软件变成 AI 代理可调用的命令行接口
人工智能·开源·资讯
冬奇Lab2 小时前
RAG 系列(十九):增量更新——知识库如何保持新鲜
人工智能·llm
浪里行舟2 小时前
你的品牌正在被AI“遗忘”?用BuildSOM找回搜索的下一个风口
人工智能·python·程序员