Loading Assignments/8_2_pytorch_lightning_template_assignment.ipynb 0 → 100644 +92 −0 Original line number Diff line number Diff line %% Cell type:code id: tags: ``` python # You are encouraged to follow this template as a guide, but feel free to make reasonable modifications as needed. # The key requirement is that your code runs successfully and produces the expected results. ``` %% Cell type:code id: tags: ``` python # NOTE: # You may choose to use ChatGPT (or any AI-based tool) to assist with your assignment, # but you must ensure that you fully understand the entire code. # You are solely responsible for the work you submit. # Please keep in mind: ChatGPT will not be available during the exam. ``` %% Cell type:code id: tags: ``` python import pandas import pytorch_lightning as pl import torch import torch.nn as nn from pytorch_lightning.callbacks import EarlyStopping from sklearn.metrics import accuracy_score from torch.utils.data import DataLoader, TensorDataset, random_split class SoybeanDiseaseClassification(pl.LightningModule): def __init__(self): super(SoybeanDiseaseClassification, self).__init__() # TODO: Implement this function def forward(self, x): # TODO: Implement this function return 0 def configure_optimizers(self): # TODO: Implement this function return 0 def training_step(self, batch, batch_idx): # TODO: Implement this function return 0 def validation_step(self, batch, batch_idx): # TODO: Implement this function return 0 def test_step(self, batch, batch_idx): # TODO: Implement this function return 0 def test_epoch_end(self, outputs): # TODO: Implement this function return 0 def preprocess_dataset(dataset): dataset = dataset.replace("?", -1) dataset.iloc[:, 0] = dataset.iloc[:, 0].astype("category") cat_columns = dataset.select_dtypes(["category"]).columns dataset[cat_columns] = dataset[cat_columns].apply(lambda x: x.cat.codes) dataset = dataset.astype("float64") X = dataset.iloc[:, 1:] y = dataset.iloc[:, 0] data = torch.Tensor(X.to_numpy()).float() labels = torch.Tensor(y.to_numpy()).long() dataset = TensorDataset(data, labels) return dataset if __name__ == "__main__": # Main function of script train = pandas.read_csv("/home/jovyan/data/IntroML/Chapter8_data/Assignment_1/soybean-large.data.csv", header=None) test = pandas.read_csv("/home/jovyan/data/IntroML/Chapter8_data/Assignment_1/soybean-large.test.csv", header=None) train_set = preprocess_dataset(train) train_set, val_set = random_split(train_set, [267, 40]) test_set = preprocess_dataset(test) # TODO: Implement data loader, learning and prediction ``` Loading
Assignments/8_2_pytorch_lightning_template_assignment.ipynb 0 → 100644 +92 −0 Original line number Diff line number Diff line %% Cell type:code id: tags: ``` python # You are encouraged to follow this template as a guide, but feel free to make reasonable modifications as needed. # The key requirement is that your code runs successfully and produces the expected results. ``` %% Cell type:code id: tags: ``` python # NOTE: # You may choose to use ChatGPT (or any AI-based tool) to assist with your assignment, # but you must ensure that you fully understand the entire code. # You are solely responsible for the work you submit. # Please keep in mind: ChatGPT will not be available during the exam. ``` %% Cell type:code id: tags: ``` python import pandas import pytorch_lightning as pl import torch import torch.nn as nn from pytorch_lightning.callbacks import EarlyStopping from sklearn.metrics import accuracy_score from torch.utils.data import DataLoader, TensorDataset, random_split class SoybeanDiseaseClassification(pl.LightningModule): def __init__(self): super(SoybeanDiseaseClassification, self).__init__() # TODO: Implement this function def forward(self, x): # TODO: Implement this function return 0 def configure_optimizers(self): # TODO: Implement this function return 0 def training_step(self, batch, batch_idx): # TODO: Implement this function return 0 def validation_step(self, batch, batch_idx): # TODO: Implement this function return 0 def test_step(self, batch, batch_idx): # TODO: Implement this function return 0 def test_epoch_end(self, outputs): # TODO: Implement this function return 0 def preprocess_dataset(dataset): dataset = dataset.replace("?", -1) dataset.iloc[:, 0] = dataset.iloc[:, 0].astype("category") cat_columns = dataset.select_dtypes(["category"]).columns dataset[cat_columns] = dataset[cat_columns].apply(lambda x: x.cat.codes) dataset = dataset.astype("float64") X = dataset.iloc[:, 1:] y = dataset.iloc[:, 0] data = torch.Tensor(X.to_numpy()).float() labels = torch.Tensor(y.to_numpy()).long() dataset = TensorDataset(data, labels) return dataset if __name__ == "__main__": # Main function of script train = pandas.read_csv("/home/jovyan/data/IntroML/Chapter8_data/Assignment_1/soybean-large.data.csv", header=None) test = pandas.read_csv("/home/jovyan/data/IntroML/Chapter8_data/Assignment_1/soybean-large.test.csv", header=None) train_set = preprocess_dataset(train) train_set, val_set = random_split(train_set, [267, 40]) test_set = preprocess_dataset(test) # TODO: Implement data loader, learning and prediction ```