TensorFlow2-开发环境安装(1)
文章目录
- 开发环境安装
- 深度学习框架介绍
- 学习建议
- 开发环境安装
- CUDA安装
- python虚拟环境安装
- 安装虚拟环境包
- 编辑环境变量
- 使环境变量生效
- 创建并进入虚拟环境
- 创建虚拟环境
- 切换虚拟环境
- 退出虚拟环境
- 列出
- 删除
- 安装TensorFlow和torch
- 为什么使用TensorFlow
- TensorFlow GPU与CPU运算
- TensorFlow2 自动求导
- TensorFlow2 与torch 简单语法比较
开发环境安装
深度学习框架介绍
Scikit-learn
- Machinelearning,No GPU
Caffe
- 2013,第一个面向深度学习的框架
- No auto-grad,C++
Keras
- wrapper
Theano
- 开发难,调试难
Torch
- Lua语言
学习建议
Pytorch和TensorFlow选一主修
- 两者都要掌握
Keras逐渐淡出
- TF+Keras
- PyTorch+Caffe2
开发环境安装
Manjaro
- sudo pacman -Syyu
Python 3.8
- 自带python3.8.3
CUDA 10.x
- cuDNN
- sudo pacman -S cuda cudnn
- TensorFlow 2.x
- PyCharm
CUDA安装
sudo pacman -S cuda cudnn
正在解析依赖关系...
:: 有 6 个软件包可提供 opencl-nvidia :
:: 软件仓库 extra
1) opencl-nvidia-340xx 2) opencl-nvidia-390xx 3) opencl-nvidia-418xx
4) opencl-nvidia-430xx 5) opencl-nvidia-435xx 6) opencl-nvidia-440xx
输入某个数字 ( 默认=1 ): 6
正在查找软件包冲突...
软件包 (5) gcc8-8.4.0-1 gcc8-libs-8.4.0-1 opencl-nvidia-440xx-440.100-1
cuda-10.2.89-5 cudnn-7.6.5.32-4
下载大小: 1994.55 MiB
全部安装大小: 5182.63 MiB
:: 进行安装吗? [Y/n]
python虚拟环境安装
- manjaro默认python3
安装虚拟环境包
pip install virtualenvwrapper
编辑环境变量
nano ~/.bashrc
or
nano ~/.zshrc
# >>> virtualenvwrapper initialize >>>
export WORKON_HOME=$HOME/.virtualenvs
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source $HOME/.local/bin/virtualenvwrapper.sh
# <<< virtualenvwrapper initialize <<<
使环境变量生效
source ~/.bashrc
or
source ~/.zshrc
创建并进入虚拟环境
创建虚拟环境
mkvirtualenv deep_learning_py38
切换虚拟环境
workon deep_learning_py38
退出虚拟环境
deactivate
列出
lsvirtualenv
删除
rmvirtualenv deep_learning_py38
安装TensorFlow和torch
切换环境变量
- workon deep_learning_py38
查看是否切换成功
- pip -V
安装 tensorflow
- pip install tensorflow tensorflow-gpu torch
测试
(deep_learning_py38) ~ python [130]
Python 3.8.3 (default, May 17 2020, 18:15:42)
[GCC 10.1.0] on linux
Type “help”, “copyright”, “credits” or “license” for more information.~ import tensorflow as tf
~ tf.constant(1.)+tf.constant(2.)
~ tf.test.is_gpu_available()
False # 未安装GPU驱动
~ quit()
为什么使用TensorFlow
- GPU加速
- 自动求导
- 神经网络Layers
TensorFlow GPU与CPU运算
import tensorflow as tf
import timeit
with tf.device('/cpu:0'):
cpu_a = tf.random.normal([10000, 1000])
cpu_b = tf.random.normal([1000, 2000])
print(cpu_a.device, cpu_b.device)
with tf.device('/gpu:0'):
gpu_a = tf.random.normal([10000, 1000])
gpu_b = tf.random.normal([1000, 2000])
print(gpu_a.device, gpu_b.device)
def cpu_run():
with tf.device('/cpu:0'):
c = tf.matmul(cpu_a, cpu_b)
return c
def gpu_run():
with tf.device('/gpu:0'):
c = tf.matmul(gpu_a, gpu_b)
return c
# warm up
cpu_time = timeit.timeit(cpu_run, number=10)
gpu_time = timeit.timeit(gpu_run, number=10)
print('warmup:', cpu_time, gpu_time)
cpu_time = timeit.timeit(cpu_run, number=10)
gpu_time = timeit.timeit(gpu_run, number=10)
print('run time:', cpu_time, gpu_time)
/job:localhost/replica:0/task:0/device:CPU:0 /job:localhost/replica:0/task:0/device:CPU:0
/job:localhost/replica:0/task:0/device:CPU:0 /job:localhost/replica:0/task:0/device:CPU:0
warmup: 1.7047466650001297 1.916131809000035
run time: 1.6430040090001512 1.641810919000136
TensorFlow2 自动求导
y = a 2 ∗ x + b ∗ x + c y = a^2 * x + b*x + c y=a2∗x+b∗x+c
x=1
a=2
b=3
c=4
import tensorflow as tf
x = tf.constant(1.)
a = tf.constant(2.)
b = tf.constant(3.)
c = tf.constant(4.)
with tf.GradientTape() as tape:
tape.watch([a, b, c])
y = a**2 * x + b * x + c
[dy_da, dy_db, dy_dc] = tape.gradient(y, [a, b, c])
print(dy_da, dy_db, dy_dc)
tf.Tensor(4.0, shape=(), dtype=float32) tf.Tensor(1.0, shape=(), dtype=float32) tf.Tensor(1.0, shape=(), dtype=float32)
TensorFlow2 与torch 简单语法比较
# TensorFlow2
import tensorflow as tf
a = tf.constant(1.)
b = tf.constant(2.)
c = tf.add(a,b)
print(float(c))
# torch
import torch
a = torch.tensor(1.)
b = torch.tensor(3.)
c = torch.add(a,b)
print(c.item())
还没有评论,来说两句吧...