Keras总览
2020-07-04 22:52
52 查看
写在前面
本文内容来源于Tensorflow Keras 教程代码,并对内容进行简要概括。本文内容包含了使用Keras创建模型的基本过程:构建模型、训练、评估、预测、保存和恢复。
Import tf.keras
tf.keras是TensorFlow对Keras API规范的实现。
from __future__ import absolute_import, division, print_function, unicode_literals try: # %tensorflow_version only exists in Colab. %tensorflow_version 2.x except Exception: pass import tensorflow as tf from tensorflow import keras
tf.keras可以运行所有Keras兼容的代码,但是要注意以下几点:
- 在最新的tf中的
tf.keras
的版本或许和最新在PyPI的keras
版本不相同,请通过tf.keras.__version__
确认版本。 - 当保存一个模型的权重时,
tf.keras.
默认为checkpoint格式。通过save_format='h5'
设置来保存为HDF5(或者通过文件的后缀.h5
也可。)
构建一个简单的模型
Sequential model
在Keras中,你通过组装layer以构建model。 模型通常是layers的图。最通常的模型类型是一个layers的堆栈:
tf.keras.Sequential模型。
构建一个简单的全连接网络
from tensorflow.keras import layers model = tf.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 an output layer with 10 output units: model.add(layers.Dense(10))
配置layers
有许多的
tf.keras.layers可用,他们大多有相同的构造器参数
activation
【激活函数】: 为该层网络设置激活函数,默认不使用任何激活函数。kernel_initializer
andbias_initializer
【内核初始化器 和 偏差初始化器】: 默认使用"Glorot uniform"初始化器kernel_regularizer
andbias_regularizer
【内核正则化器 和 偏差正则化器】: 默认不使用正则化
以下的实例
tf.keras.layers.Dense层使用构造器的参数:
# Create a relu layer: layers.Dense(64, activation='relu') # Or: layers.Dense(64, activation=tf.nn.relu) # 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))
训练和评估
训练设置
在模型构建好之后,通过调用
compile方法配置它的学习过程:
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 an output layer with 10 output units: layers.Dense(10)]) model.compile(optimizer=tf.keras.optimizers.Adam(0.01), loss=tf.keras.losses.CategoricalCrossentropy(from_logits=True), metrics=['accuracy'])
tf.keras.Model.compile有3个重要的参数:
optimizer
【优化器】: 这个对象明确了训练的过程。通过来自tf.keras.optimizers
模块的优化器实例,例如tf.keras.optimizers.Adam
或
tf.keras.optimizers.SGD
。如果你只想使用默认的参数,你也可以通过字符串来明确优化器设置,例如:'adam’
或'sgd'
loss
【损失函数】: 这个函数是用来最小化优化的过程。通常的选择包括了mse
,categorical_crossentropy
和binary_crossentropy
,损失函数通过名称或通过调用来自tf.keras.losses
模块的对象来确定。metrics
【指标】: 指标被用来监控训练过程,有一个字符串名称或来自tf.keras.metrics
模块的调用对象。
以下是一些例子:
# Configure a model for mean-squared error regression. model.compile(optimizer=tf.keras.optimizers.Adam(0.01), loss='mse', # mean squared error metrics=['mae']) # mean absolute error # Configure a model for categorical classification. model.compile(optimizer=tf.keras.optimizers.RMSprop(0.01), loss=tf.keras.losses.CategoricalCrossentropy(from_logits=True), metrics=['accuracy'])
从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)
Train on 1000 samples Epoch 1/10 1000/1000 [==============================] - 1s 664us/sample - loss: 248.7002 - accuracy: 0.1020 Epoch 2/10 1000/1000 [==============================] - 0s 76us/sample - loss: 948.1654 - accuracy: 0.1130 Epoch 3/10 1000/1000 [==============================] - 0s 75us/sample - loss: 2015.7246 - accuracy: 0.1080 Epoch 4/10 1000/1000 [==============================] - 0s 77us/sample - loss: 3351.2279 - accuracy: 0.0940 Epoch 5/10 1000/1000 [==============================] - 0s 75us/sample - loss: 4832.3537 - accuracy: 0.1100 Epoch 6/10 1000/1000 [==============================] - 0s 77us/sample - loss: 6854.4757 - accuracy: 0.1100 Epoch 7/10 1000/1000 [==============================] - 0s 75us/sample - loss: 9121.8044 - accuracy: 0.1060 Epoch 8/10 1000/1000 [==============================] - 0s 82us/sample - loss: 11787.7094 - accuracy: 0.0940 Epoch 9/10 1000/1000 [==============================] - 0s 77us/sample - loss: 14454.3959 - accuracy: 0.0950 Epoch 10/10 1000/1000 [==============================] - 0s 77us/sample - loss: 17878.5767 - accuracy: 0.0920
tf.keras.Model.fit有以下3个重要的参数:
epochs
: 训练被组织成若干个epoch。 一个epoch是对整个输入数据的一次迭代(以较小的批次完成)batch_size
: 当传递NumPy数据时,该模型会将数据切成较小的批次,并在训练期间对这些批次进行迭代。 该整数指定每个批次的大小。 请注意,如果不能通过批次大小将样本总数除以最后的批次,则最后一个批次可能较小(<batch_size)。validation_data
: 在对模型进行原型制作时,您希望轻松地在某些验证数据上监视其性能。 传递此参数(输入和标签的元组)可以使模型在每个时期结束时以推断模式显示所传递数据的损失和度量。
以下是以下使用了
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))
Train on 1000 samples, validate on 100 samples Epoch 1/10 1000/1000 [==============================] - 0s 165us/sample - loss: 20925.1987 - accuracy: 0.1210 - val_loss: 26310.0573 - val_accuracy: 0.1300 Epoch 2/10 1000/1000 [==============================] - 0s 88us/sample - loss: 24321.0911 - accuracy: 0.0870 - val_loss: 18061.3957 - val_accuracy: 0.0600 Epoch 3/10 1000/1000 [==============================] - 0s 88us/sample - loss: 28389.6354 - accuracy: 0.0870 - val_loss: 22141.4959 - val_accuracy: 0.0600 Epoch 4/10 1000/1000 [==============================] - 0s 88us/sample - loss: 32031.0515 - accuracy: 0.1050 - val_loss: 32656.4216 - val_accuracy: 0.1100 Epoch 5/10 1000/1000 [==============================] - 0s 86us/sample - loss: 37722.0383 - accuracy: 0.1150 - val_loss: 40242.5194 - val_accuracy: 0.0800 Epoch 6/10 1000/1000 [==============================] - 0s 86us/sample - loss: 41478.7577 - accuracy: 0.0820 - val_loss: 53799.4088 - val_accuracy: 0.1300 Epoch 7/10 1000/1000 [==============================] - 0s 87us/sample - loss: 47004.6546 - accuracy: 0.0960 - val_loss: 48183.4614 - val_accuracy: 0.0900 Epoch 8/10 1000/1000 [==============================] - 0s 87us/sample - loss: 52353.0959 - accuracy: 0.1030 - val_loss: 78486.1938 - val_accuracy: 0.0900 Epoch 9/10 1000/1000 [==============================] - 0s 86us/sample - loss: 57683.2655 - accuracy: 0.0970 - val_loss: 57110.6141 - val_accuracy: 0.1400 Epoch 10/10 1000/1000 [==============================] - 0s 86us/sample - loss: 61867.6552 - accuracy: 0.1020 - val_loss: 88238.6894 - val_accuracy: 0.1100
从tf.data数据集训练
使用这个数据集API来扩展到大型数据集或多设备的训练。通过
tf.data.Dataset的实例来拟合模型
# Instantiates a toy dataset instance: dataset = tf.data.Dataset.from_tensor_slices((data, labels)) dataset = dataset.batch(32) model.fit(dataset, epochs=10)
Train for 32 steps Epoch 1/10 32/32 [==============================] - 0s 9ms/step - loss: 69685.7440 - accuracy: 0.1030 Epoch 2/10 32/32 [==============================] - 0s 3ms/step - loss: 75590.5924 - accuracy: 0.1150 Epoch 3/10 32/32 [==============================] - 0s 2ms/step - loss: 82723.5186 - accuracy: 0.1050 Epoch 4/10 32/32 [==============================] - 0s 2ms/step - loss: 90571.8928 - accuracy: 0.1020 Epoch 5/10 32/32 [==============================] - 0s 3ms/step - loss: 100560.5365 - accuracy: 0.0900 Epoch 6/10 32/32 [==============================] - 0s 2ms/step - loss: 104773.0624 - accuracy: 0.0930 Epoch 7/10 32/32 [==============================] - 0s 3ms/step - loss: 111643.0691 - accuracy: 0.0970 Epoch 8/10 32/32 [==============================] - 0s 2ms/step - loss: 119917.4131 - accuracy: 0.0980 Epoch 9/10 32/32 [==============================] - 0s 2ms/step - loss: 128314.6912 - accuracy: 0.0910 Epoch 10/10 32/32 [==============================] - 0s 3ms/step - loss: 140880.0152 - accuracy: 0.0920
由于
Dataset会产生数据的各个batches(通过.batch()方法),因此此代码段(model.fit里)不需要
batch_size。
数据集也可以被用在验证集上:
dataset = tf.data.Dataset.from_tensor_slices((data, labels)) dataset = dataset.batch(32) val_dataset = tf.data.Dataset.from_tensor_slices((val_data, val_labels)) val_dataset = val_dataset.batch(32) model.fit(dataset, epochs=10, validation_data=val_dataset)
Train for 32 steps, validate for 4 steps Epoch 1/10 32/32 [==============================] - 0s 5ms/step - loss: 142411.4216 - accuracy: 0.0940 - val_loss: 137632.8926 - val_accuracy: 0.1300 Epoch 2/10 32/32 [==============================] - 0s 3ms/step - loss: 152728.9036 - accuracy: 0.1050 - val_loss: 226503.5469 - val_accuracy: 0.0900 Epoch 3/10 32/32 [==============================] - 0s 3ms/step - loss: 165234.6266 - accuracy: 0.1040 - val_loss: 214898.0352 - val_accuracy: 0.0800 Epoch 4/10 32/32 [==============================] - 0s 3ms/step - loss: 174043.3464 - accuracy: 0.1040 - val_loss: 281972.3750 - val_accuracy: 0.0800 Epoch 5/10 32/32 [==============================] - 0s 3ms/step - loss: 187527.2500 - accuracy: 0.0960 - val_loss: 150305.8633 - val_accuracy: 0.0800 Epoch 6/10 32/32 [==============================] - 0s 3ms/step - loss: 190493.9801 - accuracy: 0.0930 - val_loss: 227225.8945 - val_accuracy: 0.1300 Epoch 7/10 32/32 [==============================] - 0s 3ms/step - loss: 203611.7968 - accuracy: 0.1070 - val_loss: 214945.9609 - val_accuracy: 0.0900 Epoch 8/10 32/32 [==============================] - 0s 3ms/step - loss: 217935.7927 - accuracy: 0.0830 - val_loss: 246519.0938 - val_accuracy: 0.0900 Epoch 9/10 32/32 [==============================] - 0s 3ms/step - loss: 223697.2016 - accuracy: 0.0950 - val_loss: 261396.4180 - val_accuracy: 0.0800 Epoch 10/10 32/32 [==============================] - 0s 3ms/step - loss: 239267.6438 - accuracy: 0.0950 - val_loss: 371172.8203 - val_accuracy: 0.0800
评估和预测
tf.keras.Model.evaluate和
tf.keras.Model.predict方法可以使用NumPy和
tf.data.Dataset的数据
这里演示了如何对提供的数据进行评估损失和指标
# With Numpy arrays data = np.random.random((1000, 32)) labels = np.random.random((1000, 10)) model.evaluate(data, labels, batch_size=32) # With a Dataset dataset = tf.data.Dataset.from_tensor_slices((data, labels)) dataset = dataset.batch(32) model.evaluate(dataset)
1000/1000 [==============================] - 0s 51us/sample - loss: 356107.4905 - accuracy: 0.0990 32/32 [==============================] - 0s 2ms/step - loss: 357058.1465 - accuracy: 0.0990 [357058.146484375, 0.099]
这里演示了如何对提供的数据(data)预测并输出(result),
result = model.predict(data, batch_size=32) print(result.shape)
(1000, 10)
编写一个复杂的模型
The Functional API
tf.keras.Sequential只是一个简单的层的叠加,它并不能代表任意的模型。使用Keras functional API来构造复杂模型拓扑,例如:
- Multi-input models,【多输入的模型】
- Multi-output models,【多输出的模型】
- Models with shared layers (the same layer called several times),【层间共享的模型(同一层在一系列时间内调用多次)】
- Models with non-sequential data flows (e.g. residual connections).【使用非序列化的数据流模型】
使用Functional API构建一个模型的工作流如下:
- 一个层的实例被调用然后返回一个tensor
- 输入和输出tensor被用来定义一个
tf.keras.Model
实例 - 这个模型像顺序模型一样被训练
以下就是使用了Functional API的例子来构建一个简单的全连接网络
inputs = tf.keras.Input(shape=(32,)) # Returns an input placeholder # A layer instance is callable on a tensor, and returns a tensor. x = layers.Dense(64, activation='relu')(inputs) x = layers.Dense(64, activation='relu')(x) predictions = layers.Dense(10)(x)
给定输入和输出实例化模型
model = tf.keras.Model(inputs=inputs, outputs=predictions) # The compile step specifies the training configuration. model.compile(optimizer=tf.keras.optimizers.RMSprop(0.001), loss=tf.keras.losses.CategoricalCrossentropy(from_logits=True), metrics=['accuracy']) # Trains for 5 epochs model.fit(data, labels, batch_size=32, epochs=5)
Train on 1000 samples Epoch 1/5 1000/1000 [==============================] - 0s 399us/sample - loss: 14.3539 - accuracy: 0.1030 Epoch 2/5 1000/1000 [==============================] - 0s 77us/sample - loss: 30.8345 - accuracy: 0.1170 Epoch 3/5 1000/1000 [==============================] - 0s 79us/sample - loss: 61.1594 - accuracy: 0.1170 Epoch 4/5 1000/1000 [==============================] - 0s 76us/sample - loss: 101.4954 - accuracy: 0.1100 Epoch 5/5 1000/1000 [==============================] - 0s 75us/sample - loss: 149.6361 - accuracy: 0.1180
保存与恢复
仅保存权重值
通过
tf.keras.Model.save_weights保存和加载模型的权重
model = tf.keras.Sequential([ layers.Dense(64, activation='relu', input_shape=(32,)), layers.Dense(10)]) model.compile(optimizer=tf.keras.optimizers.Adam(0.001), loss=tf.keras.losses.CategoricalCrossentropy(from_logits=True), metrics=['accuracy'])
# Save weights to a TensorFlow Checkpoint file model.save_weights('./weights/my_model') # Restore the model's state, # this requires a model with the same architecture. model.load_weights('./weights/my_model')
默认,checkpoint格式保存权重,同样模型可以保存为HDF5格式(Keras默认的多后端实现)
# Save weights to a HDF5 file model.save_weights('my_model.h5', save_format='h5') # Restore the model's state model.load_weights('my_model.h5')
仅保存模型的配置
可以保存模型的配置-这无需任何权重就可以序列化模型体系结构。 即使没有定义原始模型的代码,保存的配置也可以重新创建和初始化相同的模型。 Keras支持JSON和YAML序列化格式:
# Serialize a model to JSON format json_string = model.to_json() json_string
'{"class_name": "Sequential", "config": {"name": "sequential_3", "layers": [{"class_name": "Dense", "config": {"name": "dense_17", "trainable": true, "batch_input_shape": [null, 32], "dtype": "float32", "units": 64, "activation": "relu", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}, {"class_name": "Dense", "config": {"name": "dense_18", "trainable": true, "dtype": "float32", "units": 10, "activation": "linear", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}]}, "keras_version": "2.2.4-tf", "backend": "tensorflow"}'
import json import pprint pprint.pprint(json.loads(json_string))
{'backend': 'tensorflow', 'class_name': 'Sequential', 'config': {'layers': [{'class_name': 'Dense', 'config': {'activation': 'relu', 'activity_regularizer': None, 'batch_input_shape': [None, 32], 'bias_constraint': None, 'bias_initializer': {'class_name': 'Zeros', 'config': {}}, 'bias_regularizer': None, 'dtype': 'float32', 'kernel_constraint': None, 'kernel_initializer': {'class_name': 'GlorotUniform', 'config': {'seed': None}}, 'kernel_regularizer': None, 'name': 'dense_17', 'trainable': True, 'units': 64, 'use_bias': True}}, {'class_name': 'Dense', 'config': {'activation': 'linear', 'activity_regularizer': None, 'bias_constraint': None, 'bias_initializer': {'class_name': 'Zeros', 'config': {}}, 'bias_regularizer': None, 'dtype': 'float32', 'kernel_constraint': None, 'kernel_initializer': {'class_name': 'GlorotUniform', 'config': {'seed': None}}, 'kernel_regularizer': None, 'name': 'dense_18', 'trainable': True, 'units': 10, 'use_bias': True}}], 'name': 'sequential_3'}, 'keras_version': '2.2.4-tf'}
从JSON重新创建模型(新初始化的)
fresh_model = tf.keras.models.model_from_json(json_string)
序列化一个模型为YAML格式要求你安装
pyyaml在你import tensorflow之前
yaml_string = model.to_yaml() print(yaml_string)
backend: tensorflow class_name: Sequential config: layers: - class_name: Dense config: activation: relu activity_regularizer: null batch_input_shape: !!python/tuple [null, 32] bias_constraint: null bias_initializer: class_name: Zeros config: {} bias_regularizer: null dtype: float32 kernel_constraint: null kernel_initializer: class_name: GlorotUniform config: {seed: null} kernel_regularizer: null name: dense_17 trainable: true units: 64 use_bias: true - class_name: Dense config: activation: linear activity_regularizer: null bias_constraint: null bias_initializer: class_name: Zeros config: {} bias_regularizer: null dtype: float32 kernel_constraint: null kernel_initializer: class_name: GlorotUniform config: {seed: null} kernel_regularizer: null name: dense_18 trainable: true units: 10 use_bias: true name: sequential_3 keras_version: 2.2.4-tf
从YAML重新创建一个模型
fresh_model = tf.keras.models.model_from_yaml(yaml_string)
保存整个模型为一个文件
整个模型可以保存到一个文件中,该文件包含权重值,模型的配置,甚至优化器的配置。 这使您可以检查模型并稍后从完全相同的状态恢复训练,而无需访问原始代码。
# Create a simple model model = tf.keras.Sequential([ layers.Dense(10, activation='relu', input_shape=(32,)), layers.Dense(10) ]) model.compile(optimizer='rmsprop', loss=tf.keras.losses.CategoricalCrossentropy(from_logits=True), metrics=['accuracy']) model.fit(data, labels, batch_size=32, epochs=5) # Save entire model to a HDF5 file model.save('my_model') # Recreate the exact same model, including weights and optimizer. model = tf.keras.models.load_model('my_model')
Train on 1000 samples Epoch 1/5 1000/1000 [==============================] - 0s 339us/sample - loss: 12.2740 - accuracy: 0.0880 Epoch 2/5 1000/1000 [==============================] - 0s 71us/sample - loss: 12.9732 - accuracy: 0.0930 Epoch 3/5 1000/1000 [==============================] - 0s 70us/sample - loss: 14.2485 - accuracy: 0.0980 Epoch 4/5 1000/1000 [==============================] - 0s 70us/sample - loss: 15.6711 - accuracy: 0.1040 Epoch 5/5 1000/1000 [==============================] - 0s 69us/sample - loss: 16.9925 - accuracy: 0.0940 WARNING:tensorflow:From /tmpfs/src/tf_docs_env/lib/python3.6/site-packages/tensorflow_core/python/ops/resource_variable_ops.py:1786: calling BaseResourceVariable.__init__ (from tensorflow.python.ops.resource_variable_ops) with constraint is deprecated and will be removed in a future version. Instructions for updating: If using Keras pass *_constraint arguments to layers. INFO:tensorflow:Assets written to: my_model/assets
相关文章推荐
- 一、正确安装 Keras & TensorFlow 并调用 TensorBoard 绘制训练曲线
- 基于keras的深度学习基本概念讲解
- keras用vgg16做图像分类
- 基于Theano的深度学习(Deep Learning)框架Keras学习随笔-05-模型
- keras2.0的一些变化
- Keras:2.3 keras实现卷积神经网络
- using learning rate schedules for deep learning models in python with keras
- Anaconda+MINGW+theano+keras安装
- keras与sklearn的结合使用
- ubuntu系统安装(双显卡)加cuda、anaconda、cudnn、theano、keras
- Keras
- Another Keras Tutorial For Neural Network Beginners
- numpy的介绍——总览
- 【转】基于keras 的神经网络股价预测模型
- learn opencv- 深度学习使用Keras - 基础知识
- 【keras】解决 example 案例中 MNIST 数据集下载不了的问题
- 【tensorflow】CycleGAN + anaconda3 + tflearn + keras
- 使用Keras和Tensorflow检测恶意请求
- theano与keras安装问题
- 第七期 使用 Keras 演示神经网络 《显卡就是开发板》