-
Notifications
You must be signed in to change notification settings - Fork 20
/
utils.py
173 lines (141 loc) · 4.7 KB
/
utils.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
import torch
import numpy as np
from sklearn.metrics import confusion_matrix
def get_user_input(args):
if torch.cuda.is_available():
args.device = 'cuda:' + input('Input GPU ID: ')
else:
args.device = 'cpu'
dataset_code = {'r': 'redd_lf', 'u': 'uk_dale'}
args.dataset_code = dataset_code[input(
'Input r for REDD, u for UK_DALE: ')]
if args.dataset_code == 'redd_lf':
app_dict = {
'r': ['refrigerator'],
'w': ['washer_dryer'],
'm': ['microwave'],
'd': ['dishwasher'],
}
args.appliance_names = app_dict[input(
'Input r, w, m or d for target appliance: ')]
elif args.dataset_code == 'uk_dale':
app_dict = {
'k': ['kettle'],
'f': ['fridge'],
'w': ['washing_machine'],
'm': ['microwave'],
'd': ['dishwasher'],
}
args.appliance_names = app_dict[input(
'Input k, f, w, m or d for target appliance: ')]
args.num_epochs = int(input('Input training epochs: '))
def set_template(args):
args.output_size = len(args.appliance_names)
if args.dataset_code == 'redd_lf':
args.window_stride = 120
args.house_indicies = [1, 2, 3, 4, 5, 6]
args.cutoff = {
'aggregate': 6000,
'refrigerator': 400,
'washer_dryer': 3500,
'microwave': 1800,
'dishwasher': 1200
}
args.threshold = {
'refrigerator': 50,
'washer_dryer': 20,
'microwave': 200,
'dishwasher': 10
}
args.min_on = {
'refrigerator': 10,
'washer_dryer': 300,
'microwave': 2,
'dishwasher': 300
}
args.min_off = {
'refrigerator': 2,
'washer_dryer': 26,
'microwave': 5,
'dishwasher': 300
}
args.c0 = {
'refrigerator': 1e-6,
'washer_dryer': 0.001,
'microwave': 1.,
'dishwasher': 1.
}
elif args.dataset_code == 'uk_dale':
args.window_stride = 240
args.house_indicies = [1, 2, 3, 4, 5]
args.cutoff = {
'aggregate': 6000,
'kettle': 3100,
'fridge': 300,
'washing_machine': 2500,
'microwave': 3000,
'dishwasher': 2500
}
args.threshold = {
'kettle': 2000,
'fridge': 50,
'washing_machine': 20,
'microwave': 200,
'dishwasher': 10
}
args.min_on = {
'kettle': 2,
'fridge': 10,
'washing_machine': 300,
'microwave': 2,
'dishwasher': 300
}
args.min_off = {
'kettle': 0,
'fridge': 2,
'washing_machine': 26,
'microwave': 5,
'dishwasher': 300
}
args.c0 = {
'kettle': 1.,
'fridge': 1e-6,
'washing_machine': 0.01,
'microwave': 1.,
'dishwasher': 1.
}
args.optimizer = 'adam'
args.lr = 1e-4
args.enable_lr_schedule = False
args.batch_size = 128
def acc_precision_recall_f1_score(pred, status):
assert pred.shape == status.shape
pred = pred.reshape(-1, pred.shape[-1])
status = status.reshape(-1, status.shape[-1])
accs, precisions, recalls, f1_scores = [], [], [], []
for i in range(status.shape[-1]):
tn, fp, fn, tp = confusion_matrix(status[:, i], pred[:, i], labels=[
0, 1]).ravel()
acc = (tn + tp) / (tn + fp + fn + tp)
precision = tp / np.max((tp + fp, 1e-9))
recall = tp / np.max((tp + fn, 1e-9))
f1_score = 2 * (precision * recall) / \
np.max((precision + recall, 1e-9))
accs.append(acc)
precisions.append(precision)
recalls.append(recall)
f1_scores.append(f1_score)
return np.array(accs), np.array(precisions), np.array(recalls), np.array(f1_scores)
def relative_absolute_error(pred, label):
assert pred.shape == label.shape
pred = pred.reshape(-1, pred.shape[-1])
label = label.reshape(-1, label.shape[-1])
temp = np.full(label.shape, 1e-9)
relative, absolute, sum_err = [], [], []
for i in range(label.shape[-1]):
relative_error = np.mean(np.nan_to_num(np.abs(label[:, i] - pred[:, i]) / np.max(
(label[:, i], pred[:, i], temp[:, i]), axis=0)))
absolute_error = np.mean(np.abs(label[:, i] - pred[:, i]))
relative.append(relative_error)
absolute.append(absolute_error)
return np.array(relative), np.array(absolute)