@@ -18,29 +18,29 @@ APIs are defined to facilitate customization within the workflow
1818** ch02/code2.2.1**
1919``` python
2020import pickle
21- from torch.utils.data import Dataset, DataLoader
22- data_path = ' /path/to/data'
23- dataset = pickle.load(open (data_path, ' rb' )) # Example for a pkl file
24- batch_size = ... # You can make it an argument of the script
25-
26- class CustomDataset (Dataset ):
27- def __init__ (self , data , labels ):
28- self .data = data
29- self .labels = labels
30-
31- def __len__ (self ):
32- return len (self .data)
33-
34- def __getitem__ (self , idx ):
35- sample = self .data[idx]
36- label = self .labels[idx]
37- return sample, label
38-
39- training_dataset = CustomDataset(dataset[' training_data' ], dataset[' training_labels' ])
40- testing_dataset = CustomDataset(dataset[' testing_data' ], dataset[' testing_labels' ])
41-
42- training_dataloader = DataLoader(training_dataset, batch_size = batch_size, shuffle = True ) # Create a training dataloader
43- testing_dataloader = DataLoader(testing_dataset, batch_size = batch_size, shuffle = False ) # Create a testing dataloader
21+ from torch.utils.data import Dataset, DataLoader
22+ data_path = ' /path/to/data'
23+ dataset = pickle.load(open (data_path, ' rb' )) # Example for a pkl file
24+ batch_size = ... # You can make it an argument of the script
25+
26+ class CustomDataset (Dataset ):
27+ def __init__ (self , data , labels ):
28+ self .data = data
29+ self .labels = labels
30+
31+ def __len__ (self ):
32+ return len (self .data)
33+
34+ def __getitem__ (self , idx ):
35+ sample = self .data[idx]
36+ label = self .labels[idx]
37+ return sample, label
38+
39+ training_dataset = CustomDataset(dataset[' training_data' ], dataset[' training_labels' ])
40+ testing_dataset = CustomDataset(dataset[' testing_data' ], dataset[' testing_labels' ])
41+
42+ training_dataloader = DataLoader(training_dataset, batch_size = batch_size, shuffle = True ) # Create a training dataloader
43+ testing_dataloader = DataLoader(testing_dataset, batch_size = batch_size, shuffle = False ) # Create a testing dataloader
4444```
4545
46462 . ** Model Definition API:** Once the data is preprocessed, users need
@@ -53,13 +53,13 @@ import pickle
5353** ch02/code2.2.2**
5454``` python
5555import torch.nn as nn
56- class CustomModel (nn .Module ):
57- def __init__ (self , input_size , output_size ):
58- super (CustomModel, self ).__init__ ()
59- self .linear = nn.Linear(input_size, output_size) # A single linear layer
60-
61- def forward (self , x ):
62- return self .linear(x)
56+ class CustomModel (nn .Module ):
57+ def __init__ (self , input_size , output_size ):
58+ super (CustomModel, self ).__init__ ()
59+ self .linear = nn.Linear(input_size, output_size) # A single linear layer
60+
61+ def forward (self , x ):
62+ return self .linear(x)
6363```
6464
65653 . ** Optimizer Definition API:** The outputs of models need to be
@@ -74,11 +74,11 @@ import torch.nn as nn
7474** ch02/code2.2.3**
7575``` python
7676import torch.optim as optim
77- import torch.nn
78- model = CustomModel(... )
79- # Optimizer definition (Adam, SGD, etc.)
80- optimizer = optim.Adam(model.parameters(), lr = 1e-4 , momentum = 0.9 )
81- loss = nn.CrossEntropyLoss() # Loss function definition
77+ import torch.nn
78+ model = CustomModel(... )
79+ # Optimizer definition (Adam, SGD, etc.)
80+ optimizer = optim.Adam(model.parameters(), lr = 1e-4 , momentum = 0.9 )
81+ loss = nn.CrossEntropyLoss() # Loss function definition
8282```
8383
84844 . ** Training API:** Given a dataset, model, loss function, and
@@ -92,17 +92,17 @@ import torch.optim as optim
9292** ch02/code2.2.4**
9393``` python
9494device = " cuda:0" if torch.cuda.is_available() else " cpu" # Select your training device
95- model.to(device) # Move the model to the training device
96- model.train() # Set the model to train mode
97- epochs = ... # You can make it an argument of the script
98- for epoch in range (epochs):
99- for batch_idx, (data, target) in enumerate (training_dataloader):
100- data, target = data.to(device), target.to(device)
101- optimizer.zero_grad() # zero the parameter gradients
102- output = model(data) # Forward pass
103- loss_value = loss(output, target) # Compute the loss
104- loss_value.backward() # Backpropagation
105- optimizer.step()
95+ model.to(device) # Move the model to the training device
96+ model.train() # Set the model to train mode
97+ epochs = ... # You can make it an argument of the script
98+ for epoch in range (epochs):
99+ for batch_idx, (data, target) in enumerate (training_dataloader):
100+ data, target = data.to(device), target.to(device)
101+ optimizer.zero_grad() # zero the parameter gradients
102+ output = model(data) # Forward pass
103+ loss_value = loss(output, target) # Compute the loss
104+ loss_value.backward() # Backpropagation
105+ optimizer.step()
106106```
107107
1081085 . ** Testing and Debugging APIs:** Throughout the training process,
@@ -116,13 +116,13 @@ device = "cuda:0" if torch.cuda.is_available() else "cpu" # Select your training
116116** ch02/code2.2.5**
117117``` python
118118model.eval() # Set the model to evaluation mode
119- overall_accuracy = []
120- for batch_idx, (data, target) in enumerate (testing_dataloader):
121- data, target = data.to(device), target.to(device)
122- output = model(data) # Forward pass
123- accuracy = your_metrics(output, target) # Compute the accuracy
124- overall_accuracy.append(accuracy) # Print the accuracy
125- # For debugging, you can print logs inside the training or evaluation loop, or use python debugger.
119+ overall_accuracy = []
120+ for batch_idx, (data, target) in enumerate (testing_dataloader):
121+ data, target = data.to(device), target.to(device)
122+ output = model(data) # Forward pass
123+ accuracy = your_metrics(output, target) # Compute the accuracy
124+ overall_accuracy.append(accuracy) # Print the accuracy
125+ # For debugging, you can print logs inside the training or evaluation loop, or use python debugger.
126126```
127127
128128![ Workflow within a machine learningsystem] ( ../img/ch03/workflow.pdf )
0 commit comments