如果一个大模型由端到端的单个模型组成,那么我(训练后的)能否只保留一个模型,并在推理过程中冻结/丢弃其他模型?
一个例子:这个struct2depth (见下文)以一种无监督的方式进行了三种模型的培训。然而,我真正需要的是物体的运动,即3D Object Motion Estimation部分。所以我想知道这是否可行
train在最初的网络上,但是inference只包含Object Motion Estimator,即其他冻结/丢弃的层?我看到在tensorflow中可以获得指定层的张量输出,但是为了节省不必要的计算,我想简单地冻结所有其他部分.不知道这是否可能。
期待着一些见解。提前感谢!

发布于 2021-01-22 14:37:59
可以通过将权重设置为0来忽略权重。为此,您可以直接获得权重W并执行W.assign(tf.mul(W,0))。我知道您关心加速推理,但是除非您重写代码以使用稀疏表示,否则您可能不会加速推理,因为权重不能完全删除。
您可以选择的方法是查看自定义层的剪枝的现有解决方案。
class MyDenseLayer(tf.keras.layers.Dense, tfmot.sparsity.keras.PrunableLayer):
def get_prunable_weights(self):
# Prune bias also, though that usually harms model accuracy too much.
return [self.kernel, self.bias]
# Use `prune_low_magnitude` to make the `MyDenseLayer` layer train with pruning.
model_for_pruning = tf.keras.Sequential([
tfmot.sparsity.keras.prune_low_magnitude(MyDenseLayer(20, input_shape=input_shape)),
tf.keras.layers.Flatten()
])例如,您可以使用ConstantSparsity (请参阅这里)并设置参数,以便您的层被完全修剪。
另一种选择是构造第二个,较小的模型,您只用于推理。然后,您可以在训练后单独保存所需的权重(而不是保存整个模型),并将它们加载到第二个模型中。
https://stackoverflow.com/questions/65844369
复制相似问题