mirror of
https://github.com/aladdinpersson/Machine-Learning-Collection.git
synced 2026-02-21 11:18:01 +00:00
add lightning code, finetuning whisper, recommender system neural collaborative filtering
This commit is contained in:
15
ML/Pytorch/pytorch_lightning/6. Restructuring/config.py
Normal file
15
ML/Pytorch/pytorch_lightning/6. Restructuring/config.py
Normal file
@@ -0,0 +1,15 @@
|
||||
# Training hyperparameters
|
||||
INPUT_SIZE = 784
|
||||
NUM_CLASSES = 10
|
||||
LEARNING_RATE = 0.001
|
||||
BATCH_SIZE = 64
|
||||
NUM_EPOCHS = 3
|
||||
|
||||
# Dataset
|
||||
DATA_DIR = "dataset/"
|
||||
NUM_WORKERS = 4
|
||||
|
||||
# Compute related
|
||||
ACCELERATOR = "gpu"
|
||||
DEVICES = [0]
|
||||
PRECISION = 16
|
||||
59
ML/Pytorch/pytorch_lightning/6. Restructuring/dataset.py
Normal file
59
ML/Pytorch/pytorch_lightning/6. Restructuring/dataset.py
Normal file
@@ -0,0 +1,59 @@
|
||||
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 torch.utils.data import random_split
|
||||
import pytorch_lightning as pl
|
||||
|
||||
|
||||
class MnistDataModule(pl.LightningDataModule):
|
||||
def __init__(self, data_dir, batch_size, num_workers):
|
||||
super().__init__()
|
||||
self.data_dir = data_dir
|
||||
self.batch_size = batch_size
|
||||
self.num_workers = num_workers
|
||||
|
||||
def prepare_data(self):
|
||||
datasets.MNIST(self.data_dir, train=True, download=True)
|
||||
datasets.MNIST(self.data_dir, train=False, download=True)
|
||||
|
||||
def setup(self, stage):
|
||||
entire_dataset = datasets.MNIST(
|
||||
root=self.data_dir,
|
||||
train=True,
|
||||
transform=transforms.ToTensor(),
|
||||
download=False,
|
||||
)
|
||||
self.train_ds, self.val_ds = random_split(entire_dataset, [50000, 10000])
|
||||
self.test_ds = datasets.MNIST(
|
||||
root=self.data_dir,
|
||||
train=False,
|
||||
transform=transforms.ToTensor(),
|
||||
download=False,
|
||||
)
|
||||
|
||||
def train_dataloader(self):
|
||||
return DataLoader(
|
||||
self.train_ds,
|
||||
batch_size=self.batch_size,
|
||||
num_workers=self.num_workers,
|
||||
shuffle=True,
|
||||
)
|
||||
|
||||
def val_dataloader(self):
|
||||
return DataLoader(
|
||||
self.val_ds,
|
||||
batch_size=self.batch_size,
|
||||
num_workers=self.num_workers,
|
||||
shuffle=False,
|
||||
)
|
||||
|
||||
def test_dataloader(self):
|
||||
return DataLoader(
|
||||
self.test_ds,
|
||||
batch_size=self.batch_size,
|
||||
num_workers=self.num_workers,
|
||||
shuffle=False,
|
||||
)
|
||||
71
ML/Pytorch/pytorch_lightning/6. Restructuring/model.py
Normal file
71
ML/Pytorch/pytorch_lightning/6. Restructuring/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)
|
||||
27
ML/Pytorch/pytorch_lightning/6. Restructuring/train.py
Normal file
27
ML/Pytorch/pytorch_lightning/6. Restructuring/train.py
Normal file
@@ -0,0 +1,27 @@
|
||||
import torch
|
||||
import pytorch_lightning as pl
|
||||
from model import NN
|
||||
from dataset import MnistDataModule
|
||||
import config
|
||||
|
||||
if __name__ == "__main__":
|
||||
model = NN(
|
||||
input_size=config.INPUT_SIZE,
|
||||
learning_rate=config.LEARNING_RATE,
|
||||
num_classes=config.NUM_CLASSES,
|
||||
)
|
||||
dm = MnistDataModule(
|
||||
data_dir=config.DATA_DIR,
|
||||
batch_size=config.BATCH_SIZE,
|
||||
num_workers=config.NUM_WORKERS,
|
||||
)
|
||||
trainer = pl.Trainer(
|
||||
accelerator=config.ACCELERATOR,
|
||||
devices=config.DEVICES,
|
||||
min_epochs=1,
|
||||
max_epochs=3,
|
||||
precision=config.PRECISION,
|
||||
)
|
||||
trainer.fit(model, dm)
|
||||
trainer.validate(model, dm)
|
||||
trainer.test(model, dm)
|
||||
Reference in New Issue
Block a user