当前位置:   article > 正文

Ubuntu 18.04+NVidia显卡+Anaconda3+Tensorflow-GPU安装、配置、测试(无需手动安装CUDA)_ubuntu18.04怎么测试显卡有没有坏

ubuntu18.04怎么测试显卡有没有坏

1. 安装显卡驱动

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

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

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

  1. sudo add-apt-repository ppa:graphics-drivers
  2. sudo apt-get update

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

2. 安装Anaconda+Tensorflow-GPU

先下载Anaconda,下载地址:

Anaconda | Anaconda Distribution

下载适合电脑Python版本和计算平台的版本。

安装 Anaconda

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

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

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

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

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

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

update conda

  1. conda update conda -y
  2. conda update anaconda -y
  3. conda update python -y
  4. conda update --all -y

安装tensorflow

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

为Tensorflow环境创建Jupyter Notebook Kernel

  1. (tf-gpu) user@computer:~$ conda install ipykernel -y
  2. (tf-gpu) user@computer:~$ conda install jupyter
  3. (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

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

  1. # Import dependencies
  2. import keras
  3. from keras.datasets import mnist
  4. from keras.models import Sequential
  5. from keras.layers import Dense, Dropout
  6. from keras.layers import Flatten, MaxPooling2D, Conv2D
  7. from keras.callbacks import TensorBoard
  8. # Load and process the MNIST data
  9. # 推荐先下载mnist.npz到目录~/.keras/datasets/
  10. (X_train,y_train), (X_test, y_test) = mnist.load_data(path="mnist.npz")
  11. X_train = X_train.reshape(60000,28,28,1).astype('float32')
  12. X_test = X_test.reshape(10000,28,28,1).astype('float32')
  13. X_train /= 255
  14. X_test /= 255
  15. n_classes = 10
  16. y_train = keras.utils.to_categorical(y_train, n_classes)
  17. y_test = keras.utils.to_categorical(y_test, n_classes)
  18. # Create the LeNet-5 neural network architecture
  19. model = Sequential()
  20. model.add(Conv2D(32, kernel_size=(3,3), activation='relu', input_shape=(28,28,1)) )
  21. model.add(Conv2D(64, kernel_size=(3,3), activation='relu'))
  22. model.add(MaxPooling2D(pool_size=(2,2)))
  23. model.add(Dropout(0.25))
  24. model.add(Flatten())
  25. model.add(Dense(128, activation='relu'))
  26. model.add(Dropout(0.5))
  27. model.add(Dense(n_classes, activation='softmax')) # Compile the model
  28. model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) # Set log data to feed to TensorBoard for visual analysis
  29. tensor_board = TensorBoard('./logs/LeNet-MNIST-1') # Train the model
  30. model.fit(X_train, y_train, batch_size=128, epochs=15, verbose=1,
  31. validation_data=(X_test,y_test), callbacks=[tensor_board])

运行完后查看误差曲线

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

 效果如下图

参考文献

https://www.pugetsystems.com/labs/hpc/Install-TensorFlow-with-GPU-Support-the-Easy-Way-on-Ubuntu-18-04-without-installing-CUDA-1170/

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/813655
推荐阅读
相关标签
  

闽ICP备14008679号