Ubuntu18.04 + NVidia显卡 + Anaconda3 + Tensorflow-GPU 安装、配置、测试 (无需手动安装CUDA)

摘要:
决定性因素是这篇文章https://www.pugetsystems.com/labs/hpc/Install-TensorFlow-with-GPU-Support-the-Easy-Way-on-Ubuntu-18-04-without-installing-CUDA-1170/注意兼容版本:https://devtalk.nvidia.com/default/topic/1047898

其中其决定作用的是这篇文章  https://www.pugetsystems.com/labs/hpc/Install-TensorFlow-with-GPU-Support-the-Easy-Way-on-Ubuntu-18-04-without-installing-CUDA-1170/

注意兼容版本:https://devtalk.nvidia.com/default/topic/1047898/cuda-setup-and-installation/cuda-10-1-tensorflow-1-13/2

1-安装显卡驱动

在终端执行如下命令,建议先切换到国内源,如huaweicloud mirrors。

sudo apt purge nvidia*
ubuntu-drivers devices            # 可以看到显卡等设备,和推荐的驱动
sudo ubuntu-drivers autoinstall   # 安装推荐驱动,通常是最新版

如果通过ubuntu-drivers devices看不到NVidia显卡,则添加

sudo add-apt-repository ppa:graphics-drivers
sudo apt-get update

安装完后,重启系统, 启动后,在图形界面运行Nvidia X Server Settings,可以看到显卡情况,如下图。

Ubuntu18.04 + NVidia显卡 + Anaconda3 + Tensorflow-GPU 安装、配置、测试 (无需手动安装CUDA)第1张

2-安装Anaconda+Tensorflow-GPU

安装 Anaconda

bash Anaconda3-5.3.0-Linux-x86_64.sh # make sure append the Anaconda executable directory to your PATH environment variable in .bashrc
source ~/.bashrc
python --version # to show the python version

装之前,推荐切换到国内源:

anaconda的源改为国内镜像, 配置文件是~/.condarc

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
conda config --set show_channel_urls yes

pip源改为国内镜像, 配置文件是~/.pip/pip.conf, 该后的文件内容如下:

[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple/
[install]
trusted-host=https://pypi.tuna.tsinghua.edu.cn

update conda

conda update conda -y
conda update anaconda -y
conda update python -y
conda update --all -y

安装tensorflow

conda create --name tf-gpu   # Create a Python "virtual environment" for TensorFlow using conda
conda activate tf-gpu       # 注意运行此命令后,命令行开头的提示变为(tf-gpu) user@computer:~$,表示tf-gpu环境处于激活状态
# 后面的命令,都在tf-gpu环境下执行,我保留了命令行的提示,以示区别
(tf-gpu) user@computer:~$ conda
install tensorflow-gpu -y # install TensorFlow with GPU acceleration and all of the dependencies.

为Tensorflow环境创建Jupyter Notebook Kernel

(tf-gpu) user@computer:~$ conda install ipykernel -y
(tf-gpu) user@computer:~$ conda install jupyter (tf-gpu) user@computer:~$ python -m ipykernel install --user --name tf-gpu --display-name "TensorFlow-GPU"

 安装keras

(tf-gpu) user@computer:~$ conda install keras -y
3-测试安装结果

用Keras 例程(Keras内部会用到Tensorflow)

打开Jupyter Notebook

jupyter notebook

创建新笔记: New下拉菜单 -> 选择TensorFlow-GPU

输入如下测试代码,并运行:

# Import dependencies
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.layers import Flatten,  MaxPooling2D, Conv2D
from keras.callbacks import TensorBoard

# Load and process the MNIST data
# 推荐先下载mnist.npz到目录~/.keras/datasets/
(X_train,y_train), (X_test, y_test) = mnist.load_data(path="mnist.npz")
X_train = X_train.reshape(60000,28,28,1).astype('float32')
X_test = X_test.reshape(10000,28,28,1).astype('float32')
X_train /= 255
X_test /= 255
n_classes = 10
y_train = keras.utils.to_categorical(y_train, n_classes)
y_test = keras.utils.to_categorical(y_test, n_classes)

# Create the LeNet-5 neural network architecture
model = Sequential()
model.add(Conv2D(32, kernel_size=(3,3), activation='relu', input_shape=(28,28,1)) )
model.add(Conv2D(64, kernel_size=(3,3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Flatten())          
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(n_classes, activation='softmax')) # Compile the model model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) # Set log data to feed to TensorBoard for visual analysis tensor_board = TensorBoard('./logs/LeNet-MNIST-1') # Train the model model.fit(X_train, y_train, batch_size=128, epochs=15, verbose=1, validation_data=(X_test,y_test), callbacks=[tensor_board])

运行完后查看误差曲线

 (tf-gpu) dbk@i9:~$ tensorboard --logdir=./logs --port 6006

 效果如下图

Ubuntu18.04 + NVidia显卡 + Anaconda3 + Tensorflow-GPU 安装、配置、测试 (无需手动安装CUDA)第2张

免责声明:文章转载自《Ubuntu18.04 + NVidia显卡 + Anaconda3 + Tensorflow-GPU 安装、配置、测试 (无需手动安装CUDA)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇嵌入式:J-link刷固件(坑)大数据(2)---HDFS集群搭建下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

linux系统redhat8.3安装R4.0.3(编译安装)

1、查看当前系统 [root@rhel8 home]# cat /etc/redhat-release Red Hat Enterprise Linux release 8.3 (Ootpa) [root@rhel8 home]# hostnamectl Static hostname: rhel8 Icon name: compu...

宝塔面板SSpanel-v3-mod安装教程 搭建sspanel v3魔改前端

教程演示环境及工具准备: vultr家的主机 【新用户注册可获取50美金】 CentOS 7X64 【每月5美元,无限流量】 ssh工具:xshell点击下载 连接VPS,开始安装宝塔面板 centos yum install -y wget && wget -O install.sh http://download.bt.cn/in...

HAProxy安装文档

HAProxy安装文档 目录 HAProxy安装文档 一、环境说明 二、安装配置 1.创建用户 2.安装 3.创建配置文件和启动文件 三、编辑配置文件 四、启动HAproxy 五、配置日志 1.配置rsyslog 2.创建haprxoy日志配置 3.开启远程日志 4.haproxy开启日志配置 5.重启rsyslog 和 haproxy 6....

【学习笔记】tensorflow图片读取

目录 图像基本概念 图像基本操作图像基本操作API 图像读取API 狗图片读取 CIFAR-10二进制数据读取 TFRecords TFRecords存储 TFRecords读取方法 图像基本概念 在图像数字化表示当中,分为黑白和彩色两种。在数字化表示图片的时候,有三个因素。分别是图片的长、图片的宽、图片的颜色通道数。那么黑白图片的颜色...

Liunx之Centos系统无人值守全自动化安装

  作者:邓聪聪 定制centos6.8自动安装ISO光盘 安装系统为centos6.8 (base server),安装方式为全新安装 使用ext4分区格式 安装前可以交互输入root密码,主机名,分区大小,然后安装过程自动化 关闭防火墙,selinux 网络为dhcp方式获取 时区为Asia/Shanghai 分区表类型为mbr 默认设置三个分区,b...

pip install 默认安装路径修改

一、使用命令查看pip默认安装目录 python -m site 这里的USER_BASE和USER_SITE其实就是默认的启用Python通过pip自动下载的脚本和依赖安装包的基础路径。 接着使用命令python -m site -help,便会看到如下:  以上说明了,路径的配置是在我们安装目录下的/root/anaconda3/lib/pytho...