xls.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. @Copyright (C) ansjer cop Video Technology Co.,Ltd.All rights reserved.
  5. @AUTHOR: ASJRD018
  6. @NAME: AnsjerFormal
  7. @software: PyCharm
  8. @DATE: 2019/4/1 9:41
  9. @Version: python3.6
  10. @MODIFY DECORD:ansjer dev
  11. @file: xls.py
  12. @Contact: chanjunkai@163.com
  13. """
  14. import torch
  15. from torch.autograd import Variable
  16. import torch.nn as nn
  17. import torch.nn.functional as F
  18. class Net(nn.Module):
  19. def __init__(self):
  20. super(Net, self).__init__()
  21. # 1 input image channel, 6 output channels, 5*5 square convolution
  22. # kernel
  23. self.conv1 = nn.Conv2d(1, 6, 5)
  24. self.conv2 = nn.Conv2d(6, 16, 5)
  25. # an affine operation: y = Wx + b
  26. self.fc1 = nn.Linear(16 * 5 * 5, 120)
  27. self.fc2 = nn.Linear(120, 84)
  28. self.fc3 = nn.Linear(84, 10)
  29. def forward(self, x):
  30. # max pooling over a (2, 2) window
  31. x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
  32. # If size is a square you can only specify a single number
  33. x = F.max_pool2d(F.relu(self.conv2(x)), 2)
  34. x = x.view(-1, self.num_flat_features(x))
  35. x = F.relu(self.fc1(x))
  36. x = F.relu(self.fc2(x))
  37. x = self.fc3(x)
  38. return x
  39. def num_flat_features(self, x):
  40. size = x.size()[1:] # all dimensions except the batch dimension
  41. num_features = 1
  42. for s in size:
  43. num_features *= s
  44. return num_features
  45. net = Net()
  46. print(net)