0. 装系统 Ubuntu 18.04
- 别装16.04
1. 安装anaconda
- 官网下载最新版本 https://www.anaconda.com/distribution/ 我的是 Anaconda3-2019.03-Linux-x86_64.sh
- 在终端输入
cd Downloads/
(默认下载路径)执行bash Anaconda3-5.2.0-Linux-x86_64.sh
,在各种问题里输入yes默认安装即可。 -
退出重新启动终端 ,输入python,可以看到已经把ubuntu自带的默认python版本切换到了anaconda
image.png - 同时终端顶部出现了base,有强迫症的同学一定受不了,输入
conda deactivate
可暂时去除 ,需要永久去除则在终端打开配置文件
gedit ~/.bashrc
在最后一行加入conda deactivate
,
2. 安装RTX 2070驱动
该方法理论上可以自动匹配到任意nvidia显卡最新驱动并进行下载安装
sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt-get update
ubuntu-drivers devices
这时候会出现几个显卡驱动型号
image.png
选择recommended那个版本进行安装 这个过程会比较久
sudo apt-get install nvidia-driver-430
安装完成后重启电脑 输入以下指令 确认显卡驱动是否安装成功
nvidia-smi
image.png
3. 安装tensorflow-gpu
输入conda install tensorflow-gpu
,会自动索引到最新版本的tensorflow-gpu以及对应版本的cudnn
成功!
-
测试
首先测试tf
import tensorflow as tf
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
查看log是否有gpu相关信息
-
调用keras中的CNN会报错!!!
- UnknownError: Failed to get convolution algorithm. This is probably because...
应该是keras的更新速度跟不上tensorflow的问题,网上说主动降级tensorflow到1.18 1.19的都有,这都不是好的解决办法。tensorflow降级需要同时改变cudnn的版本。
目前我找到的方便的解决方法是 每次在调用keras之前执行以下代码
import tensorflow as tf
from tensorflow import keras
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.Session(config = config)
keras.backend.set_session(sess)
cpu vs gpu 测试代码 https://databricks.com/tensorflow/using-a-gpu
shape的值设置的越大,速度差距越明显
import sys
import numpy as np
import tensorflow as tf
from datetime import datetime
device_name = sys.argv[1] # Choose device from cmd line. Options: gpu or cpu
shape = (int(sys.argv[2]), int(sys.argv[2]))
if device_name == "gpu":
device_name = "/gpu:0"
else:
device_name = "/cpu:0"
with tf.device(device_name):
random_matrix = tf.random_uniform(shape=shape, minval=0, maxval=1)
dot_operation = tf.matmul(random_matrix, tf.transpose(random_matrix))
sum_operation = tf.reduce_sum(dot_operation)
startTime = datetime.now()
with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as session:
result = session.run(sum_operation)
print(result)
# It can be hard to see the results on the terminal with lots of output -- add some newlines to improve readability.
print("\n" * 5)
print("Shape:", shape, "Device:", device_name)
print("Time taken:", datetime.now() - startTime)
print("\n" * 5)
-
安装chrome: 依次执行以下指令
sudo wget http://www.linuxidc.com/files/repo/google-chrome.list -P /etc/apt/sources.list.d/
sudo apt update
wget -q -O - https://dl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
apt install google-chrome-stable
#这里下载64位
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo dpkg -i google-chrome-stable_current_amd64.deb
参考文档:
https://caltong.com/158
https://github.com/moritzhambach/CPU-vs-GPU-benchmark-on-MNIST
https://caltong.com/158
https://www.liangzl.com/get-article-detail-12148.html
网友评论