forked from songyingxin/TextClassification
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
executable file
·86 lines (66 loc) · 2.37 KB
/
models.py
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
import torch
import torch.nn as nn
import torch.nn.functional as F
# Logistic Regression 二分类
class LogisticRegressionBinary(nn.Module):
def __init__(self, config):
super(LogicRegression, self).__init__()
self.LR = nn.Linear(config.input_size, config.output_size)
def forward(self, x):
out = self.LR(x)
out = torch.sigmoid(out)
return out
class LogisticRegressionMulti(nn.Module):
def __init__(self, config):
super(LogisticRegressionMulti, self).__init__()
self.config = config
self.LR = nn.Linear(config.input_size, config.output_size)
def forward(self, x):
x = x.reshape(-1, self.config.input_size)
return self.LR(x)
class LinearRegression(nn.Module):
def __init__(self, config):
super(LinearRegression, self).__init__()
self.LR = nn.Linear(config.input_size, config.output_size)
def forward(self, x):
out = self.LR(x)
return out
# 前馈神经网络, 一个隐层
class FNN(nn.Module):
def __init__(self, config):
super(FNN, self).__init__()
self.config = config
self.input_layer = nn.Linear(config.input_size, config.hidden_size)
self.relu = nn.ReLU()
self.hidden_layer = nn.Linear(config.hidden_size, config.hidden_size)
self.output_layer = nn.Linear(config.hidden_size, config.output_size)
def forward(self, x):
x = x.reshape(-1, self.config.input_size)
out = self.input_layer(x)
out = self.relu(out)
out = self.hidden_layer(out)
out = self.relu(out)
out = self.output_layer(out)
return out
class CNN(nn.Module):
def __init__(self):
super(CNN, self).__init__()
self.conv1 = nn.Conv2d(1, 6, 5)
self.conv2 = nn.Conv2d(6, 16, 3)
self.fc1 = nn.Linear(16*5*5, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
def forward(self, x):
x = F.max_pool2d(F.relu(self.conv1(x)), (2,2))
x = F.max_pool2d(F.relu(self.conv2(x)), 2)
x = x.view(-1, self.num_flat_features(x))
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
def num_flat_features(self, x):
size = x.size()[1:]
num_features = 1
for s in size:
num_features *= s
return num_features