美文网首页
Anaconda-Tensorflow-Keras 安装踩坑记

Anaconda-Tensorflow-Keras 安装踩坑记

作者: 余生回忆 | 来源:发表于2020-02-18 11:51 被阅读0次

标签:【Anaconda 3.5】+【Tensorflow-GPU 2.0.0】+【Keras 2.3.1】【Python 3.7.4】

Deep Learning技术火遍全球,想动手试试这个技术的想法有段时间了。这次新肺炎疫情让我终于有时间坐下来研究一下这个技术了。

考虑到自己非专业出身,所以根据资料选择Tensorflow-Keras组合。【后记:没有想到实际安装过程一波三折。光安装调试整整浪费了两天的事件。】

OK,Let‘s start!

Step 1 - Anaconda 安装

1)Anaconda简要介绍

Anaconda(官方网站)就是可以便捷获取包且对包能够进行管理,同时对环境可以统一管理的发行版本。Anaconda包含了conda、Python在内的超过180个科学包及其依赖项。

详细资料参考:

Anaconda介绍、安装及使用教程 https://www.jianshu.com/p/62f155eb6ac5

Anaconda官方网站 https://www.anaconda.com/

Anaconda清华镜像 https://mirrors.tuna.tsinghua.edu.cn/help/anaconda/

清华镜像使用方法:1)运行 conda config --set show_channel_urls yes ,这样会在用户目录下生成一个.condarc文件。2)用文本编辑器打开该文件,将下边的内容复制进去:

channels:

  - defaults

show_channel_urls: true

channel_alias: https://mirrors.tuna.tsinghua.edu.cn/anaconda

default_channels:

  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main

  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free

  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r

  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/pro

  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2

custom_channels:

  conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

  msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

  bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

  menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

  pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

  simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

2)Anaconda安装过程

Anaconda安装软件可以从官方下载,下载的时候可以用迅雷,笔者所在的网络官方下载非常缓慢。版本可以选择最新版64位。最新版的Tensorflow仅支持64位的Python。笔者下载的版本是:Anaconda3-5.3.1-Windows-x86_64。

开始菜单-打开Anaconda命令行

Step 2 - 安装Tensorflow-gpu

1)安装必须组件

Tensorflow有CPU和GPU两个版本。

tensorflow-gpu版本的安装需要安装Nvidia的CUDA和cuDNN库。tensorflow和CUDA及cuDNN有版本的对应。不对应的版本会导致兼容性问题。笔者的CUDA和cuDNN的版本是:cuda_10.2.89_441.22_win10和cudnn-10.2-windows10-x64-v7.6.5.32 。

https://developer.nvidia.com/cuda-downloads

https://developer.nvidia.com/cudnn

2)安装Tensorflow/Tensorflow-GPU

有两种不同的安装方式,conda和pip。
笔者开始通过pip install tensorflow-gpu。但是会遇到tensorboard 不能找到版本的错误。后来通过-i指定豆瓣的源安装完毕之后,调用报错。后来笔者在某些文章中介绍说,通过conda安装tensorflow一般不会出错,然后就重新卸载Anaconda,通过conda install tensorflow-gpu成功安装并成功通过调用。

3)tensorflow测试

注:默认情况下,即使安装的是tensorflow-gpu,tensorflow也不会调用GPU进行计算,如果想让GPU进行计算,需要在所有代码前边加入:

import os
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"  

以下为简单的测试脚本:

import tensorflow as tf

import timeit

with tf.device('/cpu:0'):

cpu_a = tf.random.normal([10000, 1000])

cpu_b = tf.random.normal([1000, 2000])

print(cpu_a.device, cpu_b.device)

with tf.device('/gpu:0'):

gpu_a = tf.random.normal([10000, 1000])

gpu_b = tf.random.normal([1000, 2000])

print(gpu_a.device, gpu_b.device)

def cpu_run():

with tf.device('/cpu:0'):

c = tf.matmul(cpu_a, cpu_b)

return c

def gpu_run():

with tf.device('/gpu:0'):

c = tf.matmul(gpu_a, gpu_b)

return c

# warm up

cpu_time = timeit.timeit(cpu_run, number=10)

gpu_time = timeit.timeit(gpu_run, number=10)

print('warmup:', cpu_time, gpu_time)

cpu_time = timeit.timeit(cpu_run, number=10)

gpu_time = timeit.timeit(gpu_run, number=10)

print('run time:', cpu_time, gpu_time)

笔者上述脚本的输出为:

/job:localhost/replica:0/task:0/device:CPU:0 /job:localhost/replica:0/task:0/device:CPU:0

/job:localhost/replica:0/task:0/device:GPU:0 /job:localhost/replica:0/task:0/device:GPU:0

warmup: 0.9505847049999971 0.42991021699999976

run time: 0.8981372370000003 0.0005334799999978657

Step 3 - 安装Keras

安装完tensorflow后,Keras的安装就比较简单了,通过pip install keras可以迅速安装。

笔者一次性通过安装。

Step 4 - 测试

1)测试前设置:

由于Keras默认采用tensorflow作为后台模块,因此笔者不需要做任何更改。如果想改为其他后台模块,请参考 https://keras.io/zh/backend/ 或者 https://keras.io/backend/。

2)测试Keras

【注1:windows版本下的Keras由于无法正确处理虚拟显存技术,默认会大量占用显存,笔者的GTX 1060 6GB显卡在运行一个脚本后几乎显存被耗尽。如果不关闭当前脚本,新的脚本运行会出现问题。笔者调试的时候还以为笔者的内存或者显存出现了问题】

打开Jupyter Notebook。

开始菜单-打开Jupyter Notebook

输入以下为线性回归的测试脚本:

import keras

import numpy as np

import matplotlib.pyplot as plt

#Sequentioal

from keras.models import  Sequential

#Dense

from keras.layers import Dense

x_data = np.random.rand(100)

noise = np.random.normal(0,0.01,x_data.shape)

y_data = x_data*0.1 + 0.2 + noise

model = Sequential()

#添加全连接层

model.add(Dense(units=1, input_dim=1))

model.compile(optimizer='sgd',loss='mse')

cost = model.train_on_batch(x_data,y_data)

for step in range(3001):

    cost = model.train_on_batch(x_data,y_data)

    if step % 500 == 0:

        print('cost:',cost)

#print weight and variation

w,b = model.layers[0].get_weights()

print('W:',w, 'b:',b)

y_pred = model.predict(x_data)

plt.scatter(x_data, y_data)

plt.plot(x_data, y_pred,'r-',lw=3)

plt.show()

运行脚本

笔者的输出:

cost: 0.1411038

cost: 0.01774438

cost: 0.004615805

cost: 0.0012335633

cost: 0.00036221492

cost: 0.00013773507

cost: 7.990343e-05

W: [[0.11383099]] b: [0.19431618]

随机数据及其线性回归预测模型

相关文章

网友评论

      本文标题:Anaconda-Tensorflow-Keras 安装踩坑记

      本文链接:https://www.haomeiwen.com/subject/mwbwfhtx.html