You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

nn.py 3.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # coding: utf-8
  2. # ================================================================#
  3. # Copyright (C) 2021 Freecss All rights reserved.
  4. #
  5. # File Name :lenet5.py
  6. # Author :freecss
  7. # Email :karlfreecss@gmail.com
  8. # Created Date :2021/03/03
  9. # Description :
  10. #
  11. # ================================================================#
  12. import torch
  13. import numpy as np
  14. from torch import nn
  15. from torch.nn import functional as F
  16. class LeNet5(nn.Module):
  17. def __init__(self, num_classes=10, image_size=(28, 28)):
  18. super().__init__()
  19. self.conv1 = nn.Conv2d(1, 6, 3, padding=1)
  20. self.conv2 = nn.Conv2d(6, 16, 3)
  21. self.conv3 = nn.Conv2d(16, 16, 3)
  22. feature_map_size = (np.array(image_size) // 2 - 2) // 2 - 2
  23. num_features = 16 * feature_map_size[0] * feature_map_size[1]
  24. self.fc1 = nn.Linear(num_features, 120)
  25. self.fc2 = nn.Linear(120, 84)
  26. self.fc3 = nn.Linear(84, num_classes)
  27. def forward(self, x):
  28. """前向传播函数"""
  29. x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
  30. x = F.max_pool2d(F.relu(self.conv2(x)), (2, 2))
  31. x = F.relu(self.conv3(x))
  32. x = x.view(-1, self.num_flat_features(x))
  33. # print(x.size())
  34. x = F.relu(self.fc1(x))
  35. x = F.relu(self.fc2(x))
  36. x = self.fc3(x)
  37. return x
  38. def num_flat_features(self, x):
  39. size = x.size()[1:]
  40. num_features = 1
  41. for s in size:
  42. num_features *= s
  43. return num_features
  44. class SymbolNet(nn.Module):
  45. def __init__(self, num_classes=4, image_size=(28, 28, 1)):
  46. super(SymbolNet, self).__init__()
  47. self.conv1 = nn.Sequential(
  48. nn.Conv2d(1, 32, 5, stride=1),
  49. nn.ReLU(),
  50. nn.MaxPool2d(kernel_size=2, stride=2),
  51. nn.BatchNorm2d(32, momentum=0.99, eps=0.001),
  52. )
  53. self.conv2 = nn.Sequential(
  54. nn.Conv2d(32, 64, 5, padding=2, stride=1),
  55. nn.ReLU(),
  56. nn.MaxPool2d(kernel_size=2, stride=2),
  57. nn.BatchNorm2d(64, momentum=0.99, eps=0.001),
  58. )
  59. num_features = 64 * (image_size[0] // 4 - 1) * (image_size[1] // 4 - 1)
  60. self.fc1 = nn.Sequential(nn.Linear(num_features, 120), nn.ReLU())
  61. self.fc2 = nn.Sequential(nn.Linear(120, 84), nn.ReLU())
  62. self.fc3 = nn.Sequential(nn.Linear(84, num_classes), nn.Softmax(dim=1))
  63. def forward(self, x):
  64. x = self.conv1(x)
  65. x = self.conv2(x)
  66. x = torch.flatten(x, 1)
  67. x = self.fc1(x)
  68. x = self.fc2(x)
  69. x = self.fc3(x)
  70. return x
  71. class SymbolNetAutoencoder(nn.Module):
  72. def __init__(self, num_classes=4, image_size=(28, 28, 1)):
  73. super(SymbolNetAutoencoder, self).__init__()
  74. self.base_model = SymbolNet(num_classes, image_size)
  75. self.softmax = nn.Softmax(dim=1)
  76. self.fc1 = nn.Sequential(nn.Linear(num_classes, 100), nn.ReLU())
  77. self.fc2 = nn.Sequential(
  78. nn.Linear(100, image_size[0] * image_size[1]), nn.ReLU()
  79. )
  80. def forward(self, x):
  81. x = self.base_model(x)
  82. # x = self.softmax(x)
  83. x = self.fc1(x)
  84. x = self.fc2(x)
  85. return x

An efficient Python toolkit for Abductive Learning (ABL), a novel paradigm that integrates machine learning and logical reasoning in a unified framework.