动手学深度学习PyTorch版初步安装过程

目录

  • [一、 预先配置](#一、 预先配置)
  • [二、 错误处理](#二、 错误处理)
    • [1. chapter_preliminaries/ndarray 数据操作](#1. chapter_preliminaries/ndarray 数据操作)
    • [2. chapter_preliminaries/pandas 数据预处理](#2. chapter_preliminaries/pandas 数据预处理)
    • [3. chapter_linear-networks/linear-regression 线性回归](#3. chapter_linear-networks/linear-regression 线性回归)
    • [4. chapter_linear-networks/softmax-regression-concise softmax回归的简洁实现](#4. chapter_linear-networks/softmax-regression-concise softmax回归的简洁实现)

时隔一段时间回来看这些东西,遇到的问题其实基本都是非常基础的包的安装,但是对于第一次接触这些的小白,会感觉有点难度,经历过这个过程就好了

一、 预先配置

在Windows环境下配置

我已安装过anaconda,pytorch,python之类的

  1. 创建环境

    创建一个新环境,会安装在Conda默认的envs目录下,我是D:\Downloads\Anaconda\envs\d2l-zh1

    shell 复制代码
    # 创建环境
    conda create -n d2l-zh python=3.8 pip//改成3.9
    
    # 激活环境
    conda activate d2l-zh
    
    # 安装需要的包
    pip install -y jupyter d2l torch torchvision
    
    # 下载压缩包
    powershell -Command "Invoke-WebRequest -Uri 'https://zh-v2.d2l.ai/d2l-zh.zip' -OutFile 'D:\Downloads\Anaconda\pkgs\d2l-zh\d2l-zh.zip'"

    下载好慢啊,估计是要梯子,建议浏览器搜索https://zh-v2.d2l.ai/d2l-zh.zip直接下载,解压到自己想存放的位置,到存放位置目录手动解压

  2. 为后面的jupyter添加内核

    shell 复制代码
    # 安装 IPython 内核
    pip install ipykernel
    
    # 将环境注册为 Jupyter 内核
    python -m ipykernel install --user --name=d2l-zh --display-name= d2l-zh
    
    # 切换到目标目录运行jupyter Notebook
    D:
    D:\Downloads\Anaconda\pkgs\d2l-zh
    
    # 启动Jupyter Notebook
    jupyter notebook                     

    目录中四个文件夹是不同的版本

二、 错误处理

1. chapter_preliminaries/ndarray 数据操作

点开/pytorch/index.ipynb,下面几个链接按顺序看就行

错误处理

  1. 错误1

    运行import torch报错显示

    shell 复制代码
    ModuleNotFoundError            Traceback (most recent call last) 
    Cell In[1], line 1 
    ----> 1 import torch 
    
    ModuleNotFoundError: No module named 'torch'

    原因:环境没安装pytorch
    解决:

    在jupyter notebook的随便一个目录下右上角新建一个d2l-zh内核的文件

    运行命令

    shell 复制代码
    !pip list

    发现没有torch包,要安装torch

    运行命令

    shell 复制代码
    !pip install torch

    要关掉或者打开梯子,并稍等一会

    再运行

    shell 复制代码
    !pip list

    检查发现已安装

  2. 错误2

    运行import torch报错显示

    网上说可能是numpy版本冲突

    1. 新建文件

    2. 卸载numpy

      shell 复制代码
      conda uninstall numpy

      显示

      shell 复制代码
      Note: you may need to restart the kernel to use updated packages. PackagesNotFoundError: The following packages are missing from the target environment: - numpy

      貌似没有环境里numpy

    3. 安装numpy

      shell 复制代码
      conda install numpy
    4. 重启jupyter notebook,解决

2. chapter_preliminaries/pandas 数据预处理

没装pandas,取消第二行的注释即可

不知道为什么第一次可以,后面不行了,遇到了这个错误

解决方式:改成inputs = inputs.fillna(inputs.mean(numeric_only=True))

3. chapter_linear-networks/linear-regression 线性回归

  1. 错误1

    原因: Python 环境中没有安装 matplotlib 库

    解决:

    1. 在jupyter notebook运行命令

      shell 复制代码
      !pip install matplotlib

    开梯子会报错,不开梯子又太慢了,放弃
    2. 打开终端,激活d2l-zh

    shell 复制代码
    pip install matplotlib

    卡在某个进度条,速度很慢

    改用下载清华源镜像

    shell 复制代码
    	pip install matplotlib -i https://pypi.tuna.tsinghua.edu.cn/simple

    但是不知道为什么提示说都已经安装过了,奇奇怪怪

  2. 错误2

    安装d2l库时, 下载清华源镜像
    shell pip install d2l -i https://pypi.tuna.tsinghua.edu.cn/simple

    会报错,要求python版本≥3.9

    运行
    shell python --version

    发现python版本为3.8.20,之前安装时安装版本过低

    升级一下python版本

    shell 复制代码
    conda install python=3.9

    然后再重新安装一下d2l

    shell 复制代码
    pip install d2l -i https://pypi.tuna.tsinghua.edu.cn/simple

    有两个警告说文件无法移除,应该没什么影响

  3. 错误3

    安装torchvision

    shell 复制代码
    pip install torchvision -i https://pypi.tuna.tsinghua.edu.cn/simple

    有一个警告说文件无法移除,应该没什么影响

  4. 错误4

    摆烂重装

    shell 复制代码
    conda deactivate
    conda remove -n d2l-zh --all
    conda create -n d2l-zh python=3.9 pip //关掉梯子
    conda activate d2l-zh
    pip install --trusted-host pypi.org --trusted-host pypi.python.org --trusted-host=files.pythonhosted.org d2l-zh //下载好慢
    pip install torch
    pip install --user d2l //之前安装的d2l-zh没有用,这个飞快
    pip install torchvision

    服了,终于成功了

4. chapter_linear-networks/softmax-regression-concise softmax回归的简洁实现

最后一行d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, trainer)

报错提示:
AttributeError: module 'd2l' has no attribute 'train_ch3
解决:
C:\Users\Forest\AppData\Roaming\Python\Python39\site-packages\d2l\torch.py文件夹(要先启动jupyter notebook打开d2l-zh内核才会显示)中加入上一节的如下函数,def前注意不要空格,然后重启jupyter notebook

python 复制代码
def evaluate_accuracy(net, data_iter)
def train_epoch_ch3(net, train_iter, loss, updater)
def train_ch3(net, train_iter, test_iter, loss, num_epochs, updater)
def predict_ch3(net, test_iter, n=6)

相关推荐
zhippt_1014几秒前
教师做课件太耗时?AI生成PPT工具与图转PPT实测梳理
人工智能·html·powerpoint
环境栈笔记4 分钟前
指纹浏览器安卓怎么用?从运行位置到功能验收,先把三种方案分清
前端·人工智能·后端·自动化
赛博山人5 分钟前
DeepSeek发布Super-agent
人工智能
鬼手点金34 分钟前
提示注入:上下文安全的核心威胁
人工智能·安全·microsoft·机器学习·缓存·prompt
山川绿水36 分钟前
DeepSeek-DSH安装报错[deploy] 命令失败:npx -y pnpm install --prefer-offline
人工智能·网络安全
ZhengEnCi37 分钟前
脉冲神经网络是什么:从LIF神经元到第三代神经网络的能耗优势
人工智能·神经网络·机器学习
小虎AI生活39 分钟前
腾讯 Buddy One 上手:跨应用 AI 协同的配置步骤与安全红线(附派活验收清单)
人工智能·超级个体·一人公司·青玥ai
Solange__43 分钟前
AI世界模型、3D场景生成和游戏引擎怎么选?按5类任务比较交互、物理与导出
开发语言·人工智能·游戏·平面·3d·自然语言处理·游戏引擎
loulanyue_1 小时前
物理AI的五次跃迁与部署态终局:从Worken度量衡到灵渠OS开源——读智元机器人联合创始人兼CTO彭志辉(稚晖君)演讲
人工智能·具身智能·智元机器人·稚晖君
Solange__1 小时前
物流园区怎么用AI生成3D调度预演?从道路底图、车辆转弯到网页评审
图像处理·人工智能·游戏·3d·ai·自然语言处理·关卡设计