Keras实践

keras

初始化器的用法

初始化定义了设置 Keras 各层权重随机初始值的方法。
用来将初始化器传入 Keras 层的参数名取决于具体的层。通常关键字为 kernel_initializer 和 bias_initializer

# Compatibility aliases
zero = zeros = Zeros
one = ones = Ones
constant = Constant
uniform = random_uniform = RandomUniform
normal = random_normal = RandomNormal
truncated_normal = TruncatedNormal
identity = Identity
orthogonal = Orthogonal

正则化器的使用

正则化器允许在优化过程中对层的参数或层的激活情况进行惩罚。 网络优化的损失函数也包括这些惩罚项。
惩罚是以层为对象进行的。具体的 API 因层而异,但 Dense,Conv1D,Conv2D 和 Conv3D 这些层具有统一的 API。
正则化器开放 3 个关键字参数:

kernel_regularizer: keras.regularizers.Regularizer 的实例
bias_regularizer: keras.regularizers.Regularizer 的实例
activity_regularizer: keras.regularizers.Regularizer 的实例

keras.regularizers.l1(0.)
keras.regularizers.l2(0.)
keras.regularizers.l1_l2(l1=0.01, l2=0.01)

约束项的使用

constraints 模块的函数允许在优化期间对网络参数设置约束(例如非负性)。约束是以层为对象进行的。
约束层开放 2 个关键字参数:
kernel_constraint 用于主权重矩阵。
bias_constraint 用于偏置。

max_norm(max_value=2, axis=0): 最大范数约束
non_neg(): 非负性约束
unit_norm(axis=0): 单位范数约束
min_max_norm(min_value=0.0, max_value=1.0, rate=1.0, axis=0): 最小/最大范数约束

优化器的用法

# Aliases.
sgd = SGD
rmsprop = RMSprop
adagrad = Adagrad
adadelta = Adadelta
adam = Adam
adamax = Adamax
nadam = Nadam

评价函数的用法

评价函数用于评估当前训练模型的性能。
binary_accuracy
categorical_accuracy
accuracy

损失函数

categorical_crossentropy
binary_crossentropy
sparse_categorical_crossentropy
kullback_leibler_divergence
poisson
cosine_proximity
mse = MSE = mean_squared_error
mae = MAE = mean_absolute_error
mape = MAPE = mean_absolute_percentage_error
msle = MSLE = mean_squared_logarithmic_error
cosine = cosine_proximity

tensor维度

第一维为12表示样本个数为12,第二维为空,表示特征维度不确定
(12,)
model.add(Reshape((3, 4), input_shape=(12,)))

还支持使用 -1 表示维度的尺寸推断
model.add(Reshape((-1, 2, 2)))

第一维为None表示样本个数不确定,第二维为32,表示特征维度为32
model.output_shape == (None, 32)

keras issue

(1) keras ValueError a dot layer should be called on a list of 2 inputs
(2) "The Merge layer is deprecated and will be removed after 08/2017. Useinstead layers from keras.layers.merge, e.g. add, concatenate, etc."

tensorflow issue

ImportError: Could not find 'msvcp140.dll'. TensorFlow requires that this DLL be installed in a directory that is named in your %PATH% environment variable. You may install this DLL by downloading Visual C++ 2015 Redistributable Update 3 from this URL: https://www.microsoft.com/en-us/download/details.aspx?id=53587
Process finished with exit code 1

solution:
download vc_redist.x64.exe并安装.

原文地址:https://www.cnblogs.com/sunzhuli/p/9696835.html