-
Notifications
You must be signed in to change notification settings - Fork 0
/
keras_mnist.py
163 lines (128 loc) · 4.38 KB
/
keras_mnist.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Aug 8 12:43:12 2018
@author: btek
"""
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, BatchNormalization, Activation
from keras.optimizers import RMSprop, SGD
from data_utils import load_dataset_mnist
batch_size = 512
num_classes = 10
epochs = 5
# the data, split between train and test sets
x_train, y_train, x_val, y_val, x_test, y_test = load_dataset_mnist(folder='../datasets/mnist/')
#(x_train, y_train), (x_test, y_test) = load_dataset_mnist()
x_train = x_train.reshape(50000, 784)
x_val = x_val.reshape(10000, 784)
x_test = x_test.reshape(10000, 784)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
#x_train /= 255
#x_test /= 255
#print(x_train.shape[0], 'train samples')
#print(x_test.shape[0], 'test samples')
# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_val = keras.utils.to_categorical(y_val, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
model = Sequential()
model.add(Dense(800, activation='linear', input_shape=(784,),name='dense1'))
model.add(BatchNormalization(name='bn1'))
model.add(Activation('relu'))
model.add(Dropout(0.25, name='drp1'))
model.add(Dense(800, activation='linear', name='dense2'))
model.add(BatchNormalization(name='bn2'))
model.add(Activation('relu'))
model.add(Dropout(0.25,name='drp2'))
model.add(Dense(num_classes, activation='softmax',name='out'))
model.get_config()
model.summary()
sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy',
optimizer=sgd,
metrics=['accuracy'])
history = model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test))
#for layer in model.layers:
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
# now loading our networks
import numpy as np
import os
import numpy as np
import matplotlib.pyplot as plt
#os.environ['THEANO_FLAGS']='device=cpu, gpuarray.preallocate=.1'
os.environ['MKL_THREADING_LAYER']='GNU'
import time
from focusing import U_numeric
#u = np.load("outputs/ESNN/mnist9/focused_s/mnist_model_focused_mlp:2,800,.25,.50_20180720-104103.npz")
u2 = np.load('outputs/ESNN/mnist10mnist_model_focused_mlp:2,800,0.25,0.25_20180808-185004.npz')
print(u2['arr_0'])
mu_1 = u2['arr_1'][0]
si_1 = u2['arr_1'][1]
weights_1= u2['arr_1'][2]
bias_1 = u2['arr_1'][3]
bn_1_beta = u2['arr_1'][4]
bn_1_gamma = u2['arr_1'][5]
bn_1_mean = u2['arr_3'] [1]
bn_1_std = u2['arr_3'] [2]
mu_2 = u2['arr_1'][6]
si_2 = u2['arr_1'][7]
weights_2= u2['arr_1'][8]
bias_2 = u2['arr_1'][9]
bn_2_beta = u2['arr_1'][10]
bn_2_gamma = u2['arr_1'][11]
bn_2_mean = u2['arr_3'] [4]
bn_2_std = u2['arr_3'] [5]
weights_out = u2['arr_1'][12]
bias_out = u2['arr_1'][13]
def prune(x):
return np.maximum(0,x-1e-6)
fis_1 = U_numeric(np.linspace(0,1,weights_1.shape[0]),mu_1,si_1,1)
fis_1 = prune(fis_1)
fis_2 = U_numeric(np.linspace(0,1,weights_2.shape[0]),mu_2,si_2,1)
fis_2 = prune(fis_2)
fw1 = (fis_1.T*weights_1)
fw2 = (fis_2.T*weights_2)
#fw1[fw1<1e-14]=0
#fw2[fw2<1e-14]=0
dense1= model.get_layer('dense1')
dense1.set_weights([fw1, bias_1])
#dense1.trainable=False
bn1=model.get_layer('bn1')
bn1_params= bn1.get_weights()
bn1.set_weights([bn_1_gamma, bn_1_beta, bn_1_mean, bn_1_std])
#bn1.trainable=False
dense2= model.get_layer('dense2')
dense2.set_weights([fw2, bias_2])
#dense2.trainable=False
bn2=model.get_layer('bn2')
bn2_params= bn2.get_weights()
bn2.set_weights([bn_2_gamma, bn_2_beta, bn_2_mean, bn_2_std])
#bn2.trainable=False
out= model.get_layer('out')
out.set_weights([weights_out,bias_out])
#out.trainable=False
model.compile(loss='categorical_crossentropy',
optimizer=sgd,
metrics=['accuracy'])
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
epochs = 1
history = model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test))
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])