我用的是tensorflow 2.6
一般来说在tf 2.x中都可以使用tf.compat.v1.xxxxxx来替代原来tf 1.x中的tf.xxxxxx
如果报错,说函数没有,其实可以直接去github的tensorflow项目里搜索这个函数,看tensorflow里是怎么用的
这里列一列遇到的
tf 1.x:tf.contrib.layers.xavier_initializer
tf 2.6:tf.keras.initializers.glorot_normal
tf 1.x:tf.contrib.layers
tf 2.6:tf.keras.layers
tf 1.x:from tensorflow.contrib import tpu
tf 2.6:from tensorflow import tpu
tf 1.x:from tensorflow.contrib import data
tf 2.6:from tensorflow import data
tf 1.x:from tensorflow.contrib import metrics
tf 2.6:from tensorflow import metrics
tf 1.x: from tensorflow.contrib import cluster_resolver
tf 2.6: from tensorflow.python.distribute import cluster_resolver
tf 1.x: tf.contrib.layers.layer_norm
tf 2.6: tf.keras.layers.LayerNormalization
这两个函数实现不同,不能只是换个名字
Keras 2.2.5:from keras.optimizers import Adam
Keras 2.6.0:from keras.optimizer_v2.adam import Adam
Keras 2.2.5:from keras.utils import to_categorical
Keras 2.6.0:from keras.utils.np_utils import to_categorical
RuntimeError: When eager execution is enabled, var_list must specify a list or dict of variables to save
当eager execution开启的时候,loss应该是一个Python函数。
在Tensorflow 2.0 中,eager execution 是默认开启的。
所以,需要先关闭eager execution
import tensorflow as tf
tf.compat.v1.disable_eager_execution()