TensorFlow2.X 和 1.X不同导致的报错
一、
TensorFlow1.X :
model = Model(input=[inputs], output=output)
报错信息:
TypeError: (‘Keyword argument not understood:’, ‘input’)
解决方法,换成TensorFlow2.X :
model = Model(inputs=..., outputs=...)
二、
TensorFlow1.X :
from keras.models import Model
from keras.optimizers import Adam
from keras import backend as K
from keras.models import load_model
from keras.utils import to_categorical
报错信息:
ModuleNotFoundError: No module named ‘keras’
TensorFlow2.X :
from tensorflow.python.keras.models import Model
from tensorflow.python.keras.optimizers import Adam
from tensorflow.python.keras import backend as K
from tensorflow.python.keras.models import load_model
from tensorflow.keras.utils import to_categorical
三、
TensorFlow1.X :
import tensorflow as tf
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.333)
sess = tf.Session(config=tf.compat.v1.ConfigProto(gpu_options=gpu_options))
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.Session(config=config)
报错信息:
AttributeError: module ‘tensorflow’ has no attribute ……
TensorFlow2.X :
import tensorflow.compat.v1 as tf
tf.compat.v1.disable_eager_execution()
或
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
替换import tensorflow as tf
四、
TensorFlow1.X :
history = model.fit(X_train, train_label, batch_size=64, nb_epoch=nb_epoch,verbose=1,
shuffle=True, validation_data=(X_val, val_label), callbacks=callback_lists)
报错信息:
TypeError: fit() got an unexpected keyword argument ‘nb_epoch’
TensorFlow2.X :
history = model.fit(X_train, train_label, batch_size=64, epochs=nb_epoch,verbose=1,
shuffle=True, validation_data=(X_val, val_label),callbacks=callback_lists)
附件
在TensorFlow 2.x版本中,将TensorFlow 1.x版本中的部分函数名字进行调整,如下:
将函数tf.random_uniform —>tf.random.uniform
将函数tf.random_crop—>tf.image.random_crop
将函数tf.random_shuffle—>tf.random.shuffle
将函数tf.read_file—>tf.io.read_file
参考链接
https://zhuanlan.zhihu.com/p/74858833