当前位置:  开发笔记 > 编程语言 > 正文

Jupyter上的TensorFlow:无法恢复变量

如何解决《Jupyter上的TensorFlow:无法恢复变量》经验,为你挑选了1个好方法。

在Jupyter笔记本中使用TensorFlow时,我似乎无法恢复已保存的变量.我训练一个ANN,然后我跑,saver.save(sess, "params1.ckpt")然后我再次训练它,保存新的结果,saver.save(sess, "params2.ckpt")但是当我运行saver.restore(sess, "params1.ckpt")我的模型时不加载保存的值params1.ckpt并保留它们params2.ckpt.

如果我运行模型,保存它params.ckpt,然后关闭并停止,然后尝试再次加载它,我收到以下错误:

---------------------------------------------------------------------------
StatusNotOK                               Traceback (most recent call last)
StatusNotOK: Not found: Tensor name "Variable/Adam" not found in checkpoint files params.ckpt
     [[Node: save/restore_slice_1 = RestoreSlice[dt=DT_FLOAT, preferred_shard=-1, _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_save/Const_0, save/restore_slice_1/tensor_name, save/restore_slice_1/shape_and_slice)]]

During handling of the above exception, another exception occurred:

SystemError                               Traceback (most recent call last)
 in ()
----> 1 saver.restore(sess, "params.ckpt")

/usr/local/lib/python3.5/site-packages/tensorflow/python/training/saver.py in restore(self, sess, save_path)
    889       save_path: Path where parameters were previously saved.
    890     """
--> 891     sess.run([self._restore_op_name], {self._filename_tensor_name: save_path})
    892 
    893 

/usr/local/lib/python3.5/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict)
    366 
    367     # Run request and get response.
--> 368     results = self._do_run(target_list, unique_fetch_targets, feed_dict_string)
    369 
    370     # User may have fetched the same tensor multiple times, but we

/usr/local/lib/python3.5/site-packages/tensorflow/python/client/session.py in _do_run(self, target_list, fetch_list, feed_dict)
    426 
    427       return tf_session.TF_Run(self._session, feed_dict, fetch_list,
--> 428                                target_list)
    429 
    430     except tf_session.StatusNotOK as e:

SystemError:  returned a result with an error set

我的培训代码是:

def weight_variable(shape, name):
  initial = tf.truncated_normal(shape, stddev=1.0, name=name)
  return tf.Variable(initial)

def bias_variable(shape, name):
  initial = tf.constant(1.0, shape=shape)
  return tf.Variable(initial, name=name)

input_file = pd.read_csv('P2R0PC0.csv') 
features = #vector with 5 feature names
targets = #vector with 4 feature names
x_data = input_file.as_matrix(features)
t_data = input_file.as_matrix(targets)

x = tf.placeholder(tf.float32, [None, x_data.shape[1]])

hiddenDim = 5

b1 = bias_variable([hiddenDim], name = "b1")
W1 = weight_variable([x_data.shape[1], hiddenDim], name = "W1")

b2 = bias_variable([t_data.shape[1]], name = "b2")
W2 = weight_variable([hiddenDim, t_data.shape[1]], name = "W2")

hidden = tf.nn.sigmoid(tf.matmul(x, W1) + b1)
y = tf.nn.sigmoid(tf.matmul(hidden, W2) + b2)
t = tf.placeholder(tf.float32, [None, t_data.shape[1]])

lambda1 = 1
beta1 = 1
lambda2 = 1
beta2 = 1
error = -tf.reduce_sum(t * tf.log(tf.clip_by_value(y,1e-10,1.0)) + (1 - t) * tf.log(tf.clip_by_value(1 - y,1e-10,1.0)))
complexity = lambda1 * tf.nn.l2_loss(W1) + beta1 * tf.nn.l2_loss(b1) + lambda2 * tf.nn.l2_loss(W2) + beta2 * tf.nn.l2_loss(b2)
loss = error + complexity

train_step = tf.train.AdamOptimizer(0.001).minimize(loss)
sess = tf.Session()

init = tf.initialize_all_variables()
sess.run(init)

ran = 25001
delta = 250

plot_data = np.zeros(int(ran / delta + 1))
k = 0;
for i in range(ran):
    train_step.run({x: data, t: labels}, sess)
    if i % delta == 0:
        plot_data[k] = loss.eval({x: data, t: labels}, sess)
        #plot_training[k] = loss.eval({x: x_test, t: t_test}, sess)
        print(str(plot_data[k]))
        k = k + 1

plt.plot(np.arange(start=2, stop=int(ran / delta + 1)), plot_data[2:])

saver = tf.train.Saver()
saver.save(sess, "params.ckpt")

error.eval({x:data, t: labels}, session=sess)

我做错了吗?为什么我不能恢复我的变量?



1> mrry..:

看起来您正在使用Jupyter来构建模型.tf.Saver使用默认参数构造a时,一个可能的问题是它将使用变量的(自动生成的)名称作为检查点中的键.由于在Jupyter中很容易多次重新执行代码单元,因此您可能最终会在保存的会话中使用变量节点的多个副本.请参阅我对此问题的回答,以解释可能出现的问题.

有一些可能的解决方案.这是最简单的:

tf.reset_default_graph()在构建模型之前调用(和Saver).这将确保变量获得您想要的名称,但它将使先前创建的图形无效.

使用显式参数tf.train.Saver()指定变量的持久名称.对于你的例子,这不应该太难(虽然它对于较大的模型变得笨重):

saver = tf.train.Saver(var_list={"b1": b1, "W1": W1, "b2": b2, "W2": W2})

tf.Graph()每次创建模型时,创建一个新的并使其成为默认值.这在Jupyter中可能很棘手,因为它迫使您将所有模型构建代码放在一个单元格中,但它适用于脚本:

with tf.Graph().as_default():
  # Model building and training/evaluation code goes here.

推荐阅读
凹凸曼00威威_694
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有