快速开始序贯(Sequential)模型

序贯模型是多个网络层的线性堆叠,也就是“一条路走到黑”。

可以通过向Sequential模型传递一个layer的list来构造该模型:

from keras.models import Sequential
from keras.layers import Dense, Activationmodel = Sequential([
Dense(32, units=784), # 代码感觉有误,应该是input_shape=(784,)
Activation('relu'),
Dense(10),
Activation('softmax'),
])

也可以通过.add()方法一个个的将layer加入模型中:

model = Sequential()
model.add(Dense(32, input_shape=(784,)))
model.add(Activation('relu'))

指定输入数据的shape

模型需要知道输入数据的shape,因此,Sequential的第一层需要接受一个关于输入数据shape的参数,后面的各个层则可以自动的推导出中间数据的shape,因此不需要为每个层都指定这个参数。有几种方法来为第一层指定输入数据的shape

  • 传递一个input_shape的关键字参数给第一层,input_shape是一个tuple类型的数据,其中也可以填入None如果填入None则表示此位置可能是任何正整数数据的batch大小不应包含在其中

  • 有些2D层,如Dense,支持通过指定其输入维度input_dim来隐含的指定输入数据shape。一些3D的时域层支持通过参数input_diminput_length来指定输入shape。

  • 如果你需要为输入指定一个固定大小的batch_size(常用于stateful RNN网络),可以传递batch_size参数到一个层中,例如你想指定输入张量的batch大小是32,数据shape是(6,8),则你需要传递batch_size=32input_shape=(6,8)

model = Sequential()
model.add(Dense(32, input_dim=784))
model = Sequential()
model.add(Dense(32, input_shape=784))

编译

在训练模型之前,我们需要通过compile来对学习过程进行配置。compile接收三个参数:

  • 优化器optimizer:该参数可指定为已预定义的优化器名,如rmspropadagrad,或一个Optimizer类的对象,详情见optimizers

  • 损失函数loss:该参数为模型试图最小化的目标函数,它可为预定义的损失函数名,如categorical_crossentropymse,也可以为一个损失函数。详情见losses

  • 指标列表metrics:对分类问题,我们一般将该列表设置为metrics=['accuracy']。指标可以是一个预定义指标的名字,也可以是一个用户定制的函数.指标函数应该返回单个张量,或一个完成metric_name - > metric_value映射的字典.请参考性能评估

# 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')# For custom metrics
import keras.backend as Kdef mean_pred(y_true, y_pred):return K.mean(y_pred)model.compile(optimizer='rmsprop',loss='binary_crossentropy',metrics=['accuracy', mean_pred])

训练

Keras以Numpy数组作为输入数据和标签的数据类型。训练模型一般使用fit函数,该函数的详情见这里。下面是一些例子。

# For a single-input model with 2 classes (binary classification):model = Sequential()
model.add(Dense(32, activation='relu', input_dim=100))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='rmsprop',loss='binary_crossentropy',metrics=['accuracy'])# Generate dummy data
import numpy as np
data = np.random.random((1000, 100))
labels = np.random.randint(2, size=(1000, 1))# Train the model, iterating on the data in batches of 32 samples
model.fit(data, labels, epochs=10, batch_size=32)
# For a single-input model with 10 classes (categorical classification):model = Sequential()
model.add(Dense(32, activation='relu', input_dim=100))
model.add(Dense(10, activation='softmax'))
model.compile(optimizer='rmsprop',loss='categorical_crossentropy',metrics=['accuracy'])# Generate dummy data
import numpy as np
data = np.random.random((1000, 100))
labels = np.random.randint(10, size=(1000, 1))# Convert labels to categorical one-hot encoding
one_hot_labels = keras.utils.to_categorical(labels, num_classes=10)# Train the model, iterating on the data in batches of 32 samples
model.fit(data, one_hot_labels, epochs=10, batch_size=32)

例子

这里是一些帮助你开始的例子

在Keras代码包的examples文件夹中,你将找到使用真实数据的示例模型:

  • CIFAR10 小图片分类:使用CNN和实时数据提升
  • IMDB 电影评论观点分类:使用LSTM处理成序列的词语
  • Reuters(路透社)新闻主题分类:使用多层感知器(MLP)
  • MNIST手写数字识别:使用多层感知器和CNN
  • 字符级文本生成:使用LSTM ...

基于多层感知器的softmax多分类:

from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation
from keras.optimizers import SGD# Generate dummy data
import numpy as np
x_train = np.random.random((1000, 20))
y_train = keras.utils.to_categorical(np.random.randint(10, size=(1000, 1)), num_classes=10)
x_test = np.random.random((100, 20))
y_test = keras.utils.to_categorical(np.random.randint(10, size=(100, 1)), num_classes=10)model = Sequential()
# Dense(64) is a fully-connected layer with 64 hidden units.
# in the first layer, you must specify the expected input data shape:
# here, 20-dimensional vectors.
model.add(Dense(64, activation='relu', input_dim=20))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy',optimizer=sgd,metrics=['accuracy'])model.fit(x_train, y_train,epochs=20,batch_size=128)
score = model.evaluate(x_test, y_test, batch_size=128)

