gpt4 book ai didi

python - tf.train.Saver() 和 GPU 的问题 - TensorFlow

转载 作者:行者123 更新时间:2023-11-28 18:06:45 25 4
gpt4 key购买 nike

我的代码结构如下:

with tf.device('/gpu:1'):
...
model = get_model(input_pl)
...
with tf.Session() as sess:
saver = tf.train.Saver()
sess.run(tf.global_variables_initializer())
for epoch in range(num_epochs):
...
for n in range(num_batches):
...
sess.run(...)
# eval epoch
saver.save(sess, ...)

我想在训练阶段后保存模型。当我运行时它给我这个错误:

InvalidArgumentError (see above for traceback): Cannot assign a device for operation 'save/SaveV2': Could not satisfy explicit device specification '/device:GPU:1' because no supported kernel for GPU devices is available.

阅读 this question我以这种方式更改了代码:

saver = tf.train.Saver()
with tf.device('/gpu:1'):
...
model = get_model(pointcloud_pl)
...
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for epoch in range(num_epochs):
...
for n in range(num_batches):
...
sess.run(...)
# eval epoch
saver.save(sess, ...)

但是现在我得到这个错误:

ValueError: No variables to save

我也试过这样做:

with tf.Session() as sess:
saver = tf.train.Saver()
...
with tf.device('/gpu:1'):
sess.run(tf.global_variables_initializer())
for epoch in range(num_epochs):
...
for n in range(num_batches):
...
sess.run()
# eval epoch
saver.save(sess, ...)

我仍然得到同样的错误。错误始终在 saver = tf.train.Saver() 行中。

我该如何解决这个问题?

最佳答案

解决了这个问题:

  1. tf.Session()
  2. 模特
  3. saver = tf.train.Saver()
  4. 使用 tf.device():

这里是示例代码

with tf.Session() as sess:
...
model = get_model(input_pl)
saver = tf.train.Saver()
...
with tf.device('/gpu:1'):
sess.run(tf.global_variables_initializer())
for epoch in range(num_epochs):
...
for n in range(num_batches):
...
sess.run()
# eval epoch
saver.save(sess, ...)

关于python - tf.train.Saver() 和 GPU 的问题 - TensorFlow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53082519/

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