Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

model.py 1.3 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
  1. # -*- coding: utf-8 -*-
  2. ########################################################################
  3. # 2. Define a Convolutional Neural Network
  4. # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  5. # Copy the neural network from the Neural Networks section before and modify it to
  6. # take 3-channel images (instead of 1-channel images as it was defined).
  7. import torch.nn as nn
  8. import torch.nn.functional as F
  9. class Net(nn.Module):
  10. def __init__(self):
  11. super(Net, self).__init__()
  12. self.conv1 = nn.Conv2d(3, 6, 5)
  13. self.pool = nn.MaxPool2d(2, 2)
  14. self.conv2 = nn.Conv2d(6, 16, 5)
  15. self.fc1 = nn.Linear(16 * 5 * 5, 120)
  16. self.fc2 = nn.Linear(120, 84)
  17. self.fc3 = nn.Linear(84, 10)
  18. def forward(self, x):
  19. x = self.pool(F.relu(self.conv1(x)))
  20. x = self.pool(F.relu(self.conv2(x)))
  21. x = x.view(-1, 16 * 5 * 5)
  22. x = F.relu(self.fc1(x))
  23. x = F.relu(self.fc2(x))
  24. x = self.fc3(x)
  25. return x
  26. net = Net()
  27. ########################################################################
  28. # 3. Define a Loss function and optimizer
  29. # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  30. # Let's use a Classification Cross-Entropy loss and SGD with momentum.
  31. import torch.optim as optim
  32. criterion = nn.CrossEntropyLoss()
  33. optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...