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

train.py 4.5 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
  1. # -*- coding: utf-8 -*-
  2. from download import trainset, trainloader, classes
  3. from model import net,criterion,optimizer
  4. import torch
  5. import os
  6. ########################################################################
  7. # 4. Train the network
  8. # ^^^^^^^^^^^^^^^^^^^^
  9. #
  10. # This is when things start to get interesting.
  11. # We simply have to loop over our data iterator, and feed the inputs to the
  12. # network and optimize.
  13. # get some random training images
  14. dataiter = iter(trainloader)
  15. images, labels = dataiter.next()
  16. if os.path.exists('./weights'):
  17. net.load_state_dict(torch.load('./weights'))
  18. net.train()
  19. for epoch in range(1): # loop over the dataset multiple times
  20. running_loss = 0.0
  21. for i, data in enumerate(trainloader, 0):
  22. # get the inputs
  23. inputs, labels = data
  24. # zero the parameter gradients
  25. optimizer.zero_grad()
  26. # forward + backward + optimize
  27. outputs = net(inputs)
  28. loss = criterion(outputs, labels)
  29. loss.backward()
  30. optimizer.step()
  31. # print statistics
  32. running_loss += loss.item()
  33. if i % 2000 == 1999: # print every 2000 mini-batches
  34. print('[%d, %5d] loss: %.3f' %
  35. (epoch + 1, i + 1, running_loss / 2000))
  36. running_loss = 0.0
  37. print('Finished Training!!!!')
  38. for param_tensor in net.state_dict():
  39. print(param_tensor, "\t", net.state_dict()[param_tensor].size())
  40. print("Optimizer's state_dict:")
  41. for var_name in optimizer.state_dict():
  42. print(var_name, "\t", optimizer.state_dict()[var_name])
  43. print('Saving weights...')
  44. torch.save(net.state_dict(), './weights')
  45. #
  46. # ########################################################################
  47. # # 5. Test the network on the test data
  48. # # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  49. # #
  50. # # We have trained the network for 2 passes over the training dataset.
  51. # # But we need to check if the network has learnt anything at all.
  52. # #
  53. # # We will check this by predicting the class label that the neural network
  54. # # outputs, and checking it against the ground-truth. If the prediction is
  55. # # correct, we add the sample to the list of correct predictions.
  56. # #
  57. # # Okay, first step. Let us display an image from the test set to get familiar.
  58. #
  59. # dataiter = iter(testloader)
  60. # images, labels = dataiter.next()
  61. #
  62. # # print images
  63. # imshow(torchvision.utils.make_grid(images))
  64. # print('GroundTruth: ', ' '.join('%5s' % classes[labels[j]] for j in range(4)))
  65. #
  66. # ########################################################################
  67. # # Okay, now let us see what the neural network thinks these examples above are:
  68. #
  69. # outputs = net(images)
  70. #
  71. # ########################################################################
  72. # # The outputs are energies for the 10 classes.
  73. # # The higher the energy for a class, the more the network
  74. # # thinks that the image is of the particular class.
  75. # # So, let's get the index of the highest energy:
  76. # _, predicted = torch.max(outputs, 1)
  77. #
  78. # print('Predicted: ', ' '.join('%5s' % classes[predicted[j]]
  79. # for j in range(4)))
  80. #
  81. # ########################################################################
  82. # # The results seem pretty good.
  83. # #
  84. # # Let us look at how the network performs on the whole dataset.
  85. #
  86. # correct = 0
  87. # total = 0
  88. # with torch.no_grad():
  89. # for data in testloader:
  90. # images, labels = data
  91. # outputs = net(images)
  92. # _, predicted = torch.max(outputs.data, 1)
  93. # total += labels.size(0)
  94. # correct += (predicted == labels).sum().item()
  95. #
  96. # print('Accuracy of the network on the 10000 test images: %d %%' % (
  97. # 100 * correct / total))
  98. #
  99. # ########################################################################
  100. # # That looks waaay better than chance, which is 10% accuracy (randomly picking
  101. # # a class out of 10 classes).
  102. # # Seems like the network learnt something.
  103. # #
  104. # # Hmmm, what are the classes that performed well, and the classes that did
  105. # # not perform well:
  106. #
  107. # class_correct = list(0. for i in range(10))
  108. # class_total = list(0. for i in range(10))
  109. # with torch.no_grad():
  110. # for data in testloader:
  111. # images, labels = data
  112. # outputs = net(images)
  113. # _, predicted = torch.max(outputs, 1)
  114. # c = (predicted == labels).squeeze()
  115. # for i in range(4):
  116. # label = labels[i]
  117. # class_correct[label] += c[i].item()
  118. # class_total[label] += 1
  119. #
  120. #
  121. # for i in range(10):
  122. # print('Accuracy of %5s : %2d %%' % (
  123. # classes[i], 100 * class_correct[i] / class_total[i]))
Tip!

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

Comments

Loading...