我有一个旧的python脚本(tf-1.15.2),需要在TensorFlow-2.2.0中运行(不能使用tf <2.2),我已将大部分代码迁移到tf-2.2.0,但是代码中使用了一些与tensorflow.contrib相关的方法。因此,我想使用旧版本tf-1.15来运行那些使用tensorflow.contrib相关API的代码行。
因此,现在的问题是我已经在全球范围内安装了tf-1.15.2 ,已经在本地地安装了tf-2.2.0 。但是如何在python进程运行时的特定时间点访问TensorFlow的特定版本呢?
示例代码如下
import tensorflow as tf # version: tf-2.2.0 (local package is imported)
isess = tf.compat.v1.Session()
tf.compat.v1.disable_eager_execution()
# Creatoin of the required placeholders
p = []
for shape in input_shapes:
p.append(tf.compat.v1.placeholder(shape=shape, dtype=input_dtype))
out = tf.einsum(equation, *p)
graph_def = isess.graph_def
# TODO
# To feed this (graph_def, feed_dict, output_tensors) to a session object of tf-1.15.2 and find the output
现在在用适当的函数(trace / dot_product / ...)替换einsum之后,在tf_1.15.2中测试tf_einsum_op_test中给出的单元测试,我想回到tf-1.15.2并检查执行情况。
基本需求是查找tf版本是否可以在python进程的执行流程期间互换。考虑使用Einsum op,因为tf-1.15.2中未直接支持它。
答案 0 :(得分:0)
在尝试使用子流程API时,我发现可以通过子流程调用在python流程执行期间在tf版本之间进行切换。
# main.py
import tensorflow as tf # version: tf-2.2.0 (local package is imported)
import subprocess
import os
isess = tf.compat.v1.Session()
tf.compat.v1.disable_eager_execution()
# Creatoin of the required placeholders
p = []
for shape in input_shapes:
p.append(tf.compat.v1.placeholder(shape=shape, dtype=input_dtype))
out = tf.einsum(equation, *p)
graph_def = isess.graph_def
#TODO: Save the graph_def in graph.pb
#TODO: Save the feed_dict in input.npz
#TODO: Save the output_tensors
#Change the python path to the global package
os.environ['PYTHONPATH'] = '/usr/local/lib/python3.6/dist-packages'
cmd = ['python3.6','run.py']
out = subprocess.check_output(cmd) #Subprocess call
#run.py
import tensorflow as tf # version: tf-1.15.2 (global package is imported)
import numpy as np
#TODO: Load the graphdef from graph.pb
#TODO: Load the feed_dict from input.npz
#TODO: Load the output tensors
g = tf.import_graph_def(graph_def,name='')
with tf.Session(graph=g) as sess:
output = sess.run(output_tensors,feed_dict)
这对我有用。