Ejemplo n.º 1
0
 def predict(self, Xt, yt, ids, results):
     tic = timeit.default_timer()
     y_dist =  pd.DataFrame(self.model.predict(Xt),index=yt.index.values)
     toc = timeit.default_timer()
     pred_time = toc-tic 
     y_samples = np.apply_along_axis(mdn.sample_from_output, 1, y_dist, self.n_out, self.n_mixes,temp=1.0)
     y_hat = pd.DataFrame(y_samples[:,0,:],index=yt.index.values)
     
     # split mixture params
     mus, sigs, pi_logits = mdn.split_mixture_params(y_dist, self.n_out, self.n_mixes)
     pis = mdn.softmax(pi_logits, t=1)
     muc = [list(t) for t in zip(*[iter(mus)]*self.n_mixes)]
     sic = [list(t) for t in zip(*[iter(sigs)]*self.n_mixes)] 
     
     # store results
     for k in y_hat.columns:
         results[ids[k]]['ytest'].append(yt.iloc[:,k])
         results[ids[k]]['yhat'].append(y_hat.iloc[:,k])
         results[ids[k]]['alphas'].append(pis)
         results[ids[k]]['mus'].append(muc[k])
         results[ids[k]]['sigs'].append(sic[k])
         results['pred_time'].append(pred_time)
         results['fit_time'].append(self.fit_time)
     
     return y_hat, results
Ejemplo n.º 2
0
def get_mu_sigma_MDN(params, output_dim, temp, sigma_temp, mixes):
    mus, sigs, pi_logits = mdn.split_mixture_params(params, output_dim, mixes)
    pis = mdn.softmax(pi_logits, t=temp)
    m = mdn.sample_from_categorical(pis)
    # Alternative way to sample from categorical:
    # m = np.random.choice(range(len(pis)), p=pis)
    mus_vector = mus[m * output_dim:(m + 1) * output_dim]
    sig_vector = sigs[m * output_dim:(m + 1) *
                      output_dim] * sigma_temp  # adjust for temperature

    return mus_vector, sig_vector
Ejemplo n.º 3
0
def sample_from_one_specific_mixture(mdn, mixture_number, params, output_dim,
                                     total_num_mixes, sigma_temp):

    #To make dreams only from 1 of the trained mixtures - helps see what's inside there.
    mus, sigs, pi_logits = mdn.split_mixture_params(params, output_dim,
                                                    total_num_mixes)
    mus_vector = mus[mixture_number * output_dim:(mixture_number + 1) *
                     output_dim]
    sig_vector = sigs[mixture_number * output_dim:(mixture_number + 1) *
                      output_dim] * sigma_temp  # adjust for temperature
    cov_matrix = np.identity(output_dim) * sig_vector
    sample = np.random.multivariate_normal(mus_vector, cov_matrix, 1)
    return sample
Ejemplo n.º 4
0
 def _mixture_components(self, predictions, num_plots=1):
     """
     Statistics of mixture components
     """
     stats = []
     for i in range(len(predictions)):
         mus, sigs, pis = mdn.split_mixture_params(
             np.squeeze(np.array(predictions))[i], self.OUTPUT_DIMS,
             self.n_mixes)
         stats.append(np.zeros(12))
         stats[i][0] = np.mean(sigs)
         stats[i][1] = np.std(sigs)
         stats[i][2] = np.max(sigs)
         stats[i][3] = np.min(sigs)
         stats[i][4] = np.mean(mus)
         stats[i][5] = np.std(mus)
         stats[i][6] = np.max(mus)
         stats[i][7] = np.min(mus)
         stats[i][8] = np.mean(pis)
         stats[i][9] = np.std(pis)
         stats[i][10] = np.max(pis)
         stats[i][11] = np.min(pis)
     stats = np.array(stats)