Gabatarwa
A cikin duniyar da ke tasowa cikin sauri na zurfafa ilmantarwa da cibiyoyin sadarwa na jijiyoyi, ɗakunan karatu da tsarin aiki suna da mahimmanci don sauƙaƙe da haɓaka tsarin ci gaba. Walƙiya PyTorch ɗaya ce irin wannan ɗakin karatu mai ƙarfi wanda aka gina a saman mashahurin PyTorch. An tsara walƙiya don ba da damar Masana Kimiyyar Bayanai da Injiniyoyi na ML don auna ƙirar su cikin sauƙi, guje wa lambar tukunyar jirgi, da haɓaka iya karantawa gabaɗaya. Koyaya, yayin aiki tare da walƙiya na PyTorch, sau da yawa kuna iya samun kanku kuna fuskantar matsaloli kamar kuskuren sifa 'pytorch_lightning.metrics'. A cikin wannan labarin, za mu magance matsalar kuma za mu bi ku ta hanyar magance ta, mu karya lambar don ƙarin fahimta. Bugu da ƙari, za mu tattauna dakunan karatu masu alaƙa da ayyukan da ke cikin warware wannan batu.
Maganin matsalar
Ɗaya daga cikin manyan matsalolin da ke da alaƙa da kuskuren '% 27pytorch_lightning%27 ba shi da sifa %27metrics%27' shine cewa kuna iya shigar da tsohuwar sigar walƙiya ta PyTorch wacce ba ta haɗa da tsarin awo ba. Don gyara wannan, kawai kuna iya haɓaka walƙiya ta PyTorch zuwa sabon sigar ta hanyar aiwatar da umarni mai zuwa:
pip install --upgrade pytorch-lightning
Bayanin mataki-mataki na Code
Da zarar kun sabunta ɗakin karatu, za mu iya fara aiki tare da ma'aunin tushen walƙiya na PyTorch. Mataki na farko shine shigo da abubuwan da ake buƙata daga PyTorch Walƙiya. Za mu yi amfani da ma'aunin Daidaitacce don dalilai na hoto a cikin wannan labarin.
import torch from pytorch_lightning import LightningModule from pytorch_lightning.metrics.functional import accuracy
Na gaba, bari mu ayyana cibiyar sadarwar mu ta hanyar amfani da LightningModule azaman ajin tushe. A cikin hanyoyin 'training_step' da 'validation_step' hanyoyin, za mu ƙididdige hasashen mu da masu sa ido na gaskiya, kuma za mu ƙididdige daidaito ta amfani da aikin awo na 'daidai' wanda PyTorch Walƙiya ke bayarwa.
class Classifier(LightningModule): def __init__(self): super().__init__() self.layer1 = torch.nn.Linear( 32, 128) self.layer2 = torch.nn.Linear(128, 10) def forward(self, x): x = torch.relu(self.layer1(x)) x = self.layer2(x) return x def training_step(self, batch, batch_idx): x, y = batch y_hat = self(x) loss = torch.nn.functional.cross_entropy(y_hat, y) acc = accuracy(y_hat, y) # Compute accuracy using PyTorch Lightning self.log('train_loss', loss) self.log('train_acc', acc, prog_bar=True) return loss def validation_step(self, batch, batch_idx): x, y = batch y_hat = self(x) loss = torch.nn.functional.cross_entropy(y_hat, y) acc = accuracy(y_hat, y) # Compute accuracy using PyTorch Lightning self.log('val_loss', loss, prog_bar=True) self.log('val_acc', acc, prog_bar=True) return loss
A ƙarshe, bin wannan tsarin lambar, yakamata ku sami damar yin aiki lafiya tare da PyTorch Lightning-metrics ba tare da fuskantar kuskuren sifa da aka ambata ba.
Dakunan karatu masu dangantaka: Torchmetrics
- Wani ɗakin karatu da ya kamata a ambata shi ne Torchmetrics, ɗakin karatu na tushen PyTorch ƙware wajen samar da ma'auni don kimanta ƙirar ilmantarwa mai zurfi. Laburaren Torchmetrics an ƙirƙira shi ta masu haɓakawa iri ɗaya kamar PyTorch Lightning, yana tabbatar da dacewa da samar da API mai sauƙi da daidaito.
- Torchmetrics yana ba da ma'auni daban-daban kamar Daidaici, Daidaitawa, Tunawa, maki F1, da ƙari mai yawa. Yana rage wahalar aiwatar da waɗannan ma'auni da hannu kuma yana ba ku damar mai da hankali kan wasu fannonin ayyukan ku.
Haɓaka Karatun Code tare da Walƙiya PyTorch
Ɗaya daga cikin mahimman fa'idodin amfani da walƙiya na PyTorch shine cewa yana sauƙaƙa tsarin madauki na horo sosai kuma yana sa lambar ta zama abin karantawa. LightningModule yana ɗaukar ainihin abubuwan haɗin yanar gizon jijiyoyi, kamar ƙirar ƙirar ƙira, dabaru na horo, da ingantaccen dabaru, yana ba ku ikon sarrafa waɗannan abubuwan ta hanyar daidaitawa. Sakamakon haka, zaku iya haɓakawa da haɓaka samfuran ku da kyau, yana ba ku kyakkyawar fahimtar lambar ku yayin da kuke haɓaka haɗin gwiwa tsakanin membobin ƙungiyar.