MLP的二分类:

import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Dropout# Generate dummy data
x_train = np.random.random((1000, 20))
y_train = np.random.randint(2, size=(1000, 1))
x_test = np.random.random((100, 20))
y_test = np.random.randint(2, size=(100, 1))model = Sequential()
model.add(Dense(64, input_dim=20, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))model.compile(loss='binary_crossentropy',optimizer='rmsprop',metrics=['accuracy'])
model.fit(x_train, y_train,epochs=20,batch_size=128)
score = model.evaluate(x_test, y_test, batch_size=128)

类似VGG的卷积神经网络:

import numpy as np
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.optimizers import SGD# Generate dummy data
x_train = np.random.random((100, 100, 100, 3))
y_train = keras.utils.to_categorical(np.random.randint(10, size=(100, 1)), num_classes=10)
x_test = np.random.random((20, 100, 100, 3))
y_test = keras.utils.to_categorical(np.random.randint(10, size=(20, 1)), num_classes=10)model = Sequential()
# input: 100x100 images with 3 channels -> (100, 100, 3) tensors.
# this applies 32 convolution filters of size 3x3 each.
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(100, 100, 3)))
model.add(Conv2D(32, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer=sgd)model.fit(x_train, y_train, batch_size=32, epochs=10)
score = model.evaluate(x_test, y_test, batch_size=32)

使用LSTM的序列分类

from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.layers import Embedding
from keras.layers import LSTMmodel = Sequential()
model.add(Embedding(max_features, output_dim=256))
model.add(LSTM(128))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))model.compile(loss='binary_crossentropy',optimizer='rmsprop',metrics=['accuracy'])model.fit(x_train, y_train, batch_size=16, epochs=10)
score = model.evaluate(x_test, y_test, batch_size=16)

使用1D卷积的序列分类

from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.layers import Embedding
from keras.layers import Conv1D, GlobalAveragePooling1D, MaxPooling1Dmodel = Sequential()
model.add(Conv1D(64, 3, activation='relu', input_shape=(seq_length, 100)))
model.add(Conv1D(64, 3, activation='relu'))
model.add(MaxPooling1D(3))
model.add(Conv1D(128, 3, activation='relu'))
model.add(Conv1D(128, 3, activation='relu'))
model.add(GlobalAveragePooling1D())
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))model.compile(loss='binary_crossentropy',optimizer='rmsprop',metrics=['accuracy'])model.fit(x_train, y_train, batch_size=16, epochs=10)
score = model.evaluate(x_test, y_test, batch_size=16)

用于序列分类的栈式LSTM

在该模型中,我们将三个LSTM堆叠在一起,是该模型能够学习更高层次的时域特征表示。

开始的两层LSTM返回其全部输出序列,而第三层LSTM只返回其输出序列的最后一步结果,从而其时域维度降低(即将输入序列转换为单个向量)

regular_stacked_lstm

from keras.models import Sequential
from keras.layers import LSTM, Dense
import numpy as npdata_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='rmsprop',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=5,validation_data=(x_val, y_val))

采用stateful LSTM的相同模型

stateful LSTM的特点是,在处理过一个batch的训练数据后,其内部状态(记忆)会被作为下一个batch的训练数据的初始状态。状态LSTM使得我们可以在合理的计算复杂度内处理较长序列

请FAQ中关于stateful LSTM的部分获取更多信息

from keras.models import Sequential
from keras.layers import LSTM, Dense
import numpy as npdata_dim = 16
timesteps = 8
num_classes = 10
batch_size = 32# Expected input batch shape: (batch_size, timesteps, data_dim)
# Note that we have to provide the full batch_input_shape since the network is stateful.
# the sample of index i in batch k is the follow-up for the sample i in batch k-1.
model = Sequential()
model.add(LSTM(32, return_sequences=True, stateful=True,batch_input_shape=(batch_size, timesteps, data_dim)))
model.add(LSTM(32, return_sequences=True, stateful=True))
model.add(LSTM(32, stateful=True))
model.add(Dense(10, activation='softmax'))model.compile(loss='categorical_crossentropy',optimizer='rmsprop',metrics=['accuracy'])# Generate dummy training data
x_train = np.random.random((batch_size * 10, timesteps, data_dim))
y_train = np.random.random((batch_size * 10, num_classes))# Generate dummy validation data
x_val = np.random.random((batch_size * 3, timesteps, data_dim))
y_val = np.random.random((batch_size * 3, num_classes))model.fit(x_train, y_train,batch_size=batch_size, epochs=5, shuffle=False,validation_data=(x_val, y_val))

