建立深度学习工作站:在Ubuntu上安装Keras及其依赖

一、建立深度学习工作站的步骤

(1)安装Python科学套件( NumpyScipy) + 基础线性代数子程序库( BLAS )
(2)安装HDF5(用于保存大型的神经网络文件) + Graphviz(用于将神经网络架构可视化)
(3)安装CUDA驱动程序和cuDNN
(4)安装一个Keras后端:TensorFlow
(5)安装Keras

1.参考书目:《Python深度学习(Keras)》附录
2.说明:假设已经配备NVIDIA GPU(并装好了驱动),以及pip3是最新版本!(很重要!)
3.我的环境:
①OS:Ubuntu 18.04.4 LTS
②Python:3.6.9
③Pip3:21.1.3
4.版本选择(我的选择,仅供参考):
①CUDA:10.0.130
②cuDNN:7.6.1
③tensorflow-gpu:1.15.0
④keras:2.3.1

二、基础安装

1.安装BLAS

sudo apt-get install build-essential cmake git unzip \
pkg-config libopenblas-dev liblapack-dev

2.安装Python科学套件:NumpySciPyMatplotlib

sudo apt-get install python-numpy python-scipy python-matplotlib python-yaml

3.安装HDF5

sudo apt-get install libhdf5-serial-dev python-h5py

4.安装Graphviz和pydot-ng

sudo apt-get install graphviz
sudo pip install pydot-ng

这里没用pip3影响不大。

5.安装python-opencv

sudo apt-get install python-opencv

上述的安装属于基础安装,比较顺利。

三、重点:安装CUDAcuDNN

按照我写的博客,我在2台服务器上都顺利安装了CUDA和cuDNN。

四、安装tensorflow-gpu

1.安装tensorflow-gpu==1.15.0
pip3 install tensorflow-gpu==1.15.0 -i https://pypi.tuna.tsinghua.edu.cn/simple在这里插入图片描述

务必用最新的pip3安装,否则会遇到如下问题:
在这里插入图片描述
更新pip3的命令:pip3 install --upgrade pip

在安装过程中,Python3.7失败过,所以,我修改了Python版本。

2.为特定用户修改Python版本的方法。(不用修改Python版本,慎用alias)

vim ~/.bashrc
source ~/.bashrc

在这里插入图片描述
在这里插入图片描述

.bashrc文件末尾添加上图内容。
前提:装了Python3.6!

3.验证
在这里插入图片描述

4.补充:这里的安装容易超时,下面是各种镜像地址
(1)中国科学技术大学:https://pypi.mirrors.ustc.edu.cn/simple/
(2)阿里云:http://mirrors.aliyun.com/pypi/simple/
(3)豆瓣:http://pypi.douban.com/simple/
(4)清华大学:https://pypi.tuna.tsinghua.edu.cn/simple/

五、安装keras

pip3 install keras==2.3.1 -i https://pypi.tuna.tsinghua.edu.cn/simple
在这里插入图片描述
在这里插入图片描述

六、验证

1.深度学习的Hello World:手写数字识别
(1)代码

# keras这个库,自带了一些数据集,包括经典的手写数字识别数据集MNIST
from keras.datasets import mnist

#加载数据 (keras文档可以查到这种用法)
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()

# 网络架构
from keras import models
from keras import layers

network = models.Sequential()
# layers.Dense(...) 构建全连接层
network.add(layers.Dense(512, activation='relu', input_shape=(28 * 28,)))
network.add(layers.Dense(10, activation='softmax'))

#编译
network.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])

# 数据预处理
train_images = train_images.reshape((60000, 28 * 28))
train_images = train_images.astype('float32') / 255
test_images = test_images.reshape((10000, 28 * 28))
test_images = test_images.astype('float32') / 255

# 准备标签
from keras.utils import  to_categorical
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)

# 拟合模型,fit
network.fit(train_images, train_labels, epochs=5, batch_size=128)

# 测试
test_loss, test_acc = network.evaluate(test_images, test_labels)
print('test_acc:', test_acc)

(2)运行结果
在这里插入图片描述

  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值