value error:if' bert'选择模型,然后是Preprocess_mode =' bert'应该使用,反之亦然

发布于 2025-02-13 08:13:19 字数 3387 浏览 3 评论 0原文

我有问题。我收到以下错误valueerror:如果选择了'bert'模型,则应使用Preprocess_mode ='Bert',反之亦然。但是我看不到任何问题。我的代码怎么了?

%%time
#Importing
import ktrain
from ktrain import text
(x_train_bert, y_train_bert), (x_val_bert, y_val_bert), preproc = text.texts_from_array(
                                                                        x_train=train_X.tolist(), y_train=train_y.tolist(),
                                                                        x_test=test_X.tolist(), y_test=test_y.tolist(),
                                                                        class_names=df_complete['forwarder_name'].unique(),
                                                                        preprocess_mode='bert',
                                                                        lang='en',
                                                                        maxlen=65,
                                                                        max_features=35000)                          


model = text.text_classifier(name='bert', train_data=(train_X, train_y), preproc=preproc)
learner = ktrain.get_learner(model,train_data=(train_X, train_y), val_data=(test_X, test_y), batch_size=6)

完全错误


model = text.text_classifier(name='bert', train_data=(train_X, train_y), preproc=preproc)
#learner = ktrain.get_learner(model,train_data=(train_X, train_y), val_data=(test_X, test_y), batch_size=6)
model = text.text_classifier(name='bert', train_data=(train_X, train_y), preproc=preproc)
#learner = ktrain.get_learner(model,train_data=(train_X, train_y), val_data=(test_X, test_y), batch_size=6)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [111], in <cell line: 1>()
----> 1 model = text.text_classifier(name='bert', train_data=(train_X, train_y), preproc=preproc)

File ~\AppData\Roaming\Python\Python39\site-packages\ktrain\text\models.py:589, in text_classifier(name, train_data, preproc, multilabel, metrics, verbose)
    585 if preproc is not None and not preproc.get_classes():
    586     raise ValueError(
    587         "preproc.get_classes() is empty, but required for text classification"
    588     )
--> 589 return _text_model(
    590     name,
    591     train_data,
    592     preproc=preproc,
    593     multilabel=multilabel,
    594     classification=True,
    595     metrics=metrics,
    596     verbose=verbose,
    597 )

File ~\AppData\Roaming\Python\Python39\site-packages\ktrain\text\models.py:109, in _text_model(name, train_data, preproc, multilabel, classification, metrics, verbose)
    107 is_bert = U.bert_data_tuple(train_data)
    108 if (is_bert and name != BERT) or (not is_bert and name == BERT):
--> 109     raise ValueError(
    110         "if '%s' is selected model, then preprocess_mode='%s' should be used and vice versa"
    111         % (BERT, BERT)
    112     )
    113 is_huggingface = U.is_huggingface(data=train_data)
    114 if (is_huggingface and name not in HUGGINGFACE_MODELS) or (
    115     not is_huggingface and name in HUGGINGFACE_MODELS
    116 ):

ValueError: if 'bert' is selected model, then preprocess_mode='bert' should be used and vice versa

I have a problem. I got the following error ValueError: if 'bert' is selected model, then preprocess_mode='bert' should be used and vice versa. But I do not see any problem. What is wrong with my code?

%%time
#Importing
import ktrain
from ktrain import text
(x_train_bert, y_train_bert), (x_val_bert, y_val_bert), preproc = text.texts_from_array(
                                                                        x_train=train_X.tolist(), y_train=train_y.tolist(),
                                                                        x_test=test_X.tolist(), y_test=test_y.tolist(),
                                                                        class_names=df_complete['forwarder_name'].unique(),
                                                                        preprocess_mode='bert',
                                                                        lang='en',
                                                                        maxlen=65,
                                                                        max_features=35000)                          


model = text.text_classifier(name='bert', train_data=(train_X, train_y), preproc=preproc)
learner = ktrain.get_learner(model,train_data=(train_X, train_y), val_data=(test_X, test_y), batch_size=6)

Complete error


model = text.text_classifier(name='bert', train_data=(train_X, train_y), preproc=preproc)
#learner = ktrain.get_learner(model,train_data=(train_X, train_y), val_data=(test_X, test_y), batch_size=6)
model = text.text_classifier(name='bert', train_data=(train_X, train_y), preproc=preproc)
#learner = ktrain.get_learner(model,train_data=(train_X, train_y), val_data=(test_X, test_y), batch_size=6)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [111], in <cell line: 1>()
----> 1 model = text.text_classifier(name='bert', train_data=(train_X, train_y), preproc=preproc)

File ~\AppData\Roaming\Python\Python39\site-packages\ktrain\text\models.py:589, in text_classifier(name, train_data, preproc, multilabel, metrics, verbose)
    585 if preproc is not None and not preproc.get_classes():
    586     raise ValueError(
    587         "preproc.get_classes() is empty, but required for text classification"
    588     )
--> 589 return _text_model(
    590     name,
    591     train_data,
    592     preproc=preproc,
    593     multilabel=multilabel,
    594     classification=True,
    595     metrics=metrics,
    596     verbose=verbose,
    597 )

File ~\AppData\Roaming\Python\Python39\site-packages\ktrain\text\models.py:109, in _text_model(name, train_data, preproc, multilabel, classification, metrics, verbose)
    107 is_bert = U.bert_data_tuple(train_data)
    108 if (is_bert and name != BERT) or (not is_bert and name == BERT):
--> 109     raise ValueError(
    110         "if '%s' is selected model, then preprocess_mode='%s' should be used and vice versa"
    111         % (BERT, BERT)
    112     )
    113 is_huggingface = U.is_huggingface(data=train_data)
    114 if (is_huggingface and name not in HUGGINGFACE_MODELS) or (
    115     not is_huggingface and name in HUGGINGFACE_MODELS
    116 ):

ValueError: if 'bert' is selected model, then preprocess_mode='bert' should be used and vice versa

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

格子衫的從容 2025-02-20 08:13:20

您的代码中有一个错字。问题是您使用train_xtrain_y(未为bert进行预处理),而不是x_train_berty_train_bert (用于伯特的处理)。

而是使用此:

model = text.text_classifier(name='bert', train_data=(x_train_bert, y_train_bert), preproc=preproc)
learner = ktrain.get_learner(model,train_data=(x_train_bert, y_train_bert), val_data=(x_val_bert, y_val_bert), batch_size=6)

There's a typo in your code. The problem is that you're using train_X and train_y (not preprocessed for BERT) instead of x_train_bert and y_train_bert (which were processed for BERT).

Use this instead:

model = text.text_classifier(name='bert', train_data=(x_train_bert, y_train_bert), preproc=preproc)
learner = ktrain.get_learner(model,train_data=(x_train_bert, y_train_bert), val_data=(x_val_bert, y_val_bert), batch_size=6)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文