Loading Assignments/8_1_template_assignment.ipynb +2 −2 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 numpy as np import pandas def init_layers(): # TODO: Implement this function return 0 def activation_function(): # TODO: Implement this function return 0 def activation_derivative(): # TODO: Implement this function return 0 def single_layer_forward_propagation(): # TODO: Implement this function return 0 def full_forward_propagation(): # TODO: Implement this function return 0 def get_loss_value(): # TODO: Implement this function return 0 def single_layer_backward_propagation(): # TODO: Implement this function return 0 def full_backward_propagation(): # TODO: Implement this function return 0 def update(): # TODO: Implement this function return 0 def train(): # TODO: Implement this function return 0 def preprocess_dataset(dataset): filtered_dataset = dataset.loc[ (dataset.iloc[:, 0] == "phytophthora-rot") | (dataset.iloc[:, 0] == "alternarialeaf-spot") ] filtered_dataset = filtered_dataset.replace("?", -1) filtered_dataset.iloc[:, 0] = filtered_dataset.iloc[:, 0].astype("category") cat_columns = filtered_dataset.select_dtypes(["category"]).columns filtered_dataset[cat_columns] = filtered_dataset[cat_columns].apply( lambda x: x.cat.codes ) filtered_dataset = filtered_dataset.astype("float64") X = filtered_dataset.iloc[:, 1:] y = filtered_dataset.iloc[:, 0] return X, y # an auxiliary function that converts probability into class def convert_prob_into_class(probs): probs_ = np.copy(probs) probs_[probs_ > 0.5] = 1 probs_[probs_ <= 0.5] = 0 return probs_ def get_accuracy_value(Y_hat, Y): Y_hat_ = convert_prob_into_class(Y_hat) return (Y_hat_ == Y).all(axis=0).mean() if __name__ == "__main__": # Main function of script train_data = pandas.read_csv("/home/jovyan/data/IntroML/Chapter8_data/Assignment_1/soybean-large.data.csv", header=None) train_data = pandas.read_csv("/home/jovyan/work/Chapter8/Chapter8_data/Assignment_1/soybean-large.data.csv", header=None) test_data = pandas.read_csv("/home/jovyan/data/IntroML/Chapter8_data/Assignment_1/soybean-large.test.csv", header=None) test_data = pandas.read_csv("/home/jovyan/work/Chapter8/Chapter8_data/Assignment_1/soybean-large.test.csv", header=None) X_train, y_train = preprocess_dataset(train_data) X_test, y_test = preprocess_dataset(test_data) # TODO: Implement learning and prediction ``` Assignments/8_2_pytorch_lightning_template_assignment.ipynb +2 −2 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) train = pandas.read_csv("/home/jovyan/work/Chapter8/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) test = pandas.read_csv("/home/jovyan/work/Chapter8/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 ``` Assignments/8_2_pytorch_template_assignment.ipynb +2 −2 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 torch import torch.nn as nn from sklearn.metrics import accuracy_score from torch.autograd import Variable class Model(nn.Module): def __init__(self, input_features, hidden_layer1, hidden_layer2, output_features): super().__init__() # TODO: Implement this function def forward(self, x): # 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] return X, y 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) train = pandas.read_csv("/home/jovyan/work/Chapter8/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) test = pandas.read_csv("/home/jovyan/work/Chapter8/Chapter8_data/Assignment_1/soybean-large.test.csv", header=None) X_train, y_train = preprocess_dataset(train) X_test, y_test = preprocess_dataset(test) # TODO: Implement learning and prediction ``` Assignments/8_4_custom_cnn_template_assignment.ipynb +2 −2 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 pytorch_lightning as pl import torch import torch.nn as nn import torch.nn.functional as F import torchvision as torchvision from pytorch_lightning.callbacks import EarlyStopping from sklearn.metrics import accuracy_score from torch.utils.data import DataLoader, random_split class PLANTSCNN(pl.LightningModule): def __init__(self): super(PLANTSCNN, 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): avg_loss = torch.stack([x["test_loss"] for x in outputs]).mean() avg_test_acc = torch.stack([x["test_acc"] for x in outputs]).mean() logs = {"test_loss": avg_loss, "test_acc": avg_test_acc} results = { "avg_test_loss": avg_loss, "avg_test_acc": avg_test_acc, "log": logs, "progress_bar": logs, } self.log_dict(results) return results if __name__ == "__main__": # Main function of script # TODO: Implement data loading, learning and prediction # data path: # train "/home/jovyan/data/IntroML/Chapter8_data/Assignment_4/train_crop_images" # test "/home/jovyan/data/IntroML/Chapter8_data/Assignment_4/test_crop_images" # train "/home/jovyan/work/Chapter8/Chapter8_data/Assignment_4/train_crop_images" # test "/home/jovyan/work/Chapter8/Chapter8_data/Assignment_4/test_crop_images" ``` Assignments/8_4_transfer_template_assignment.ipynb +2 −2 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 pytorch_lightning as pl import torch import torch.nn as nn import torchvision as torchvision import torchvision.models as models from pytorch_lightning.callbacks import EarlyStopping from sklearn.metrics import accuracy_score from torch.utils.data import DataLoader, random_split class TransferCNN(pl.LightningModule): def __init__(self): super(TransferCNN, 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): avg_loss = torch.stack([x["test_loss"] for x in outputs]).mean() avg_test_acc = torch.stack([x["test_acc"] for x in outputs]).mean() logs = {"test_loss": avg_loss, "test_acc": avg_test_acc} results = { "avg_test_loss": avg_loss, "avg_test_acc": avg_test_acc, "log": logs, "progress_bar": logs, } self.log_dict(results) return results if __name__ == "__main__": # Main function of script # TODO: Implement data loading, learning and prediction # data path: # train "/home/jovyan/data/IntroML/Chapter8_data/Assignment_4/train_crop_images" # test "/home/jovyan/data/IntroML/Chapter8_data/Assignment_4/test_crop_images" # train "/home/jovyan/work/Chapter8/Chapter8_data/Assignment_4/train_crop_images" # test "/home/jovyan/work/Chapter8/Chapter8_data/Assignment_4/test_crop_images" ``` Loading
Assignments/8_1_template_assignment.ipynb +2 −2 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 numpy as np import pandas def init_layers(): # TODO: Implement this function return 0 def activation_function(): # TODO: Implement this function return 0 def activation_derivative(): # TODO: Implement this function return 0 def single_layer_forward_propagation(): # TODO: Implement this function return 0 def full_forward_propagation(): # TODO: Implement this function return 0 def get_loss_value(): # TODO: Implement this function return 0 def single_layer_backward_propagation(): # TODO: Implement this function return 0 def full_backward_propagation(): # TODO: Implement this function return 0 def update(): # TODO: Implement this function return 0 def train(): # TODO: Implement this function return 0 def preprocess_dataset(dataset): filtered_dataset = dataset.loc[ (dataset.iloc[:, 0] == "phytophthora-rot") | (dataset.iloc[:, 0] == "alternarialeaf-spot") ] filtered_dataset = filtered_dataset.replace("?", -1) filtered_dataset.iloc[:, 0] = filtered_dataset.iloc[:, 0].astype("category") cat_columns = filtered_dataset.select_dtypes(["category"]).columns filtered_dataset[cat_columns] = filtered_dataset[cat_columns].apply( lambda x: x.cat.codes ) filtered_dataset = filtered_dataset.astype("float64") X = filtered_dataset.iloc[:, 1:] y = filtered_dataset.iloc[:, 0] return X, y # an auxiliary function that converts probability into class def convert_prob_into_class(probs): probs_ = np.copy(probs) probs_[probs_ > 0.5] = 1 probs_[probs_ <= 0.5] = 0 return probs_ def get_accuracy_value(Y_hat, Y): Y_hat_ = convert_prob_into_class(Y_hat) return (Y_hat_ == Y).all(axis=0).mean() if __name__ == "__main__": # Main function of script train_data = pandas.read_csv("/home/jovyan/data/IntroML/Chapter8_data/Assignment_1/soybean-large.data.csv", header=None) train_data = pandas.read_csv("/home/jovyan/work/Chapter8/Chapter8_data/Assignment_1/soybean-large.data.csv", header=None) test_data = pandas.read_csv("/home/jovyan/data/IntroML/Chapter8_data/Assignment_1/soybean-large.test.csv", header=None) test_data = pandas.read_csv("/home/jovyan/work/Chapter8/Chapter8_data/Assignment_1/soybean-large.test.csv", header=None) X_train, y_train = preprocess_dataset(train_data) X_test, y_test = preprocess_dataset(test_data) # TODO: Implement learning and prediction ```
Assignments/8_2_pytorch_lightning_template_assignment.ipynb +2 −2 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) train = pandas.read_csv("/home/jovyan/work/Chapter8/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) test = pandas.read_csv("/home/jovyan/work/Chapter8/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 ```
Assignments/8_2_pytorch_template_assignment.ipynb +2 −2 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 torch import torch.nn as nn from sklearn.metrics import accuracy_score from torch.autograd import Variable class Model(nn.Module): def __init__(self, input_features, hidden_layer1, hidden_layer2, output_features): super().__init__() # TODO: Implement this function def forward(self, x): # 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] return X, y 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) train = pandas.read_csv("/home/jovyan/work/Chapter8/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) test = pandas.read_csv("/home/jovyan/work/Chapter8/Chapter8_data/Assignment_1/soybean-large.test.csv", header=None) X_train, y_train = preprocess_dataset(train) X_test, y_test = preprocess_dataset(test) # TODO: Implement learning and prediction ```
Assignments/8_4_custom_cnn_template_assignment.ipynb +2 −2 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 pytorch_lightning as pl import torch import torch.nn as nn import torch.nn.functional as F import torchvision as torchvision from pytorch_lightning.callbacks import EarlyStopping from sklearn.metrics import accuracy_score from torch.utils.data import DataLoader, random_split class PLANTSCNN(pl.LightningModule): def __init__(self): super(PLANTSCNN, 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): avg_loss = torch.stack([x["test_loss"] for x in outputs]).mean() avg_test_acc = torch.stack([x["test_acc"] for x in outputs]).mean() logs = {"test_loss": avg_loss, "test_acc": avg_test_acc} results = { "avg_test_loss": avg_loss, "avg_test_acc": avg_test_acc, "log": logs, "progress_bar": logs, } self.log_dict(results) return results if __name__ == "__main__": # Main function of script # TODO: Implement data loading, learning and prediction # data path: # train "/home/jovyan/data/IntroML/Chapter8_data/Assignment_4/train_crop_images" # test "/home/jovyan/data/IntroML/Chapter8_data/Assignment_4/test_crop_images" # train "/home/jovyan/work/Chapter8/Chapter8_data/Assignment_4/train_crop_images" # test "/home/jovyan/work/Chapter8/Chapter8_data/Assignment_4/test_crop_images" ```
Assignments/8_4_transfer_template_assignment.ipynb +2 −2 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 pytorch_lightning as pl import torch import torch.nn as nn import torchvision as torchvision import torchvision.models as models from pytorch_lightning.callbacks import EarlyStopping from sklearn.metrics import accuracy_score from torch.utils.data import DataLoader, random_split class TransferCNN(pl.LightningModule): def __init__(self): super(TransferCNN, 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): avg_loss = torch.stack([x["test_loss"] for x in outputs]).mean() avg_test_acc = torch.stack([x["test_acc"] for x in outputs]).mean() logs = {"test_loss": avg_loss, "test_acc": avg_test_acc} results = { "avg_test_loss": avg_loss, "avg_test_acc": avg_test_acc, "log": logs, "progress_bar": logs, } self.log_dict(results) return results if __name__ == "__main__": # Main function of script # TODO: Implement data loading, learning and prediction # data path: # train "/home/jovyan/data/IntroML/Chapter8_data/Assignment_4/train_crop_images" # test "/home/jovyan/data/IntroML/Chapter8_data/Assignment_4/test_crop_images" # train "/home/jovyan/work/Chapter8/Chapter8_data/Assignment_4/train_crop_images" # test "/home/jovyan/work/Chapter8/Chapter8_data/Assignment_4/test_crop_images" ```