tensorflow 如何在2.x 版本 与 1.x 版本间切换

 

tensorflow 2.0.0 运行 1.0.0的代码:

 (只需在代码前面加上如下代码)

   import tensorflow.compat.v1 as tf

   tf.disable_v2_behavior()

 

将v1版升级为v2版工具:

       1.升级单个文件:

tf_upgrade_v2 --infile "1.x的代码文件"  --outfile "2.x的代码文件"

       2.升级所有文件:

tf_upgrade_v2 --intree foo/ --outtree foo-upgraded/
  • 例: 将下列v1代码升级为v2

文件v1.py:

import tensorflow as tf

foo = tf.placeholder(tf.int32,shape=[1], name='foo')
bar = tf.constant(2,name='bar')
result = foo + bar
with tf.Session() as sess:
  print(sess.run(result,feed_dict={foo:[3]}))

运行tf_upgrade_v2 --infile v1.py --outfile v2.py 得到v2.py文件:

import tensorflow as tf
foo = tf.compat.v1.placeholder(tf.int32,shape=[1], name='foo')
bar = tf.constant(2,name='bar')
result = foo + bar
with tf.compat.v1.Session() as sess:
  print(sess.run(result,feed_dict={foo:[3]}))

你可能感兴趣的:(TensorFlow)