选择可训练的变量来计算梯度"没有变量来优化"

时间:2018-01-24 18:47:17

标签: python tensorflow neural-network

我试图选择可训练变量的一个子集,即第一个和第二个隐藏层中的变量,使用get_collection和范围来执行一些带有渐变的操作(例如,剪切渐变)名称范围。但是,出于某种原因,Tensorflow没有找到任何要优化的变量。下面,我复制一个可复制的示例,复制我的错误。我想也许这与我在scope中输入get_collection的方式有关:

import tensorflow as tf
from tensorflow.contrib.layers import fully_connected

X = tf.placeholder(dtype=tf.float32, shape=(None, 50), name='X')
Y = tf.placeholder(dtype= tf.float32, shape= (None), name = 'Y')

with tf.name_scope('DNN') as scope:
    hidden1 = fully_connected(X, 20, scope= 'hidden1' )
    hidden2 = fully_connected(hidden1, 10, scope= 'hidden2')
    hidden3 = fully_connected(hidden2, 5, scope= 'hidden3')
    output = fully_connected(hidden3, 1, activation_fn= None ,scope = 'outs')
    logits = tf.nn.sigmoid(output, name= 'logists_out')

with tf.name_scope('loss') as scope:
    loss = tf.reduce_mean(Y - logits)

learning_rate = 0.01
with tf.name_scope('train') as scope:
    optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)
    train_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope= '[12]')
    grads_and_vars = optimizer.compute_gradients(loss, var_list = train_vars)
    clipped_grads = [(tf.clip_by_value(grad,-1,1), var) for grad, var in grads_and_vars]
    train_op = optimizer.apply_gradients(clipped_grads)


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-6-a8007f96c949> in <module>()
     16     optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)
     17     train_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope= '[12]')
---> 18     grads_and_vars = optimizer.compute_gradients(loss, var_list = train_vars)
     19     clipped_grads = [(tf.clip_by_value(grad,-1,1), var) for grad, var in grads_and_vars]
     20     train_op = optimizer.apply_gradients(clipped_grads)

/home/diego/Documents/python27/ML/local/lib/python2.7/site-packages/tensorflow/python/training/optimizer.pyc in compute_gradients(self, loss, var_list, gate_gradients, aggregation_method, colocate_gradients_with_ops, grad_loss)
    406     processors = [_get_processor(v) for v in var_list]
    407     if not var_list:
--> 408       raise ValueError("No variables to optimize.")
    409     var_refs = [p.target() for p in processors]
    410     grads = gradients.gradients(

ValueError: No variables to optimize.

注意: 即使我在dense中使用fully_connected而不是contrib,我也会收到同样的错误:

with tf.name_scope('DNN') as scope:
    hidden1 = tf.layers.dense(X, 20, name= 'hidden1', activation= tf.nn.relu )
    hidden2 = tf.layers.dense(hidden1, 10, name= 'hidden2', activation= tf.nn.relu)
    hidden3 = tf.layers.dense(hidden2, 5, name= 'hidden3', activation= tf.nn.relu)
    output = tf.layers.dense(hidden3, 1, activation= None ,name = 'outs')
    logits = tf.nn.sigmoid(output, name= 'logists_out')

with tf.name_scope('loss') as scope:
    #loss = tf.reduce_mean(Y - logits)
    xentropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=Y, logits=logits)
    loss = tf.reduce_mean(xentropy, name="loss")


learning_rate = 0.01
with tf.name_scope('train') as scope:
    optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)
    train_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope= '[12]')
    grads_and_vars = optimizer.compute_gradients(loss, var_list = train_vars)
    clipped_grads = [(tf.clip_by_value(grad,-1,1), var) for grad, var in grads_and_vars]
    train_op = optimizer.apply_gradients(clipped_grads)

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-37-c30495bc394c> in <module>()
     19     optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)
     20     train_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope= '[12]')
---> 21     grads_and_vars = optimizer.compute_gradients(loss, var_list = train_vars)
     22     clipped_grads = [(tf.clip_by_value(grad,-1,1), var) for grad, var in grads_and_vars]
     23     train_op = optimizer.apply_gradients(clipped_grads)

/home/diego/Documents/python27/ML/local/lib/python2.7/site-packages/tensorflow/python/training/optimizer.pyc in compute_gradients(self, loss, var_list, gate_gradients, aggregation_method, colocate_gradients_with_ops, grad_loss)
    406     processors = [_get_processor(v) for v in var_list]
    407     if not var_list:
--> 408       raise ValueError("No variables to optimize.")
    409     var_refs = [p.target() for p in processors]
    410     grads = gradients.gradients(

ValueError: No variables to optimize.

1 个答案:

答案 0 :(得分:4)

如果你写

print train_vars
你会看到,这个集合确实是空的。您可以通过

获取所有变量
print tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)

要获得我建议做的子集

filter_vars = ['hidden1', 'hidden2']
train_vars = []
for var in tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES):
    for filter_var in filter_vars:
        if filter_var in var.name:
            train_vars.append(var)
# print train_vars

train_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope="hidden1")
train_vars += tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope="hidden2")
# print train_vars

两者都给你:

[<tf.Variable 'hidden1/weights:0' shape=(50, 20) dtype=float32_ref>, 
 <tf.Variable 'hidden1/biases:0' shape=(20,) dtype=float32_ref>, 
 <tf.Variable 'hidden2/weights:0' shape=(20, 10) dtype=float32_ref>, 
 <tf.Variable 'hidden2/biases:0' shape=(10,) dtype=float32_ref>]

但我最喜欢的是使用RegEx:

import re
regex_pattern = 'hidden[12]'
train_vars = []
for var in tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES):
    if re.match(regex_pattern, var.op.name):
        train_vars.append(var)
print train_vars