我对张力流很陌生,我很难理解如何使用它。我目前正在尝试使用它来识别数字,所以我使用了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 the tf.InteractiveSession() id_collec是' 28x28'或者' 56x56' x是输入图像的占位符 y_conv是tf.matmul的结果 精确度取决于tf.reduce_mean的结果 y_定义了类数的占位符 keep_prob是浮点数的占位符 train_step = tf.train.AdamOptimizer的结果 我只是一个更改模型输出目录的数字 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为集合,所以我使它不同。但错误在于恢复本身甚至不是get集合。 我听说有某种方式可以让某个人的范围允许避免这两个模型一起分享,但我不知道这是如何工作的。
当我寻求网络答案时,我发现了许多信息,但对于tensorflow是新手,我未能将这些信息应用到我的代码中。 任何想法?
Ps:如果因为我需要它们,我会将这些值放在合作中,如果我想要两个,可以在以后继续训练,或者启动sess.run。
答案 0 :(得分:1)
好的,我找到了解决方案,我补充说
dim = int(sys.argv[5])
with tf.variable_scope('%dx%d' % (dim, dim)):
在调用我的函数之前定义了整个图形图,我在恢复图形之前添加了相同的行,它运行时没有崩溃
答案 1 :(得分:0)
问题可能在于您将两个模型还原到同一个图表中。您可能希望为每个模型初始化单独的图形:
List<WebElement> ajaxValues1 = ajaxHolder1.findElements(By.tagName("li"));
Select dropdown= new Select(ajaxValues1);
dropdown.selectByIndex(0);
dropdown.selectByIndex(1);
dropdown.selectByIndex(2);