gpt4 book ai didi

python - Tensorflow:使用 Adam 优化器

转载 作者:IT老高 更新时间:2023-10-28 20:34:07 30 4
gpt4 key购买 nike

我正在 tensorflow 中试验一些简单的模型,包括一个看起来与第一个 MNIST for ML Beginners example 非常相似的模型。 ,但具有更大的维度。我能够毫无问题地使用梯度下降优化器,获得足够好的收敛性。当我尝试使用 ADAM 优化器时,我收到如下错误:

tensorflow.python.framework.errors.FailedPreconditionError: Attempting to use uninitialized value Variable_21/Adam
[[Node: Adam_2/update_Variable_21/ApplyAdam = ApplyAdam[T=DT_FLOAT, use_locking=false, _device="/job:localhost/replica:0/task:0/cpu:0"](Variable_21, Variable_21/Adam, Variable_21/Adam_1, beta1_power_2, beta2_power_2, Adam_2/learning_rate, Adam_2/beta1, Adam_2/beta2, Adam_2/epsilon, gradients_11/add_10_grad/tuple/control_dependency_1)]]

提示未初始化的特定变量根据运行而变化。这个错误是什么意思?它表明什么是错误的?无论我使用什么学习率,它似乎都会发生。

最佳答案

AdamOptimizer 类创建额外的变量,称为“槽”,用于保存“m”和“v”累加器的值。

如果您好奇,请参阅此处的源代码,它实际上非常易读: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/training/adam.py#L39 .其他优化器,例如 Momentum 和 Adagrad 也使用插槽。

必须先初始化这些变量,然后才能训练模型。

初始化变量的常规方法是调用 tf.initialize_all_variables(),它会在调用时添加操作以初始化图中存在的变量

(旁白:不像它的名字暗示的那样,initialize_all_variables() 不会初始化任何东西,它只会添加在运行时初始化变量的操作。)

你必须做的是调用initialize_all_variables()你已经添加了优化器:

...build your model...
# Add the optimizer
train_op = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
# Add the ops to initialize variables. These will include
# the optimizer slots added by AdamOptimizer().
init_op = tf.initialize_all_variables()

# launch the graph in a session
sess = tf.Session()
# Actually intialize the variables
sess.run(init_op)
# now train your model
for ...:
sess.run(train_op)

关于python - Tensorflow:使用 Adam 优化器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33788989/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com