安装pytorch和tensorflow

系统:macOS

0x00: 下载软件anaconda

0x01: anaconda 安装pytorch

1、打开电脑终端,输入:

1
2
3
4
5
6
7
8
9
10
conda create -n MyPytorch python=3.7
conda activate MyPytorch

#安装
conda install pytorch torchvision torchaudio -c pytorch

#测试安装成功
import torch
torch.__version__
x=torch.rand(2,3)

2、anaconda使用命令

1
2
3
4
5
6
7
8
9
10
# 查看安装了哪些包。
conda list
# 查看当前存在哪些虚拟环境
conda env list 或 conda info -e或 conda info --envs
# 删除环境
conda remove -n your_env_name(虚拟环境名称) --all
# 克隆环境
conda create --name clone_env --clone envname
# 查看conda环境信息
conda info --envs

0x02: anconda 安装tensorflow 1.9

1
2
3
conda create -n tensorflow1.9 python=3.6
conda activate tensorflow1.9
conda install tensorflow==1.9

测试

1
2
3
4
5
6
7
8
9
conda activate tensorflow1.9
python
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))

如无误则输出以下:
b'Hello, TensorFlow!'

0x03: 安装pytorch和tensorflow2.0

1
2
3
4
conda create -n PtANDTf python=3.7
conda activate PtANDTf
conda install tensorflow==2.0
conda install pytorch torchvision torchaudio -c pytorch

2.0版本的tf 移除了session用上面的测试会报错

1
module 'tensorflow' has no attribute 'Session'

tensorflow的测试代码改为以下代码再运行测试
1
2
3
4
import tensorflow as tf
version = tf.__version__
gpu_ok = tf.test.is_gpu_available()
print("tf version:",version,"\nif use GPU",gpu_ok)

显示结果如下 tf安装成功
1
2
tf version: 2.0.0 
if use GPU False