tensorflow1迁移到tensorflow2

早期的tf1代码在3090上跑不了了,因此复现基于tensorflow1的代码只能
1.创建一个还在维护的tf1.x环境
2.将tf1.x代码迁移到tf2上。

第一种方法:

conda create -n tf115 python==3.6.9
pip install nvidia-pyindex==1.0.5
pip install nvidia-tensorflow==1.15.4+nv20.10
pip install torch==1.7.0+cu110 torchvision==0.8.1+cu110 torchaudio===0.7.0 -f https://download.pytorch.org/whl/torch_stable.html
conda activate tf115

这种方法可能会遇到代码中有的包现在只支持tf2.x,只能找替代或者自己实现,有点麻烦。

第二种方法
ref:https://www.tensorflow.org/guide/migrate

将tf1.x迁移到tf2.x的过程中,主要有两大类改动:

  1. tf.contrib->?
  2. tf ->tf.compat.v1

第一类tf.contrib,这个接口在tf2中已经被取消了,里面的包要么被删除,要么被移动到其他包里,要么被归到了tf核心里。
第二类tf.compat.v1 允许tf2调用tf1的包

这里列出我碰到过的需要转移的包和解决方法以备后续需要
ref:https://github.com/tensorflow/community/blob/master/rfcs/20180907-contrib-sunset.md

package name in tf1 solution in tf2
from tensorflow.contrib import layers from tf_slim import layers
from tensorflow.contrib.framework.python.ops import arg_scope from tf_slim import arg_scope
from tensorflow.contrib.layers.python.layers import layers as layers_lib from tf_slim.layers import layers as layers_lib
from tensorflow.contrib.layers.python.layers import utils from tf_slim.layers import utils
tf.train.get_or_create_global_step() tf.compat.v1.train.get_or_create_global_step()
tf.variable_scope() tf.compat.v1.variable_scope()
tf.get_variable() tf.compat.v1.get_variable()
tf.layers() tf.compat.v1.layers()
tf.logging.debug() tf.compat.v1.logging.debug()
tf.compat.v1.uniform_unit_scaling_initializer() tf.compat.v1.uniform_unit_scaling_initializer()
tf.nn.xw_plus_b tf.compat.v1.nn.xw_plus_b()
tf.summary.merge_all() tf.compat.v1.summary.merge_all()
tf.contrib.distributions tf.compat.v1.distributions

从报错来看:
AttributeError: module ‘tensorflow’ has no attribute ‘xxx’一般的解决方法是把tf换成tf.compar.v1
ModuleNotFoundError: No module named ‘tensorflow.contrib’ ,就要查一下tf.contrib底下的包给迁移到哪里了(一般是tf_slim)

你可能感兴趣的:(debug,tensorflow,深度学习,python)