eaglelandsonce commited on
Commit
b101d4e
1 Parent(s): a761b67

Update pages/1_TensorIntro.py

Browse files
Files changed (1) hide show
  1. pages/1_TensorIntro.py +37 -78
pages/1_TensorIntro.py CHANGED
@@ -202,86 +202,45 @@ print("Normalized data:", normalized_data)
202
  '''
203
  },
204
 
205
- "Final Project: Image Classification with a Simple CNN": {
206
- "description": "In this project, you will build and train a simple Convolutional Neural Network (CNN) for image classification using the CIFAR-10 dataset. This involves loading the dataset, defining the CNN model, and training the model to classify images into one of the 10 classes.",
207
  "code": '''import torch
208
- import torch.nn as nn
209
- import torch.optim as optim
210
- import torchvision
211
- import torchvision.transforms as transforms
212
-
213
- # Define the transformation for the dataset
214
- transform = transforms.Compose(
215
- [transforms.ToTensor(),
216
- transforms.Normalize((0.5, 0.5, 0.5))])
217
-
218
- # Load the CIFAR-10 dataset
219
- trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)
220
- trainloader = torch.utils.data.DataLoader(trainset, batch_size=32, shuffle=True, num_workers=2)
221
-
222
- testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=transform)
223
- testloader = torch.utils.data.DataLoader(testset, batch_size=32, shuffle=False, num_workers=2)
224
-
225
- # Define the CNN model
226
- class SimpleCNN(nn.Module):
227
- def __init__(self):
228
- super(SimpleCNN, self).__init__()
229
- self.conv1 = nn.Conv2d(3, 6, 5)
230
- self.pool = nn.MaxPool2d(2, 2)
231
- self.conv2 = nn.Conv2d(6, 16, 5)
232
- self.fc1 = nn.Linear(16 * 5 * 5, 120)
233
- self.fc2 = nn.Linear(120, 84)
234
- self.fc3 = nn.Linear(84, 10)
235
 
236
- def forward(self, x):
237
- x = self.pool(nn.functional.relu(self.conv1(x)))
238
- x = self.pool(nn.functional.relu(self.conv2(x)))
239
- x = x.view(-1, 16 * 5 * 5)
240
- x = nn.functional.relu(self.fc1(x))
241
- x = nn.functional.relu(self.fc2(x))
242
- x = self.fc3(x)
243
- return x
244
-
245
- # Instantiate the model, loss function, and optimizer
246
- net = SimpleCNN()
247
- criterion = nn.CrossEntropyLoss()
248
- optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)
249
-
250
- # Training loop
251
- for epoch in range(5): # loop over the dataset multiple times
252
- running_loss = 0.0
253
- for i, data in enumerate(trainloader, 0):
254
- inputs, labels = data
255
-
256
- optimizer.zero_grad()
257
-
258
- outputs = net(inputs)
259
- loss = criterion(outputs, labels)
260
- loss.backward()
261
- optimizer.step()
262
-
263
- running_loss += loss.item()
264
- if i % 200 == 199: # print every 200 mini-batches
265
- print(f'[{epoch + 1}, {i + 1}] loss: {running_loss / 200:.3f}')
266
- running_loss = 0.0
267
-
268
- print('Finished Training')
269
-
270
- # Save the trained model
271
- torch.save(net.state_dict(), 'simple_cnn.pth')
272
-
273
- # Testing the model
274
- correct = 0
275
- total = 0
276
- with torch.no_grad():
277
- for data in testloader:
278
- images, labels = data
279
- outputs = net(images)
280
- _, predicted = torch.max(outputs.data, 1)
281
- total += labels.size(0)
282
- correct += (predicted == labels).sum().item()
283
-
284
- print(f'Accuracy of the network on the 10000 test images: {100 * correct / total}%')
285
 
286
  '''
287
  },
 
202
  '''
203
  },
204
 
205
+ "Final Project: Basic Tensor Operations": {
206
+ "description": "This project demonstrates the usage of PyTorch tensors through basic mathematical operations such as addition, multiplication, and reshaping. The aim is to help you understand the fundamental operations you can perform with tensors.",
207
  "code": '''import torch
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
208
 
209
+ # Create two tensors
210
+ tensor_a = torch.tensor([[1, 2], [3, 4]])
211
+ tensor_b = torch.tensor([[5, 6], [7, 8]])
212
+
213
+ # Perform basic operations
214
+ # Addition
215
+ add_result = tensor_a + tensor_b
216
+ print("Addition result:")
217
+ print(add_result)
218
+
219
+ # Element-wise multiplication
220
+ mul_result = tensor_a * tensor_b
221
+ print("Element-wise multiplication result:")
222
+ print(mul_result)
223
+
224
+ # Matrix multiplication
225
+ matmul_result = torch.matmul(tensor_a, tensor_b)
226
+ print("Matrix multiplication result:")
227
+ print(matmul_result)
228
+
229
+ # Reshaping tensor_a
230
+ reshaped_tensor = tensor_a.view(4, 1)
231
+ print("Reshaped tensor_a:")
232
+ print(reshaped_tensor)
233
+
234
+ # Slicing tensor_b
235
+ sliced_tensor = tensor_b[:, 1]
236
+ print("Sliced tensor_b (second column):")
237
+ print(sliced_tensor)
238
+
239
+ # Compute the mean of tensor_a
240
+ mean_result = torch.mean(tensor_a.float())
241
+ print("Mean of tensor_a:")
242
+ print(mean_result)
243
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
244
 
245
  '''
246
  },