Ramesh-X
Ramesh-X

Reputation: 5055

Train only some of the variables in tensorflow

I'm using tensorflow to do a gradient decent classification.

train_op = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

here cost is the cost function that I have used in optimization. After launching the Graph in the Session, the Graph can be fed as:

sess.run(train_op, feed_dict)

And with this, all the variables in the cost function will be updated in order to minimized the cost.

Here is my question. How can I update only some variables in the cost function when training..? Is there a way to convert created variables into constants or something..?

Upvotes: 3

Views: 3626

Answers (1)

Jean Mercat
Jean Mercat

Reputation: 88

There are several good answers, this subject should already be closed: stackoverflow Quora

Just to avoid another click for people getting here :

The minimize function of the tensorflow optimizer takes a var_list argument for that purpose:

first_train_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,
                                     "scope/prefix/for/first/vars")
first_train_op = optimizer.minimize(cost, var_list=first_train_vars)

second_train_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,
                                      "scope/prefix/for/second/vars")                     
second_train_op = optimizer.minimize(cost, var_list=second_train_vars)

I took it as is from mrry

To get the list of the names you should use instead of "scope/prefix/for/second/vars" you can use :

tf.get_default_graph().get_collection_ref(tf.GraphKeys.TRAINABLE_VARIABLES)

Upvotes: 4

Related Questions