【python keras实战】快速开始:30秒上手 Keras

一、keras 介绍

Keras 是一个用 Python 编写的高级神经网络 API,它能够以 TensorFlow, CNTK, 或者 Theano 作为后端运行。Keras 的开发重点是支持快速的实验。能够以最小的时延把你的想法转换为实验结果,是做好研究的关键。

如果你有如下需求,请选择 Keras:

- 允许简单而快速的原型设计(用户友好,高度模块化,可扩展性)。
- 同时支持卷积神经网络和循环神经网络,以及两者的组合。
- 在CPU和GPU上无缝运行与切换。

Keras 兼容的 Python 版本: Python 2.7-3.6。

二、快速开始keras

Keras 的核心数据结构是 model,一种组织网络层的方式。最简单的模型是 Sequential 模型,它是由多网络层线性堆叠的栈。对于更复杂的结构,你应该使用 Keras 函数式 API,它允许构建任意的神经网络图。

Sequential 模型如下所示:

from keras.models import Sequential

model = Sequential()

可以简单地使用 .add() 来堆叠模型:

from keras.layers import Dense

model.add(Dense(units=64, activation='relu', input_dim=100))
model.add(Dense(units=10, activation='softmax'))

在完成了模型的构建后, 可以使用 .compile() 来配置学习过程:

model.compile(loss='categorical_crossentropy',
              optimizer='sgd',
              metrics=['accuracy'])

如果需要,你还可以进一步地配置优化器。Keras 的一个核心原则是使事情变得相当简单,同时又允许用户在需要的时候能够进行完全的控制(终极的控制是源代码的易扩展性)。

model.compile(loss=keras.losses.categorical_crossentropy,
              optimizer=keras.optimizers.SGD(lr=0.01, momentum=0.9, nesterov=True))

现在,你可以批量地在训练数据上进行迭代了:

# x_train and y_train are Numpy arrays --just like in the Scikit-Learn API.
model.fit(x_train, y_train, epochs=5, batch_size=32)

或者,你可以手动地将批次的数据提供给模型:

model.train_on_batch(x_batch, y_batch)

只需一行代码就能评估模型性能:

loss_and_metrics = model.evaluate(x_test, y_test, batch_size=128)

或者对新的数据生成预测:

classes = model.predict(x_test, batch_size=128)

构建一个问答系统,一个图像分类模型,一个神经图灵机,或者其他的任何模型,就是这么的快。深度学习背后的思想很简单,那么它们的实现又何必要那么痛苦呢?

三、keras 学习和实战
有关 Keras 更深入的教程,请查看:

keras中文官方文档:https://keras.io/zh/
keras作者写的书籍:https://github.com/fchollet/deep-learning-with-python-notebooks

四、安装keras

在安装 Keras 之前,请安装以下后端引擎之一:TensorFlow, Theano,或者 CNTK。我们推荐 TensorFlow 后端。

  • TensorFlow 安装指引。
  • Theano 安装指引。
  • CNTK 安装指引。

然后你就可以安装 Keras 本身了。有两种方法安装 Keras:

使用 PyPI 安装 Keras (推荐):

sudo pip install keras

如果你使用 virtualenv 虚拟环境, 你可以避免使用 sudo:

pip install keras

或者:使用 Github 源码安装 Keras:
首先,使用 git 来克隆 Keras:

git clone https://github.com/keras-team/keras.git

然后,cd 到 Keras 目录并且运行安装命令:

cd keras
sudo python setup.py install
已标记关键词 清除标记
<div><p>This is a sister PR to #2202 - whereas #2202 focuses on GUI translations and annotating, this PR is focused on EVE database translations. It will eventually be merged into that PR tho</p> <p>EVE data already includes these translations and it's possible to generate them via Phobos which is wonderful. However, there's plenty of gotchas surrounding translated texts from EVE. I'll attempt to go over the various changes here and what still needs to be done / decided upon.</p> <p>Screenshot: <img alt="image" src="https://img-blog.csdnimg.cn/img_convert/17a439d3339ef3793069987427fc1fa9.png" /></p> <h3>Changes:</h3> <ul><li>~~Stores files in <code>staticdata</code> in LFS. This is something new for this repo, and isn't something I've worked with in the past. I'm not sure if other contributors need to install the LFS extension for this or not. This was required due to the size of some of the files (we could technically split them if we added support in Phobos, but meh)~~ </li><li>Stores staticdata files as split. This is to avoid the GitHub limit of 100MB per file, which happens with <code>evetypes.json</code> when you do translations.</li><li>EVE database generator and schema changes<ul><li>So, lots of changes here of course. I'm going to try to describe the flow as best I can, but keep in mind that the current schema is old as fuck and has been through several iterations over the years, that there wasn't a one-size-fits-all solution.</li><li>I tried my best to have the English text target the original column if possible</li><li>I took advantage of the <code>synonym()</code> definitions that we already had in place to dynamically determine which translated text to target</li><li>There's some columns that I had to rename altogether (tbh, this was before I found the field map functionality in db_update, so maybe these weren't needed /shrug)</li></ul> </li><li>There's many places in the source where we reference a hardcoded value with the assumption that it's English. I've gone through and changed these to target the literal raw English property. Likewise, there's plenty of places that weren't using the synonym to display but instead using the base column - I've changed these as well.</li></ul> <h3>TODO:</h3> <ul><li>[x] Go through codebase and try to identify any more instances of hard coding to the incorrect property that need to be address</li><li>[x] Fix the broken preference saving</li><li>[ ] Test things like item / ship searching (make need a flag for the query to switch on <code>typeName</code> (always english) vs <code>name</code> ("display name") to handle the hardcoded type searches)</li><li>[x] erm... export / import. No idea how this stuff should work </li></ul> <h3>BUGS:</h3> <ul><li>[x] <code>db_update</code> fails when pyfa startup runs it and the locale is not English. This is due to the fact that <code>db_udpate</code> needs to be run within an English context</li></ul><p>该提问来源于开源项目:pyfa-org/Pyfa</p></div>
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页