-
Notifications
You must be signed in to change notification settings - Fork 1
/
netTest.py
217 lines (196 loc) · 8.92 KB
/
netTest.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# !/usr/bin/python
# coding: utf8
import torch
#from torch import nn, optim
from torch import nn
import torch.nn.functional as nnFunction
## Define a Net class, inherited from torch.nn.Module
## This net has only one layer
class OneLayer(nn.Module):
"""
Define a simply net class, only one layer
"""
# Net ini
def __init__(self, n_feature, n_hidden, n_output):
# 继承父类的初始化函数
super(OneLayer, self).__init__()
# 网络的隐藏层创建,名称可以随便起
self.hidden_layer = nn.Sequential(nn.Linear(n_feature, n_hidden), nn.ReLU(True))#self.hidden_layer = nn.Linear(n_feature, n_hidden)#
# 输出层(预测层)创建,接收来自隐含层的数据
self.predict_layer = nn.Linear(n_hidden, n_output)
# 网络的传播函数,构造计算图
def forward(self, x):
# 用relu函数处理隐含层输出的结果并传给输出层
#hidden_result = self.hidden_layer(x)
x = self.hidden_layer(x)#relu_result = nnFunction.relu(hidden_result)#非常重要,曲面弯曲
predict_result = self.predict_layer(x)#predict_result = self.predict_layer(relu_result)#
return predict_result
class simpleNet(nn.Module):
"""
定义了一个简单的三层全连接神经网络,每一层都是线性的
"""
# Net类的初始化函数
def __init__(self, in_dim, n_hidden_1, n_hidden_2, out_dim):
super(simpleNet, self).__init__()
self.layer1 = nn.Linear(in_dim, n_hidden_1)
self.layer2 = nn.Linear(n_hidden_1, n_hidden_2)
self.layer3 = nn.Linear(n_hidden_2, out_dim)
def forward(self, x):
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
return x
class Activation_Net(nn.Module):
"""
在上面的simpleNet的基础上,在每层的输出部分添加了激活函数
"""
def __init__(self, in_dim, n_hidden_1, n_hidden_2, out_dim):
super(Activation_Net, self).__init__()
self.layer1 = nn.Sequential(nn.Linear(in_dim, n_hidden_1), nn.ReLU(True))
self.layer2 = nn.Sequential(nn.Linear(n_hidden_1, n_hidden_2), nn.ReLU(True))
self.layer3 = nn.Sequential(nn.Linear(n_hidden_2, out_dim))
"""
这里的Sequential()函数的功能是将网络的层组合到一起。
"""
def forward(self, x):
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
return x
class Batch_Net(nn.Module):
"""
在上面的Activation_Net的基础上,增加了一个加快收敛速度的方法——批标准化
"""
def __init__(self, in_dim, n_hidden_1, n_hidden_2, out_dim):
super(Batch_Net, self).__init__()
self.layer1 = nn.Sequential(nn.Linear(in_dim, n_hidden_1), nn.BatchNorm1d(n_hidden_1), nn.ReLU(True))
self.layer2 = nn.Sequential(nn.Linear(n_hidden_1, n_hidden_2), nn.BatchNorm1d(n_hidden_2), nn.ReLU(True))
self.layer3 = nn.Sequential(nn.Linear(n_hidden_2, out_dim))
def forward(self, x):
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
return x
class Batch_Net_5(nn.Module):
"""
在上面的Activation_Net的基础上,增加了一个加快收敛速度的方法——批标准化
"""
def __init__(self, in_dim, n_hidden_1, n_hidden_2, n_hidden_3, n_hidden_4, n_hidden_5, out_dim):
super(Batch_Net_5, self).__init__()
self.layer1 = nn.Sequential(nn.Linear(in_dim, n_hidden_1), nn.BatchNorm1d(n_hidden_1), nn.ReLU(True))
self.layer2 = nn.Sequential(nn.Linear(n_hidden_1, n_hidden_2), nn.BatchNorm1d(n_hidden_2), nn.ReLU(True))
self.layer3 = nn.Sequential(nn.Linear(n_hidden_2, n_hidden_3), nn.BatchNorm1d(n_hidden_3), nn.ReLU(True))
self.layer4 = nn.Sequential(nn.Linear(n_hidden_3, n_hidden_4), nn.BatchNorm1d(n_hidden_4), nn.ReLU(True))
self.layer5 = nn.Sequential(nn.Linear(n_hidden_4, n_hidden_5), nn.BatchNorm1d(n_hidden_5), nn.ReLU(True))
self.layer6 = nn.Sequential(nn.Linear(n_hidden_5, out_dim))
def forward(self, x):
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.layer5(x)
x = self.layer6(x)
return x
class Batch_Net_5_2(nn.Module):
"""
在上面的Activation_Net的基础上,增加了一个加快收敛速度的方法——批标准化
"""
def __init__(self, in_dim, n_hidden_1, n_hidden_2, n_hidden_3, n_hidden_4, n_hidden_5, out_dim):
super(Batch_Net_5_2, self).__init__()
self.layer1 = nn.Sequential(nn.Linear(in_dim, n_hidden_1), nn.ReLU(True))
self.layer2 = nn.Sequential(nn.Linear(n_hidden_1, n_hidden_2), nn.ReLU(True))
self.layer3 = nn.Sequential(nn.Linear(n_hidden_2, n_hidden_3), nn.ReLU(True))
self.layer4 = nn.Sequential(nn.Linear(n_hidden_3, n_hidden_4), nn.ReLU(True))
self.layer5 = nn.Sequential(nn.Linear(n_hidden_4, n_hidden_5), nn.ReLU(True))
self.layer6 = nn.Sequential(nn.Linear(n_hidden_5, out_dim))
def forward(self, x):
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.layer5(x)
x = self.layer6(x)
return x
class Batch_Net_6(nn.Module):
"""
在上面的Activation_Net_5_2的基础上,增加了一层
"""
def __init__(self, in_dim, n_hidden_1, n_hidden_2, n_hidden_3, n_hidden_4, n_hidden_5, n_hidden_6, out_dim):
super(Batch_Net_6, self).__init__()
self.layer1 = nn.Sequential(nn.Linear(in_dim, n_hidden_1), nn.Tanh())
self.layer2 = nn.Sequential(nn.Linear(n_hidden_1, n_hidden_2), nn.Tanh())
self.layer3 = nn.Sequential(nn.Linear(n_hidden_2, n_hidden_3), nn.Tanh())
self.layer4 = nn.Sequential(nn.Linear(n_hidden_3, n_hidden_4), nn.Tanh())
self.layer5 = nn.Sequential(nn.Linear(n_hidden_4, n_hidden_5), nn.Tanh())
self.layer6 = nn.Sequential(nn.Linear(n_hidden_5, n_hidden_6), nn.Tanh())
self.layer7 = nn.Sequential(nn.Linear(n_hidden_6, out_dim))
def forward(self, x):
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.layer5(x)
x = self.layer6(x)
x = self.layer7(x)
return x
# class Batch_Net_5_3(nn.Module):
# """
# 在上面的Activation_Net的基础上,增加了一个加快收敛速度的方法——批标准化
# """
# def __init__(self, in_dim, n_hidden_1, n_hidden_2, n_hidden_3, n_hidden_4, n_hidden_5, out_dim, bias=False):
# super(Batch_Net_5_3, self).__init__()
# self.layer1 = nn.Linear(in_dim, n_hidden_1, bias=bias)
# #nn.init.kaiming_normal_(self.layer1.weight)
# self.relu1 = nn.ReLU(inplace=True)
# self.layer2 = nn.Linear(in_dim, n_hidden_2, bias=bias)
# #nn.init.kaiming_normal_(self.layer2.weight)
# self.relu2 = nn.ReLU(inplace=True)
# self.layer3 = nn.Linear(n_hidden_2, n_hidden_3, bias=bias)
# #nn.init.kaiming_normal_(self.layer3.weight)
# self.relu3 = nn.ReLU(inplace=True)
# self.layer4 = nn.Linear(n_hidden_3, n_hidden_4, bias=bias)
# #nn.init.kaiming_normal_(self.layer4.weight)
# self.relu4 = nn.ReLU(inplace=True)
# self.layer5 = nn.Linear(n_hidden_4, n_hidden_5, bias=bias)
# #nn.init.kaiming_normal_(self.layer5.weight)
# self.relu5 = nn.ReLU(inplace=True)
# self.layer6 = nn.Linear(n_hidden_5, out_dim, bias=bias)
# #nn.init.kaiming_normal_(self.layer6.weight)
# self.relu6 = nn.ReLU(inplace=True)
# def forward(self, x):
# x = self.layer1(x)
# x = self.layer2(x)
# x = self.layer3(x)
# x = self.layer4(x)
# x = self.layer5(x)
# x = self.layer6(x)
# return x
# Define the loss function here
# the total loss is just the sum of different MSE
# the total loss is actually the sum of different MSE loss.
def NN_UT_help(prediction):
NN_UT = np.zeros((len(prediction), 2))
index = -1
for i in range(len(prediction)):
index += 1
NN_UT[index] = np.array([prediction[index][0], prediction[index][1]])
NN_UT = torch.from_numpy(NN_UT).float()
return NN_UT
def NN_p_help(prediction, out_dim):
# the out_dim should be the dim of p
NN_p = np.zeros((len(prediction), out_dim))
index = -1
for i in range(len(prediction)):
index += 1
for k in range(0, out_dim):
NN_p[index][k] = prediction[index][k+2]
NN_p = torch.from_numpy(NN_p).float()
return NN_p
def NN_p_sum_help(NN_p):
NN_p_sum = np.zeros((len(NN_p), 1))
index = -1
for i in range(len(NN_p)):
index += 1
NN_p_sum[index] = sum(NN_p[index])
NN_p_sum = torch.from_numpy(NN_p_sum).float()
return NN_p_sum