tensorflow2.16(解决keras、tensorflow 2.0版本报错问题)
导读:前言 主要是解决由于1.0版本和2.0版本代码实现不同造成的报错...
前言
主要是解决由于1.0版本和2.0版本代码实现不同造成的报错
报错一:cannot import name Adam fromkeras.optimizers
报错语句:from keras.optimizers import Adam 正确语句:from keras.optimizers import adam_v2报错二:module tensorflow has no attribute app
报错语句:
import tensorflow as tf
flags =tf.app.flags
FLAGS = flags.FLAGS正确语句:
import tensorflow.compat.v1 as tf
flags=tf.app.flags
FLAGS = tf.app.flags.FLAGS报错三:No module named tensorflow.contrib
报错语句:import tensorflow.contrib.keras as kr
正确语句:import keras as kr
报错四: module tensorflow has no attribute placeholder
错误代码:import tensorflow as tf 正确代码: import tensorflow.compat.v1 as tf tf.disable_v2_behavior() tf.compat.v1.disable_eager_execution()但是改之后出现错误五
报错五:module tensorflow.compat.v1 has no attribute contrib
错误代码:Cell = tf.contrib.rnn.DropoutWrapper(cell, self.keep_pro)
正确代码:Cell =tf.nn.rnn_cell.DropoutWrapper(cell, self.keep_pro)
(此时tf=tensorflow.compat.v1)
报错六: module tensorboard.summary._tf.summary has no attribute merge_all
错误语句:import tensorflow as tf
正确语句:import tensorflow.compat.v1 as tf
报错七:module tensorflow has no attribute Session
解决同六
报错八:module keras.utils has no attribute to_categorical
错误语句:
import keras as kr y_pad = kr.utils.to_categorical(label_id)正确语句:
import tensorflow as tf y_pad = tf.keras.utils.to_categorical(label_id)遇到版本不一致问题 ,可以直接到官网搜索对应的函数 ,会出来相应的导入路径
创心域SEO版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!