# Tensorflow
###### tags: `Tensorflow` `Anaconda`
## 安裝tensorflow-gpu
`nvidia-smi`
## 一. 安裝tensorflow-gpu
`更新日期20220219
`環境:win10
1. 建立Anaconda 虛擬環境
`conda create --name arg `
查看tensorflow_gpu 對應版本
2. 檢查tensorflow_gpu 對應cuda版本
https://www.tensorflow.org/install/source_windows
這裡是使用`tf2.3`
3. 下載 cudnn 7.6
`\cuda\bin ` 把下載的cudnn壓縮檔解壓縮後把`bin`這個資料夾位置加入到環境變數
4. 下載 cuda 10.1
https://developer.nvidia.com/cuda-10.1-download-archive-base?target_os=Windows&target_arch=x86_64&target_version=10&target_type=exelocal
`C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1`
`bin` `libnvvp` 通常會自動掛上環境變數
5. 下載 tensorflow_gpu
`pip install tensorflow-gpu==2.3`
## 二.指定某個GPU做計算
```python=
tf.debugging.set_log_device_placement(True)
# Place tensors on the CPU
print("Num GPUs Available: ", len(tf.config.list_physical_devices('GPU')))
print(tf.config.list_physical_devices('GPU'))
with tf.device('/GPU:1'):
a = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
b = tf.constant([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])
# Run on the GPU
c = tf.matmul(a, b)
print(c)
```
## cuda多版本控制
https://www.daimajiaoliu.com/daima/485a735ba900404
利用 cudatoolkit 來做控制
`pip install tensorflow-gpu==2.6.0`
`conda install cudatoolkit=11.3.1`
`conda install cudnn=8.2.1`
> 他會載到最新的 Keras,要自己刪掉裝對應的 Keras 2.6.0 版本
## 問題
### spyder 閃退
遇到編碼錯誤 `cp950`
推測是版本問題,後來降版本就不會閃退了
## 其他使用技巧
```python=
def checkGPU():
import tensorflow as tf
closeLoggin() # 關到他媽tensorflow 垃圾 loggin 輸出
print('GPU is avilible -> {}'.format(tf.test.is_gpu_available()))
def closeLoggin(): # 關到他媽tensorflow 垃圾 loggin 輸出
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
def limitGPU():
""" 限制GPU 增長 防止爆掉 """
import tensorflow as tf
gpus = tf.config.experimental.list_physical_devices('GPU')
tf.config.experimental.set_memory_growth(gpus[0], True) ```