当前位置 博文首页 > TensorFlow低版本代码自动升级为1.0版本

    TensorFlow低版本代码自动升级为1.0版本

    作者:adrianna_xy 时间:2021-07-25 17:42

    Reference:
    https://www.tensorflow.org/install/migration

    tensorflow 更新到1.0之后,0.n版本不兼容,除了手动更改代码之外,tensorflow官方还提供了自动更新的脚本。

    下载链接:https://github.com/tensorflow/tensorflow/tree/master/tensorflow/tools/compatibility

    使用方法:

    更新一个文件:

    原本代码为foo.py, 使用tf_upgrade.py自动升级为1.0版本,新的文件名为foo-upgraded.py:

    tf_upgrade.py --infile foo.py --outfile foo-upgraded.py

    目录下的所有文件都更新:

     tf_upgrade.py --intree InputDir --outtree OutputDir
    

    目录下的所有文件都更新,并复制除了python文件之外的其他文件到新文件夹:

    运行之后所有.py文件都会更新并放在OutputDir目录下,如果想要目录中的其他文件(.txt等)也复制到新的文件夹,可以设置

    copyotherfiles为True:
    tf_upgrade.py --intree InputDir --outtree OutputDir --copyotherfiles True
    

    更新完毕后脚本会自动生成一个log文件,其中包含了更新的内容。

    third_party/tensorflow/tools/compatibility/test_file_v0.11.py Line 125
    
    Renamed keyword argument from `dim` to `axis`
    Renamed keyword argument from `squeeze_dims` to `axis`
    
      Old:          [[1, 2, 3]], dim=1), squeeze_dims=[1]).eval(),
                        ~~~~  ~~~~~~~~~~~~~
      New:          [[1, 2, 3]], axis=1), axis=[1]).eval(),
                        ~~~~~  ~~~~~

    拓展阅读

    tf_upgrade.py 有一些局限性:

    • 它不能改变 tf.reverse() 的参数,因此必须手动修复。
    • 对于参数列表重新排序的方法,如 tf.split() 和 tf.reverse_split(),它会尝试引入关键字参数,但实际上并不能重新排列参数。

    有些结构必须手动替换,例如:

    tf.get.variable_scope().reuse_variables() 
    
    

    替换为:

    with tf.variable_scope(tf.get.variable_scope(),reuse=True):
    
    jsjbwy
    下一篇:没有了