-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsave_load_model.py
34 lines (27 loc) · 1.11 KB
/
save_load_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
import numpy as np
class save_load:
def load_weights(self,model,filename):
data = np.load(str(filename))
lst = data.files
counter = 0
# Loop trough model layers
for i in range(len(model.layers)):
if model.layers[i].__class__.__name__ == "Linearlayer":
model.layers[i].weight = data[lst[counter]]
counter+=1
if model.layers[i].b:
model.layers[i].bias = data[lst[counter]]
counter+=1
def save_weights(self,model,filename):
model_weights = {}
counter = 0
# Loop trough model layers
for i in range(len(model.layers)):
if model.layers[i].__class__.__name__ == "Linearlayer":
model_weights[str(counter)] = model.layers[i].weight
counter+=1
if model.layers[i].b:
model_weights[str(counter)] = model.layers[i].bias
counter+=1
# Create file with weights
np.savez_compressed(str(filename), **model_weights)