Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tensorflow保存多个会话之一_Python_Tensorflow - Fatal编程技术网

Python Tensorflow保存多个会话之一

Python Tensorflow保存多个会话之一,python,tensorflow,Python,Tensorflow,我有一个Python脚本,其中我实例化了一个神经网络类的两个对象。 每个对象定义自己的会话,并提供保存图形的方法 import tensorflow as tf import os, shutil class TestNetwork: def __init__(self, id): self.id = id tf.reset_default_graph() self.s = tf.placeholder(tf.float32, [N

我有一个Python脚本,其中我实例化了一个神经网络类的两个对象。 每个对象定义自己的会话,并提供保存图形的方法

import tensorflow as tf
import os, shutil

class TestNetwork:

    def __init__(self, id):
        self.id = id

        tf.reset_default_graph()

        self.s = tf.placeholder(tf.float32, [None, 2], name='s')
        w_initializer, b_initializer = tf.random_normal_initializer(0., 1.0), tf.constant_initializer(0.1)
        self.k = tf.layers.dense(self.s, 2, kernel_initializer=w_initializer,
                    bias_initializer=b_initializer, name= 'k')

        '''Defines self.session and initialize the variables'''
        session_conf = tf.ConfigProto(
            allow_soft_placement = True,
            log_device_placement = False)
        self.session = tf.Session(config = session_conf)
        self.session.run(tf.global_variables_initializer())



    def save_model(self, output_dir):
        '''Save the network graph and weights to disk'''
        if os.path.exists(output_dir):
            # if provided output_dir already exists, remove it
            shutil.rmtree(output_dir)

        builder = tf.saved_model.builder.SavedModelBuilder(output_dir)
        builder.add_meta_graph_and_variables(
            self.session,
            [tf.saved_model.tag_constants.SERVING],
            clear_devices=True)
        # create a new directory output_dir and store the saved model in it
        builder.save()


t1 = TestNetwork(1)
t2 = TestNetwork(2)


t1.save_model("t1_model")
t2.save_model("t2_model")
我得到的错误是

TypeError:无法将提要索引键解释为张量:名称 “save/Const:0”引用的张量不存在。手术,, “save/Const”在图形中不存在

import tensorflow as tf
import os, shutil

class TestNetwork:

    def __init__(self, id):
        self.id = id

        tf.reset_default_graph()

        self.s = tf.placeholder(tf.float32, [None, 2], name='s')
        w_initializer, b_initializer = tf.random_normal_initializer(0., 1.0), tf.constant_initializer(0.1)
        self.k = tf.layers.dense(self.s, 2, kernel_initializer=w_initializer,
                    bias_initializer=b_initializer, name= 'k')

        '''Defines self.session and initialize the variables'''
        session_conf = tf.ConfigProto(
            allow_soft_placement = True,
            log_device_placement = False)
        self.session = tf.Session(config = session_conf)
        self.session.run(tf.global_variables_initializer())



    def save_model(self, output_dir):
        '''Save the network graph and weights to disk'''
        if os.path.exists(output_dir):
            # if provided output_dir already exists, remove it
            shutil.rmtree(output_dir)

        builder = tf.saved_model.builder.SavedModelBuilder(output_dir)
        builder.add_meta_graph_and_variables(
            self.session,
            [tf.saved_model.tag_constants.SERVING],
            clear_devices=True)
        # create a new directory output_dir and store the saved model in it
        builder.save()


t1 = TestNetwork(1)
t2 = TestNetwork(2)


t1.save_model("t1_model")
t2.save_model("t2_model")
我读到一些东西说这个错误是由于
tf.train.Saver
引起的

因此,我在
\uuuu init\uuu
方法的末尾添加了以下行:

self.saver = tf.train.Saver(tf.global_variables(), max_to_keep = 5)

但是我仍然得到错误。

tf.reset\u default\u graph
将清除默认图形堆栈并重置全局默认图形

注意:默认图形是当前线程的属性。这 函数仅应用于当前线程。调用此函数 当tf.Session或tf.InteractiveSession处于活动状态时,将导致 未定义的行为使用以前创建的任何tf.操作或 调用此函数后,Tensor对象将导致未定义 行为。

您应该单独指定
图形
,并在相应的图形范围中定义所有这些

def __init__(self, id):
    self.id = id

    self.graph = tf.Graph()
    with self.graph.as_default():
        self.s = tf.placeholder(tf.float32, [None, 2], name='s')
        w_initializer, b_initializer = tf.random_normal_initializer(0., 1.0), tf.constant_initializer(0.1)
        self.k = tf.layers.dense(self.s, 2, kernel_initializer=w_initializer,
                    bias_initializer=b_initializer, name= 'k')
        init = tf.global_variables_initializer()

    '''Defines self.session and initialize the variables'''
    session_conf = tf.ConfigProto(
        allow_soft_placement = True,
        log_device_placement = False)
    self.session = tf.Session(config = session_conf,graph=self.graph)
    self.session.run(init)
tf.train.Saver
是另一种保存模型变量的方法

编辑 若得到空“变量”,则应将模型保存在图形中:

def save_model(self, output_dir):
    '''Save the network graph and weights to disk'''
    if os.path.exists(output_dir):
        # if provided output_dir already exists, remove it
        shutil.rmtree(output_dir)

    with self.graph.as_default():
        builder = tf.saved_model.builder.SavedModelBuilder(output_dir)
        builder.add_meta_graph_and_variables(
            self.session,
            [tf.saved_model.tag_constants.SERVING],
            clear_devices=True)
        # create a new directory output_dir and store the saved model in it
        builder.save()

tf.reset\u default\u graph
将清除默认图堆栈并重置全局默认图

注意:默认图形是当前线程的属性。这 函数仅应用于当前线程。调用此函数 当tf.Session或tf.InteractiveSession处于活动状态时,将导致 未定义的行为使用以前创建的任何tf.操作或 调用此函数后,Tensor对象将导致未定义 行为。

您应该单独指定
图形
,并在相应的图形范围中定义所有这些

def __init__(self, id):
    self.id = id

    self.graph = tf.Graph()
    with self.graph.as_default():
        self.s = tf.placeholder(tf.float32, [None, 2], name='s')
        w_initializer, b_initializer = tf.random_normal_initializer(0., 1.0), tf.constant_initializer(0.1)
        self.k = tf.layers.dense(self.s, 2, kernel_initializer=w_initializer,
                    bias_initializer=b_initializer, name= 'k')
        init = tf.global_variables_initializer()

    '''Defines self.session and initialize the variables'''
    session_conf = tf.ConfigProto(
        allow_soft_placement = True,
        log_device_placement = False)
    self.session = tf.Session(config = session_conf,graph=self.graph)
    self.session.run(init)
tf.train.Saver
是另一种保存模型变量的方法

编辑 若得到空“变量”,则应将模型保存在图形中:

def save_model(self, output_dir):
    '''Save the network graph and weights to disk'''
    if os.path.exists(output_dir):
        # if provided output_dir already exists, remove it
        shutil.rmtree(output_dir)

    with self.graph.as_default():
        builder = tf.saved_model.builder.SavedModelBuilder(output_dir)
        builder.add_meta_graph_and_variables(
            self.session,
            [tf.saved_model.tag_constants.SERVING],
            clear_devices=True)
        # create a new directory output_dir and store the saved model in it
        builder.save()

谢谢,通过修复它可以工作,但是保存的模型目录包含一个空的“variable”子目录,因此我得到一个错误:传递的save_路径不是有效的检查点。谢谢,通过修复它可以工作,但是保存的模型目录包含一个空的“variable”子目录,因此我得到一个错误:传递的save_路径不是有效的检查点