首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ValueError:如果选择“bert”模型,则应该使用预处理_mode= 'bert‘,反之亦然

ValueError:如果选择“bert”模型,则应该使用预处理_mode= 'bert‘,反之亦然
EN

Stack Overflow用户
提问于 2022-07-04 12:30:48
回答 1查看 68关注 0票数 0

我有个问题。我得到了下面的错误ValueError: if 'bert' is selected model, then preprocess_mode='bert' should be used and vice versa。但我看不出有什么问题。我的密码怎么了?

代码语言:javascript
复制
%%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)

完全误差

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2022-07-14 12:04:19

你的代码里有个错误。问题是,您使用的是train_Xtrain_y (而不是对BERT进行预处理),而不是x_train_berty_train_bert (为BERT处理)。

用这个代替:

代码语言:javascript
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72856811

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档