如何零基础使用KERAS搭建实用深度模型

September 3, 2017
By
如何零基础使用KERAS搭建实用深度模型

在这篇小文章中,我们将简要讨论如何使用KERAS这个现在最新的深度学习框架来构造实用的深度学习模型。

 深度学习是目前最热门的高级分析技术之一,在很多方面表现出了超越传统机器学习方法的有效性。但是在常用的TensorFlow,CNTK,Theano等计算环境中实现不同的深度学习模型仍然需要耗费很多时间来编写程序。KERAS的出现提供了一个高度抽象的环境来搭建深度学习模型,特别是其简单易用,跟网络结构一一对应的特点使得其迅速在数据科学家这个使用人群中流行起来。

## 什么是KERAS

KEARS是Google工程师François Chollet为主创人员,基于Python开发和维护的一个抽象的神经网络建模环境,提供了一系列的API供用户调用构造自己的深度学习网络。KERAS的出发点就是为用户提供一个能够快速实现模型的手段,从而缩短建模迭代的时间,加快模型试验的频率。用KERAS开发者的话说,就是要做好的科研必须尽可能地缩短从想法到实现结果的时间。在业界工作中这也是成功的关键要素之一。

相比较于常见的深度学习环境,比如TensorFlow,CNTK,Theano,Caffe等,KERAS有以下几个不同:

 1. 设计初衷就是方便以模块化地方式快速构造深度学习模型的原型;

2. 可以很方便地在CPU和GPU之间切换

3. KERAS本身只是描述模型的环境,其计算平台目前依赖于TensorFlow,CNTK和Theano这三种,以后会拓展到其他流行的计算平台上,比如mxNet等;

4. KERAS的拓展性既可以通过自定义KERAS里的激活函数或者损失函数等能自定义的部分进行,也可以通过引用对应的计算平台的自定义部分进行,具有一定的灵活性;

跟这些流行的计算平台一样,KERAS也支持常见的深度学习模型,比如卷积神经网络,循环神经网络以及二者的组合等。

使用KERAS构造深度神经网络有一系列相对固定的步骤:

1. 首先要将原始数据处理成KERAS的API能够接受的格式,一般是一个张量的形式,通常在维度上表示为(批量数,)。这里 是一个通用的说法,对应于不同类型的模型,数据有不同的要求。 通常,如果是一个简单的全链接模型,则单一样本对应张量的维度就是特征个数; 如果是一维的时间序列数据,并要用循环神经网络模型训练的话,则单一样本对应张量的维度是时间步和每个时间步对应的回看序列长度; 如果输入数据是图像,并使用卷积神经网络模型进行训练,则单一样本张量对应图像的高,宽和色彩频道三个维度。但是如果是使用全连接模型训练图像数据,则单一样本对应张量是该图像扁化(Flatten)以后的向量长度,其为高,宽和色彩频道各个维度数量的乘积。一般卷积神经网络最靠近输出层的那层都设置一个全连接层,因此也需要扁化输入张量。

2. 其次要构造需要的深度学习模型。这一步又分为模型的选择和模型的细化两个步骤:
   - 选择模型的类型。KERAS里定义了两大类模型
             1)序列模型(Sequential);
             2)通用模型(Model)。

            序列模型指的是深度模型每一层之间都是前后序列关系,如下图所示:

Figure 1。MLP是一个典型的序列模型,(http://article.sapub.org/10.5923.j.ajis.20120204.01.html) 可以看到从左到右,输入层到隐含层到输出层每一层之间都是前后依次相连的简单关系。这个简单的网络结构可以用三句KERAS命令实现:

model=Sequential()
model.add(Dense(5, input_shape=(4,), activation=’sigmoid’))
model.add(Dense(1, activation=’sigmoid’))

而通用模型则是对应更广义的模型,具备更大的灵活性。上面提到的序列模型也可以用通用模型来表达,这个我们在后一节详解。 当然通用模型更能用来描述层与层之间有较复杂关系的情况,比如非相邻的层之间进行连接,或者多个神经网络的合并等。比如我们可以使用通用模型进行矩阵分解:

user_in = Input(shape=(1,), dtype='int64', name='user_in')
...
Read more »

Where do modern day pirates attack?

September 1, 2017
By

International Talk Like a Pirate Day is Sept 19 ... which always gets me thinking and wondering about modern-day pirates. Most movies usually focus on pirates from the Golden Age of piracy (a couple hundred years ago), when pirates typically stole ships and booty (treasure). But modern day pirates usually board

The post Read more »

The singular value decomposition and low-rank approximations

August 30, 2017
By

Aa previous article discussed the mathematical properties of the singular value decomposition (SVD) and showed how to use the SVD subroutine in SAS/IML software. This article uses the SVD to construct a low-rank approximation to an image. Applications include image compression and denoising an image. Construct a grayscale image The

The post Read more »

Jedi SAS Tricks – Variable Lists by Text Pattern

August 29, 2017
By

I hate hard-coding variable lists in my SAS code. I love the special variable lists SAS provides, like name:, _numeric_, etc. But back in 2011, I wrote a post titled Jedi SAS Tricks - Building a Name Suffix variable list lamenting the lack of a special...
Read more »

Learn SAS from wherever you are: Live Web classes go global

August 28, 2017
By

My daughter is a junior in high school, and for almost every semester she’s taken an online course as part of her studies. This semester she’s taking Spanish 3, an advanced level course where every word of instruction is spoken in Spanish. Each morning she joins the class from our

The post Read more »

The singular value decomposition: A fundamental technique in multivariate data analysis

August 28, 2017
By

The singular value decomposition (SVD) could be called the "billion-dollar algorithm" since it provides the mathematical basis for many modern algorithms in data science, including text mining, recommender systems (think Netflix and Amazon), image processing, and classification problems. Although the SVD was mathematically discovered in the late 1800s, computers have

The post Read more »

Using Notepad++ as your SAS code editor

August 25, 2017
By

News flash: My favorite SAS code editor is SAS Enterprise Guide. However, my favorite general purpose text editor is Notepad++, and I often find myself using that tool for viewing SAS log files and for making small modifications to SAS programs. Judging from the popularity of this SAS Support Communities

The post Read more »

When’s the next total solar eclipse?

August 25, 2017
By

The U.S. was really fortunate in having the recent total solar eclipse pass through so many of its states! This gave lots of people an opportunity to see it, with just a short (or moderate) drive. I think a little kid spoke for all of us when they said...
Read more »

Learn neural networks and more at Analytics Experience 2017

August 24, 2017
By

What’s your favorite time of day?  For me, it’s the morning with a cup of coffee, just as the sun starts to appear on the horizon, emitting a pleasant warm glow.  My least favorite part of the day?  Mornings without coffee when that same big ball of light makes a

The post Read more »

Welcome!

SAS-X.com offers news and tutorials about the various SAS® software packages, contributed by bloggers. You are welcome to subscribe to e-mail updates, or add your SAS-blog to the site.

Sponsors





Dear readers, proc-x is looking for sponsors who would be willing to support the site in exchange for banner ads in the right sidebar of the site. If you are interested, please e-mail me at: tal.galili@gmail.com
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration.