Aru*_*run 3 python neural-network tensorflow
我正在尝试使用Keras教程TensorFlow 2.0 Magnitude-based weight pruning 并遇到参数initial_sparsity
import tensorflow_model_optimization as tfmot
from tensorflow_model_optimization.sparsity import keras as sparsity
import numpy as np
epochs = 12
num_train_samples = x_train.shape[0]
end_step = np.ceil(1.0 * num_train_samples / batch_size).astype(np.int32) * epochs
print('End step: ' + str(end_step))
pruning_params = {
'pruning_schedule': sparsity.PolynomialDecay(initial_sparsity=0.50,
final_sparsity=0.90,
begin_step=2000,
end_step=end_step,
frequency=100)
}
Run Code Online (Sandbox Code Playgroud)
教程说:
这里使用的参数是指:
Spasity PolynomialDecay 用于整个训练过程。我们从 50% 的稀疏度开始,逐渐训练模型达到 90% 的稀疏度。X% 稀疏性意味着 X% 的权重张量将被修剪掉。
我的问题是,您不应该从0% 的initial_sparsity开始,然后剪掉 90% 的权重吗?
以 50% 的initial_sparsity开头是什么意思?这是否意味着先修剪 50% 的权重,然后实现 90% 的稀疏修剪?
另外,对于tfmot.sparsity.keras.ConstantSparsity,API 如下:
pruning_params_unpruned = {
'pruning_schedule': sparsity.ConstantSparsity(
target_sparsity=0.0, begin_step=0,
end_step = 0, frequency=100
)
}
Run Code Online (Sandbox Code Playgroud)
初始化具有恒定稀疏性的修剪计划。
在每个频率步长的区间 [begin_step, end_step] 中应用稀疏性。在每个适用的步骤中,稀疏度 (%) 是恒定的。
这是否意味着如果神经网络模型已经处于 50% 的稀疏级别,但target_sparsity = 0.5,那么修剪计划是否会执行以下操作:
你可以在PolynomialDecay和ConstantSparsity 中阅读它
谢谢
小智 9
所以我还发现 Tensorflow 上关于权重修剪的文档非常稀疏,所以我花了一些时间使用调试器来弄清楚一切是如何工作的。
在最基本的层面上,修剪时间表只是一个将步骤作为输入并产生稀疏百分比的函数。然后使用该稀疏值生成一个掩码,该掩码用于删除绝对值小于由绝对值权重分布和稀疏百分比给出的k-1值的权重。
类定义:Github 链接
上面类定义中包含的注释帮助我了解 PolynomialDecay 调度程序的工作原理。
剪枝率从initial_sparsity开始快速增长,然后缓慢达到目标稀疏度。
应用的函数是
current_sparsity = final_sparsity + (initial_sparsity - final_sparsity) * (1 - (step - begin_step)/(end_step - begin_step)) ^ 指数
由上式可知,当step == begin_step再current_sparsity = initial_sparsity。因此,权重将被修剪为参数initial_sparsity指定的步骤begin_step。
我同意您的评估,因为您通常希望以低于 50% 的稀疏度开始修剪,但我没有任何已发表的研究可以引用来支持这一说法。您可以在 PolynomialDecay 类定义引用的论文中找到更多信息,尽管我自己没有机会阅读。
类定义:Github Link
这个调度器的用途似乎非常有限。对于每个有效的修剪步骤,target_sparsity都会返回 。因此,多个修剪步骤是非常多余的。此调度程序的用例似乎是在训练期间进行一次修剪。多次使用此调度程序进行修剪的能力是将其与其父抽象类和其他修剪调度程序对齐。
如果上面的两个调度程序没有让你的船漂浮,抽象类PruningSchedule公开了一个端点,这使得创建自己的修剪调度程序变得非常容易,尽管它可能很复杂。下面是我自己创建的一个例子。
免责声明:此调度程序是一个 19 岁大学生的想象力的创造,没有任何已发表的文献依据。
PruningSchedule = tfmot.sparsity.keras.PruningSchedule
class ExponentialPruning(PruningSchedule):
def __init__(self, rate=0.01, begin_step=0, frequency=100, max_sparsity=0.9):
self.rate = rate
self.begin_step = begin_step
self.frequency = frequency
self.max_sparsity = max_sparsity
# Validation functions provided by the parent class
# The -1 parameter is for the end_step
# as this pruning schedule does not have one
# The last true value is a boolean flag which says it is okay
# to have no end_step
self._validate_step(self.begin_step, -1, self.frequency, True)
self._validate_sparsity(self.max_sparsity, 'Max Sparsity')
def __call__(self, step):
# Sparsity calculation endpoint
# step is a integer tensor
# The sparsity returned by __call__ must be a tensor
# of dtype=tf.float32, so tf.math is required.
# In the logic below, you can assume that a valid
# pruning step is passed.
p = tf.math.divide(
tf.cast(step - self.begin_step, tf.float32),
tf.constant(self.frequency, dtype=tf.float32)
)
sparsity = tf.math.subtract(
tf.constant(1, dtype=tf.float32),
tf.math.pow(
tf.constant(1 - self.rate, dtype=tf.float32),
p
)
)
sparsity = tf.cond(
tf.math.greater(sparsity, tf.constant(self.max_sparsity, dtype=tf.float32)),
lambda: tf.constant(self.max_sparsity, dtype=tf.float32),
lambda: sparsity
)
# This function returns a tuple of length 2
# The first value determines if pruning should occur on this step
# I recommend using the parent class function below for this purpose
# The negative one value denotes no end_step
# The second value is the sparsity to prune to
return (self._should_prune_in_step(step, self.begin_step, -1, self.frequency),
sparsity)
def get_config(self):
# A function required by the parent class
# return the class_name and the input parameters as
# done below
return {
'class_name': self.__class__.__name__,
'config': {
'rate': self.rate,
'begin_step': self.begin_step,
'frequency': self.frequency,
'max_sparsity': self.max_sparsity
}
}
Run Code Online (Sandbox Code Playgroud)
如果您只想修剪某些层,而不是所有可修剪的层,您可以prune_low_magnitude在要添加到模型的层上调用该函数。
prune_low_magnitude = tfmot.sparsity.keras.prune_low_magnitude
model = keras.models.Sequential()
...
model.add(prune_low_magnitude(keras.layers.Dense(8, activation='relu', kernel_regularizer=keras.regularizers.l1(0.0001)),
ExponentialPruning(rate=1/8)))
Run Code Online (Sandbox Code Playgroud)
还要确保将UpdatePruningStep实例传递给训练回调:
m.fit(train_input, train_labels, epochs=epochs, validation_data=[test_input, test_labels],
callbacks=[UpdatePruningStep()])
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
603 次 |
| 最近记录: |