mirror of
https://github.com/aladdinpersson/Machine-Learning-Collection.git
synced 2026-04-10 12:33:44 +00:00
add lightning code, finetuning whisper, recommender system neural collaborative filtering
This commit is contained in:
71
ML/Pytorch/pytorch_lightning/7. Callbacks/model.py
Normal file
71
ML/Pytorch/pytorch_lightning/7. Callbacks/model.py
Normal file
@@ -0,0 +1,71 @@
|
||||
import torch
|
||||
import torch.nn.functional as F
|
||||
import torchvision.datasets as datasets
|
||||
import torchvision.transforms as transforms
|
||||
from torch import nn, optim
|
||||
from torch.utils.data import DataLoader
|
||||
from tqdm import tqdm
|
||||
import pytorch_lightning as pl
|
||||
import torchmetrics
|
||||
from torchmetrics import Metric
|
||||
|
||||
|
||||
class NN(pl.LightningModule):
|
||||
def __init__(self, input_size, learning_rate, num_classes):
|
||||
super().__init__()
|
||||
self.lr = learning_rate
|
||||
self.fc1 = nn.Linear(input_size, 50)
|
||||
self.fc2 = nn.Linear(50, num_classes)
|
||||
self.loss_fn = nn.CrossEntropyLoss()
|
||||
self.accuracy = torchmetrics.Accuracy(
|
||||
task="multiclass", num_classes=num_classes
|
||||
)
|
||||
self.f1_score = torchmetrics.F1Score(task="multiclass", num_classes=num_classes)
|
||||
|
||||
def forward(self, x):
|
||||
x = F.relu(self.fc1(x))
|
||||
x = self.fc2(x)
|
||||
return x
|
||||
|
||||
def training_step(self, batch, batch_idx):
|
||||
loss, scores, y = self._common_step(batch, batch_idx)
|
||||
accuracy = self.accuracy(scores, y)
|
||||
f1_score = self.f1_score(scores, y)
|
||||
self.log_dict(
|
||||
{
|
||||
"train_loss": loss,
|
||||
"train_accuracy": accuracy,
|
||||
"train_f1_score": f1_score,
|
||||
},
|
||||
on_step=False,
|
||||
on_epoch=True,
|
||||
prog_bar=True,
|
||||
)
|
||||
return {"loss": loss, "scores": scores, "y": y}
|
||||
|
||||
def validation_step(self, batch, batch_idx):
|
||||
loss, scores, y = self._common_step(batch, batch_idx)
|
||||
self.log("val_loss", loss)
|
||||
return loss
|
||||
|
||||
def test_step(self, batch, batch_idx):
|
||||
loss, scores, y = self._common_step(batch, batch_idx)
|
||||
self.log("test_loss", loss)
|
||||
return loss
|
||||
|
||||
def _common_step(self, batch, batch_idx):
|
||||
x, y = batch
|
||||
x = x.reshape(x.size(0), -1)
|
||||
scores = self.forward(x)
|
||||
loss = self.loss_fn(scores, y)
|
||||
return loss, scores, y
|
||||
|
||||
def predict_step(self, batch, batch_idx):
|
||||
x, y = batch
|
||||
x = x.reshape(x.size(0), -1)
|
||||
scores = self.forward(x)
|
||||
preds = torch.argmax(scores, dim=1)
|
||||
return preds
|
||||
|
||||
def configure_optimizers(self):
|
||||
return optim.Adam(self.parameters(), lr=self.lr)
|
||||
Reference in New Issue
Block a user