简体   繁体   English

在Keras中对可训练参数进行排序

[英]Sort trainable parameters in Keras

When I have some trainable parameters, say layer.trainable_weights . 当我有一些可训练的参数时,请说layer.trainable_weights I want to sort those weights before feed into other operations, is it possible for me to do that? 我想在进行其他操作之前对这些权重进行排序,我可以这样做吗? Can I use something like 我可以使用类似的东西吗

import tensorflow as tf
p = layer.trainable_weights
p = tf.sort(p)

or are there any particular ways in Keras? 还是Keras中有什么特别的方法?

I'm new to Keras and TensorFlow. 我是Keras和TensorFlow的新手。 Really appreciate if someone can answer my questions, thanks in advance! 非常感谢有人可以回答我的问题,谢谢!


EDIT: For "other operations", I want to feed those sorted trainable weights into another neural network, but that neural network is fixed (not trainable). 编辑:对于“其他操作”,我想将这些排序的可训练权重输入另一个神经网络,但该神经网络是固定的(不可训练)。 So what I want to do is something like 所以我想做的是

import tensorflow as tf
p = model.layer[0].trainable_weights
p = tf.sort(p)

another_model.trainable = False
x = another_model(p)
# x is involved in the loss function of the original model

Hope this is clear, and hope anyone can help me! 希望这很清楚,希望任何人都能帮助我! (also, can I just use x=another_model.predict(p) instead of x=another_model(p) above?) (另外,我可以只使用x=another_model.predict(p)代替上面的x=another_model(p)吗?)

Of course, you can do it. 当然可以。 Since you have not stated clearly what these downstreaming operations are, it is more difficult to answer your question. 由于您尚未清楚说明这些下游操作是什么,因此很难回答您的问题。

If you only want to do something to monitor the training process, eg monitoring a custom metric to measure the cumulative distribution function of the weight matrix of interest, feel free to use tf.sort (you may use K.stop_gradient(W) before you sort it to further ensure no gradient is flowed back from any downstreaming process). 如果您只想做一些事情来监视训练过程,例如监视自定义指标以测量感兴趣的权重矩阵的累积分布函数,请随时使用tf.sort (在使用前可以使用K.stop_gradient(W)对其进行排序,以进一步确保没有任何下游过程回流的梯度。

If you want to do things other than monitoring, eg computing a custom regularization term to play a part in training optimization, you should rethink how to implement this part both efficiently and effectively, but directly using tf.sort does not help! 如果您想做除监视之外的其他事情,例如计算自定义正则项以在培训优化中发挥作用,您应该重新考虑如何高效地实现这一部分, 但是直接使用tf.sort并没有帮助! Why? 为什么? Because this function is not implemented with back-propagation, (you might want to double check on this to see whether the latest version supports this feature) 由于此功能未通过反向传播实现,(您可能需要对此进行仔细检查,以查看最新版本是否支持此功能)

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

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