AI知识分享AI知识分享
✿导航
  • 人工智能
  • 神经网络
  • 机器学习
  • 深度学习
  • 强化学习
  • 自然语言处理
  • 计算机视觉
  • 大模型基础
  • 动手学深度学习
  • 理论理解
  • 工程实践
  • 应用开发
  • AI For Everyone
  • AIGC_2024大会
  • AIGC_2025大会
  • Transformer
  • Pytorch
  • HuggingFace
  • 蒸馏
  • RAG
  • 目标检测
  • MCP
  • 概念
  • 意图识别
  • 工具
✿导航
  • 人工智能
  • 神经网络
  • 机器学习
  • 深度学习
  • 强化学习
  • 自然语言处理
  • 计算机视觉
  • 大模型基础
  • 动手学深度学习
  • 理论理解
  • 工程实践
  • 应用开发
  • AI For Everyone
  • AIGC_2024大会
  • AIGC_2025大会
  • Transformer
  • Pytorch
  • HuggingFace
  • 蒸馏
  • RAG
  • 目标检测
  • MCP
  • 概念
  • 意图识别
  • 工具
  • Transformer

    • Transformer - 概述
    • Transformer - Encoding and Decoding Context with Attention
    • Transformer - Tokenizers
    • Transformer - 架构
    • Transformer - Block
    • Transformer - 自注意力
    • Transformer - MoE
    • Transformer - Transformer
  • Pytorch

    • Pytorch - Dataset
    • Pytorch - TensorBoard
    • Pytorch - transforms
    • Pytorch - DataLoader
    • Pytorch - nn
    • Pytorch - Model
    • Pytorch - train
    • Pytorch - Practice
    • Pytorch - pytorch
  • HuggingFace

    • HuggingFace - Transformers
    • HuggingFace - Pipeline
    • HuggingFace - Tokenizer
    • HuggingFace - Model
    • HuggingFace - Datasets
    • HuggingFace - Evaluate
    • HuggingFace - Trainer

Anaconda 使用

创建 pytorch 的环境

