-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmodel.py
174 lines (137 loc) · 7.85 KB
/
model.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
'''
* @author [Liang Zhang]
* @email [[email protected]]
Different NN models for PSSE provided in this file
'''
import tensorflow as tf
from keras import optimizers
from keras import regularizers
from keras.models import Model
from keras.layers import Dense, Activation, add, Dropout, Lambda
from keras.layers import Input, average
from keras import backend as K
from keras.layers.normalization import BatchNormalization
def huber_loss(y_true, y_pred, clip_delta=1.0):
error = y_true - y_pred
cond = K.abs(error) < clip_delta
squared_loss = 0.5 * K.square(error)
linear_loss = clip_delta * (K.abs(error) - 0.5 * clip_delta)
return tf.where(cond, squared_loss, linear_loss)
def huber_loss_mean(y_true, y_pred):
return K.mean(huber_loss(y_true, y_pred))
def st_activation(tensor, th = 0.2):
'''Performs the soft thresholding operation, an alternative activation'''
cond = K.abs(tensor) < th
st_tensor = tensor - th*K.sign(tensor)
return tf.where(cond, tf.zeros(tf.shape(tensor)), st_tensor)
def nn1_psse(input_shape, num_classes, weights=None):
'''
:param input_shape:
:param num_classes:
:param weights: 6 layers
:return: estimated voltages
'''
data = Input(shape=input_shape, dtype='float', name='data')
dense1 = Dense(units = input_shape[0], activation='relu', use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros')(data)
dense2 = Dense(units = input_shape[0], activation='relu', use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros')(dense1)
dense3 = Dense(units = input_shape[0], activation='relu', use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros')(dense2)
dense4 = Dense(units = input_shape[0], activation='relu', use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros')(dense3)
dense5 = Dense(units = input_shape[0], activation='relu', use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros')(dense4)
dense6 = Dense(units = input_shape[0], activation='relu', use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros')(dense5)
predictions = Dense(units = num_classes, activation=None, use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros')(dense6)
model = Model(inputs=data, outputs=predictions)
if weights is not None:
model.load_weights(weights)
sgd = optimizers.adam(lr=0.001)
# sgd = optimizers.SGD(lr=0.001, momentum=0.9, nesterov=True)
model.compile(optimizer=sgd, loss=huber_loss_mean,
metrics=['mae'])
return model
def nn1_8H_psse(input_shape, num_classes, weights=None):
'''
:param input_shape:
:param num_classes:
:param weights: 8 layers
:return: estimated voltages
'''
data = Input(shape=input_shape, dtype='float', name='data')
dense1 = Dense(units = input_shape[0], activation='relu', use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros')(data)
dense2 = Dense(units = input_shape[0], activation='relu', use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros')(dense1)
dense3 = Dense(units = input_shape[0], activation='relu', use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros')(dense2)
dense4 = Dense(units = input_shape[0], activation='relu', use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros')(dense3)
dense5 = Dense(units = input_shape[0], activation='relu', use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros')(dense4)
dense6 = Dense(units = input_shape[0], activation='relu', use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros')(dense5)
dense7 = Dense(units = input_shape[0], activation='relu', use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros')(dense6)
dense8 = Dense(units = input_shape[0], activation='relu', use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros')(dense7)
predictions = Dense(units = num_classes, activation=None, use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros')(dense8)
model = Model(inputs=data, outputs=predictions)
if weights is not None:
model.load_weights(weights)
sgd = optimizers.adam(lr=0.001)
# sgd = optimizers.SGD(lr=0.001, momentum=0.9, nesterov=True)
model.compile(optimizer=sgd, loss=huber_loss_mean,
metrics=['mae'])
return model
def lav_psse(input_shape, num_classes, weights=None):
'''
:param input_shape:
:param num_classes:
:param weights:
:return: estimated voltages
'''
data = Input(shape=input_shape, dtype='float', name='data')
merged1 = Dense(units = input_shape[0], activation=None, use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros')(data)
u01 = Activation('relu')(merged1)
dense1 = Dense(units = input_shape[0], activation=None, use_bias=True, kernel_initializer='glorot_uniform')(u01)
add1 = add([merged1, dense1])
u02 = Activation('relu')(add1)
dense2 = Dense(units = input_shape[0], activation=None, use_bias=True, kernel_initializer='glorot_uniform')(u02)
add2 = add([merged1, dense2])
u03 = Activation('relu')(add2)
dense3 = Dense(units = input_shape[0], activation=None, use_bias=True, kernel_initializer='glorot_uniform')(u03)
dense4 = Dense(units = input_shape[0], activation=None, use_bias=True, kernel_initializer='glorot_uniform')(data)
merged2 = add([dense3, dense4])
u11 = Activation('relu')(merged2)
dense5 = Dense(units = input_shape[0], activation=None, use_bias=True, kernel_initializer='glorot_uniform')(u11)
add3 = add([merged2, dense5])
u12 = Activation('relu')(add3)
dense6 = Dense(units = input_shape[0], activation=None, use_bias=True, kernel_initializer='glorot_uniform')(u12)
add4 = add([merged2, dense6])
u13 = Activation('relu')(add4)
dense_o1 = Dense(units = input_shape[0], activation=None, use_bias=True, kernel_initializer='glorot_uniform')(data)
add_o1 = add([u13, dense_o1])
predictions = Dense(units = num_classes, activation=None, use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros')(add_o1)
model = Model(inputs=data, outputs=predictions)
if weights is not None:
model.load_weights(weights)
sgd = optimizers.adam(lr=0.001)
model.compile(optimizer=sgd, loss=huber_loss_mean,
metrics=['mae'])
return model
def st_lav_psse(input_shape, num_classes, weights=None):
'''
soft_max activation
:param input_shape:
:param num_classes:
:param weights:
:return: estimated voltages
'''
data = Input(shape=input_shape, dtype='float', name='data')
merged1 = Dense(units = input_shape[0], activation=None, use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros')(data)
u01 = Lambda(st_activation, name='st0')(merged1)
dense1 = Dense(units = input_shape[0], activation=None, use_bias=True, kernel_initializer='glorot_uniform')(u01)
add1 = add([merged1, dense1])
u02 = Lambda(st_activation, name='st1')(add1)
dense2 = Dense(units = input_shape[0], activation=None, use_bias=True, kernel_initializer='glorot_uniform')(u02)
add2 = add([merged1, dense2])
u03 = Lambda(st_activation, name='st2')(add2)
dense_o1 = Dense(units = input_shape[0], activation=None, use_bias=True, kernel_initializer='glorot_uniform')(data)
add_o1 = add([u03, dense_o1])
predictions = Dense(units = num_classes, activation=None, use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros')(add_o1)
model = Model(inputs=data, outputs=predictions)
if weights is not None:
model.load_weights(weights)
sgd = optimizers.adam(lr=0.001)
model.compile(optimizer=sgd, loss=huber_loss_mean,
metrics=['mae'])
return model