TensorFlow2-开发环境安装(1)

淡淡的烟草味﹌ 2022-11-29 03:02 262阅读 0赞

文章目录

  • 开发环境安装
    • 深度学习框架介绍
    • 学习建议
    • 开发环境安装
      • 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安装

  1. sudo pacman -S cuda cudnn
  2. 正在解析依赖关系...
  3. :: 6 个软件包可提供 opencl-nvidia
  4. :: 软件仓库 extra
  5. 1) opencl-nvidia-340xx 2) opencl-nvidia-390xx 3) opencl-nvidia-418xx
  6. 4) opencl-nvidia-430xx 5) opencl-nvidia-435xx 6) opencl-nvidia-440xx
  7. 输入某个数字 ( 默认=1 ): 6
  8. 正在查找软件包冲突...
  9. 软件包 (5) gcc8-8.4.0-1 gcc8-libs-8.4.0-1 opencl-nvidia-440xx-440.100-1
  10. cuda-10.2.89-5 cudnn-7.6.5.32-4
  11. 下载大小: 1994.55 MiB
  12. 全部安装大小: 5182.63 MiB
  13. :: 进行安装吗? [Y/n]

python虚拟环境安装

  • manjaro默认python3

安装虚拟环境包

  1. pip install virtualenvwrapper

编辑环境变量

  1. nano ~/.bashrc
  2. or
  3. nano ~/.zshrc
  4. # >>> virtualenvwrapper initialize >>>
  5. export WORKON_HOME=$HOME/.virtualenvs
  6. VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
  7. source $HOME/.local/bin/virtualenvwrapper.sh
  8. # <<< virtualenvwrapper initialize <<<

使环境变量生效

  1. source ~/.bashrc
  2. or
  3. source ~/.zshrc

创建并进入虚拟环境

创建虚拟环境

  1. mkvirtualenv deep_learning_py38

切换虚拟环境

  1. workon deep_learning_py38

退出虚拟环境

  1. deactivate

列出

  1. lsvirtualenv

删除

  1. 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.)

  1. ~ tf.test.is_gpu_available()
  2. False # 未安装GPU驱动
  3. ~ quit()

为什么使用TensorFlow

  • GPU加速
  • 自动求导
  • 神经网络Layers

TensorFlow GPU与CPU运算

  1. import tensorflow as tf
  2. import timeit
  3. with tf.device('/cpu:0'):
  4. cpu_a = tf.random.normal([10000, 1000])
  5. cpu_b = tf.random.normal([1000, 2000])
  6. print(cpu_a.device, cpu_b.device)
  7. with tf.device('/gpu:0'):
  8. gpu_a = tf.random.normal([10000, 1000])
  9. gpu_b = tf.random.normal([1000, 2000])
  10. print(gpu_a.device, gpu_b.device)
  11. def cpu_run():
  12. with tf.device('/cpu:0'):
  13. c = tf.matmul(cpu_a, cpu_b)
  14. return c
  15. def gpu_run():
  16. with tf.device('/gpu:0'):
  17. c = tf.matmul(gpu_a, gpu_b)
  18. return c
  19. # warm up
  20. cpu_time = timeit.timeit(cpu_run, number=10)
  21. gpu_time = timeit.timeit(gpu_run, number=10)
  22. print('warmup:', cpu_time, gpu_time)
  23. cpu_time = timeit.timeit(cpu_run, number=10)
  24. gpu_time = timeit.timeit(gpu_run, number=10)
  25. print('run time:', cpu_time, gpu_time)
  26. /job:localhost/replica:0/task:0/device:CPU:0 /job:localhost/replica:0/task:0/device:CPU:0
  27. /job:localhost/replica:0/task:0/device:CPU:0 /job:localhost/replica:0/task:0/device:CPU:0
  28. warmup: 1.7047466650001297 1.916131809000035
  29. 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

  1. x=1
  2. a=2
  3. b=3
  4. c=4
  5. import tensorflow as tf
  6. x = tf.constant(1.)
  7. a = tf.constant(2.)
  8. b = tf.constant(3.)
  9. c = tf.constant(4.)
  10. with tf.GradientTape() as tape:
  11. tape.watch([a, b, c])
  12. y = a**2 * x + b * x + c
  13. [dy_da, dy_db, dy_dc] = tape.gradient(y, [a, b, c])
  14. print(dy_da, dy_db, dy_dc)
  15. tf.Tensor(4.0, shape=(), dtype=float32) tf.Tensor(1.0, shape=(), dtype=float32) tf.Tensor(1.0, shape=(), dtype=float32)

TensorFlow2 与torch 简单语法比较

  1. # TensorFlow2
  2. import tensorflow as tf
  3. a = tf.constant(1.)
  4. b = tf.constant(2.)
  5. c = tf.add(a,b)
  6. print(float(c))
  7. # torch
  8. import torch
  9. a = torch.tensor(1.)
  10. b = torch.tensor(3.)
  11. c = torch.add(a,b)
  12. print(c.item())

发表评论

表情:
评论列表 (有 0 条评论,262人围观)

还没有评论,来说两句吧...

相关阅读