主要命令

  • conda create -n pytorch python=3.11 (创建conda环境)
  • conda activate pytorch (激活该环境)
  • pip3 install torch torchvision torchaudio (在 https://pytorch.org/get-started/locally/ 选择对应安装pytorch的命令)
    • 无英伟达显卡命令
(base) C:\Users\summer>conda create -n pytorch python=3.11
WARNING: A conda environment already exists at 'D:\software\Anaconda\envs\pytorch'

Remove existing environment?
This will remove ALL directories contained within this specified prefix directory, including any other conda environments.

 (y/[n])? y

Channels:
 - defaults
Platform: win-64
Collecting package metadata (repodata.json): done
Solving environment: done

## Package Plan ##

  environment location: D:\software\Anaconda\envs\pytorch

  added / updated specs:
    - python=3.11


The following NEW packages will be INSTALLED:

  bzip2              pkgs/main/win-64::bzip2-1.0.8-h2bbff1b_6
  ca-certificates    pkgs/main/win-64::ca-certificates-2025.2.25-haa95532_0
  libffi             pkgs/main/win-64::libffi-3.4.4-hd77b12b_1
  openssl            pkgs/main/win-64::openssl-3.0.16-h3f729d1_0
  pip                pkgs/main/win-64::pip-25.0-py311haa95532_0
  python             pkgs/main/win-64::python-3.11.11-h4607a30_0
  setuptools         pkgs/main/win-64::setuptools-75.8.0-py311haa95532_0
  sqlite             pkgs/main/win-64::sqlite-3.45.3-h2bbff1b_0
  tk                 pkgs/main/win-64::tk-8.6.14-h0416ee5_0
  tzdata             pkgs/main/noarch::tzdata-2025a-h04d1e81_0
  vc                 pkgs/main/win-64::vc-14.42-haa95532_4
  vs2015_runtime     pkgs/main/win-64::vs2015_runtime-14.42.34433-he0abc0d_4
  wheel              pkgs/main/win-64::wheel-0.45.1-py311haa95532_0
  xz                 pkgs/main/win-64::xz-5.6.4-h4754444_1
  zlib               pkgs/main/win-64::zlib-1.2.13-h8cc25b3_1


Proceed ([y]/n)? y


Downloading and Extracting Packages:

Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
#     $ conda activate pytorch
#
# To deactivate an active environment, use
#
#     $ conda deactivate


(base) C:\Users\summer>conda activate pytorch

(pytorch) C:\Users\summer>pip3 install torch torchvision torchaudio
Collecting torch
...

验证 pytorch 的环境是否可用

python (验证环境)

  • python
  • import torch
  • torch.cuda.is_available()
(pytorch) C:\Users\summer>python
Python 3.11.11 | packaged by Anaconda, Inc. | (main, Dec 11 2024, 16:34:19) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> torch.cuda.is_available()
False
>>> exit()

安装Jupyter

  • conda install nb_conda (安装nb_conda以支持Jupyter)
    • 发现python3.11 不支持该中方式安装
(pytorch) C:\Users\summer>conda install nb_conda
Channels:
 - defaults
Platform: win-64
Collecting package metadata (repodata.json): done
Solving environment: / warning  libmamba Added empty dependency for problem type SOLVER_RULE_UPDATE
failed

LibMambaUnsatisfiableError: Encountered problems while solving:
  - package nb_conda-2.2.1-py27_0 requires python >=2.7,<2.8.0a0, but none of the providers can be installed

Could not solve for environment specs
The following packages are incompatible
├─ nb_conda is installable with the potential options
│  ├─ nb_conda 2.2.1 would require
│  │  └─ python >=2.7,<2.8.0a0 , which can be installed;
│  ├─ nb_conda 2.2.1 would require
│  │  └─ python >=3.5,<3.6.0a0 , which can be installed;
│  ├─ nb_conda 2.2.1 would require
│  │  └─ python >=3.6,<3.7.0a0 , which can be installed;
│  ├─ nb_conda 2.2.1 would require
│  │  └─ python >=3.7,<3.8.0a0 , which can be installed;
│  └─ nb_conda 2.2.1 would require
│     └─ python >=3.8,<3.9.0a0 , which can be installed;
└─ pin-1 is not installable because it requires
   └─ python 3.11.* , which conflicts with any installable versions previously reported.

(pytorch) C:\Users\summer>

如果上述命令OK,使用下面方法打开Jupyter(conda install nb_conda 安装失败,待解决)

(base) C:\Users\summer>conda activate pytorch
(pytorch) C:\Users\summer>jupyter notebook

解决 conda install nb_conda 报错,python3.11需要使用以下命令

(pytorch) C:\Users\summer>pip install jupyter_server
(pytorch) C:\Users\summer>conda install nb_conda_kernels
(pytorch) C:\Users\summer>conda install -c conda-forge nb_conda_kernels
(pytorch) C:\Users\summer>jupyter notebook

(pytorch) C:\Users\summer>pip install tensorboard

TensorBoard 是 TensorFlow 的一个强大的可视化工具套件

(pytorch) C:\Users\summer>pip3 install pycocotools

Python学习的两大函数

命令

  • dir(): 打开操作,能看到内部结构
  • help(): 使用说明书
dir(torch.cuda.is_available)

['__annotations__', '__builtins__', '__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__getstate__', '__globals__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__kwdefaults__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']

help(torch.cuda.is_available)

Help on function is_available in module torch.cuda:
is_available() -> bool
    Return a bool indicating if CUDA is currently available.

Jupyter 使用

(base) C:\Users\summer>conda activate pytorch (pytorch) C:\Users\summer>jupyter notebook

Pytorch 加载数据

Data: 原始数据

Dataset: 提供一种方式去获取数据及其lable

DataLoader: 为后续的网络提供不同的数据形式

最近更新: 2025/5/2 18:29
Contributors: klc407073648
Prev
Pytorch - Practice