gpt4 book ai didi

tensorflow - 在 Tensorflow 中,如果元图使用 TFRecord 输入(没有占位符),如何使用恢复的元图

转载 作者:行者123 更新时间:2023-12-03 08:24:21 25 4
gpt4 key购买 nike

我用 TFRecord 输入管道训练了一个网络。换句话说,没有占位符。简单的例子是:

input, truth = _get_next_batch()  # TFRecord. `input` is not a tf.placeholder
net = Model(input)
net.set_loss(truth)
optimizer = tf...(net.loss)

比方说,我获得了三个文件, ckpt-20000.meta , ckpt-20000.data-0000-of-0001 , ckpt-20000.index .我知道,以后可以使用 .meta 导入元图。文件和访问张量,例如:
new_saver = tf.train.import_meta_graph('ckpt-20000.meta')
new_saver.restore(sess, 'ckpt-20000')
logits = tf.get_collection("logits")[0]

但是,元图在管道中从一开始就没有占位符。有没有办法可以使用元图和查询推理输入?

有关信息,在查询应用程序(或脚本)中,我曾经使用占位符和恢复的模型权重定义模型(见下文)。我想知道我是否可以在不重新定义的情况下使用元图,因为它会简单得多。
input = tf.placeholder(...)
net = Model(input)
tf.restore(sess, 'ckpt-2000')
lgt = sess.run(net.logits, feed_dict = {input:img})

最佳答案

您可以构建一个使用 placeholder_with_default() 的图表。对于输入,因此可以同时使用 TFRecord input pipeline以及 feed_dict{} .

一个例子:

input, truth = _get_next_batch()
_x = tf.placeholder_with_default(input, shape=[...], name='input')
_y = tf.placeholder_with_default(truth, shape-[...], name='label')

net = Model(_x)
net.set_loss(_y)
optimizer = tf...(net.loss)

然后在推理过程中,
loaded_graph = tf.Graph()
with tf.Session(graph=loaded_graph) as sess:
new_saver = tf.train.import_meta_graph('ckpt-20000.meta')
new_saver.restore(sess, 'ckpt-20000')

# Get the tensors by their variable name
input = loaded_graph.get_tensor_by_name('input:0')
logits = loaded_graph.get_tensor_by_name(...)

# Now you can feed the inputs to your tensors
lgt = sess.run(logits, feed_dict = {input:img})

在上面的例子中,如果你不输入输入,那么输入将从 TFRecord input pipeline 中读取。 .

关于tensorflow - 在 Tensorflow 中,如果元图使用 TFRecord 输入(没有占位符),如何使用恢复的元图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44769126/

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