[英]how to freeze the tensorflow model?
I have a model.py file that save a class of rnn. 我有一个保存rnn类的model.py文件。 for example: 例如:
class TextRNN:
def __init__(self, hidden_size, num_classes, learning_rate...):
self.input_data = tf.placeholder(tf.int32,[batch_size,None],name="input_data")
self.output_label = tf.placeholder(tf.float32,[batch_size,num_classes],name="output_label")
self.dropout_rate = tf.placeholder(tf.float32,name="dropout_rate")
with tf.name_scope("embedding"):
...
with tf.name_scope("hidden"):
...
with tf.name_scope("output"):
...
In my main.py, I used the following code to use this model. 在main.py中,我使用了以下代码来使用此模型。
with tf.Graph().as_default():
sess = tf.Session()
with sess.as_default():
rnn = TextRNN(...)
...
#training step
def train_step(x_batch, y_batch...):
feed_dict = {rnn.input_data:x_batch,rnn.output_label:y_batch,rnn.dropout_rate:0.5}
sess.run(feed_dict)
...
#testing step
def test_step(x_batch, y_batch...):
feed_dict = {rnn.input_data:x_batch,rnn.output_label:y_batch,rnn.dropout_rate:0.5}
sess.run(feed_dict)
My question is how to freeze the model in the testing step? 我的问题是在测试步骤中如何冻结模型? I know if I run training step, the weights of the model would be updated. 我知道如果运行训练步骤,该模型的权重将被更新。 But when I run testing step, I don't want to update the weights anymore, I only want to get the predicted results? 但是当我运行测试步骤时,我不再想要更新权重了,我只想获得预测的结果? How should I modify my code to make this happen? 我应该如何修改我的代码以实现此目的?
To train your model, you need to define a training op an run it, eg train_op = tf.train.AdamOptimizer(1e-4).minimize(loss_op)
would mean that each time you run train_op
it will proceed one step of the AdamOptimizer on the loss_op
and it will update the variables. 要训练模型,您需要定义一个训练来运行模型, 例如 train_op = tf.train.AdamOptimizer(1e-4).minimize(loss_op)
意味着每次您运行train_op
它都会进行AdamOptimizer的一步在loss_op
,它将更新变量。
But if you want to evaluate your model, just run you output_op
and you'll get your result. 但是,如果要评估模型,只需运行output_op
即可得到结果。 TensorFlow will only run the part of the graph that is required and not more. TensorFlow将仅运行图表中所需的部分,而不会更多。 So when you ask for output_op
or loss_op
it won't modify the variables. 所以,当你问output_op
或loss_op
它不会改变的变量。
You might find this page useful to understand more how things work in TensorFlow. 您可能会发现此页面有助于了解更多内容在TensorFlow中的工作方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.