概述
这一节学习了一下keras的sequential models的大致流程。
以下记录笔记使用的keras版本为1.0.0
流程分为以下几步:
一:The Model layer
Getting started with the Keras Sequential model
首先获得keras的Sequential models
生成这个models是初始化一些基本信息包括input_shape输入类型、激活函数等信息
生成代码如下:
from keras.models import Sequential
model = Sequential([
Dense(32, input_dim=784),
Activation('relu'),
Dense(10),
Activation('softmax'),
])
对于Sequential model的第一层必须包含input_shape,因为必须通过这个来告诉模型你输入的是什么类型的数据,而后续的中间层就不需要这个了,因为会默认使用第一层的shape类型。
代码如下所示:
model = Sequential()
model.add(Dense(32, input_shape=(784,)))
二:The Merge layer
合并层
有时为了简化中间层可以先初始化两个或多个中间层,然后将他们merge后一起输入给sequential
代码形式如下所示:
from keras.layers import Merge
left_branch = Sequential()
left_branch.add(Dense(32, input_dim=784))
right_branch = Sequential()
right_branch.add(Dense(32, input_dim=784))
merged = Merge([left_branch, right_branch], mode='concat')
final_model = Sequential()
final_model.add(merged)
final_model.add(Dense(10, activation='softmax'))
图像化该过程为:
Merge层主要做的是将输入数据进行数据处理,通过Merge函数中的mode参数来确定数据处理方式,其中主要方法包括:
sum
(defualt):逐元素相加concat
:张量串联,可以通过提供concat_axis
的关键字参数指定按照哪个轴进行串联mul
:逐元素相乘ave
:张量平均dot
:张量相乘,可以通过dot_axis
关键字参数来指定要消去的轴cos
:计算2D张量(即矩阵)中各个向量的余弦距离
三:Compilation编译
该步骤的主要任务是编译训练过程,在编译过程中会调用optimizer、loss、metrics属性
其中optimizer属性的主要功能是优化函数,loss是编译过程中是用的损失函数。Metrics属性是说查询结果是否要求精确,通常该属性只有accuracy这一属性值,或者没有。
该过程的代码表示形式如下所示:
以下三种编译方法的区别主要在于损失函数的不同,损失函数通常有categorical_crossentropy,binary_crossentropy,mse
# for a multi-class classification problem
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
# for a binary classification problem
model.compile(optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['accuracy'])
# for a mean squared error regression problem
model.compile(optimizer='rmsprop',
loss='mse')
四、训练
通过上边的步骤已经构建出了模型,此时还需要通过该功能模块来进行训练功能,通常是为了训练模型的参数值。该模块使用fit函数来进行拟合。
Keras以Numpy数组作为输入数据和标签的数据类型
代码形式如下所示:
model = Sequential()
model.add(Dense(1, input_dim=784, activation='softmax'))
model.compile(optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['accuracy'])
# generate dummy data
import numpy as np
data = np.random.random((1000, 784))
labels = np.random.randint(2, size=(1000, 1))
# train the model, iterating on the data in batches
# of 32 samples
model.fit(data, labels, nb_epoch=10, batch_size=32)
其他代码形式如下所示:
# for a multi-input model with 10 classes:
left_branch = Sequential()
left_branch.add(Dense(32, input_dim=784))
right_branch = Sequential()
right_branch.add(Dense(32, input_dim=784))
merged = Merge([left_branch, right_branch], mode='concat')
model = Sequential()
model.add(merged)
model.add(Dense(10, activation='softmax'))
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
# generate dummy data
import numpy as np
from keras.utils.np_utils import to_categorical
data_1 = np.random.random((1000, 784))
data_2 = np.random.random((1000, 784))
# these are integers between 0 and 9
labels = np.random.randint(10, size=(1000, 1))
# we convert the labels to a binary matrix of size (1000, 10)
# for use with categorical_crossentropy
labels = to_categorical(labels, 10)
# train the model
# note that we are passing a list of Numpy arrays as training data
# since the model has 2 inputs
model.fit([data_1, data_2], labels, nb_epoch=10, batch_size=32)
小结:
通过以上几个功能模块就可以实现一个简单的深度学习神经网络了,是不是很酷啊。大致回忆一下,分别是建立模型,建立中间层,将中间层导入进模型中,编译整个模型,使用参数进行训练。
通过上述可以进行一些example试验了
最后
以上就是缓慢乐曲为你收集整理的2——快速开始Sequential模型的全部内容,希望文章能够帮你解决2——快速开始Sequential模型所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复