gpt4 book ai didi

machine-learning - 使用 tf.train.save 时无法恢复 Adam Optimizer 的变量

转载 作者:行者123 更新时间:2023-11-30 08:37:51 24 4
gpt4 key购买 nike

当我尝试恢复 tensorflow 中保存的模型时,出现以下错误:

 W tensorflow/core/framework/op_kernel.cc:1192] Not found: Key out_w/Adam_5 not found in checkpoint
W tensorflow/core/framework/op_kernel.cc:1192] Not found: Key b1/Adam not found in checkpoint
W tensorflow/core/framework/op_kernel.cc:1192] Not found: Key b1/Adam_4 not found in checkpoint

我想我无法保存 Adam Optimizer 的变量。有修复吗?

最佳答案

考虑这个小实验:

import tensorflow as tf

def simple_model(X):
with tf.variable_scope('Layer1'):
w1 = tf.get_variable('w1', initializer=tf.truncated_normal((5, 2)))
b1 = tf.get_variable('b1', initializer=tf.ones((2)))
layer1 = tf.matmul(X, w1) + b1
return layer1

def simple_model2(X):
with tf.variable_scope('Layer1'):
w1 = tf.get_variable('w1_x', initializer=tf.truncated_normal((5, 2)))
b1 = tf.get_variable('b1_x', initializer=tf.ones((2)))
layer1 = tf.matmul(X, w1) + b1
return layer1

tf.reset_default_graph()
X = tf.placeholder(tf.float32, shape = (None, 5))
model = simple_model(X)
saver = tf.train.Saver()

with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
saver.save(sess, './Checkpoint', global_step = 0)

tf.reset_default_graph()
X = tf.placeholder(tf.float32, shape = (None, 5))
model = simple_model(X) # Case 1
#model = simple_model2(X) # Case 2
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
tf.train.Saver().restore(sess, tf.train.latest_checkpoint('.'))

在情况 1 中,一切正常。但在 Case2 中,您会得到类似 Key Layer1/b1_x not found in checkpoint 的错误,这是因为模型中的变量名称不同(尽管两个变量的形状和数据类型相同)。确保变量在您要恢复的模型中具有相同的名称。

要检查检查点中存在的变量名称,请检查此 answer .

关于machine-learning - 使用 tf.train.save 时无法恢复 Adam Optimizer 的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47432784/

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