问题是在一个Python项目中将TensorFlow与HuggingGPT的transformers组合时遇到的。

huangapple go评论66阅读模式
英文:

Problems combining tensorflow with huggingGPT transformers on a python project

问题

这是我的代码:

import os
from dotenv import load_dotenv
from database import create_connection
import mysql.connector
# from user_interface import user_interface
from flask import Flask, request, jsonify
import pandas as pd
import mysql.connector
from transformers import BertTokenizer, TFBertForSequenceClassification
import tensorflow as tf

# 从.env文件加载环境变量
load_dotenv()

print(tf.__version__)

app = Flask(__name__)

# 获取环境变量
host = os.getenv('DB_HOST')
user = os.getenv('DB_USER')
password = os.getenv('DB_PASSWORD')
db_name = os.getenv('DB_NAME')

connection = create_connection(host, user, password, db_name)

# 加载模型和分词器
# model = TFBertForSequenceClassification.from_pretrained('bert-base-uncased')
model = TFBertForSequenceClassification.from_pretrained('bert-base-uncased')
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')

# 定义类别

但是当我尝试运行时,总是收到这个错误:

Traceback (most recent call last):
  File "/opt/homebrew/lib/python3.11/site-packages/transformers/utils/import_utils.py", line 1076, in _get_module
    return importlib.import_module("." + module_name, self.__name__)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.11/3.11.3/Frameworks/Python.framework/Versions/3.11/lib/python3.11/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1206, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1178, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1149, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/opt/homebrew/lib/python3.11/site-packages/transformers/models/bert/modeling_tf_bert.py", line 38, in <module>
    from ...modeling_tf_utils import (
  File "/opt/homebrew/lib/python3.11/site-packages/transformers/modeling_tf_utils.py", line 70, in <module>
    from keras.engine import data_adapter
ModuleNotFoundError: No module named 'keras.engine'

上面的异常是以下异常的直接原因向上查看其回溯):

