我对 tensorflow 还很陌生,我很难理解如何使用它。我目前正在尝试使用它来识别数字,因此我使用了 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 成为 tf.InteractiveSession() id_collec 是“28x28”或“56x56” x 是输入图像的占位符 y_conv tf.matmul 的结果 准确率取决于 tf.reduce_mean 的结果 y_ 定义类编号的占位符 keep_prob float 的占位符 train_step = tf.train.AdamOptimizer 的结果 i 只是一个用于更改模型输出目录的数字 modelDir = 将在其中创建模型目录
然后在另一个程序中我恢复模型如下:
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,所以我将其设置为不同的。但错误在于恢复本身,甚至不是获取集合。 我听说有一种方法可以以某种方式建立某种范围,从而避免两个模型一起共享内容,但我不明白它是如何工作的。
当我在网上寻找答案时,我发现了很多信息,但作为 tensorflow 新手,我未能将这些信息应用到我的代码中。 有什么想法吗?
Ps:如果我将这些值放入集合中,如果因为我需要它们,要么在以后继续训练(如果我想要两个),要么启动 sess.run。
最佳答案
好的,我找到了解决方案,我添加了
dim = int(sys.argv[5])
with tf.variable_scope('%dx%d' % (dim, dim)):
在调用定义图形的函数之前,我在恢复图形之前添加了相同的行,并且它运行时没有崩溃
关于python - 同时恢复和使用多个 tensorflow 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42789125/