-
Notifications
You must be signed in to change notification settings - Fork 0
/
NN_learn.py
178 lines (127 loc) · 4.63 KB
/
NN_learn.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
import numpy as np
from matplotlib import pyplot as plt
import tensorflow as tf
import keras
from tensorflow.keras import layers
from tensorflow.keras.layers import Activation
from tensorflow.keras import backend as K
from tensorflow.keras.utils import get_custom_objects
def swish(x):
return x * K.sigmoid(x)
get_custom_objects().update({'swish': Activation(swish)})
tf.keras.backend.set_floatx('float64')
def save_model_wts(H):
N = len(H)
path_list = []
for u in range(N):
modelpath = "saved_model/model"+ str(u+1) +".h5"
path_list.append(modelpath)
H[u].save_weights(modelpath)
print("Weights saved around"+modelpath)
return path_list
def train_NN_ising(samples,model, epochs, batch_size=200, eta =0.01 ,stopping_crit=False):
n_variables = len(samples[0])
n_samples = len(samples)
learnedH = []
STEPS_PER_EPOCH = n_samples/batch_size
for i in range(n_variables):
learnedH.append(tf.keras.models.clone_model(model))
def GRISE_loss( H_u, s_u):
return tf.reduce_mean(tf.exp(-1*tf.multiply(s_u,H_u)))
for u in range(n_variables):
indices = list(range(n_variables))
del indices[u]
s_u = samples[:, u].reshape(n_samples,1)
s_bar_u = samples[:, indices]
learnedH[u].compile(optimizer=tf.keras.optimizers.Adam(eta),
loss=GRISE_loss)
print("Learning variable:", u)
learnedH[u].fit(s_bar_u, s_u, epochs=epochs, batch_size=batch_size)
return learnedH
def train_NN(samples,q,model, epochs, batch_size=200, eta = 0.01, stopping_crit=False, trans_inv = False):
n_variables = len(samples[0])
b = -1.0/q
a = 1.0 + b
n_samples = len(samples)
learnedH = []
STEPS_PER_EPOCH = n_samples/batch_size
for i in range(n_variables):
learnedH.append(tf.keras.models.clone_model(model))
def GRISE_loss( H_u, s_u):
return tf.reduce_mean(tf.exp(-1* tf.reduce_sum(tf.multiply(s_u,H_u), axis=1)))
var_list = [0] if trans_inv else range(n_variables)
for u in var_list:
indices = list(range(n_variables))
del indices[u]
s_u = samples[:, u].reshape(n_samples,1).astype(int)
s_u = tf.one_hot(s_u, depth=q, on_value=a, off_value=b)[:,0,:]
s_u = tf.cast(s_u, tf.float64)
#s_bar_u = samples[:, indices] + 1 #This is for Julia
learnedH[u].compile(optimizer=tf.keras.optimizers.Adam(eta),
loss=GRISE_loss)
print("Learning variable:", u)
learnedH[u].fit(samples[:, indices] + 1, s_u, epochs=epochs, batch_size=batch_size)
if trans_inv==True:
for u in range(1,n_variables):
learnedH[u] = learnedH[0]
return learnedH
def NN_MCMC(H, n, n_samples, burn_in = 10000):
def toss(prob):
if np.random.random() < prob:
return 1.0
else:
return -1.0
state_list = []
state = np.ones((1, n), dtype=np.float64)
prob_dict = {}
indices = []
for u in range(n):
a = list(range(n))
a.remove(u)
indices.append(a[:])
for t in range(burn_in):
for u in range(n):
if (u, tuple(state[:].flatten())) in prob_dict:
p = prob_dict[(u, tuple(state[:].flatten()))]
else:
p = np.exp(H[u]( state[:, indices[u]] ))/ (2 * np.cosh(H[u]( state[:, indices[u]] )) )
prob_dict[(u, tuple(state[:].flatten()))] = p
state[0,u] = toss(p[0][0])
print("burn in complete")
c = 0.1
for t in range(n_samples):
if t/n_samples > c:
print(c*100, "% complete" )
c += 0.1
for u in range(n):
if (u, tuple(state[:].flatten())) in prob_dict:
p = prob_dict[(u, tuple(state[:].flatten()))]
else:
p = np.exp(H[u]( state[:, indices[u]] ))/ (2 * np.cosh(H[u]( state[:, indices[u]] )) )
prob_dict[(u, tuple(state[:].flatten()))] = p
state[0,u] = toss(p[0][0])
state_list.append(state[:].flatten())
return np.vstack(state_list)
def list2dict(s):
d= {}
for j in s:
if j in d:
d[j] += 1
else:
d[j] = 1
return d
def TVD (s1, s2, ns):
s1_list = [tuple( s1[i,:].astype(int) ) for i in range(ns)]
s2_list = [tuple( s2[i,:].astype(int) ) for i in range(ns)]
d1 = list2dict(s1_list)
d2 = list2dict(s2_list)
TV =0.0
for i in d1.keys():
if i in d2:
TV += abs(d1[i] - d2[i])
else:
TV += abs(d1[i])
for i in d2.keys():
if i not in d1:
TV += abs(d2[i])
return TV/(2.0*ns)