我的操作系统是Windows 10 64位。

1. 安装Anaconda

Anaconda是一个基于Python(以及R语言)的数据分析处理平台,集成了Conda和Python等工具和依赖项。使用它可以很方便地管理Python的依赖项和工具包,节约开发时间。

Anaconda有三个特点:1. 高效安装(便捷安装常用数据处理Packages);2. 管理方便(通过Conda管理);3. 开源,支持可视化。

之前看过一点博客,会有关于Python版本的说法,我选择仍然下载最新版本的Anaconda Distribution(Version 5.2 | Release Date: May 30, 2018,Download for Windows,Python 3.6,64-Bit Graphical Installer)https://www.anaconda.com/download/#windows。

之所以选择下载最新版本的Anaconda,是因为即使因为Python的版本造成什么问题,我们也可以很方便地更换Python的版本。在Anaconda官方的文档中,明确指出了更换Python版本的方法,详见Frequently asked questions。而Anaconda支持Python的2.7,3.4,3.5,3.6版本https://conda.io/docs/user-guide/tasks/manage-python.html。

下载下来的文件叫“Anaconda3-5.2.0-Windows-x86_64.exe”。安装起来很大,3个G左右。安装完后,有一个关于是否安装Visual Studio Code的选项,我选了是,它给我把Visual Studio Code安装到了C盘,这里Visual Studio Code的作用就相当于Pycharm,占用不大100多M。但是由于对我个人而言,Visual Studio Code很不好用,还是用pycharm吧,好像spider不太好用,要是想卸载可以去C:\Program Files\Microsoft VS Code下卸载。

安装完后,Anaconda有两个东西比较重要,一个是Anaconda Navigator (Anaconda的图形化界面);另一个是Anaconda Prompt (Anaconda的命令行界面)。

2. 安装TensorFlow

TensorFlow™ 是一个开放源代码软件库,用于进行高性能数值计算。借助其灵活的架构,用户可以轻松地将计算工作部署到多种平台(CPU、GPU、TPU)和设备(桌面设备、服务器集群、移动设备、边缘设备等)。TensorFlow™ 最初是由 Google Brain 团队(隶属于 Google 的 AI 部门)中的研究人员和工程师开发的,可为机器学习和深度学习提供强力支持,并且其灵活的数值计算核心广泛应用于许多其他科学领域。–Tensorflow官网:

TensorFlow

打开Anaconda Prompt,我们希望Anaconda能够帮我们管理TensorFlow软件库,所以我们要在Anaconda Prompt里操作。

由于直接访问国外的链接网速会很慢,所以这里修改清华的镜像,如下:

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/

conda config --set show_channel_urls yes

升级conda,如下:

conda update -n base conda

创建python3.6的TensorFlow虚拟环境,如下:

conda create -n tensorflow python=3.6

开启TensorFlow虚拟环境,如下:

conda activate tensorflow

TensorFlow虚拟环境是可以关闭的,如下,不过这里先不要关

conda deactivate

安装cpu版本的TensorFlow软件库,如下:

pip install --upgrade --ignore-installed tensorflow

安装pip软件 (pip是python包管理工具),如下:

python -m pip install --upgrade pip

这里我出了一个问题,pip安装的时候携带了setuptools工具包,版本过高,如下:

所以,在这里我准备卸载现有的setuptools并安装合适版本的setuptools。

卸载现有的setuptools,如下:

pip uninstall setuptools

安装合适版本的setuptools,如下:

pip install --ignore-installed setuptools==39.1.0

简单地测试一下TensorFlow,如下:

python

import tensorflow

同样,可以在CMD下测试,在Anaconda的安装目录下envs\tensorflow,把这个路径传给环境变量的path。打开CMD,测试一下如下:

python

import tensorflow

我的没报错,暂时就先认定没问题吧。

3. 安装keras

Keras是一个高层神经网络API,Keras由纯Python编写而成并基

Tensorflow、

Theano以及

CNTK后端。Keras 为支持快速实验而生,能够把你的idea迅速转换为结果,如果你有如下需求,请选择Keras:

简易和快速的原型设计(keras具有高度模块化,极简,和可扩充特性)

支持CNN和RNN,或二者的结合

无缝CPU和GPU切换

Keras适用的Python版本是:Python 2.7-3.6

