gpt4 book ai didi

python - tensorflow :在两个单独的图中初始化的两个模型,但它们仅在最后一张图中看到

转载 作者:行者123 更新时间:2023-12-01 07:15:11 25 4
gpt4 key购买 nike

import tensorflow as tf
import numpy as np

class SimpleModel():
pass

def declare_placeholders(self):

self.input_batch = tf.placeholder(dtype=tf.int32, shape=[None, None], name='input_batch')

SimpleModel.__declare_placeholders = classmethod(declare_placeholders)

def init_model(self):
self.__declare_placeholders()

SimpleModel.__init__ = classmethod(init_model)

g_1 = tf.Graph()
with g_1.as_default():
model1 = SimpleModel()

g_2 = tf.Graph()
with g_2.as_default():
model2 = SimpleModel()

我不希望在下一种情况下出现断言错误:

assert model1.input_batch.graph is g_1

---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-5-1ffc6a2e45a2> in <module>
----> 1 assert model1.input_batch.graph is g_1

AssertionError:

但我看到接下来:

assert model1.input_batch.graph is g_2 (????)

但是我已经在 g_1 中初始化了 model1!

如何修改SimpleModel来解决这个问题?

最佳答案

我做了类似的事情来在 2 个单独的图表中加载 2 个不同的模型:

这里的 PATH_TO_MODEL_CKPT 是保存的模型文件的路径,或者您可以在图中构建模型。

import tensorflow as tf
from tensorflow.python.platform import gfile
import os

class Graph1(object):
def __init__(self, PATH_TO_MODEL_CKPT):
graph1 = tf.Graph()
with self.graph1.as_default():
model_exp = os.path.expanduser(PATH_TO_CKPT)
print('Model filename: %s' % model_exp)
od_graph_def = tf.GraphDef()
with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='')

sess1 = tf.Session(graph=graph1)

class Graph2(object):
def __init__(self, PATH_TO_MODEL_CKPT):
graph2 = tf.Graph()
with self.graph2.as_default():
model_exp = os.path.expanduser(PATH_TO_CKPT)
print('Model filename: %s' % model_exp)
od_graph_def = tf.GraphDef()
with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='')
sess2 = tf.Session(graph=graph2)

关于python - tensorflow :在两个单独的图中初始化的两个模型,但它们仅在最后一张图中看到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58012270/

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