且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Tensorflow负载预训练模型使用不同的优化器

更新时间:2022-04-11 01:07:41

您可能必须将张量导入到变量中,该变量是先前输入到您的Adam Optimizer中的损失函数/交叉熵 >.现在,只需将其通过您的SGD优化器输入即可.

You probably would have to import the tensor into a variable which is the loss function/cross-entropy that feeds into your Adam Optimizer previously. Now, just feed it through your SGD optimizer instead.

saver = tf.train.import_meta_graph('filename.meta')
saver.restore(sess,tf.train.latest_checkpoint('./'))
graph = tf.get_default_graph()
cross_entropy = graph.get_tensor_by_name("entropy:0") #Tensor to import

optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cross_entropy)

在这种情况下,在训练我的训练前模型之前,我已经标记了交叉熵张量,就像这样

In this case, I have tagged the cross-entropy Tensor before training my pre-train model with the name entropy, as such

tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv), name = 'entropy')

如果您无法更改预训练模型,则可以从graph获取模型中的张量列表(在导入后),然后推断出所需的张量.我没有使用Tensorlayer的经验,因此本指南旨在提供更多的理解.您可以看一下 Tensorlayer层,他们应该解释如何获得张量由于Tensorlayer是建立在Tensorflow之上的,因此大多数功能仍然应该可用.

If you are unable to make changes to your pretrain model, you can obtain the list of Tensors in your model(after you have imported it) from graph and deduce which Tensor you require. I have no experience with Tensorlayer, so this guide is to provide more of an understanding. You can take a look at Tensorlayer-Layers, they should explain how to obtain your Tensor. As Tensorlayer is built on top of Tensorflow, most of the functions should still be available.