Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
f1e9977
add kullbackleibler
scarlehoff Feb 5, 2021
78fad20
add a positive_bound control to the diagonal of the matrix
scarlehoff Feb 5, 2021
38eced2
use internal cma stuff
scarlehoff Feb 5, 2021
ec3baed
small changes
scarlehoff Feb 8, 2021
101021a
added an option to force positivity
scarlehoff Feb 9, 2021
baf3e4e
use cholesky decomposition when forcing positivity
scarlehoff Feb 9, 2021
be21468
add gaussian init
scarlehoff Feb 10, 2021
19ea636
add notebook\
scarlehoff Feb 10, 2021
f0dd7c4
add automatic gaussian initialization
scarlehoff Feb 10, 2021
107c471
some improvements to the gaussian init
scarlehoff Feb 10, 2021
17b8666
set the bounds according to the gaussian initialization
scarlehoff Feb 11, 2021
74e1291
ask directly the rtbm for rho
scarlehoff Feb 11, 2021
5616d2b
bugfix
scarlehoff Feb 11, 2021
4f21294
update nb
scarlehoff Feb 11, 2021
9816854
update nb
scarlehoff Feb 11, 2021
c950ee7
test
scarlehoff Feb 12, 2021
68752fd
try other scaling
scarlehoff Mar 1, 2021
09c082b
styled
scarlehoff Mar 2, 2021
5153c5e
add samplings
scarlehoff Mar 2, 2021
db5216f
new nb
scarlehoff Mar 2, 2021
0de9051
parallel
scarlehoff Mar 3, 2021
e81136c
entrenamiento
scarlehoff Mar 3, 2021
34f9b98
entrenando con nhid=3
scarlehoff Mar 3, 2021
de849d1
entrenando con minmax
scarlehoff Mar 4, 2021
9c06afe
minmax_nhid=3
scarlehoff Mar 4, 2021
5955a91
incremental
scarlehoff Mar 5, 2021
f55bd41
entrenamiento perfe
scarlehoff Mar 5, 2021
110d33d
entrenamiento perfe
scarlehoff Mar 5, 2021
bb4cf19
entrenamiento good con tanh, nh=3
scarlehoff Mar 6, 2021
06129b0
add more activations
scarlehoff Mar 6, 2021
dbdacb7
incremental
scarlehoff Mar 6, 2021
a555616
buen fit incluso para nh=3, nv=2
scarlehoff Mar 9, 2021
01476ba
some more changes
scarlehoff Mar 18, 2021
032d84f
tanh between 0 and 1
scarlehoff Mar 18, 2021
bd262a5
fix comments
scarlehoff Mar 30, 2021
27246ad
allow for different activations with differently configured gaussians
scarlehoff Apr 8, 2021
bc5597b
add inverse transformation
scarlehoff Apr 13, 2021
15844a9
remove notebooks
scarlehoff Apr 13, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions theta/costfunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,27 @@ def cost(self, X, *Y):
@abstractmethod
def gradient(self, X):
pass


class kullbackLeibler(costfunction):

@staticmethod
def cost(x, y):
if (x < 0).any():
import ipdb; ipdb.set_trace()
return -np.sum(y*np.log(x)) # + np.sum(y*np.log(y))

@staticmethod
def gradient(x, y):
return -np.sum(y/x)


class mse(costfunction):
""" Mean squared error """

@staticmethod
def cost(x, y):
return np.sum(np.mean((x-y)**2,axis=1))
return np.sum(np.mean((x-y)**2,axis=-1))

@staticmethod
def gradient(x, y):
Expand All @@ -49,7 +62,7 @@ def cost(x, *y):
return -np.sum(x)

@staticmethod
def gradient(x):
def gradient(x, y):
sys.exit("Gradient not implemented!")


Expand All @@ -61,7 +74,7 @@ def cost(x, y):
return np.sqrt(np.sum(np.mean((y-x)**2,axis=1)))

@staticmethod
def gradient(x):
def gradient(x, y):
return 1.0/np.sqrt(np.sum(np.mean((y-x)**2,axis=1)))*(x-y)


Expand All @@ -74,6 +87,5 @@ def cost(x, y):
return -np.sum(np.mean(np.multiply(y,lx),axis=1))

@staticmethod
def gradient(x):
def gradient(x, y):
return -1.0/y.shape[1]*y/x

15 changes: 11 additions & 4 deletions theta/mathtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
RTBM_precision= 1e-8


