Ejemplo n.º 1
0
    def step(engine, batch):
        model.train()
        x, _ = batch
        x = x.to(device)
        x_quant = q.preprocess(x)

        recon, kl = model(x_quant)

        nll = get_recon_error(recon, x,
                              sigma(engine.state.epoch, args.sigma_switch))
        loss = nll + kl
        elbo = -loss

        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        lr = optimizer.param_groups[0]['lr']
        ret = {
            'elbo': elbo.item() / len(x),
            'nll': nll.item() / len(x),
            'kl': kl.item() / len(x),
            'lr': lr,
            'sigma': sigma(engine.state.epoch, args.sigma_switch)
        }
        return ret
Ejemplo n.º 2
0
def pred(x, beta_hat):
    """
        x:        Punto a clasificar (vector de numpy)
        betahat:  Arreglo de numpy beta.shape=(p,)
        yhat:     Vector binario de predicciones
    """
    return 1 if sigma(beta_hat @ x) >= 0.5 else 0
Ejemplo n.º 3
0
def lnlike(theta, data, data_covs, data_dists):
    shifts = data - theta[:3]
    s0s, r0s, alphas = 10**theta[3:6], theta[6:9], theta[9:12]
    sigmas = np.array([u.sigma(r, s0s, r0s, alphas) for r in data_dists])
    cov_theta = np.array([np.diag(sig**2) for sig in sigmas])
    covs = data_covs + cov_theta
    icovs = np.linalg.inv(covs)
    lnlike = np.sum(
        [shift @ (icov @ shift) for shift, icov in zip(shifts, icovs)])
    lnlike += np.sum(np.log(np.linalg.det(covs)))
    return -lnlike
Ejemplo n.º 4
0
def lnlike_lp_sample(theta, data):
    ii = np.random.randint(low=0, high=data.shape[-1])
    sample = data[:, :, ii]
    shifts = sample[:, :3] - theta[:3]
    s0s, r0s, alphas = theta[3:6], theta[6:9], theta[9:12]
    sigmas = np.array([u.sigma(r, s0s, r0s, alphas) for r in data_dists])
    covs = np.zeros((38, 3, 3)) + np.diag(theta[3:]**2)
    icovs = np.linalg.inv(covs)
    lnlike = np.sum(
        [shift @ (icov @ shift) for shift, icov in zip(shifts, icovs)])
    lnlike += np.sum(np.log(np.linalg.det(covs)))
    return -lnlike
Ejemplo n.º 5
0
def lnlike(theta, data, data_covs, data_dists):
    shifts = data - np.array([0, 0, theta[0]])
    s0s = np.array([theta[1], theta[2], theta[2]])
    r0s = np.array([theta[3], theta[4], theta[4]])
    alphas = np.array([theta[5], theta[6], theta[6]])
    sigmas = np.array([u.sigma(r, s0s, r0s, alphas) for r in data_dists])
    cov_theta = np.array([np.diag(sig**2) for sig in sigmas])
    covs = data_covs + cov_theta
    icovs = np.linalg.inv(covs)
    lnlike = np.sum(
        [shift @ (icov @ shift) for shift, icov in zip(shifts, icovs)])
    lnlike += np.sum(np.log(np.linalg.det(covs)))
    return -lnlike
Ejemplo n.º 6
0
def grad_F(X, y, beta):
    '''
    Método que calcula el gradiente de F para la función de regresión logística

        (X,y):  Datos de entrenamiento [X.shape=(m,p), y.shape=(m,)]
                X,y matrices de numpy
        beta:   Arreglo de numpy beta.shape=(p,)
    '''
    x_t = pd.DataFrame(X).T
    X_aux = pd.DataFrame(X).copy()
    z = X_aux.apply(lambda x_i: sigma(beta @ x_i), axis=1)
    z = pd.DataFrame(z - y)
    resp = (x_t @ z)

    resp = resp.iloc[:, 0] if resp.iloc[:, 0].size > resp.iloc[
        0, :].size else resp.iloc[0, :]

    return resp
