如何在不初始化变量的情况下保存张量流图? [英] How to save a tensorflow graph without initialise a variable?

查看:36
本文介绍了如何在不初始化变量的情况下保存张量流图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的问题可以反映如下:

The problem I encountered can be reflected as follow:

tf.reset_default_graph()

x = tf.placeholder(dtype=tf.int32, shape=())
init = tf.zeros(shape=tf.squeeze(x), dtype=tf.float32)

v = tf.get_variable('foo', initializer=init, validate_shape=False)


v_sig = tf.saved_model.signature_def_utils.build_signature_def(
            inputs={"x_input": tf.saved_model.utils.build_tensor_info(x)},
            outputs={
                'v_output': tf.saved_model.utils.build_tensor_info(v)
            },
            method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME
)

with tf.Session() as sess:
    builder = tf.saved_model.builder.SavedModelBuilder(export_dir="~/test/")
    sess.run(tf.global_variables_initializer())  # here leads to problem
    builder.add_meta_graph_and_variables(
        sess, [tf.saved_model.tag_constants.SERVING],
        signature_def_map={
            'v_sig': v_sig
        },
        main_op=tf.tables_initializer(),
        strip_default_attrs=True
    )
    builder.save()

我有一个变量 foo,它的形状是动态计算的(取决于占位符 x 的输入).当我尝试将其保存为图形时,我遇到了错误:

I have a variable foo whose shape is calculated on the fly(depending on the input of placeholder x). When I tried to save it as a graph, I met error:

您必须使用 dtype int32 为占位符张量Placeholder"提供一个值

You must feed a value for placeholder tensor 'Placeholder' with dtype int32

如果我不运行global_variables_initializer,它会报错variable does not exist.

If I don't run global_variables_initializer, it would has error variable does not exists.

那么如何解决这种情况呢?我已经被困在这个问题上很长一段时间了,感谢您的回答.

So how to resolve the situation? I have been stuck on this for quite some time, appreciate for an answer.

推荐答案

您可以将图形保存为元图形对象,而无需像这样初始化变量:

You could save your graph as meta graph object without initializing the variables like this:

import tensorflow as tf
import json

x = tf.placeholder(dtype=tf.int32, shape=(), name='x')
init = tf.zeros(shape=tf.squeeze(x), dtype=tf.float32, name='init')
v = tf.get_variable('foo', initializer=init, validate_shape=False)
tensor_names = {
    'x': x.name,
    'v': v.name
}
with open('tensor_names.json', 'w') as fo:
  json.dump(tensor_names, fo)

fname = 'graph.meta'
proto = tf.train.export_meta_graph(filename=fname,
                                   graph=tf.get_default_graph())

然后恢复此图:

import tensorflow as tf
import json

with open('tensor_names.json', 'r') as fo:
  tensor_names = json.load(fo)

graph = tf.Graph()
with graph.as_default():
  tf.train.import_meta_graph(fname)
  x = graph.get_tensor_by_name(tensor_names['x'])
  v = graph.get_tensor_by_name(tensor_names['v'])

# works as expected: 
with tf.Session(graph=graph) as sess:
  sess.run(tf.global_variables_initializer(), {x:5})
  print(v.eval()) # [0. 0. 0. 0. 0.]

这篇关于如何在不初始化变量的情况下保存张量流图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