如何将变量添加到tf.trainable_variables?

时间:2017-05-30 21:22:40

标签: python tensorflow

我读了这篇关于如何用TensorFlow制作快速神经网络的tutorial,并且效果很好。

但是,我想更多地了解它是如何运作的。

在代码中,我们使用:

定义神经网络
def neural_network_model(data):
    hidden_1_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl0, n_nodes_hl1])),
                      'biases':tf.Variable(tf.random_normal([n_nodes_hl1]))}

    hidden_2_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2])),
                      'biases':tf.Variable(tf.random_normal([n_nodes_hl2]))}

    hidden_3_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl2, n_nodes_hl3])),
                      'biases':tf.Variable(tf.random_normal([n_nodes_hl3]))}

    output_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl3, n_classes])),
                    'biases':tf.Variable(tf.random_normal([n_classes]))}

    l1 = tf.add(tf.matmul(data,hidden_1_layer['weights']), hidden_1_layer['biases'])
    l1 = tf.nn.relu(l1)

    l2 = tf.add(tf.matmul(l1,hidden_2_layer['weights']), hidden_2_layer['biases'])
    l2 = tf.nn.relu(l2)

    l3 = tf.add(tf.matmul(l2,hidden_3_layer['weights']), hidden_3_layer['biases'])
    l3 = tf.nn.relu(l3)

    output = tf.matmul(l3,output_layer['weights']) + output_layer['biases']

    return output

然后最终运行

cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y) )
optimizer = tf.train.AdamOptimizer().minimize(cost)

我想弄清楚AdamOptimizer如何知道要改变的基质,因为它们都没有传递给最小化函数。

所以,我查了AdamOptimizer,发现minimize有一个可选参数:

var_list: Optional list or tuple of Variable objects to update to minimize loss. Defaults to the list of variables collected in the graph under the key GraphKeys.TRAINABLE_VARIABLES.

然后我查了GraphKeys.TRAINABLE_VARIABLES并找到了:

When passed trainable=True, the Variable() constructor automatically adds new variables to the graph collection GraphKeys.TRAINABLE_VARIABLES. This convenience function returns the contents of that collection.

然后我在我的代码中搜索了术语trainable,却一无所获。

那么AdamOptimizer在世界上如何知道它应该改变什么来优化?

1 个答案:

答案 0 :(得分:1)

trainable参数传递给Variable构造函数,隐式默认为true。在代码中将其设置为false,以避免对某些变量进行培训。

相关问题