gpt4 book ai didi

python - 导入 BERT : module 'tensorflow._api.v2.train' has no attribute 'Optimizer' 时出错

转载 作者:行者123 更新时间:2023-12-04 11:55:32 31 4
gpt4 key购买 nike

我尝试使用 bert-tensorflow在 Google Colab 中,但出现以下错误:

--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) in () 1 import bert ----> 2 from bert import run_classifier_with_tfhub # run_classifier 3 from bert import optimization 4 from bert import tokenization

1 frames /usr/local/lib/python3.6/dist-packages/bert/optimization.py in () 85 86 ---> 87 class AdamWeightDecayOptimizer(tf.train.Optimizer): 88 """A basic Adam optimizer that includes "correct" L2 weight decay.""" 89

AttributeError: module 'tensorflow._api.v2.train' has no attribute 'Optimizer'



这是我试过的代码:
  • 安装库:
  • !pip install --upgrade --force-reinstall tensorflow
    !pip install --upgrade --force-reinstall tensorflow-gpu
    !pip install tensorflow_hub
    !pip install sentencepiece
    !pip install bert-tensorflow
  • 运行此代码:
  • from sklearn.model_selection import train_test_split
    import pandas as pd
    from datetime import datetime
    from tensorflow.keras import optimizers
    import bert
    from bert import run_classifier
    from bert import optimization
    from bert import tokenization

    我也试过 import tensorflow.compat.v1 as tf
    tf.disable_v2_behavior()

    但得到了同样的错误。

    最佳答案

    我在我自己的 colab notebook 中做了一些实验(请下次提供链接),我发现在错误消息中,有

    class AdamWeightDecayOptimizer(tf.train.Optimizer):

    这是类(class)的标题。但没有什么像 tf.train.optimizer相反,它应该是:
    class AdamWeightDecayOptimizer(tf.compat.v1.train.Optimizer):

    与 (lol) 完全相同的行存在确切问题的链接是 here

    关于python - 导入 BERT : module 'tensorflow._api.v2.train' has no attribute 'Optimizer' 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61250311/

    31 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com