Spaces:
Runtime error
Runtime error
eaglelandsonce
commited on
Commit
•
b101d4e
1
Parent(s):
a761b67
Update pages/1_TensorIntro.py
Browse files- 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:
|
206 |
-
"description": "
|
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 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
print(
|
269 |
-
|
270 |
-
|
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 |
},
|