登录
首页 >  文章 >  python教程

使用BERT在Kaggle上使用NLP入门

来源:dev.to

时间:2025-01-30 18:15:20 139浏览 收藏

一分耕耘,一分收获!既然都打开这篇《使用BERT在Kaggle上使用NLP入门》,就坚持看下去,学下去吧!本文主要会给大家讲到等等知识点,如果大家对本文有好的建议或者看到有不足之处,非常欢迎大家积极提出!在后续文章我会继续更新文章相关的内容,希望对大家都有所帮助!

1,进口和eda

import os
iskaggle = os.environ.get('kaggle_kernel_run_type', '')
from pathlib import path
if iskaggle:
    path = path('/kaggle/input/us-patent-phrase-to-phrase-matching')
import pandas as pd
df = pd.read_csv(path/'train.csv')
df['input'] = 'text1: ' + df.context + '; text2: ' + df.target + '; anc1: ' + df.anchor
df.input.head()

2,令牌化

from datasets import dataset, datasetdict
ds = dataset.from_pandas(df)
import warnings,logging,torch
warnings.simplefilter('ignore')
logging.disable(logging.warning)
model_nm = 'anferico/bert-for-patents'
# load model directly
from transformers import automodelforsequenceclassification, autotokenizer
model = automodelforsequenceclassification.from_pretrained(model_nm, num_labels=1)
tokenizer = autotokenizer.from_pretrained('anferico/bert-for-patents')
def tok_func(x):
    return tokenizer(x['input'])
# tokenize all the sentences using the tokenizer
tok_ds = ds.map(tok_func, batched=true)
tok_ds = tok_ds.rename_columns({'score':'labels'})

3,测试和验证集

eval_df = pd.read_csv(path/'test.csv')
dds = tok_ds.train_test_split(0.25, seed=42)
eval_df['input'] = 'text1: ' + eval_df.context + '; text2: ' + eval_df.target + '; anc1: ' + eval_df.anchor
eval_ds = dataset.from_pandas(eval_df).map(tok_func, batched=true)

4,指标和相关性

import numpy as np
def corr(x,y): 
    ## change the 2-d array into 1-d array
    return np.corrcoef(x.flatten(), y)[0,1]
def corr_d(eval_pred): return {'pearson': corr(*eval_pred)}

5,训练我们的模型

14625233945

image description

6,在测试集中获取预测

preds = trainer.predict(eval_ds).predictions.astype(float)
preds = np.clip(preds, 0, 1)
import datasets

submission = datasets.Dataset.from_dict({
    'id': eval_ds['id'],
    'score': preds
})

submission.to_csv('submission.csv', index=False)

今天关于《使用BERT在Kaggle上使用NLP入门》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

声明:本文转载于:dev.to 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>