我正在使用tf.get_collection()
的范围正则表达式获取我想要使用单独的优化器运行的变量集合。然后我想要一个单独的集合,其中包含我指定的集合中未包含的所有其他变量,以便我可以确保所有这些变量都传递给其他优化器。我提出了一种方法,但我不确定它是否最有效。在tensorflow中有更好的方法吗?考虑下面的例子..
import tensorflow as tf
图表= tf.Graph() 与graph.as_default():
with tf.name_scope('some_scope1'):
a = tf.Variable(1, 'a')
b = tf.Variable(2, 'b')
c = tf.Variable(3, 'c')
with tf.name_scope('some_scope2'):
d = tf.Variable(4, 'd')
e = tf.Variable(5, 'e')
f = tf.Variable(6, 'f')
h = tf.Variable(8, 'h')
trainable_collection = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)
scope_collection = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope='some_scope')
no_scope = [var for var in trainable_collection if var not in scope_collection]
for i in trainable_collection:
if i not in scope_collection:
print i.name # i.name if you want just a name
for i in no_scope:
print i.name
no_scope
确实包含scope_collection
中未包含的所有变量,但我不确定这是否是执行此操作的最佳方法?还有其他选择吗?