Tensorflow版本问题的一系列报错解决方案

一下的报错均是由于将tensorflow1版本的代码迁移到tensorflow2上产生的错误,主要是一些api的改动。

  File "D:\python\paperRecurrence\CasRel\CasRel-master\model.py", line 23, in E2EModel
    tokens_in = Input(shape=(None,),dtype=tf.int32)
  File "E:\environment\python\lib\site-packages\keras\engine\input_layer.py", line 178, in Input
    input_tensor=tensor)
  File "E:\environment\python\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "E:\environment\python\lib\site-packages\keras\engine\input_layer.py", line 39, in __init__
    name = prefix + '_' + str(K.get_uid(prefix))
  File "E:\environment\python\lib\site-packages\keras\backend\tensorflow_backend.py", line 74, in get_uid
    graph = tf.get_default_graph()
AttributeError: module 'tensorflow' has no attribute 'get_default_graph'

将keras.layers.Input改为tf.keras.layers.Input

 其他类似的改动还有:

将keras.layers.Lambda改为tf.keras.layers.Lambda

将keras.layers.Dense改为tf.keras.layers.Dense

将keras.models.Model改为tf.keras.models.Model

将keras.layers.Add改为tf.keras.layers.Add

 

  File "D:/python/paperRecurrence/CasRel/CasRel-master/run.py", line 47, in 
    subject_model, object_model, hbt_model = E2EModel(bert_config_path, bert_checkpoint_path, LR, num_rels)
  File "D:\python\paperRecurrence\CasRel\CasRel-master\model.py", line 63, in E2EModel
    sub_tails_loss = K.binary_crossentropy(gold_sub_tails, pred_sub_tails)
  File "E:\environment\python\lib\site-packages\keras\backend\tensorflow_backend.py", line 3376, in binary_crossentropy
    output = tf.log(output / (1 - output))
AttributeError: module 'tensorflow' has no attribute 'log'

将K.binary_crossentropy(K是keras.backend)改为tf.keras.losses.binary_crossentropy

 

ValueError: ('Could not interpret optimizer identifier:', )

将keras.optimizers.Adam改为tf.keras.optimizers.Adam

你可能感兴趣的:(Deep,Learning,python)