-
Notifications
You must be signed in to change notification settings - Fork 0
/
012SymClassD.py
194 lines (121 loc) · 5.23 KB
/
012SymClassD.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# -*- coding: utf-8 -*-
"""
Created on Thu Dec 14 18:03:18 2023
@author: sebas
"""
from __future__ import print_function
import tensorflow as tf
#tf.enable_eager_execution()
import tensorflow.keras
from tensorflow.keras.models import Sequential,Model
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, Subtract,Input,Lambda, Reshape
from tensorflow.keras.layers import Conv2D, MaxPooling2D,Conv1D, MaxPooling1D, LocallyConnected1D, Add
from tensorflow.keras.callbacks import ModelCheckpoint,CSVLogger,ReduceLROnPlateau,EarlyStopping
from tensorflow.keras.constraints import max_norm
from tensorflow.keras import regularizers
import matplotlib.pyplot as plt
import numpy as np
import itertools
import os
np.random.seed(seed=2)
features=3
noise=0.01
l2=0.0001
def probe_function(x):
return x[0]*np.exp(-0.5*(x[1]**2+x[2]**2))
def target_function(x):
if probe_function(x)*np.random.normal(1,noise)> 0.2:
return 1
else:
return 0
def explicit_gradient(x):
return [probe_function(x)/x[0], probe_function(x) *(-x[1]), probe_function(x) *(-x[2]) ]
print('data generation')
x=np.random.rand(10000,features)*4 -2
x[:,0]=np.random.rand(10000)*2
y=np.array([target_function(sample) for sample in x])
randomized_order=np.random.permutation(len(x))
x=x[randomized_order]
y=y[randomized_order]
x_train=x[:int(0.8*len(x))]
y_train=y[:int(0.8*len(x))]
x_test=x[int(0.8*len(x)):]
y_test=y[int(0.8*len(x)):]
batch_size = 100
epochs = 1#1000
input_layer=Input(shape=(x_train.shape[-1],),name='input_layer')
layer_a1=Dense(1000,activation='elu',name='layer_a1',kernel_regularizer=regularizers.L2(l2),bias_regularizer=regularizers.L2(l2))(input_layer) ## does sigmoid give smoother gradients then relu?
layer_a1=Dropout(0.2)(layer_a1)
layer_a2=Dense(1000,activation='elu',name='layer_a2',kernel_regularizer=regularizers.L2(l2),bias_regularizer=regularizers.L2(l2))(layer_a1)
layer_a2=Dropout(0.2)(layer_a2)
output_latent=Dense(1,name='output')(layer_a2)
output=Activation(activation='sigmoid')(output_latent)
model = Model(inputs=input_layer, outputs=output)
model_latent = Model(inputs=input_layer, outputs=output_latent)
model.summary()
# Let's train the model
model.compile(loss='binary_crossentropy',
#optimizer=tensorflow.keras.optimizers.Adadelta(lr=0.0001),
optimizer=tensorflow.keras.optimizers.Adam(),
metrics=['acc'])
reduce_lr = ReduceLROnPlateau(monitor='loss', factor=0.5,
patience=20, verbose=1,min_lr=0)
early_stop= EarlyStopping(monitor='loss', patience=40, verbose=1)
history = model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
validation_data=(x_test, y_test),
shuffle=True,
callbacks=[reduce_lr,early_stop])
# Plot training & test loss values
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Model loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='upper left')
plt.show()
#### data analysis
## inverse sigmoid
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def inverse_sigmoid(x):
#return x
return np.log(x/(1-x))
predictions=model.predict(x)
predictions_latent=model_latent.predict(x)
y_analysis = [inverse_sigmoid(prediction) for prediction in predictions]
y_probe = [probe_function(sample) for sample in x]
plt.scatter(predictions_latent,y_probe,label="latent model")
#plt.scatter(y_analysis,y_probe,label="inverse sigmoid")
# plt.xlim(-5, 5)
# plt.ylim(-0.1, 0.1)
plt.title('prediction correlation with true function')
plt.ylabel('true function')
plt.xlabel('model prediction')
plt.legend(loc='upper left')
plt.show()
x_unsure=x[np.where((predictions > 0.0001) & (predictions < 0.9999))[0]]
## neural net gradients
data_set=x_unsure
input_variable = tf.Variable(data_set, dtype=tf.float32)
with tf.GradientTape() as tape:
tape.watch(input_variable)
preds = model_latent(input_variable)
nn_gradients = tape.gradient(preds, input_variable)
## explicit gradients
explicit_gradients = np.array([explicit_gradient(sample) for sample in data_set])
def normalize(x):
norm=np.sqrt(np.sum(x**2))
return x/norm
normalized_nn_gradients=np.array([normalize(sample) for sample in nn_gradients])
normalized_explicit_gradients=np.array([normalize(sample) for sample in explicit_gradients])
dot_products=[np.dot(normalized_explicit_gradients[i],normalized_nn_gradients[i]) for i in range(len(normalized_explicit_gradients))]
MSE=[np.average((normalized_explicit_gradients[i]-normalized_nn_gradients[i])**2) for i in range(len(normalized_explicit_gradients))]
print("average dot produt between grads of nn and true function: ",np.average(dot_products))
print("median dot produt between grads of nn and true function: ",np.median(dot_products))
print("average MSE between grads of nn and true function: ",np.average(MSE))
print("median MSE produt between grads of nn and true function: ",np.median(MSE))
print("length x_unsure", len(x_unsure))
np.savetxt('X.out', x, delimiter=',')
np.savetxt('Y.out', y, delimiter=',')