-
Notifications
You must be signed in to change notification settings - Fork 1
/
findtheright.py
146 lines (103 loc) · 4.03 KB
/
findtheright.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Jun 7 14:24:39 2019
@author: herbert
"""
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import numpy as np
from scipy.special import factorial
from scipy.interpolate import interp1d
from scipy.stats import gaussian_kde
from sklearn.metrics import mean_squared_error
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import skimage as sk
import time
import json
import tensorflow as tf
from tensorflow.keras import layers
from tensorflow import keras
from keras.utils import to_categorical
from keras.models import model_from_json
from tensorflow.keras.callbacks import ModelCheckpoint
from genSample import *
from model import *
#%%
N=-1 #dimension of rho
s=10000 #number of samples
nphi=10#45 #number of angleSteps
nxs=10
xmax=5
lxs=np.linspace(-xmax, xmax, nxs)
[xs, ys]=np.meshgrid(lxs,lxs);
phispace=np.linspace(0,180,nphi, endpoint=False)
[px, py]=np.meshgrid(lxs,phispace)
P, W=generateDatasetWithShiftAndSqueezed(N,s,phispace,lxs)
np.save('data/P10000_10_10_shift_squeezed', P)
np.save('data/W10000_10_10_shift_squeezed', W)
'''
P=np.load('data/P10000_10_10_shift_squeezed.npy')
W=np.load('data/W10000_10_10_shift_squeezed.npy')
'''
inputV=np.zeros((s,nxs*nphi))
outputV=np.zeros((s,nxs*nxs))
for i in range(0, len(P)):
inputV[i]=P[i].flatten()
outputV[i]=W[i].flatten()
#%%
ai=smallDeepNN1(nxs,nphi)
ai.model.compile(optimizer=keras.optimizers.SGD(),#tf.train.GradientDescentOptimizer(0.005),#optimizer=tf.train.AdamOptimizer(0.001),
loss='mean_squared_error')
checkpoint = ModelCheckpoint('models/ai_checkpoint.h5', monitor='val_loss', verbose=1, save_best_only=True, mode='min')
callbacks_list = [checkpoint]
history1=ai.model.fit(inputV, outputV, epochs=20, batch_size=32, verbose=1, validation_split=0.1, callbacks=callbacks_list)
c1=ai.model.count_params()
#%%
ai=smallDeepNN2(nxs,nphi)
ai.model.compile(optimizer=keras.optimizers.SGD(),#tf.train.GradientDescentOptimizer(0.005),#optimizer=tf.train.AdamOptimizer(0.001),
loss='mean_squared_error')
checkpoint = ModelCheckpoint('models/ai_checkpoint.h5', monitor='val_loss', verbose=1, save_best_only=True, mode='min')
callbacks_list = [checkpoint]
history2=ai.model.fit(inputV, outputV, epochs=20, batch_size=32, verbose=1, validation_split=0.1, callbacks=callbacks_list)
c2=ai.model.count_params()
#%%
ai=smallDeepNN3(nxs,nphi)
ai.model.compile(optimizer=keras.optimizers.SGD(),#tf.train.GradientDescentOptimizer(0.005),#optimizer=tf.train.AdamOptimizer(0.001),
loss='mean_squared_error')
checkpoint = ModelCheckpoint('models/ai_checkpoint.h5', monitor='val_loss', verbose=1, save_best_only=True, mode='min')
callbacks_list = [checkpoint]
history3=ai.model.fit(inputV, outputV, epochs=20, batch_size=32, verbose=1, validation_split=0.1, callbacks=callbacks_list)
c3=ai.model.count_params()
#%%
ai=smallDeepNN4(nxs,nphi)
ai.model.compile(optimizer=keras.optimizers.SGD(),#tf.train.GradientDescentOptimizer(0.005),#optimizer=tf.train.AdamOptimizer(0.001),
loss='mean_squared_error')
checkpoint = ModelCheckpoint('models/ai_checkpoint.h5', monitor='val_loss', verbose=1, save_best_only=True, mode='min')
callbacks_list = [checkpoint]
history4=ai.model.fit(inputV, outputV, epochs=20, batch_size=32, verbose=1, validation_split=0.1, callbacks=callbacks_list)
c4=ai.model.count_params()
#%%
ai.model.load_weights('models/ai_checkpoint.h5')
#%%
with open('models/ai_model.json', 'w') as json_file:
json_file.write(ai.model.to_json())
ai.model.save_weights('models/ai_weights.h5')
with open('models/ai_history.json', 'w') as json_file:
json.dump(history4.history, json_file)
#save AI
#%%
plt.semilogy(history1.history['val_loss'])
plt.semilogy(history2.history['val_loss'])
plt.semilogy(history3.history['val_loss'])
plt.semilogy(history4.history['val_loss'])
plt.title('Model loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['256-128-128', '256-256', '256-128', '512-128'], loc='upper right')
plt.show()
print("#Param1=", c1)
print("#Param2=", c2)
print("#Param3=", c3)
print("#Param4=", c4)