简体   繁体   English

Keras 修剪:将权重设置为零不会加速推理?

[英]Keras Pruning: Setting Weights to Zero Doesn't Accelerate Inference?

I'm writing a pruning algorithm for tf.keras that simply removes the lowest x percentile of weights from a layer / filter.我正在为 tf.keras 编写一个修剪算法,它只是从层/过滤器中删除最低的 x 百分位数的权重。 To do this, I've tried setting the value of the weights to prune to zero.为此,我尝试将要修剪的权重值设置为零。 Having read other sources, I'm under the impression that this has the same effect as "removing" a weight from a network, but even if I set all the weights in a network to be zero, no decrease in inference time is noted.阅读其他来源后,我的印象是这与从网络中“删除”权重具有相同的效果,但即使我将网络中的所有权重设置为零,也不会注意到推理时间的减少。

If I were to hypothetically set all the weights in a layer to zero, the code would be as follows:如果我假设将图层中的所有权重设置为零,则代码如下:

    flat_weights = np.array(self.model.layers[layer_index].get_weights()[0]).flatten()

    weight_index = 0 
    for weight in flat_weights:
        #if weight < self.delta_percentiles[index]:
        flat_weights[weight_index] = 0
        weight_index += 1

    weights[0] = np.reshape(flat_weights, original_shape)
    weights[1] = np.zeros(np.shape(weights[1]))

    self.model.layers[index].set_weights(weights)

Theoretically, the inference time of a model pruned in such a way should decrease but no change is found.从理论上讲,以这种方式修剪的模型的推理时间应该会减少,但没有发现任何变化。 Am I pruning correctly?我修剪正确吗?

Setting a weight to zero is kind of the same as removing a weight, as then the network would be functionally equivalent if you had the same architecture, but with the same weights and one less neuron in that layer.将权重设置为零与删除权重有点相同,因为如果您具有相同的架构,但具有相同的权重并且该层中少一个神经元,那么网络在功能上将是等效的。 The predictions you would get are the same.你会得到的预测是一样的。

But it does not have an effect on computational performance, as you noticed.但正如您所注意到的,它不会对计算性能产生影响。 For computation time to change, you would have to define a new network with one less weight, and then load the weights from the other architecture.为了改变计算时间,您必须定义一个权重较小的新网络,然后从其他架构加载权重。 You are now imagining that doing this is not easy, and it is the reason why we do not do it generally for evaluation, as we want to find out how predictive performance (like accuracy or mean squared error) changes as you prune weights.您现在认为这样做并不容易,这就是为什么我们通常不这样做进行评估的原因,因为我们想了解在您修剪权重时预测性能(如准确性或均方误差)如何变化。

So in order to get the computational advantages of pruning, you have to do a lot more than just setting weights to zero.因此,为了获得修剪的计算优势,您必须做的不仅仅是将权重设置为零。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM