【跟着沐神学深度学习笔记——环境配置篇】

本文详细介绍了如何通过Anaconda安装和配置深度学习环境,包括创建新环境、安装PyTorchGPU版本、d2l包,以及设置Jupyter代码自动补全和主题字体。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


前言

本文章是跟着沐神学习深度学习的笔记,代码全部来源于互联网,本文章只是把一些代码整合在一起,以实现快速配置好深度学习环境的功能。

写下这篇文章是为了在下一次配置环境时,不用再到处搜索代码怎么写。

一、安装anaconda

搜索anaconda,打开官网,下载安装。

二、配置新环境

1. 配置conda镜像源

在C盘的用户文件夹中找到 【.condarc】 文件,若没有这个文件就新建一个。
目录示例:C:\Users\S2513\.condarc
用记事本打开文件,修改文件内容为:

show_channel_urls: true
channel_alias: http://mirrors.tuna.tsinghua.edu.cn/anaconda
default_channels:
  - http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  - http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  - http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
  - http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/pro
  - http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:
  conda-forge: http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  msys2: http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  bioconda: http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  menpo: http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  pytorch: http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  simpleitk: http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
channels:
  - defaults
ssl_verify: true

2. 创建一个新环境(d2l)

打开anaconda prompt
输入以下代码创建一个名为 d2l 的环境

conda create --name d2l python=3.8 -y

3. 配置d2l环境

1)输入以下代码激活环境

conda activate d2l

在这里插入图片描述

2)安装pytorch(GPU版本)

输入以下代码进行安装:

conda install pytorch==1.12.0 torchvision==0.13.0 torchaudio==0.12.0 cudatoolkit=11.6 -c pytorch -c conda-forge

3)安装d2l包

输入以下代码进行安装:

pip install d2l==0.17.6

如果长时间无反应可能是pip源无法连接
可以把pip换成镜像源再尝试,在base环境下使用以下代码:

pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

4)配置完成,打开jupyter

在 d2l 环境中输入:

jupyter notebook

即可打开

三、设置jupyter代码自动补齐

安装jupyter拓展插件nbextensions
在anaconda prompt中激活d2l环境,运行以下代码安装插件

pip install jupyter_contrib_nbextensions -i https://pypi.tuna.tsinghua.edu.cn/simple
# -i https://pypi.tuna.tsinghua.edu.cn/simple给pip换源
jupyter nbextensions_configurator enable --user

重新打开jupyter notebook
在这里插入图片描述
点开 Nbextensions 的选项,并勾选 Hinterland选项,如果点不了的话上面disable选项修改一下

在这里插入图片描述
按 Tab 键即可使用

四、设置jupyter主题字体

conda 激活 d2l 环境
查找jupyter路径:

jupyter --data-dir

在路径下创建文件夹【nbextensions/jupyter_themes】
示例:C:\Users\S2513\AppData\Roaming\jupyter\nbextensions\jupyter_themes

下载配置文件 theme_selector.js 地址:

https://github.com/merqurio/jupyter_themes/blob/master/theme_selector.js

把配置文件放到上面创建的目录下
激活配置文件

jupyter nbextension enable jupyter_themes/theme_selector

打开jupyter notebook
在这里插入图片描述

### 配置d2l的Jupyter笔记本环境 为了使 Jupyter 笔记本能够顺利运行 `d2l` 库并适配不同的虚拟环境,需遵循特定配置流程。 #### 创建和激活虚拟环境 对于每一个独立项目或学习阶段,建议创建一个新的 Python 虚拟环境来隔离依赖项。这可以通过命令行工具如 `conda` 或者 `venv` 来实现: ```bash # 使用 conda 创建名为 d2l_env 的新环境,并指定 python 版本 conda create --name d2l_env python=3.8 # 激活该环境 conda activate d2l_env ``` #### 安装必要的库和支持组件 一旦进入所需的虚拟环境中,则可以继续安装 `d2l` 及其所需的支持库。确保所有必需的Python包都已更新至最新版本以获得最佳兼容性和性能支持[^3]。 ```bash pip install -U d2l ``` 此操作会自动处理大部分基础设置工作,包括但不限于下载对应的资源文件以及调整内部参数以便于后续调用。 #### 设置Jupyter Notebook内核关联 为了让当前环境下安装好的 `d2l` 正常运作于 Jupyter Notebook 中,还需注册相应的 IPython 内核给 Jupyter 认识。通过执行下面这条指令可达成目的: ```bash python -m ipykernel install --user --name=d2l_env --display-name "Python (d2l)" ``` 这里定义了一个新的 Kernel 名称为 “Python (d2l)” ,当启动 Jupyter 后可以在新建文档时选择这个选项从而应用正确的解释器路径。 #### 自定义Jupyter配置 考虑到不同项目的特殊需求,可能需要针对各个虚拟环境单独定制化 Jupyter 行为模式。按照先前提到的方法,在相应位置放置经过适当编辑后的配置文件即可满足个性化要求[^1]。 例如,如果希望更改默认保存目录或是启用某些扩展功能,都可以通过对位于各环境下的 `etc/jupyter/` 文件夹里的 `.json` 文件做适当修改来达到效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值