2

我们总是使用tf.train.Saver()来保存和恢复权重,就像在这个例子中一样。

但是如何使用 Eager Execution 来保存呢?如何更改以下示例?

另一个问题,使用 Eager 是个好主意吗?

我在tf.contrib.eager.Saver 这里找到了,但它说,

“Saver 的基于名称的检查点策略很脆弱”。

这是什么意思?

# Create some variables.
v1 = tf.get_variable("v1", shape=[3], initializer = tf.zeros_initializer)
v2 = tf.get_variable("v2", shape=[5], initializer = tf.zeros_initializer)

inc_v1 = v1.assign(v1+1)
dec_v2 = v2.assign(v2-1)

# Add an op to initialize the variables.
init_op = tf.global_variables_initializer()

# Add ops to save and restore all the variables.
saver = tf.train.Saver()

# Later, launch the model, initialize the variables, do some work, and save the
# variables to disk.
with tf.Session() as sess:
  sess.run(init_op)
  # Do some work with the model.
  inc_v1.op.run()
  dec_v2.op.run()
  # Save the variables to disk.
  save_path = saver.save(sess, "/tmp/model.ckpt")
  print("Model saved in path: %s" % save_path)
4

0 回答 0