用Conda配置Pytorch环境 (pytorch==2.2.1)

用Conda配置Pytorch环境 (pytorch==2.2.1)

本文主要讲解: 如何用Conda搭建Pytorch环境,用Conda的方式安装,不需要单独去安装Cuda了。

1. 安装miniconda

https://docs.anaconda.com/free/miniconda/index.html
在这里插入图片描述

2. 搭建虚拟环境

激活python虚拟环境

conda create -n env310 python==3.10.14 -y
conda activate env310

安裝Pytorch

conda install pytorch==2.2.1 torchvision==0.17.1 torchaudio==2.2.1 pytorch-cuda=11.8 -c pytorch -c nvidia

安裝常用包

pip install scikit-learn einops ipywidgets pandas tqdm jupyterlab matplotlib seaborn

3. Conda设置清华源

conda config --show channels

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 --set show_channel_urls yes

4. pip设置清华源

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

5. 一个分类网络的例子

测试Pytorch环境是否Okay

conda activate env310
python mnist.py

文件mnist.py内容:

# Task
# Our task is simple, recognize handwritten digits. We will use MNIST dataset for this tutorial.
# 

# # Import necessary library
# In this tutorial, we are going to use pytorch, the cutting-edge deep learning framework to complete our task.

# In[2]:


import torch
import torchvision


# In[3]:


## Create dataloader, in PyTorch, we feed the trainer data with use of dataloader
## We create dataloader with dataset from torchvision, 
## and we dont have to download it seperately, all automatically done

# Define batch size, batch size is how much data you feed for training in one iteration
batch_size_train = 64 # We use a small batch size here for training
batch_size_test = 1024 #

# define how image transformed
image_transform = torchvision.transforms.Compose([
                               torchvision.transforms.ToTensor(),
                               torchvision.transforms.Normalize(
                                 (0.1307,), (0.3081,)

相关推荐

  1. Pip配置Pytorch环境 (Pytorch==2.3.0)

    2024-06-06 11:56:05       9 阅读
  2. pytorch环境配置

    2024-06-06 11:56:05       41 阅读
  3. ROS2/ROS+conda+pytorch配置

    2024-06-06 11:56:05       41 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-06 11:56:05       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-06 11:56:05       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-06 11:56:05       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-06 11:56:05       18 阅读

热门阅读

  1. 固定数量点云采样算法

    2024-06-06 11:56:05       8 阅读
  2. 自然语言处理(NLP)中的迁移学习

    2024-06-06 11:56:05       8 阅读
  3. 只需三步,使用 KRaft 建立多节点 Kafka 集群

    2024-06-06 11:56:05       12 阅读
  4. vue进阶

    2024-06-06 11:56:05       8 阅读
  5. Github 2024-06-02 开源项目日报Top10

    2024-06-06 11:56:05       10 阅读
  6. mac设置截屏的默认保存位置

    2024-06-06 11:56:05       10 阅读