首页 文章

同时恢复和使用多个张量流模型

提问于
浏览
0

我'm quiet new to tensorflow and I struggle to understand how to us it. I'我正在尝试使用它来识别数字,所以我使用了mnist教程(https://www.tensorflow.org/get_started/mnist/pros)中提供的代码,几乎没有修改 . 我使用自己的源代码而不是mnist和我给出的源代码更改了代码的一部分,以便我可以使用不同大小的源创建模型 . (28x28和56x56)我将模型保存如下:

def save_progression(sess, id_collec, x, y_conv, y_, accuracy, keep_prob,  train_step, i, modelDir):
  saver = tf.train.Saver()
  print(modelDir)
  modelNamePrefix=os.path.join(modelDir,  "step%s" % str(i))
  if (os.path.isdir(modelNamePrefix) == False):
    os.makedirs(modelNamePrefix)
  if (len(tf.get_collection(id_collec)) > 0):
    tf.get_collection_ref(id_collec)[0] = x
    tf.get_collection_ref(id_collec)[1] = y_conv
    tf.get_collection_ref(id_collec)[2] = y_
    tf.get_collection_ref(id_collec)[3] = accuracy
    tf.get_collection_ref(id_collec)[4] = keep_prob
    tf.get_collection_ref(id_collec)[5] = train_step
  else:
    tf.add_to_collection(id_collec, x)
    tf.add_to_collection(id_collec, y_conv)
    tf.add_to_collection(id_collec, y_)
    tf.add_to_collection(id_collec, accuracy)
    tf.add_to_collection(id_collec, keep_prob)
    tf.add_to_collection(id_collec, train_step)
  saver.save(sess, os.path.join(modelNamePrefix, "myModel"));

sess beign tf.InteractiveSession()id_collec是'28x28'或'56x56'x是输入想象的占位符y_conv tf.matmul精度的结果bett tf.reduce_mean的结果y_定义了类数的占位符keep_prob是浮点数的占位符train_step = tf.train.AdamOptimizer的结果我只是一个数字,用于更改模型modelDir =的out目录,其中将创建模型目录

然后在另一个程序中我恢复模型如下:

self._sess = tf.Session()
print("import meta graph %s.meta" % (os.path.join(modelDir, modelName)))
saver = tf.train.import_meta_graph("%s.meta" % (os.path.join(modelDir, modelName)))
print("restoring %s" % (os.path.join(modelDir, modelName)))
saver.restore(self._sess, "%s" % (os.path.join(modelDir, modelName)));
self._placeHolder_x, self._predictNode, _, _, self._placeHolder_keep_prob, _ = tf.get_collection('%dx%d' % (dim, dim))

当我加载一个模型时没关系,但是当我加载两个不同的模型(一个基于28x28图像,一个基于56x56图像)时,我在第二个tf.restore上出错了 .

[...]
tensorflow.python.framework.errors_impl.InvalidArgumentError: Assign requires shapes of both tensors to match. lhs shape= [3136,1024] rhs shape= [5,5,64,128]    [[Node: save/Assign_14 = Assign[T=DT_FLOAT,
_class=["loc:@Variable_4"], use_locking=true, validate_shape=true, _device="/job:localhost/replica:0/task:0/cpu:0"](Variable_4/Adam_1, save/RestoreV2_14)]]

Caused by op u'save/Assign_14'
[...]
InvalidArgumentError (see above for traceback): Assign requires shapes of both tensors to match. lhs shape= [3136,1024] rhs shape= [5,5,64,128]      [[Node: save/Assign_14 = Assign[T=DT_FLOAT, _class=["loc:@Variable_4"], use_locking=true, validate_shape=true,
_device="/job:localhost/replica:0/task:0/cpu:0"](Variable_4/Adam_1, save/RestoreV2_14)]]

我做错了什么?显然,这两个模型使用一些变量或其他东西 . 我首先虽然这是因为我使用相同的id为集合,所以我使它不同 . 但错误在于恢复本身甚至不是get集合 . 我听说有某种方式可以让某个人的范围允许避免两个模型共同分享,但我不明白这是怎么回事 .

当我寻求网络答案时,我发现了许多信息,但对于tensorflow是新手,我没有将这些信息应用于我的代码 . 任何的想法 ?

Ps:如果我因为需要它而将这些值放在colelction中,要么在以后继续训练,如果我想要两个,要么启动sess.run .

2 回答

  • 1

    好的,我找到了解决方案,我补充说

    dim = int(sys.argv[5])
    with tf.variable_scope('%dx%d' % (dim, dim)):
    

    在调用我的函数之前定义图表整个图形,我在恢复图形之前添加了相同的行,它运行时没有崩溃

  • 0

    问题可能在于您将两个模型还原到同一个图表中 . 您可能希望为每个模型初始化单独的图形:

    graph1 = tf.Graph()
    graph2 = tf.Graph()
    
    with tf.Session(graph = graph1) as sess1:
        saver.restore(.....)
    with tf.Session(graph = graph2) as sess2:
        saver.restore(...)
    

相关问题