Traceback (most recent call last):
  File "/Users/pedrospecter/Downloads/work/cw1/autonomous_ai/main.py", line 9, in <module>
    from transformers import BertTokenizer, TFBertForSequenceClassification
  File "<frozen importlib._bootstrap>", line 1231, in _handle_fromlist
  File "/opt/homebrew/lib/python3.11/site-packages/transformers/utils/import_utils.py", line 1067, in __getattr__
    value = getattr(module, name)
            ^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/transformers/utils/import_utils.py", line 1066, in __getattr__
    module = self._get_module(self._class_to_module[name])
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/transformers/utils/import_utils.py", line 1078, in _get_module
    raise RuntimeError(
RuntimeError: Failed to import transformers.models.bert.modeling_tf_bert because of the following error (look up to see its traceback):
No module named 'keras.engine'

我已经使用以下命令安装了transformers和tensorflow:

pip3 install transformers tensorflow

我使用的是MacBook Pro M2 Max。有人有关于如何运行此代码的提示吗?代码只包括到出现错误的关键部分,但下面还有更多的代码。

祝好运!

英文:

This is my code:

mport os
from dotenv import load_dotenv
from database import create_connection
import mysql.connector
# from user_interface import user_interface
from flask import Flask, request, jsonify
import pandas as pd
import mysql.connector
from transformers import BertTokenizer, TFBertForSequenceClassification
import tensorflow as tf



# Load environment variables from .env file
load_dotenv()

print(tf.__version__)

app = Flask(__name__)

# Get environment variables
host = os.getenv(&#39;DB_HOST&#39;)
user = os.getenv(&#39;DB_USER&#39;)
password = os.getenv(&#39;DB_PASSWORD&#39;)
db_name = os.getenv(&#39;DB_NAME&#39;)

connection = create_connection(host, user, password, db_name)

# Load the model and tokenizer
# model = TFBertForSequenceClassification.from_pretrained(&#39;bert-base-uncased&#39;)
model = TFBertForSequenceClassification.from_pretrained(&#39;bert-base-uncased&#39;)
tokenizer = BertTokenizer.from_pretrained(&#39;bert-base-uncased&#39;)

# Define categories

but when I try to run I always get this error:

Traceback (most recent call last):
  File &quot;/opt/homebrew/lib/python3.11/site-packages/transformers/utils/import_utils.py&quot;, line 1076, in _get_module
    return importlib.import_module(&quot;.&quot; + module_name, self.__name__)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File &quot;/opt/homebrew/Cellar/python@3.11/3.11.3/Frameworks/Python.framework/Versions/3.11/lib/python3.11/importlib/__init__.py&quot;, line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 1206, in _gcd_import
  File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 1178, in _find_and_load
  File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 1149, in _find_and_load_unlocked
  File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 690, in _load_unlocked
  File &quot;&lt;frozen importlib._bootstrap_external&gt;&quot;, line 940, in exec_module
  File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 241, in _call_with_frames_removed
  File &quot;/opt/homebrew/lib/python3.11/site-packages/transformers/models/bert/modeling_tf_bert.py&quot;, line 38, in &lt;module&gt;
    from ...modeling_tf_utils import (
  File &quot;/opt/homebrew/lib/python3.11/site-packages/transformers/modeling_tf_utils.py&quot;, line 70, in &lt;module&gt;
    from keras.engine import data_adapter
ModuleNotFoundError: No module named &#39;keras.engine&#39;

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File &quot;/Users/pedrospecter/Downloads/work/cw1/autonomous_ai/main.py&quot;, line 9, in &lt;module&gt;
    from transformers import BertTokenizer, TFBertForSequenceClassification
  File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 1231, in _handle_fromlist
  File &quot;/opt/homebrew/lib/python3.11/site-packages/transformers/utils/import_utils.py&quot;, line 1067, in __getattr__
    value = getattr(module, name)
            ^^^^^^^^^^^^^^^^^^^^^
  File &quot;/opt/homebrew/lib/python3.11/site-packages/transformers/utils/import_utils.py&quot;, line 1066, in __getattr__
    module = self._get_module(self._class_to_module[name])
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File &quot;/opt/homebrew/lib/python3.11/site-packages/transformers/utils/import_utils.py&quot;, line 1078, in _get_module
    raise RuntimeError(
RuntimeError: Failed to import transformers.models.bert.modeling_tf_bert because of the following error (look up to see its traceback):
No module named &#39;keras.engine&#39;

I have installed both transformers and tensorflow using:

pip3 install transformers tensorflow

I am on a MacBook Pro M2 Max

does anyone have a tip on how can I run this? The code is just the essential until the error shows up, but there is way more code under.

Cheers

答案1

得分: 1

I recommend using the latest stable version and not a release candidate. In this case install tf and keras 2.12.

Also I don't recommend using brew python. Install conda and create a virtual env to experiment with installations there if you are not very experienced with packages and versions.

Final the error that you mention TFBertForSequenceClassification requires the TensorFlow library but it was not found in your environment can be easily solved. Google is your friend.

So to wrap up.

  1. Install conda
  2. Setup a virtual env
  3. Install everything in that virtual env (make sure to activate it before).
  4. Install stuff with conda and not with pip (recommendation not mandatory).
  5. Make sure you are using gpu tf, pip install tensorflow-gpu for example (the last error is related to this). Conda handles quite good gpu support.

Conda, getting started

英文:

I recommend using the latest stable version and not a release candidate. In this case install tf and keras 2.12.

Also I don't recommend using brew python. Install conda and create a virtual env to experiment with installations there if you are not very experienced with packages and versions.

Final the error that you mention TFBertForSequenceClassification requires the TensorFlow library but it was not found in your environment can be easily solved. Google is your friend.

So to wrap up.

  1. Install conda
  2. Setup a virtual env
  3. Install everything in that virtual env (make sure to activate it before).
  4. Install stuff with conda and not with pip (recommendation not mandatory).
  5. Make sure you are using gpu tf, pip install tensorflow-gpu for example (the last error is related to this). Conda handles quite good gpu support.

Conda, getting started

huangapple
  • 本文由 发表于 2023年5月21日 21:46:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76300212.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定