英文:
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('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)
# Load the model and tokenizer
# model = TFBertForSequenceClassification.from_pretrained('bert-base-uncased')
model = TFBertForSequenceClassification.from_pretrained('bert-base-uncased')
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
# Define categories
but when I try to run I always get this error:
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'
The above exception was the direct cause of the following exception:
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'
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.
- Install conda
- Setup a virtual env
- Install everything in that virtual env (make sure to activate it before).
- Install stuff with conda and not with pip (recommendation not mandatory).
- 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.
英文:
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.
- Install conda
- Setup a virtual env
- Install everything in that virtual env (make sure to activate it before).
- Install stuff with conda and not with pip (recommendation not mandatory).
- 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论