Ixazululiwe: i-hugging face model pytorch

imodeli yobuso be-hugging enhle Eminyakeni yakamuva, kube nentshisekelo ekhulayo ekucutshungulweni kolimi lwemvelo (NLP) nokufunda komshini, ngenxa yokuthuthukiswa kwamamodeli anamandla afana nama-Hugging Face's Transformers. Lawa mamodeli aguqule indlela esihlaziya ngayo futhi sicubungula umbhalo, anikeze ukuqonda okungcono nokuqonda. Ukushuna kahle lawa mamodeli sekuphenduke isihloko esidumile, njengoba kuvumela abathuthukisi ukuthi benze ngendlela oyifisayo amamodeli aqeqeshwe ngaphambilini emisebenzini yawo ethile futhi bazuze ukusebenza okuphezulu. Kulesi sihloko, sizoxoxa ngendlela yokushuna kahle imodeli ye-Hugging Face Transformer, sidlule encazelweni yesinyathelo ngesinyathelo sekhodi, futhi sijule kwezinye izihloko ezihlobene, imisebenzi, kanye nemitapo yolwazi ehilelekile kule nqubo.

Ukushuna kahle imodeli yobuso obungangana kuhlanganisa ukulungisa imodeli eqeqeshwe kusengaphambili emsebenzini othile owenzayo ngokwenza ukuqeqeshwa okwengeziwe ngedathasethi encane. Le nqubo inenzuzo, njengoba isivumela ukuthi sisebenzise ulwazi olubanzi olujwayelekile lwamamodeli aqeqeshwe kusengaphambili futhi sigxile kakhulu ezintweni ezingavamile zomsebenzi wethu.

Inqubo yokushuna kahle

Isinyathelo sokuqala sokulungisa kahle imodeli ye-Hugging Face ukukhetha imodeli eqeqeshwe kusengaphambili efanele umsebenzi. Amamodeli asetshenziswa kakhulu afaka i-BERT, GPT-2, ne-RoBERTa. Kubalulekile ukungenisa imitapo yolwazi edingekayo, njengeHugging Face's Umtapo wolwazi we-Transformers, i-PyTorch, noma i-TensorFlow.

import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification

Ngemuva kokungenisa, khetha imodeli eqeqeshwe ngaphambilini futhi uqinisekise ithokheni nemodeli, uqiniseke ukuthi ucacisa indawo yokuhlola eyimodeli kanye nohlobo lwemodeli.

model_checkpoint = "distilbert-base-uncased-finetuned-sst-2-english"
tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)
model = AutoModelForSequenceClassification.from_pretrained(model_checkpoint)

Okulandelayo, lungiselela idathasethi yakho ukuze ilungiswe kahle. Lokhu kungase kuhlanganise ukwenza amathokheni, ukupheda, nokudalwa kwenqwaba. Sebenzisa indlela yethokheni ethi `encode_plus` ukuze wenze amathokheni umbhalo wokufakwayo, futhi udale i-DataLoader yokuhlanganiswa kwedatha.

from torch.utils.data import DataLoader

# Tokenize the dataset
inputs = tokenizer(sentences, return_tensors="pt", padding=True, truncation=True)

# Create a DataLoader
dataloader = DataLoader(inputs, batch_size=16)

Manje njengoba idathasethi isilungile, ungaqala inqubo yokulungisa kahle. Chaza a iluphu yokuqeqesha ngesilungiseleli esicacisiwe, esifana ne-AdamW, kanye nesihleli sezinga lokufunda. Buyekeza ngokuphindaphindiwe izisindo zemodeli kuyilapho unciphisa umsebenzi wokulahlekelwa.

from transformers import AdamW, get_linear_schedule_with_warmup

optimizer = AdamW(model.parameters(), lr=5e-5)
scheduler = get_linear_schedule_with_warmup(optimizer, num_warmup_steps=100, num_training_steps=len(dataloader))

for batch in dataloader:
    outputs = model(**batch)
    loss = outputs.loss
    loss.backward()
    optimizer.step()
    scheduler.step()
    optimizer.zero_grad()

Ngemva kokulungisa kahle, hlola imodeli kusethi yokuhlola bese uyilondoloza ukuze uyisebenzise kamuva uma kudingeka.

Imitapo yolwazi kanye nemisebenzi

Imitapo yolwazi eminingi ebalulekile kanye nemisebenzi ibalulekile enqubweni yokulungisa kahle:

  • Umtapo wolwazi we-Transformers: Ithuthukiswe i-Hugging Face, le labhulali inikeza amamodeli anhlobonhlobo aqeqeshwe ngaphambilini futhi ibalulekile ekulungiseni kahle. Isekela zombili izinhlaka ze-PyTorch kanye ne-TensorFlow.
  • I-PyTorch/TensorFlow: Lezi zinhlaka zokufunda ezijulile ezidumile zihlinzeka ngamathuluzi abalulekile okuqeqeshwa okuyimodeli, njengezilungiseleli kanye nabahleli bezinga lokufunda, abadingekayo ngesikhathi sokushuna kahle.
  • I-AdamW: Lokhu ukuqaliswa kwe-PyTorch kwe-Adam optimizer edumile ngokubola kwesisindo esakhelwe ngaphakathi. Isetshenziswa kabanzi ukulungisa kahle amamodeli we-Transformer.
  • get_linear_schedule_with_warmup: Lesi sihleli sezinga lokufunda, esinikezwe umtapo wezincwadi we-Transformers, sinyusa kancane izinga lokufunda ekuqaleni kokuqeqeshwa ukuze sigweme izinguquko ezisheshayo futhi sinciphise ngokushelela ekupheleni kokuqeqeshwa ukuze sicushwe kahle kakhulu.

Sengiphetha, ukulungisa kahle amamodeli e-Hugging Face Transformer kuyindlela ebalulekile yokufeza ukusebenza okuphezulu emisebenzini ethile ye-NLP. Ngokuqonda inqubo, ikhodi, namalabhulali abandakanyekayo nemisebenzi, ungakha amamodeli enziwe ngendlela efanele ahamba phambili ezinhlelweni ezihlukahlukene.

Okuthunyelwe okuhlobene:

Shiya amazwana