-
Notifications
You must be signed in to change notification settings - Fork 0
/
trainTunnleWorld.py
215 lines (154 loc) · 6.55 KB
/
trainTunnleWorld.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
#Author:ike yang
import sys
sys.path.append(r"c:\users\user\anaconda3\lib\site-packages")
import torch
from torch.optim.lr_scheduler import StepLR, ReduceLROnPlateau
import torch.optim as optim
import torch.nn as nn
from model import LinearModel
import torch.nn.functional as F
import pickle
from torch.utils.data import Dataset
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
import numpy as np
def MAPELoss(output, target):
return torch.mean(torch.abs((target - output) / (target+1e-12)))
def trainLinear(maxiter=20,wp=(6*5,6*1),lamuda=0.00,printOut=True):
class SCADADataset(Dataset):
# +jf33N train together
def __init__(self, name):
filename = 'dataTW10'+name
with open(filename, 'rb') as f:
self.dataH,self.dataT= pickle.load(f)
def __len__(self):
return self.dataH.shape[0]
def __getitem__(self, idx):
x = np.copy(self.dataH[idx,:])
x = torch.from_numpy(x).float()
y = np.copy(self.dataT[idx])
y = torch.from_numpy(y).float()
return x,y
windL, predL = wp
(inputD,outD)=(windL,predL)
batch_size = 512
lr = 2e-4
weight_decay=0.0000
print(' lr: ',lr,' weight_decay: ',weight_decay,' windL: ',windL,' predL: ',predL,
' batch_size: ',batch_size,' inputD: ',inputD,' outD: ',outD,' lamuda:',lamuda)
epochs = maxiter
start_epoch = 0
loadModel = False
outf = r'C:\YANG Luoxiao\Model\WindSpeed'
model=LinearModel(inputD,outD).to(device)
optimizer = optim.Adam(list(model.parameters()), lr=lr,weight_decay=weight_decay)
scheduler = ReduceLROnPlateau(optimizer, 'min', patience=20, verbose=True)
minloss = 10
# if loadModel:
# checkpoint = torch.load('%s/%s%d.pth' % (outf, "LSTMMutiTS4Best", num)) # largeNew5 Large5
# model.load_state_dict(checkpoint['model'])
# # D.load_state_dict(checkpoint['D'])
# optimizer.load_state_dict(checkpoint['optimizer'])
# # optimizerD.load_state_dict(checkpoint['optimizerD'])
# start_epoch = num
scadaTrainDataset = SCADADataset( name='Train')
dataloader = torch.utils.data.DataLoader(scadaTrainDataset, batch_size=batch_size,
shuffle=True, num_workers=int(0))
scadaValDataset = SCADADataset(name='Val')
dataloaderVAl = torch.utils.data.DataLoader(scadaValDataset, batch_size=1024,
shuffle=True, num_workers=int(0))
lossTrain=np.zeros(([1,1]))
lossVal=np.zeros(([1,1]))
acc=np.zeros(([1,1]))
for epoch in range(start_epoch, start_epoch + epochs):
model.train()
for i, (x,y) in enumerate(dataloader):
optimizer.zero_grad()
# y = y.to(torch.long)
x = x.to(device).view(x.shape[0],-1)
y = y.to(device=device, dtype=torch.int64).view(-1)
ypred=model(x)
# y=y.long()
# l1loss=l1Norm(model)
# loss=F.mse_loss(tgt_y* 25.55 + 0.4, tgtpred* 25.55 + 0.4)
loss=F.cross_entropy(ypred, y)
loss.backward()
lossTrain=np.vstack((lossTrain,loss.detach().cpu().numpy().reshape((-1,1))))
optimizer.step()
model.eval()
c = 0
loss = 0
accucry = 0
with torch.no_grad():
for p, (x, y) in enumerate(dataloaderVAl):
# if p>10:
# break
c += 1
x = x.to(device).view(x.shape[0], -1)
y = y.to(device=device, dtype=torch.int64).view(-1)
ypred = model(x)
loss += F.cross_entropy(ypred, y)
predict = torch.argmax(ypred, dim=1)
accucry += torch.sum(predict == y)
lengA = len(scadaValDataset)
accucry = accucry.cpu().numpy()
accucry = accucry / lengA
lossVal = np.vstack((lossVal, (loss/c).cpu().numpy().reshape((-1, 1))))
acc = np.vstack((acc, accucry.reshape((-1, 1))))
model.train()
#
# break
if printOut:
if (i) % 2000 == 0:
print('[%d/%d][%d/%d]\tLoss: %.4f\t '
% (epoch, start_epoch + epochs, i, len(dataloader), loss))
model.eval()
c=0
loss = 0
accucry=0
with torch.no_grad():
for l, (x,y) in enumerate(dataloaderVAl):
c += 1
x = x.to(device).view(x.shape[0], -1)
y = y.to(device=device, dtype=torch.int64).view(-1)
ypred = model(x)
loss += F.cross_entropy(ypred, y)
# lossVal = np.vstack((lossVal, F.cross_entropy(ypred, y).cpu().numpy().reshape((-1, 1))))
predict=torch.argmax(ypred,dim=1)
accucry+=torch.sum(predict==y)
lengA=len(scadaValDataset)
accucry=accucry.cpu().numpy()
accucry=accucry/lengA
if printOut:
print('VAL loss= ', loss / c, ' VAL accucry ', accucry)
scheduler.step(loss / c)
if minloss > (loss / c):
state = {'model': model.state_dict(),'optimizer': optimizer.state_dict(),
'epoch': epoch}
if lamuda==0:
torch.save(state, '%s/TW10RLLinearWT%d.pth' % (outf, int(predL / 6)))
else:
torch.save(state, '%s/RLlassoWT%d.pth' % (outf, int(predL / 6)))
minloss = loss / c
# minmapeloss=lossm/ c
if printOut:
print('bestaccucry: ', accucry)
return lossTrain[1:,:],lossVal[1:,:],acc[1:,:]
# linear
# print('Linear')
# lossT,lossV,acc=trainLinear(maxiter=100,wp=(10*2,4),lamuda=0.00,printOut=True)
# with open('10TWRes','wb') as f:
# pickle.dump((lossT,lossV,acc),f)
with open('10TWRes','rb') as f:
lossT,lossV,acc=pickle.load(f)
import matplotlib.pyplot as plt
f, axs = plt.subplots(2, 1)
# axs[0].set_xlabel('MiniBatch')
axs[0].set_ylabel('Loss')
axs[0].plot(lossT,label='Train')
axs[0].plot(lossV,label='Validation')
axs[0].legend()
axs[1].set_xlabel('MiniBatch')
axs[1].set_ylabel('Accuracy')
axs[1].plot(acc)
plt.show()
# f.savefig('10TW.png', dpi=600, bbox_inches='tight')