简洁安装配置在Windows环境下使用vscode开发pytorch

简洁安装配置在Windows环境下使用vscode开发pytorch

使用anaconda安装pytorch,通过vscode集成环境开发pytorch

下载 anaconda

  1. 下载网址,选择对应系统的版本 https://repo.anaconda.com/archive/
    windows可以选择Anaconda3-2024.10-1-Windows-x86_64.exe
  2. 检查 conda 是否安装成功
    conda --version
  3. 升级 conda
    conda update conda
    添加镜像源
bash 复制代码
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/

查看已经添加的channels

bash 复制代码
conda config --get channels
  1. 安装conda完成 配置安装Python虚拟环境
bash 复制代码
conda create --name py311 python=3.11
conda activate py311
  1. 使用命令检查配置环境
bash 复制代码
conda env list 

安装pytorch conda install pytorch

  1. 直接执行
    安装pytorch,它默认安装的是cpu版本的pytorch。
bash 复制代码
conda install pytorch 

要安装很多插件,需要等一段时间

配置vs code 的pytorch的开发环境

  • 安装如下插件

  1. 测试pytorch是否正确安装

    新建一个扩展名为py的文件,代码如下:

    bash 复制代码
    import torch
    
    # 检查是否可以使用GPU
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    print(f"Using device: {device}")
    
    # 创建一个简单的张量
    x = torch.tensor([1.0, 2.0, 3.0], device=device)
    print(f"Tensor x: {x}")
    
    # 创建y张量
    y = torch.tensor([4.0, 5.0, 6.0], device=device)
    print(f"Tensor : {y}")
    # 张量加法
    z = x + y
    print(f"Tensor z (x + y): {z}")
    
    # 张量乘法
    w = x * y
    print(f"Tensor w (x * y): {w}")
  2. 运行py文件,右下角选择py311环境

  3. 输出如下使用CPU的环境

bash 复制代码
Using device: cpu
Tensor x: tensor([1., 2., 3.])
Tensor : tensor([4., 5., 6.])
Tensor z (x + y): tensor([5., 7., 9.])
Tensor w (x * y): tensor([ 4., 10., 18.])
相关推荐
默_笙2 天前
🍙 给每个请求过安检:FastAPI 是怎么把校验写进类型注解的
python
qq_426003962 天前
启动playwright录制codegen生成自动化测试脚本
python·自动化
虎头金猫2 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
长沙三为智能科技2 天前
家政小程序开发从0到上线:五阶段交付流程与验收清单
python
风合星语2 天前
2026 具身智能技术实战(一):VLA 到底怎么控制机器人?——用 LeRobot 跑通 SmolVLA 推理
pytorch·机器人·具身智能·vla·lerobot·smolvla
伞伞悦读2 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
只睡四小时2 天前
Canvas 弹道联机实战:700 行 + 固定时间步长
python·websocket·html5·游戏开发·canvas
Tancenter2 天前
sequeeze()和unsequeeze()
pytorch·tensorr
奇思妙想聪明勤奋的小羊2 天前
DeepAgents第5章:子Agent 与上下文隔离—让 Agent学会委派
人工智能·python·学习·语言模型
lpfasd1232 天前
2026年第38周GitHub趋势周报
python·科技·github