Keras快速入门

tf.keras是一个高层接口,它可用于快速设计原型、高级研究和生产。 keras的3个优点:方便用户使用、模块化和可组合、易于扩展。

Keras快速入门

1.1、导入tf.keras

tensorflow2推荐使用keras构建网络,常见的神经网络都包含在keras.layer中(最新的tf.keras的版本可能和keras不同)

from __future__ import absolute_import, division, print_function, unicode_literals

import tensorflow as tf

from tensorflow import keras

1.2、构建简单模型

1.2.1、Sequential model


# 第一种: 构建层
model = keras.Sequential([
    layers.Flatten(input_shape=(28, 28)),     # 第一层,转换图像格式: 将二维(28*28)的图像数组转为一维(28*28=784), 这层没有学习参数,只是格式数据
    layers.Dense(128, activation='relu'),     # 第二层, 128个节点
    layers.Dense(10, activation='softmax')    # 第三层,10个节点的softmax层, 输出10个可能的分数,加在一起和为1,表示10个分类的可能性分数
])

# 第二种: 或者使用add
model2 = keras.Sequential()
# Adds a densely-connected layer with 64 units to the model:
model.add(layers.Dense(64, activation='relu'))
# Add another:
model.add(layers.Dense(64, activation='relu'))
# Add a softmax layer with 10 output units:
model.add(layers.Dense(10, activation='softmax'))

1.2.2、配置网络层

tf.keras.layers中网络配置:

  • activation:设置层的激活函数。此参数由内置函数的名称指定,或指定为可调用对象。默认情况下,系统不会应用任何激活函数。
  • kernel_initializer 和 bias_initializer:创建层权重(核和偏差)的初始化方案。此参数是一个名称或可调用对象,默认为 “Glorot uniform” 初始化器。
  • kernel_regularizer 和 bias_regularizer:应用层权重(核和偏差)的正则化方案,例如 L1 或 L2 正则化。默认情况下,系统不会应用正则化函数。
# 激活函数
# Create a sigmoid layer:
layers.Dense(64, activation='sigmoid')
# Or:
layers.Dense(64, activation=tf.keras.activations.sigmoid)

# 层权重的正则化方案
# A linear layer with L1 regularization of factor 0.01 applied to the kernel matrix:
layers.Dense(64, kernel_regularizer=tf.keras.regularizers.l1(0.01))

# A linear layer with L2 regularization of factor 0.01 applied to the bias vector:
layers.Dense(64, bias_regularizer=tf.keras.regularizers.l2(0.01))

# 层权重(核和偏差)
# A linear layer with a kernel initialized to a random orthogonal matrix:
layers.Dense(64, kernel_initializer='orthogonal')

# A linear layer with a bias vector initialized to 2.0s:
layers.Dense(64, bias_initializer=tf.keras.initializers.Constant(2.0))

1.3、训练与评估

1.3.1、编译模型

在训练模型前,有一些设置需要在编译模型:

  • Loss function —This measures how accurate the model is during training. You want to minimize this function to “steer” the model in the right direction.
  • Optimizer —This is how the model is updated based on the data it sees and its loss function.
  • Metrics —Used to monitor the training and testing steps. The following example uses accuracy, the fraction of the images that are correctly classified.
model = tf.keras.Sequential([
# Adds a densely-connected layer with 64 units to the model:
layers.Dense(64, activation='relu', input_shape=(32,)),
# Add another:
layers.Dense(64, activation='relu'),
# Add a softmax layer with 10 output units:
layers.Dense(10, activation='softmax')])

model.compile(optimizer=tf.keras.optimizers.Adam(0.01),
              loss='categorical_crossentropy',
              metrics=['accuracy'])

1.3.2、输入Numpy

对于小型数据集,使用内存中的NumPy数组来训练和评估模型。模型采用fit方法对训练数据进行训练:

import numpy as np

# 训练集
data = np.random.random((1000, 32))
labels = np.random.random((1000, 10))
# 训练模型
model.fit(data, labels, epochs=10, batch_size=32)

tf.keras.Model.fit 有三个重要参数:

  • epochs: Training is structured into epochs. An epoch is one iteration over the entire input data (this is done in smaller batches).
  • batch_size: When passed NumPy data, the model slices the data into smaller batches and iterates over these batches during training. This integer specifies the size of each batch. Be aware that the last batch may be smaller if the total number of samples is not divisible by the batch size.
  • validation_data: When prototyping a model, you want to easily monitor its performance on some validation data. Passing this argument—a tuple of inputs and labels—allows the model to display the loss and metrics in inference mode for the passed data, at the end of each epoch.

下面是一个validation_data的例子

import numpy as np

data = np.random.random((1000, 32))
labels = np.random.random((1000, 10))

val_data = np.random.random((100, 32))
val_labels = np.random.random((100, 10))

model.fit(data, labels, epochs=10, batch_size=32,
          validation_data=(val_data, val_labels))

1.3.3、输入tf.data datasets

使用 DataSet API 扩展到大数据集或者多设备训练,给fit方法一个tf.data.DataSet实例。

# Train from tf.data datasets
dataset = tf.data.Dataset.from_tensor_slices((data, labels))
dataset = dataset.batch(32)

model.fit(dataset, epochs=10) # 应为DataSet生成变量数据, 所以不需要 batch_size

1.3.4、评估

使用tf.keras.Model.evaluate方法进行评估

import numpy as np
data = np.random.random((1000, 32))
labels = np.random.random((1000, 10))
# 评估 Numpy Array
model.evaluate(data, labels, batch_size=32)

# 评估 DataSet
dataset = tf.data.Dataset.from_tensor_slices((data, labels))
dataset = dataset.batch(32)

model.evaluate(dataset)

1.3.5、预测

使用tf.keras.Model.predict方法进行预测

# 预测 for numpy array
result = model.predict(data, batch_size=32)
print(result.shape)

训练和评估中展示完成流程, 包括自定义trainning loops

1.4、构建复杂模型

1.4.1、The Functional API

1.4.2、模型子类化(Model subclassing)

1.5、Callbacks

1.6、保存和序列化模型

1.7、Eager execution

发布了784 篇原创文章 · 获赞 90 · 访问量 44万+

猜你喜欢

转载自blog.csdn.net/wuxintdrh/article/details/103562137