在Anaconda Prompt里,

pip install --ignore-installed keras

提示缺少msgpack,

pip install msgpack

提示升级pip,

python -m pip install --upgrade pip

测试一下keras,在CMD中,

python

import keras

4. 进一步测试TensorFlow和Keras

4.1. TensorFlow测试

在运行一段简单的TensorFlow代码时候

警告的意思就是说,TensorFlow的CPU版本并没有编译支持AVX2的版本,性能可能不会很好,意思就是说用GPU的效果比较好,或者重新编译支持AVX2的TensorFlow,可以看这篇博文警告:Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA

把代码都写python脚本里,用CMD里python运行,并展示tensorboard的图,代码如下:

import tensorflow as tf

print('version:', tf.__version__)

foo = tf.Variable(3, name='foo')

bar = tf.Variable(2, name='bar')

result = tf.add(foo, bar, name='add')

# 初始化变量

init = tf.global_variables_initializer()

# 启动图 (graph)

sess = tf.Session()

sess.run(init)

res = sess.run(result)

print('result:', res)

train_writer = tf.summary.FileWriter('/tmp/tensorflow/add/logs/testTf/train',

sess.graph)

writer = tf.summary.FileWriter("logs/",sess.graph)

这样运行之后,在运行路径下会生成一个logs文件夹下会有一个events.out.tfevents.xxx.DESKTOP-L9ILLL2之类的日志文件,同样在C:\tmp\tensorflow\add\logs\testTf\train下也会有 (原因是代码里tf.summary.FileWriter()这个函数搞的鬼),在Anaconda Prompt下,

activate tensorflow

tensorboard --logdir=C:\tmp\tensorflow\add\logs\testTf\train

显示“TensorBoard 1.10.0 at http://DESKTOP-L9ILLL2:6006 (Press CTRL+C to quit)”

那么TensorFlow的测试就暂时到这里,应该是装好了的,只是CPU版本性能上有点告警。

4.2. Keras测试

使用代码入下

from keras.models import Sequential

from keras.layers import LSTM, Dense

import numpy as np

data_dim = 16

timesteps = 8

num_classes = 10

# expected input data shape: (batch_size, timesteps, data_dim)

model = Sequential()

model.add(LSTM(32, return_sequences=True,

input_shape=(timesteps, data_dim))) # returns a sequence of vectors of dimension 32

model.add(LSTM(32, return_sequences=True)) # returns a sequence of vectors of dimension 32

model.add(LSTM(32)) # return a single vector of dimension 32

model.add(Dense(10, activation='softmax'))

model.compile(loss='categorical_crossentropy',

optimizer='sgd',

metrics=['accuracy'])

# Generate dummy training data

x_train = np.random.random((1000, timesteps, data_dim))

y_train = np.random.random((1000, num_classes))

# Generate dummy validation data

x_val = np.random.random((100, timesteps, data_dim))

y_val = np.random.random((100, num_classes))

model.fit(x_train, y_train,

batch_size=64, epochs=1,

validation_data=(x_val, y_val))

运行结果如下,

验证通过,可以发现CPU版本的和上面说的一样,一样是性能告警,后面我们就搞GPU的吧,那样运算也快。

5. 安装PyCharm

下载下来的PyCharm版本:pycharm-community-2018.2.3.exe。

一路安装。

使用PyCharm来进行TensorFlow/Keras的编写

其实我们上面那样好像用不到PyCharm,PyCharm它其实就是一个辅助编写代码,辅助开发的一个集成环境,我们要调试代码,肯定不可避免的,因为用文本那样的类型开发,挺累的。

下面,我们就把上面的一段代码放到PyCharm里去运行,测试PyCharm好了没

配置PyCharm,选existing interpreter,选Anaconda里建的虚拟环境下的Python

然后把上面的代码拷贝进去,我拷贝的keras的代码,因为keras的是把tensorflow作为了它的后端,所以想先测试keras的测试用例

一开始使用PyCharm会要更新,直接去界面工具栏run就行了,它会自己更新的,情况如下:

更新完后,就支持”代码编辑处右键选run“来进行run了,

keras测试没有问题

下面测试tensorflow的

TensorFlow的也没有问题

参考:(全文提及的所有链接均在参考范围内)

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