Keras在linux下安装和配置备忘录
1、先安装anaconda.下载python2.7 https://www.continuum.io/downloads安装命令bash Anaconda2-4.2.0-Linux-x86_64.sh2.
参考:Keras中文文档:http://keras-cn.readthedocs.io/en/latest/getting_started/keras_linux/
1、
1、先安装anaconda.
下载python2.7 https://www.continuum.io/downloads
安装命令
bash Anaconda2-4.2.0-Linux-x86_64.sh
2. Ubuntu初始环境设置
安装开发包打开终端输入:
# 系统升级
>>> sudo apt update
>>> sudo apt upgrade
# 安装python基础开发包
>>> sudo apt install -y python-devpython-pip python-nose gcc g++ git gfortran vim
安装运算加速库打开终端输入:
>>> sudo apt install -ylibopenblas-dev liblapack-dev libatla
3. CUDA开发环境的搭建(CPU加速跳过)
如果您的仅仅采用cpu加速,可跳过此步骤 -下载CUDA8.0
下载地址:
https://developer.nvidia.com/cuda-downloads
之后打开终端输入:
>>> sudo dpkg -i cuda-repo-ubuntu1604-8-0-local_8.0.44-1_amd64.deb
>>> sudo apt update
>>> sudo apt install cuda
自动配置成功就好。
· 将CUDA路径添加至环境变量在终端输入:
>>> sudo vim /etc/bash.bashrc
在bash.bashrc文件中添加:
export CUDA_HOME=/usr/local/cuda-8.0
export PATH=/usr/local/cuda-8.0/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
之后source vim/etc/.bashrc即可同样,在终端输入:
>>> sudo vim ~/.bashrc
在.bashrc中添加如上相同内容(如果您使用的是zsh,在~/.zshrc添加即可)
· 测试在终端输入:
>>> nvcc -V (注意大写)
会得到相应的nvcc编译器相应的信息,那么CUDA配置成功了。记得重启系统
安装cuDNN 5.1, Linux目前就是cudnn-8.0-win-x64-v5.1-prod.zip。
tar ‐zxf cudnn‐8.0‐linux‐x64‐v5.1.tgz
cd cuda
sudo cp lib64/* /usr/local/cuda/lib64/
sudo cp include/* /usr/local/cuda/include/
Keras框架搭建
相关开发包安装
在终端中输入:
>>> sudo pip install -U --pre pipsetuptools wheel
>>> sudo pip install -U --prenumpy scipy matplotlib scikit-learn scikit-image
>>> pip install -U --pre theano(注意不要带sudo)
>>> pip install -U --pre keras
安装完毕后,输入python,然后输入:
>>> import theano
>>> import keras
如果没有任何提示,则表明安装已经成功
Import keras 时会出现错误,需要配置keras的环境。
Keras环境设置
- 修改默认keras后端 在终端中输入:
>>> vim ~/.keras/keras.json
- "image_dim_ordering": "th",
- "epsilon": 1e-07,
- "floatx": "float32",
- "backend": "theano"
配置theano文件在终端中输入:
>>> vim ~/.theanorc
并写入以下:
[global]
openmp=False
device = gpu
floatX = float32
allow_input_downcast=True
[lib]
cnmem = 0.8
[blas]
ldflags= -lopenblas
[nvcc]
fastmath = True
之后可以验证keras是否安装成功,在命令行中输入Python命令进入Python变成命令行环境:
>>>import keras
速度测试
新建一个文件test.py,内容为:
from theano import function, config,shared, sandbox
import theano.tensor as T
import numpy
import time
vlen = 10 * 30 * 768 # 10 x #cores x # threads per core
iters = 1000
rng = numpy.random.RandomState(22)
x = shared(numpy.asarray(rng.rand(vlen),config.floatX))
f = function([], T.exp(x))
print(f.maker.fgraph.toposort())
t0 = time.time()
for i in xrange(iters):
r= f()
t1 = time.time()
print("Looping %d times took %f seconds"% (iters, t1 - t0))
print("Result is %s" % (r,))
if numpy.any([isinstance(x.op, T.Elemwise)for x in f.maker.fgraph.toposort()]):
print('Used the cpu')
else:
print('Usedthe gpu')结果:$python test.py
Using gpu device 0: GeForce GTX 1070 (CNMeM is enabled with initial size: 80.0% of memory, cuDNN 5105)
/home/aaa/anaconda2/lib/python2.7/site-packages/theano/sandbox/cuda/__init__.py:600: UserWarning: Your cuDNN version is more recent than the one Theano officially supports. If you see any problems, try updating Theano or downgrading cuDNN to version 5.
warnings.warn(warn)
[GpuElemwise{exp,no_inplace}(<CudaNdarrayType(float32, vector)>), HostFromGpu(GpuElemwise{exp,no_inplace}.0)]
Looping 1000 times took 0.380512 seconds
Result is [ 1.23178029 1.61879349 1.52278066 ..., 2.20771813 2.29967761
1.62323296]
Used the gpu
更多推荐
所有评论(0)