-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.py
141 lines (119 loc) · 4.83 KB
/
functions.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
from refs import *
import torch
import torch.nn as nn
import random
import pandas as pd
import numpy as np
import os
def evaluation(tr_y, real_y, bound):
sum = 0
for b in range(len(real_y)):
for i in range(len(real_y[0])): # output timewindow
for j in range(len(real_y[0][0])): # 6 zones * 2 sensors
if abs(tr_y[b][i][j] - real_y[b][i][j]) <= bound:
sum += 1
ratio = sum / (len(real_y) * len(real_y[0]) * len(real_y[0][0])) * 100
return ratio
def evaluation1(tr_y, real_y, bound):
sum = 0
for b in range(len(real_y)):
for j in range(len(real_y[0][0])): # 6 zones * 2 sensors
if abs(tr_y[b][-1][j] - real_y[b][-1][j]) <= bound:
sum += 1
ratio = sum / (len(real_y) * len(real_y[0][0])) * 100
return ratio
def seed_torch(seed=1):
random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
np.random.seed(seed)
torch.manual_seed(seed) # 设置 cpu 的随机数种子
torch.cuda.manual_seed(seed) # 对于单张显卡,设置 gpu 的随机数种子
# torch.cuda.manual_seed_all(seed) # 对于多张显卡,设置所有 gpu 的随机数种子
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
def input_encode(data):
res = torch.zeros(timewindow-1,48)
for n in range(timewindow-1):
for i in range(6):
res[n,8*i+int(data[6*i+1])] = 1
res[n,8*i+4] = data[n, 6*i+2] / 10
res[n,8*i+5] = (data[n, 6*i+3] - indoor_t_lower) / (indoor_t_upper - indoor_t_lower)
res[n,8*i+6] = (data[n, 6*i+4] - indoor_t_lower) / (indoor_t_upper - indoor_t_lower)
res[n,8*i+7] = (data[n, 6*i+5] - outdoor_t_lower) / (outdoor_t_upper - outdoor_t_lower)
return res
def input_encode_new(data):
res = torch.zeros(timewindow-1,30)
for n in range(timewindow-1):
for i in range(6):
res[n,5*i] = data[n, 5*i] / 3
res[n,5*i+1] = data[n, 5*i+1] / 10
res[n,5*i+2] = (data[n, 5*i+2] - indoor_t_lower) / (indoor_t_upper - indoor_t_lower)
res[n,5*i+3] = (data[n, 5*i+3] - indoor_t_lower) / (indoor_t_upper - indoor_t_lower)
res[n,5*i+4] = (data[n, 5*i+4] - outdoor_t_lower) / (outdoor_t_upper - outdoor_t_lower)
return res
def output_encode(data):
res = torch.zeros(30, 12)
for n in range(len(data)):
for i in range(6):
res[n,2*i] = (data[n, 2*i] - indoor_t_lower) / (indoor_t_upper - indoor_t_lower)
res[n,2*i+1] = (data[n, 2*i+1] - indoor_t_lower) / (indoor_t_upper - indoor_t_lower)
return res
def input_decode(data):
res = torch.zeros(36)
for d in range(len(data)):
if d % 8 in [0, 1, 2, 3]:
res[int(d/8)*6] = data[d]
elif d % 8 == 4:
res[int(d/8)*6+2] = data[d] * 10
elif d % 8 == 5:
res[int(d/8)*6+3] = data[d]*(indoor_t_upper - indoor_t_lower) + indoor_t_lower
elif d % 8 == 6:
res[int(d/8)*6+4] = data[d]*(indoor_t_upper - indoor_t_lower) + indoor_t_lower
elif d % 8 == 7:
res[int(d/8)*6+5] = data[d]*(outdoor_t_upper - outdoor_t_lower) + outdoor_t_lower
return res
def input_decode_to(data):
res = torch.zeros(12)
for d in range(len(data)):
if d%2 == 1:
res[d] = data[d] * (outdoor_t_upper - outdoor_t_lower) + outdoor_t_lower
return res
def input_decode_new(data):
res = torch.zeros(len(data),len(data[0]))
for r in range(len(data)):
for d in range(len(data[0])):
if d % 5 == 0:
res[r, d] = data[r, d] * 3
elif d % 5 == 1:
res[r,d] = data[r,d] * 10
elif d % 5 == 2:
res[r,d] = data[r,d]*(indoor_t_upper - indoor_t_lower) + indoor_t_lower
elif d % 5 == 3:
res[r,d] = data[r,d]*(indoor_t_upper - indoor_t_lower) + indoor_t_lower
elif d % 5 == 4:
res[r,d] = data[r,d]*(outdoor_t_upper - outdoor_t_lower) + outdoor_t_lower
return res
def output_decode(data):
res = torch.zeros(len(data), 12)
for r in range(len(data)):
for d in range(len(data[0])):
res[r,d] = data[r,d]*(indoor_t_upper - indoor_t_lower) + indoor_t_lower
return res
def output_decode2(data):
res = np.zeros(12)
for d in range(len(data)):
res[d] = data[d] * (indoor_t_upper - indoor_t_lower) + indoor_t_lower
return res
def BATCH_CVRMSE(y_test, y_real):
tot = 0
for b in range(len(y_real)):
total = len(y_real[0]) * len(y_real[0][0])
s = 0
y_ = 0
for i in range(len(y_real)):
for j in range(len(y_real[0])):
s += torch.square(y_test[i,j]-y_real[i,j]) / total
y_ += y_real[i,j] / total
res = pow(s, 0.5) / y_ * 100
tot += torch.sum(res)
return tot