简洁安装配置在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.])
相关推荐
happylifetree19 小时前
Python09:核心语法-数据存储与运算-字面量
python
心之语歌19 小时前
Tkinter 画布基本梳理
运维·服务器·python
Wx-bishekaifayuan19 小时前
django个性化旅游路线推荐平台49005-计算机课程设计、毕业设计
spring boot·后端·python·django·课程设计·express·旅游
小白快快跑哦20 小时前
python-字符串全解(六):正则表达式-量词
python·正则表达式·字符串
禹凕21 小时前
机器学习之Selenium(Machina Learning about Selenium)
爬虫·python·selenium·测试工具·机器学习
ebiobiz1 天前
基于 GD32 Embedded Builder (GEB) 与 Nimmake 的 MCU 工程搭建指南
c++·python·单片机·嵌入式硬件·mcu
老歌老听老掉牙1 天前
穿越时空的物流密码:VRP与VRPTW的深度剖析及实战求解
python·物流·vrp
FYKJ_20101 天前
django电影推荐系统55066-计算机课程设计、毕业设计
vue.js·spring boot·python·mysql·typescript·spark·django
ShyanZh1 天前
【Python3基础】13-Python 常用设计模式
开发语言·python·设计模式
小静AI工程实验室1 天前
RSA 算法详解:从模幂原理到 OAEP 加密、PSS 签名与 Python 实验
python·算法