PyTorch学习二 —— CentOS 7 安装多环境 Anaconda3-5.3.0 + PyTorch 0.4.1

安装 Anaconda3-5.3.0

1、进入官网获取下载链接

官网:https://www.anaconda.com/download/#linux -> Download 右击复制链接地址
在这里插入图片描述

2、在CentOS 中执行安装

# 下载
wget https://repo.anaconda.com/archive/Anaconda3-5.3.0-Linux-x86_64.sh

# 执行安装
sh Anaconda3-5.3.0-Linux-x86_64.sh

安装过程中会显示配置路径
Prefix=/home/app/anaconda3
在这里插入图片描述
设置环境变量
在这里插入图片描述
验证安装 执行

python

显示以下内容则安装成功
在这里插入图片描述

3、创建 Anaconda 的 PyTorch 环境

此部分来源于:https://blog.csdn.net/thormas1996/article/details/80822268

创建一个新的python3环境,新环境一般会安装在anaconda/bin/envs下,也可以自定义路径

conda create --name PyTorch python=3.7

注:如果报错python3不在channel中,表明bin目录下没有python3文件。
配置需要的python版本前先到anaconda/bin下查看有哪些python的版本,文件名字是什么。
比方说有时候python3会被默认命名为python或者python36,不确定python版本的话,执行 python 命令查看。

执行后如下图:
在这里插入图片描述

激活 PyTorch 环境

source activate PyTorch

# 其它相关命令
# 退出虚拟环境
source deactivate

# 查看已建立的虚拟环境
conda info --envs

# 删除环境
conda remove -n PyTorch --all

# 修改环境名称
conda create --name pytorch-0.4.1 --clone PyTorch

如下图所示:
在这里插入图片描述

安装 PyTorch 0.4.1

1、官网查询下载命令

官网:https://pytorch.org/get-started/locally/

在这里插入图片描述

2、安装 PyTorch

conda install pytorch -c pytorch
pip3 install torchvision

如下图所示:
在这里插入图片描述
在这里插入图片描述

3、验证安装

python

import torch

a = torch.Tensor((3,5))
print(a)

如图所示
在这里插入图片描述

自动化安装脚本

installPyTorch.sh
下载地址:https://download.csdn.net/download/qq_28413435/10752554
注:执行到安装Anaconda时需要在是否将变量写入系统环境变量选项中选择yes,否则需要自行配置环境变量,导致安装失败不能继续进行,默认安装路径:/home/app/anaconda3,如需修改请自行修改Shell文件。

#!/bin/bash

# Download Anaconda3-5.3.0-Linux-x86_64.sh
if [ ! -f "/root/Anaconda3-5.3.0-Linux-x86_64.sh" ];then
    echo "===================== Downloading the Anaconda3-5.3.0-Linux-x86_64.sh ====================="
    wget https://repo.anaconda.com/archive/Anaconda3-5.3.0-Linux-x86_64.sh
fi

# Run Anaconda3-5.3.0-Linux-x86_64.sh
echo "===================== Installing the Anaconda3-5.3.0 ====================="
yum install -y bzip2
sh Anaconda3-5.3.0-Linux-x86_64.sh
if [ $? -ne 0 ];then
    rm -rf /home/app/anaconda3
    exit $?
fi

source ~/.bashrc

# Show Conda Version
conda -V
if [ $? -ne 0 ];then
    exit $?
fi

# Set up Tsinghua mirror image
# echo "===================== Set up Tsinghua mirror image ====================="
# 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/pytorch/

# conda config --set show_channel_urls yes

# Create Anaconda Environments
echo "===================== Create Anaconda Environments ====================="
conda create --name PyTorch python=3.7

# Activate the PyTorch Environment
source activate PyTorch

# Show Python Version
python -V

# Installing PyTorch0.4.1
echo "===================== Installing PyTorch0.4.1 ====================="
conda install pytorch -c pytorch
if [ $? -ne 0 ];then
    exit $?
fi
pip install torchvision
if [ $? -ne 0 ];then
    exit $?
fi

猜你喜欢

转载自blog.csdn.net/qq_28413435/article/details/83449113