Skip to content

Commit

Permalink
loss function changes
Browse files Browse the repository at this point in the history
  • Loading branch information
tsikes committed Aug 23, 2021
1 parent cba7b9e commit 774f70d
Show file tree
Hide file tree
Showing 3 changed files with 172,839 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Development/Random Testing/loss func integral/loss_integral.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# This file is part of Frhodo. Copyright © 2020, UChicago Argonne, LLC
# and licensed under BSD-3-Clause. See License.txt in the top-level
# directory for license and copyright information.

import numpy as np
from scipy import integrate # used to integrate weights numerically

def generalized_loss_fcn(x, mu=0, a=2, c=1): # defaults to L2 loss
x_c_2 = ((x-mu)/c)**2

if a == 1: # generalized function reproduces
loss = (x_c_2 + 1)**(0.5) - 1
if a == 2:
loss = 0.5*x_c_2
elif a == 0:
loss = np.log(0.5*x_c_2+1)
elif a == -2: # generalized function reproduces
loss = 2*x_c_2/(x_c_2 + 4)
elif a <= -1000: # supposed to be negative infinity
loss = 1 - np.exp(-0.5*x_c_2)
else:
loss = np.abs(a-2)/a*((x_c_2/np.abs(a-2) + 1)**(a/2) - 1)

return loss*c**a + mu # multiplying by c^2 is not necessary, but makes order appropriate

alpha_vec = np.geomspace(-60, -1, 1000)
alpha_vec = np.concatenate([alpha_vec, np.linspace(-1,2, int(3/(alpha_vec[-1] - alpha_vec[-2])-1))[1:]])
tau = 100
for alpha in alpha_vec:
for c in np.geomspace(1, 5, 100):
int_func = lambda x: np.exp(-generalized_loss_fcn(x, a=alpha, c=c))
val, err = integrate.quadrature(int_func, -tau, tau, tol=1E-14, maxiter=10000)
print(f'{alpha:0.3f} {c:0.3f} {val:0.14e}')

Loading

0 comments on commit 774f70d

Please sign in to comment.