Keras 模块
正如我们前面学到的,Keras 模块包含预定义的类、函数和变量,这些对于深度学习算法非常有用。本章让我们学习 Keras 提供的模块。
可用模块
让我们首先看看 Keras 中可用的模块列表。
初始化器 - 提供初始化器函数列表。我们可以在 Keras 的层章节中详细学习它,在机器学习的模型创建阶段。
正则化器 - 提供正则化器函数列表。我们可以在Keras 层章节中详细学习它。
约束 - 提供约束函数列表。我们可以在Keras 层章节中详细学习它。
激活函数 - 提供激活函数列表。我们可以在Keras 层章节中详细学习它。
损失函数 - 提供损失函数列表。我们可以在模型训练章节中详细学习它。
度量 - 提供度量函数列表。我们可以在模型训练章节中详细学习它。
优化器 - 提供优化器函数列表。我们可以在模型训练章节中详细学习它。
回调函数 - 提供回调函数列表。我们可以在训练过程中使用它来打印中间数据,并根据某些条件停止训练本身(EarlyStopping 方法)。
文本处理 - 提供将文本转换为适合机器学习的 NumPy 数组的函数。我们可以在机器学习的数据准备阶段使用它。
图像处理 - 提供将图像转换为适合机器学习的 NumPy 数组的函数。我们可以在机器学习的数据准备阶段使用它。
序列处理 - 提供根据给定输入数据生成基于时间的函数。我们可以在机器学习的数据准备阶段使用它。
后端 - 提供后端库(如TensorFlow 和 Theano)的函数。
实用工具 - 提供许多在深度学习中非常有用的实用工具函数。
本章让我们看看后端模块和实用工具模块。
后端模块
后端模块用于 Keras 后端操作。默认情况下,Keras 运行在 TensorFlow 后端之上。如果需要,您可以切换到其他后端,如 Theano 或 CNTK。默认后端配置定义在根目录下的 .keras/keras.json 文件中。
可以使用以下代码导入 Keras 后端模块
>>> from keras import backend as k
如果我们使用默认后端TensorFlow,则以下函数将返回如下所示的基于TensorFlow 的信息:
>>> k.backend() 'tensorflow' >>> k.epsilon() 1e-07 >>> k.image_data_format() 'channels_last' >>> k.floatx() 'float32'
让我们简要了解一些用于数据分析的重要后端函数:
get_uid()
它是默认图的标识符。定义如下:
>>> k.get_uid(prefix='') 1 >>> k.get_uid(prefix='') 2
reset_uids
它用于重置 uid 值。
>>> k.reset_uids()
现在,再次执行get_uid()。这将被重置并再次更改为 1。
>>> k.get_uid(prefix='') 1
placeholder
它用于实例化一个占位符张量。一个简单的用于保存 3 维形状的占位符如下所示:
>>> data = k.placeholder(shape = (1,3,3)) >>> data <tf.Tensor 'Placeholder_9:0' shape = (1, 3, 3) dtype = float32> If you use int_shape(), it will show the shape. >>> k.int_shape(data) (1, 3, 3)
dot
它用于将两个张量相乘。假设 a 和 b 是两个张量,c 将是 ab 相乘的结果。假设 a 的形状是 (4,2),b 的形状是 (2,3)。定义如下:
>>> a = k.placeholder(shape = (4,2)) >>> b = k.placeholder(shape = (2,3)) >>> c = k.dot(a,b) >>> c <tf.Tensor 'MatMul_3:0' shape = (4, 3) dtype = float32> >>>
ones
它用于初始化所有值为1。
>>> res = k.ones(shape = (2,2)) #print the value >>> k.eval(res) array([[1., 1.], [1., 1.]], dtype = float32)
batch_dot
它用于对批量数据执行乘积。输入维度必须为 2 或更高。如下所示:
>>> a_batch = k.ones(shape = (2,3)) >>> b_batch = k.ones(shape = (3,2)) >>> c_batch = k.batch_dot(a_batch,b_batch) >>> c_batch <tf.Tensor 'ExpandDims:0' shape = (2, 1) dtype = float32>
variable
它用于初始化一个变量。让我们在这个变量中执行简单的转置操作。
>>> data = k.variable([[10,20,30,40],[50,60,70,80]])
#variable initialized here
>>> result = k.transpose(data)
>>> print(result)
Tensor("transpose_6:0", shape = (4, 2), dtype = float32)
>>> print(k.eval(result))
[[10. 50.]
[20. 60.]
[30. 70.]
[40. 80.]]
如果要从 numpy 访问:
>>> data = np.array([[10,20,30,40],[50,60,70,80]]) >>> print(np.transpose(data)) [[10 50] [20 60] [30 70] [40 80]] >>> res = k.variable(value = data) >>> print(res) <tf.Variable 'Variable_7:0' shape = (2, 4) dtype = float32_ref>
is_sparse(tensor)
它用于检查张量是否是稀疏的。
>>> a = k.placeholder((2, 2), sparse=True)
>>> print(a) SparseTensor(indices =
Tensor("Placeholder_8:0",
shape = (?, 2), dtype = int64),
values = Tensor("Placeholder_7:0", shape = (?,),
dtype = float32), dense_shape = Tensor("Const:0", shape = (2,), dtype = int64))
>>> print(k.is_sparse(a)) True
to_dense()
它用于将稀疏转换为稠密。
>>> b = k.to_dense(a)
>>> print(b) Tensor("SparseToDense:0", shape = (2, 2), dtype = float32)
>>> print(k.is_sparse(b)) False
random_uniform_variable
它使用均匀分布的概念进行初始化。
k.random_uniform_variable(shape, mean, scale)
这里:
shape - 以元组的形式表示行和列。
mean - 均匀分布的均值。
scale - 均匀分布的标准差。
让我们来看一下下面的示例用法:
>>> a = k.random_uniform_variable(shape = (2, 3), low=0, high = 1) >>> b = k. random_uniform_variable(shape = (3,2), low = 0, high = 1) >>> c = k.dot(a, b) >>> k.int_shape(c) (2, 2)
utils 模块
utils 提供了深度学习中非常有用的实用工具函数。utils 模块提供的一些方法如下:
HDF5Matrix
它用于以 HDF5 格式表示输入数据。
from keras.utils import HDF5Matrix data = HDF5Matrix('data.hdf5', 'data')
to_categorical
它用于将类向量转换为二进制类矩阵。
>>> from keras.utils import to_categorical >>> labels = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> to_categorical(labels) array([[1., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 1., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 1., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 1., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 1., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 1., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 1., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 1., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 1., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 0., 1.]], dtype = float32) >>> from keras.utils import normalize >>> normalize([1, 2, 3, 4, 5]) array([[0.13483997, 0.26967994, 0.40451992, 0.53935989, 0.67419986]])
print_summary
它用于打印模型的摘要。
from keras.utils import print_summary print_summary(model)
plot_model
它用于创建模型在点格式下的表示并将其保存到文件中。
from keras.utils import plot_model plot_model(model,to_file = 'image.png')
此plot_model 将生成一个图像来了解模型的性能。