【问题标题】:Train only some of the variables in tensorflow只训练 tensorflow 中的一些变量
【发布时间】:2016-08-17 05:57:28
【问题描述】:

我正在使用 tensorflow 进行梯度分类。

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

这里cost 是我在优化中使用的成本函数。 在 Session 中启动 Graph 后,可以将 Graph 输入为:

sess.run(train_op, feed_dict)

这样,成本函数中的所有变量都将被更新以最小化成本。

这是我的问题。训练时如何只更新成本函数中的一些变量..?有没有办法将创建的变量转换为常量或其他东西..?

【问题讨论】:

标签: python python-2.7 tensorflow


【解决方案1】:

有几个很好的答案,这个主题应该已经关闭了: stackoverflow Quora

只是为了避免人们再次点击:

tensorflow 优化器的最小化函数为此使用 var_list 参数:

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)

我按原样从mrry 得到它

要获取您应该使用的名称列表,而不是 "scope/prefix/for/second/vars",您可以使用:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-30
    • 1970-01-01
    • 2020-07-16
    相关资源
    最近更新 更多