Ejemplo n.º 7
0
def hess_F(X, y, beta):
    """
    Método que calcula la hessiana de F.

        (X,y):  Datos de entrenamiento [X.shape=(m,p), y.shape=(m,)]
                X,y matrices de numpy
        beta:   Arreglo de numpy beta.shape=(p,)
    """
    x_t = pd.DataFrame(X).T
    X_aux = pd.DataFrame(X).copy()
    s_ii = X_aux.apply(lambda x_i: sigma(beta @ x_i), axis=1)
    s_ii = s_ii * (1 - s_ii)

    S = numpy.diag(s_ii)

    resp = S @ X
    resp = x_t @ resp

    return resp
Ejemplo n.º 8
0
    def validate(engine):
        model.eval()

        val_elbo = 0
        val_kl = 0
        val_nll = 0

        with torch.no_grad():
            for i, (x, _) in enumerate(test_loader):
                x = x.to(device)
                x_quant = q.preprocess(x)
                recon, kl = model(x_quant)
                nll = get_recon_error(
                    recon, x, sigma(engine.state.epoch, args.sigma_switch))
                loss = nll + kl
                elbo = -loss

                val_elbo += elbo
                val_kl += kl
                val_nll += nll
                if i == 0:
                    batch, *xdims = x.shape
                    row = 8
                    n = min(x.shape[0], row)
                    comparison = torch.cat([x[:n], recon[:n]])
                    grid = make_grid(comparison.detach().cpu().float(),
                                     nrow=row)
                    writer.add_image('val/reconstruction', grid,
                                     engine.state.iteration)
            val_elbo /= len(test_loader.dataset)
            val_kl /= len(test_loader.dataset)
            val_nll /= len(test_loader.dataset)
            writer.add_scalar('val/elbo', val_elbo.item(),
                              engine.state.iteration)
            writer.add_scalar('val/kl', val_kl.item(), engine.state.iteration)
            writer.add_scalar('val/nll', val_nll.item(), engine.state.iteration)
            print('{:3d} /{:3d} : ELBO: {:.4f}, KL: {:.4f}, NLL: {:.4f}'.format(
                engine.state.epoch, engine.state.max_epochs, val_elbo, val_kl,
                val_nll))
Ejemplo n.º 9
0
    plt.xlim(-3,1)
    plt.ylim(bottom=0.0)
    plt.legend(loc='upper left');
    plt.savefig(pltpth+'uniform.pdf', bbox_inches='tight')
    plt.close()

# # ## ### ##### ######## ############# #####################
### Fig 6: Variable results for MW
# # ## ### ##### ######## ############# #####################
if remake[5]:
    print('Variable results for MW')
    sample = 'variable_simple'
    fig = plt.figure(figsize=[smallwidth, smallwidth])
    rvals = np.arange(15,265,5)
    samples = np.load(u.SIM_DIR+'beta/mcmc/data/'+sample+'.npy')
    sigmas = [u.sigma(r, samples[:,1:3], samples[:,3:5], samples[:,5:])\
                for r in rvals]
    sigmas = np.array(sigmas)
    betas = [1-(sigmas[i,:,1]**2 + sigmas[i,:,1]**2)/(2*sigmas[i,:,0]**2)\
                for i in range(len(rvals))]
    betas = np.array(betas)
    beta_median = np.median(betas, axis=1)

    lower = np.percentile(betas, 15.9, axis=1)
    upper = np.percentile(betas, 84.1, axis=1)

    gs = gridspec.GridSpec(2, 1, height_ratios=[1, 1])
    gs.update(hspace=0.0)

    ax0 = plt.subplot(gs[0])
    ax0.plot(rvals, beta_median, '-')
Ejemplo n.º 10
0
#!python3

import text
import xlsxwriter
import re
from utils import sigma


# In a few words, Sigma method depends on the average distance between the same words in a text.
# Thus, the words which are the most equally distributed along all the document will have the
# highest rank.
corpus = text.get_text_corpus(9999999, 'texts/books')
workbook = xlsxwriter.Workbook('sigma.xlsx')
for txt in corpus:
	sorted_words = sigma(txt)
	worksheet = workbook.add_worksheet(re.sub('[\[\]:*?/\\\]', '', txt['title'][0:28]))
	worksheet.write(0, 0, txt['title'])
	worksheet.write(1, 0, '#')
	worksheet.write(1, 1, 'Rank')
	worksheet.write(1, 2, 'Word')
	row = 0
	for word, data in sorted_words:
		row += 1
		worksheet.write(row + 1, 0, row)
		worksheet.write(row + 1, 1, data['s'])
		worksheet.write(row + 1, 2, word)
workbook.close()
print('\nDone!')