Keras的基本使用(1)--创建,编译,训练模型

参考链接:https://blog.csdn.net/yuezhilanyi/article/details/79201705

Keras 是一个用 Python 编写的,高级的神经网络 API,使用 TensorFlow,Theano 等作为后端。快速,好用,易验证是它的优点。
官方文档传送门:http://keras.io/
中文文档传送门:http://keras.io/zh
中文第三方文档:http://keras-cn.readthedocs.io

1.搭建模型

方法一:使用 Sequential() 搭建模型

Sequential 是实现全连接网络的最好方式。

1)Sequential 模型是多个网络层的线性堆栈,可以从 keras 的模型库中导入 Sequential 模型:

from keras.models import Sequential
import tensorflow as tf

# create a sequential model 
model = Sequential()
Using TensorFlow backend.

2)将一些网络层 layers 通过 add() 叠加,构成一个网络:

from keras.layers import Dense,Activation

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

也可以直接输入一个 list 完成 Sequential 模型的搭建:

model = Sequential([
                  (Dense(units=64,input_dim=100)),
                  (Activation('relu')),
                  (Dense(units=10)),
                  (Activation('softmax'))
                  ])

简便之处是,除第一层输入数据的 shape 要指定外,其他层的数据的 shape 框架会自动推导。

  • 可以使用 input_shape 这个关键字来指定第一层输入的 shape,input_shape 是一个 tuple 类型的数据(可以是整数也可以填入 None,如果填入 None 则表示此位置可能是任何正整数)但需要注意的是,数据的 batch大小不应包含在其中
  • 有些 2D 层,可以使用 Dense,指定第一层输入维度 input_dim 来隐含的指定输入数据的 shape,它是一个 Int 类型的数据。还有一些 3D 的时域层支持通过参数 input_diminput_length 来指定输入 shape。
model = Sequential()
model.add(Dense(32, input_shape=(784,)))



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

3)创建好模型后可以使用 model.summary() 来查看最终的模型的结构

方法二:使用Model()搭建模型

方法一是使用 Sequential() (中文文档中的翻译为:序贯模型)来搭建模型,这里使用Model()(即:函数式模型)来搭建模型。

中文文档中的说明:Keras 函数式模型接口是用户定义多输出模型、非循环有向模型或具有共享层的模型等复杂模型的途径。一句话,只要你的模型不是类似 VGG 一条路走到黑的模型,或者你的模型需要多于一个的输出,那么你总应该选择函数式模型。函数式模型是最广泛的一类模型,序贯模型(Sequential)只是它的一种特殊情况。

先来一个简单的例子:

from keras.layers import Input, Dense
from keras.models import Model

# This returns a tensor
inputs = Input(shape=(784,))

# a layer instance is callable on a tensor, and returns a tensor
x = Dense(64, activation='relu')(inputs)
x = Dense(64, activation='relu')(x)
predictions = Dense(10, activation='softmax')(x)

# This creates a model that includes
# the Input layer and three Dense layers
model = Model(inputs=inputs, outputs=predictions)
model.compile(optimizer='rmsprop',
              loss='categorical_crossentropy',
              metrics=['accuracy'])
model.fit(data, labels)  # starts training

函数式模型提供了接口供我们使用,利用接口可以很便利的调用已经训练好的模型,比如像 VGG,Inception 这些强大的网络。但要注意的是,调用模型的同时,也调用了它的权重数据。函数式模型创建好之后也能够像序贯模型一样 compile 和 fit,方法一致。

更多详见:http://keras-cn.readthedocs.io/en/latest/getting_started/functional_API/#functional

2.编译创建好的模型

网络模型搭建完后,需要对网络的学习过程进行配置,否则在调用 fit 或 evaluate 时会抛出异常。可以使用 compile(self, optimizer, loss, metrics=None, sample_weight_mode=None, weighted_metrics=None, target_tensors=None) 来完成配置
compile() 主要接收前三个参数:

  • loss:字符串类型,用来指定损失函数,如:categorical_crossentropy,binary_crossentropy
  • optimizer:字符串类型,用来指定优化方式,如:rmsprop,adam,sgd
  • metrics:列表类型,用来指定衡量模型的指标,如:accuracy
model.compile(loss='categorical_crossentropy', 
                                optimizer='sgd', metrics=['accuracy'])

3.训练模型

训练模型一般使用fit()函数:

fit(self, x, y, batch_size=32, epochs=10, verbose=1, 
                callbacks=None, validation_split=0.0,
                validation_data=None, shuffle=True,
                class_weight=None, sample_weight=None, 
                initial_epoch=0)
  • x: 训练数据数组。如果输入的是框架本地的张量(如 Tensorflow 的数据 tensors ), x 可以是 None (默认) 。
  • y: 目标(标签)数据数组。如果输入的是框架本地的张量(如 Tensorflow 的数据 tensors ), y 可以是 None (默认) 。
  • batch_size: 指定 batch 的大小,为整数或者为 None。如果没有指定,默认为 32。
  • epochs: 指定训练时全部样本的迭代次数,为整数。

下面是训练模型的例子(来自官方文档):

# 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)



作者:caoqi95
链接:https://www.jianshu.com/p/6aa40d99fc9e
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

猜你喜欢

转载自blog.csdn.net/yimixgg/article/details/90694172