def check_normalization_consistency(t, q, w):
def normalization_consistency(t, q, w):
c = q - np.transpose(w).dot(np.linalg.inv(t).dot(w))
return np.all(np.linalg.eigvals(c) > 0)
return np.linalg.eigvals(c)


def check_normalization_consistency(t, q, w):
return np.all(normalization_consistency(t, q, w) > 0)


def check_pos_def(x):
Expand Down Expand Up @@ -57,7 +61,10 @@ def rtbm_probability(v, bv, bh, t, w, q, mode=1):
uR1, vR1 = RiemannTheta.parts_eval((vT.dot(w) + BhT) / (2.0j * np.pi), -q / (2.0j * np.pi), mode, epsilon=RTBM_precision)
uR2, vR2 = RiemannTheta.parts_eval((BhT - BtiTW) / (2.0j * np.pi), (-q + WtiTW) / (2.0j * np.pi), mode, epsilon=RTBM_precision)

return np.sqrt(detT / (2.0 * np.pi) ** (v.shape[0])) * ExpF * vR1 / vR2 * np.exp(uR1-uR2)
# In order to avoid problems at multiprocessing, let's add a maximum value for the exponent
# And a minimum to avoid division by 0
res = np.sqrt(detT / (2.0 * np.pi) ** (v.shape[0])) * ExpF * vR1 / (vR2+RTBM_precision) * np.exp( np.minimum(uR1-uR2, 250) )
return res


def rtbm_log_probability(v, bv, bh, t, w, q, mode=1):
Expand Down Expand Up @@ -263,4 +270,4 @@ def rtbm_ph(model, h):
BBTWh = np.dot(BBTW ,h)
ExpF = np.exp(-0.5*hTQWTWh-BBTWh)
u, v = RiemannTheta.parts_eval((BhT-BtiTW)/(2j*np.pi),(-model.q+WtiTW)/(2j*np.pi), mode=1, epsilon=RTBM_precision)
return ExpF / v * np.exp(-u)
return ExpF / v * np.exp(-u)
39 changes: 23 additions & 16 deletions theta/minimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ def worker_initialize(cost, model, x_data, y_data):
def worker_compute(params):
if not resource.model.set_parameters(params):
return np.NaN
res = resource.cost_function.cost(np.real(resource.model(resource.x_data)), resource.y_data)
prob = np.real(resource.model(resource.x_data))
if (prob < 0.0).any():
import ipdb; ipdb.set_trace()
res = resource.cost_function.cost(prob, resource.y_data)
return res


Expand All @@ -47,7 +50,8 @@ class CMA(object):
parallel (bool): if set to True the algorithm uses multi-processing.
ncores (int): limit the number of cores when ``parallel=True``.
"""
def __init__(self, parallel=False, ncores=0):
def __init__(self, parallel=False, ncores=0, verbose=True):
self._verbose = verbose
super(CMA, self).__init__()
if parallel:
if(ncores==0):
Expand Down Expand Up @@ -77,11 +81,20 @@ def train(self, cost, model, x_data, y_data=None, tolfun=1e-11, popsize=None, ma
Note:
The parameters of the model are changed by this algorithm.
"""
if self._verbose:
vb = 0
else:
vb = -9

initsol = np.real(model.get_parameters())
args = {'bounds': model.get_bounds(),

# Prepare the bounds
bounds = model.get_bounds()

args = {'bounds': bounds,
'tolfun': tolfun,
'verb_log': 0}
'verbose': vb,
'verb_log': vb}
sigma = np.max(model.get_bounds()[1])*0.1

if popsize is not None:
Expand Down Expand Up @@ -112,18 +125,12 @@ def train(self, cost, model, x_data, y_data=None, tolfun=1e-11, popsize=None, ma
pool.terminate()
else:
worker_initialize(cost, model, x_data, y_data)
while not es.stop():
f_values, solutions = [], []
while len(solutions) < es.popsize:
curr_fit = x = np.NaN
while np.isnan(curr_fit):
x = es.ask(1, gradf=grad)[0]
curr_fit = worker_compute(x)
solutions.append(x)
f_values.append(curr_fit)
es.tell(solutions, f_values)
es.disp()
print(es.result)
# By using ask_and_eval we let the cma decide what to do with NaNs
solutions, f_values = es.ask_and_eval(worker_compute)
es.tell(solutions, f_values)
es.disp()
if self._verbose:
print(es.result)

model.set_parameters(es.result[0])
return es.result[0]
Expand Down
Loading