0


解决keras、tensorflow 2.0版本报错问题

前言

主要是解决由于1.0版本和2.0版本代码实现不同造成的报错

报错一:cannot import name 'Adam' from'keras.optimizers'

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA55m95bCP55m95ZGA,size_20,color_FFFFFF,t_70,g_se,x_16

报错语句: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)

遇到版本不一致问题,可以直接到官网搜索对应的函数,会出来相应的导入路径


本文转载自: https://blog.csdn.net/weixin_44855297/article/details/122599024
版权归原作者 白小白呀 所有, 如有侵权,请联系我们删除。

“解决keras、tensorflow 2.0版本报错问题”的评论:

还没有评论