Keras---序贯模型相关推荐

  1. Keras —— 序贯模型和函数式模型

    序贯模型 序贯模型是多个网络层的线性堆叠,是函数式模型的简略版,为最简单的线性.从头到尾的结构顺序,不发生分叉. 1.应用序贯模型的基本步骤 model.add,添加层: model.compile, ...

  2. Keras【Deep Learning With Python】—Keras实现序贯模型

    文章目录 Sequential 程序说明 代码实现 运行结果 Sequential 初步了解了tensorflow以后,发现了基于tensorflow的非常简洁的深度学习框架keras,只需要短短几行 ...

  3. Python机器学习笔记:深入理解Keras中序贯模型和函数模型

     先从sklearn说起吧,如果学习了sklearn的话,那么学习Keras相对来说比较容易.为什么这样说呢? 我们首先比较一下sklearn的机器学习大致使用流程和Keras的大致使用流程: skl ...

  4. 浅谈深度学习:LSTM对股票的收益进行预测(Sequential 序贯模型,Keras实现)

    浅谈深度学习:LSTM对股票的收益进行预测(Sequential 序贯模型,Keras实现) 总包含文章: 一个完整的机器学习模型的流程 浅谈深度学习:了解RNN和构建并预测 浅谈深度学习:基于对LS ...

  5. 深度学习:Keras基础--序贯模型(sequential)

    深度学习:Keras入门(一)之基础篇 1.Keras搭建神经网络: Keras有两种类型的模型,序贯模型(Sequential)和函数式模型(Model),函数式模型应用更为广泛,序贯模型是函数式模 ...

  6. 【Keras】序贯模型Sequential学习笔记

    序贯模型是多个网络层的线性堆叠,也就是"一条路走到黑". 1.通过Sequential构建模型 可以通过向Sequential模型传递一个layer的list来构造该模型: #导入 ...

  7. Keras学习笔记(一)认识序贯(Sequential)模型

    Keras由纯Python编写而成并基Tensorflow.Theano以及CNTK后端.Keras 为支持快速实验而生,能够把你的idea迅速转换为结果,它的可读性非常强!笔者在这参考了中文文档给自 ...

  8. 【keras】序贯Sequential模型实例之MLP的二分类

    MLP的二分类: # -*- coding: utf-8 -*- """ Created on Mon Jan 8 23:52:46 2018 序贯模型实例 @autho ...

  9. 【1】Keras复习之模型,层,训练,评估与预测

    本系列主要是针对文档的学习,文档地址是: www.keras.io,文档非常详细. Keras的核心数据结构就是模型,最简单的模型就是序贯模型,也就是Sequential模型,是层的线性堆砌.如果是想 ...

最新文章

  1. 百度CES大秀,Apollo2.0与DuerOS新产品背后的百度开放新姿态
  2. JAVA常见算法题(三十二)---找规律
  3. 多索引表 (1)boost::multi_index多索引容器
  4. Citrix产品及技术解析
  5. 图表插件Highcharts的动态化赋值,实现图表数据的动态化设置显示
  6. Eclipse And Android 使用心得
  7. 软件性能测试与LoadRunner实战可以在网上和书店买到了
  8. 使用 Syslog 连接 Sentinel
  9. java中fmt标签库_jsp fmt标签详解
  10. zookeeper配置集群
  11. java 日期只计算年月日大小_java 日期加减天数、月数、年数的计算方式
  12. shell进入特权模式_GRUB引导下进Linux单用户模式的三种方式,修改root密码
  13. mmap 系统调用 的使用
  14. (笔记) SpringCloud之Hystrix断路器 属性详解
  15. ORACLE 11GR2 配置GATEWAY FOR SERVER 问题
  16. 问题 J: 亚运会旗帜
  17. win10高性能模式
  18. lvs负载均衡之配置lvs-tun模式的httpd负载集群
  19. 天猫手机卖到第一:手机厂在想什么
  20. java程序设计案例教程答案,2年以上经验必看

热门文章

  1. Doris新增字段后立马向新增字段updat和insert显示没有该新增字段或者新增字段没有updata和insert数据
  2. Linux中的命令updata
  3. 《严凤英》这部电视剧与母亲
  4. 基于4G网卡的Telnet远程登录RT-Thread的Finsh/MSH Shell
  5. 嵌入式开发,没有串口如何看日志?
  6. 基于BS的校园招聘网站
  7. 紫外线(UV)是什么?
  8. 浙江大学计算机专业录取,2016年浙江大学计算机学院复试成绩及拟录取名单
  9. Java策略模式项目中的运用
  10. windows无法启动Docker