def classifyByGMMHMM(seq, models, configs): Y = [] for config in configs: _rawdata_type = config["logType"] _event_type = config["eventType"] _motion_type = config["motionType"] _sound_type = config["soundType"] _location_type = config["locationType"] d = Dataset( rawdata_type=_rawdata_type, event_type=_event_type, motion_type=_motion_type, sound_type=_sound_type, location_type=_location_type ) # Initiation of data need prediction. y = np.array(d._convetNumericalSequence(seq)) Y.append(y) _GMMHMMs = [] for model in models: _GMMs = [] for gmm in model["gmmParams"]["params"]: _GMM = GMM( n_components=model["nMix"], covariance_type=model["covarianceType"] ) _GMM.covars_ = np.array(gmm["covars"]) _GMM.means_ = np.array(gmm["means"]) _GMM.weights_ = np.array(gmm["weights"]) _GMMs.append(_GMM) _GMMHMM = GMMHMM( n_components=model["nComponent"], n_mix=model["nMix"], startprob=np.array(model["hmmParams"]["startProb"]), transmat=np.array(model["hmmParams"]["transMat"]), gmms=_GMMs, covariance_type=model["covarianceType"] ) _GMMHMMs.append(_GMMHMM) results = [] # for _GMMHMM in _GMMHMMs: # res = _GMMHMM.score(Y) # results.append(res) for i in range(0, len(models)): res = _GMMHMMs[i].score(Y[i]) results.append(res) return results
def adaptation(self, ti): kadp = np.ones(self.nadp) kadp[0] = STD_RATIO1 kadp[1:3] = STD_RATIO2 kadp[3:5] = STD_RATIO3 sumX = 0 sumX2 = 0 for iadp in range(self.nadp): nCompo = self.gmdistr.get_params()['n_components'] tmp_gmdistr = GMM(n_components=nCompo, covariance_type='full') tmp_gmdistr.weights_ = self.gmdistr.weights_ tmp_gmdistr.means_ = self.gmdistr.means_ tmp_gmdistr.covars_ = self.gmdistr.covars_ * kadp[iadp]**2 subSmp = MainSmp(self.nsmp, tmp_gmdistr, self.rv_array, self.r_array, self.sl_array, self.gd_array, self.gcov_array) i = 0 while i <= MAX_NUM_UPDATE: try: smps = subSmp.sample() f = subSmp.priorPDF(smps) g = 1 - subSmp.condAvailability(smps, ti) hv, responsibilities = subSmp.score_samples(smps) gf2hv = g * f / hv self.updateParameter(smps, responsibilities, gf2hv) i += 1 break except ValueError: i += 1 continue sumX += np.sum(gf2hv) sumX2 += np.sum(gf2hv**2) npre = self.nadp * self.nsmp pfpre = 1. / npre * sumX Spf2pre = 1. / (npre * (npre - 1)) * (sumX2 - 2 * sumX * pfpre + npre * pfpre**2) return pfpre, Spf2pre
def setPreSmp(self, r_array, sl_array, rate_array, gd_array, gcov_array): # initial peaks corrTarget = np.eye(self.ncomp) rv_list = [] for icomp in xrange(self.ncomp): cov_variable = Normal('cov', mean=0., stdv=COVT0_COV) rv_list.append(cov_variable.rv) rv_array = np.array(rv_list) peaks0 = getInitialPeaks(rv_array, self.ncomp, corrTarget=corrTarget, options={'disp':False, 'anneal':True}) weights0 = 1./self.ncomp * np.ones(self.ncomp) covar0 = getInitialCovar(rv_array, self.ncomp) # initial values gmdistr = GMM(n_components=self.ncomp , covariance_type='full') gmdistr.weights_ = weights0 gmdistr.means_ = peaks0 gmdistr.covars_ = covar0 self.preSmp = PreSmp(self.analysis_type, self.nadp, self.nsmp, gmdistr, rv_array, r_array, sl_array, rate_array, gd_array, gcov_array)
def getKopt(self, k_array, ti): pf = np.zeros(k_array.shape) for k in k_array: nCompo = self.gmdistr.get_params()['n_components'] tmp_gmdistr = GMM(n_components=nCompo,covariance_type='full') tmp_gmdistr.weights_ = self.gmdistr.weights_ tmp_gmdistr.means_ = self.gmdistr.means_ tmp_gmdistr.covars_ = self.gmdistr.covars_ * k**2 subSmp = MainSmp(self.nsmp, tmp_gmdistr, self.rv_array, self.r_array, self.sl_array, self.gd_array, self.gcov_array) smps = subSmp.sample() indx = np.where(k_array==k)[0][0] pf[indx], dummy = subSmp.getPf(smps, ti) if indx>0 and pf[indx] < KRATIO_CRITERIA * np.mean(pf[:indx]): k_indx = indx-1 break else: k_indx = -1 return k_array[k_indx]
def getKopt(self, k_array, ti): pf = np.zeros(k_array.shape) for k in k_array: nCompo = self.gmdistr.get_params()['n_components'] tmp_gmdistr = GMM(n_components=nCompo, covariance_type='full') tmp_gmdistr.weights_ = self.gmdistr.weights_ tmp_gmdistr.means_ = self.gmdistr.means_ tmp_gmdistr.covars_ = self.gmdistr.covars_ * k**2 subSmp = MainSmp(self.nsmp, tmp_gmdistr, self.r_array, self.sl_array, self.gd_array) smps = subSmp.sample() indx = np.where(k_array == k)[0][0] pf[indx], dummy = subSmp.getPf(smps, ti) if indx > 0 and pf[indx] < KRATIO_CRITERIA * np.mean(pf[:indx]): k_indx = indx - 1 break else: k_indx = -1 return k_array[k_indx]
def adaptation(self, ti): kadp = np.ones(self.nadp) kadp[0] = STD_RATIO1 kadp[1:3] = STD_RATIO2 kadp[3:5] = STD_RATIO3 sumX = 0; sumX2 = 0; for iadp in range(self.nadp): nCompo = self.gmdistr.get_params()['n_components'] tmp_gmdistr = GMM(n_components=nCompo,covariance_type='full') tmp_gmdistr.weights_ = self.gmdistr.weights_ tmp_gmdistr.means_ = self.gmdistr.means_ tmp_gmdistr.covars_ = self.gmdistr.covars_ * kadp[iadp]**2 subSmp = MainSmp(self.nsmp, tmp_gmdistr, self.rv_array, self.r_array, self.sl_array, self.gd_array, self.gcov_array) i = 0 while i<=MAX_NUM_UPDATE: try: smps = subSmp.sample() f = subSmp.priorPDF(smps) g = 1 - subSmp.condAvailability(smps, ti) hv, responsibilities = subSmp.score_samples(smps) gf2hv = g*f/hv self.updateParameter(smps, responsibilities, gf2hv) i += 1 break except ValueError: i += 1 continue sumX += np.sum(gf2hv) sumX2 += np.sum(gf2hv**2) npre = self.nadp * self.nsmp pfpre = 1./npre * sumX Spf2pre = 1./(npre*(npre-1)) * (sumX2 - 2*sumX*pfpre + npre * pfpre**2) return pfpre, Spf2pre
def __init__(self, _models): super(GMMHMMClassifier, self).__init__(_models) self.gmmhmms = {} self.predict_data_ = None for label, value in _models.iteritems(): _model = value['param'] hmm_params = _model['hmmParams'] gmm_params = _model['gmmParams'] n_iter = _model.get('nIter', 50) transmat = np.array(hmm_params['transMat']) transmat_prior = np.array(hmm_params['transMatPrior']) n_component = hmm_params['nComponent'] startprob = np.array(hmm_params['startProb']) startprob_prior = np.array(hmm_params['startProbPrior']) n_mix = gmm_params['nMix'] covariance_type = gmm_params['covarianceType'] gmms = gmm_params.get('gmms', None) gmm_obj_list = [] if not gmms: gmm_obj_list = None else: for gmm in gmms: gmm_obj = GMM(n_components=gmm['nComponent'], covariance_type=gmm['covarianceType']) gmm_obj.covars_ = np.array(gmm['covars']) gmm_obj.means_ = np.array(gmm['means']) gmm_obj.weights_ = np.array(gmm['weights']) gmm_obj_list.append(gmm_obj) gmmhmm = GMMHMM(n_components=n_component, n_mix=n_mix, gmms=gmm_obj_list, n_iter=n_iter, covariance_type=covariance_type, transmat=transmat, transmat_prior=transmat_prior, startprob=startprob, startprob_prior=startprob_prior) self.gmmhmms[label] = {'gmmhmm': gmmhmm, 'status_set': value['status_set']}
if __name__ == '__main__': ti = 1 nCompo = 4 nsmp = 400 nadp = 20 nk = 100 muV0 = np.array([[2768.03327821769, 1519.21878505205, 2060.98121494796, 812.166721782312], [812.166721782312, 2060.98121494796, 1519.21878505205, 2768.03327821769]]).T muV0 = np.load('peaks0.npy') piV0 = np.array([0.25, 0.25, 0.25, 0.25]) covarV0 = np.array([[[80300.0, 0.0],[0.0, 80300.0]], [[80300.0, 0.0],[0.0, 80300.0]], [[80300.0, 0.0],[0.0, 80300.0]], [[80300.0, 0.0],[0.0, 80300.0]]]) gmdistr = GMM(n_components=4, covariance_type='full') gmdistr.weights_ = piV0 gmdistr.means_ = muV0 gmdistr.covars_ = covarV0 resistRv = Lognormal('r', mean=1790.1, stdv=283.37) rDistr = resistRv.rv #r_array = np.array([stats.norm(loc=1790.1, scale=283.37), stats.norm(loc=1790.1, scale=283.37)]) r_array = np.array([rDistr, rDistr]) sl_array = np.array([stats.norm(loc=301.1, scale=120.44)]) ti_mean = 15.91 a = 0.0135; b = 0.8580 gd = lambda x: (1-a*(x-ti_mean)**b) if x>ti_mean else 1.0 gd_array = np.array([gd, gd]) k_array = np.arange(2.0, 1.0-0.05/2, -0.05) testSmp = PreSmp(nadp, nsmp, gmdistr, r_array, sl_array, gd_array) pf, Spf2 = testSmp.adaptation(ti) piVopt = testSmp.gmdistr.weights_ muVopt = testSmp.gmdistr.means_
nk = 100 muV0 = np.array([[ 2768.03327821769, 1519.21878505205, 2060.98121494796, 812.166721782312 ], [ 812.166721782312, 2060.98121494796, 1519.21878505205, 2768.03327821769 ]]).T muV0 = np.load('peaks0.npy') piV0 = np.array([0.25, 0.25, 0.25, 0.25]) covarV0 = np.array([[[80300.0, 0.0], [0.0, 80300.0]], [[80300.0, 0.0], [0.0, 80300.0]], [[80300.0, 0.0], [0.0, 80300.0]], [[80300.0, 0.0], [0.0, 80300.0]]]) gmdistr = GMM(n_components=4, covariance_type='full') gmdistr.weights_ = piV0 gmdistr.means_ = muV0 gmdistr.covars_ = covarV0 resistRv = Lognormal('r', mean=1790.1, stdv=283.37) rDistr = resistRv.rv #r_array = np.array([stats.norm(loc=1790.1, scale=283.37), stats.norm(loc=1790.1, scale=283.37)]) r_array = np.array([rDistr, rDistr]) sl_array = np.array([stats.norm(loc=301.1, scale=120.44)]) ti_mean = 15.91 a = 0.0135 b = 0.8580 gd = lambda x: (1 - a * (x - ti_mean)**b) if x > ti_mean else 1.0 gd_array = np.array([gd, gd]) k_array = np.arange(2.0, 1.0 - 0.05 / 2, -0.05) testSmp = PreSmp(nadp, nsmp, gmdistr, r_array, sl_array, gd_array) pf, Spf2 = testSmp.adaptation(ti) piVopt = testSmp.gmdistr.weights_
def getPreSmpObject(analysis_type, new_service_time, frp_mean_history, frp_cov_history): # time array time_array = np.arange(RELIABILITY_DT,SERVICE_LIFE+RELIABILITY_DT,RELIABILITY_DT) # resistance array and live load array flexure_mean_history = frp_mean_history[0] flexure_cov_history = frp_cov_history[0] shear_mean_history = frp_mean_history[1] shear_cov_history = frp_cov_history[1] rm_mean = flexure_mean_history[0] rv_mean = shear_mean_history[0] r_array = np.array([rm_mean, rv_mean]) slmRv = Normal('slm', mean=M_LLIM_MEAN, stdv=M_LLIM_MEAN*M_LLIM_COV) slvRv = Normal('slv', mean=V_LLIM_MEAN, stdv=V_LLIM_MEAN*V_LLIM_COV) sldRv = Normal('sld', mean=M_LLIM_DECK_MEAN, stdv=M_LLIM_DECK_MEAN*M_LLIM_DECK_COV) slmDistr = slmRv.rv slvDistr = slvRv.rv sldDistr = sldRv.rv sl_array = np.array([slmDistr, slvDistr, sldDistr]) rate_m = LL_ARRIVAL_RATE rate_v = LL_ARRIVAL_RATE rate_d = LL_ARRIVAL_RATE_DECK rate_array = np.array([rate_m, rate_v, rate_d]) # function gd(t) and gcov(t) #def gd_func(x, a, b): # return a+b*x def gd_func(x, a, b, c, d, e): return a+b*x**1+c*x**2+d*x**3+e*x**4 gt_flex = flexure_mean_history/flexure_mean_history[0] / np.sqrt(1+flexure_cov_history**2) gt_shear = shear_mean_history/shear_mean_history[0] / np.sqrt(1+shear_cov_history**2) gd_flex_popt, pcov = curve_fit(gd_func, new_service_time, gt_flex) gd_shear_popt,pcov = curve_fit(gd_func, new_service_time, gt_shear) gd_flexure = lambda x: gd_func(x, gd_flex_popt[0], gd_flex_popt[1], gd_flex_popt[2], gd_flex_popt[3], gd_flex_popt[4]) gd_shear = lambda x: gd_func(x, gd_shear_popt[0], gd_shear_popt[1], gd_shear_popt[2], gd_shear_popt[3], gd_shear_popt[4]) #gd_flexure = lambda x: gd_func(x, gd_flex_popt[0], gd_flex_popt[1]) #gd_shear = lambda x: gd_func(x, gd_shear_popt[0], gd_shear_popt[1]) def gcov_func(x, a, b): # dummy function, not used return a+b*x at_flex = np.sqrt(np.log(flexure_cov_history**2+1)/COVT0_COV**2) at_shear = np.sqrt(np.log(shear_cov_history**2+1)/COVT0_COV**2) gcov_flex_popt, pcov = curve_fit(gcov_func, new_service_time, at_flex) gcov_shear_popt,pcov = curve_fit(gcov_func, new_service_time, at_shear) #gcov_flexure = lambda x: gcov_func(x, gcov_flex_popt[0], gcov_flex_popt[1], gcov_flex_popt[2], gcov_flex_popt[3], gcov_flex_popt[4]) #gcov_shear = lambda x: gcov_func(x, gcov_shear_popt[0], gcov_shear_popt[1], gcov_shear_popt[2], gcov_shear_popt[3], gcov_shear_popt[4]) gcov_flexure = lambda x: at_flex[0] + 0*x gcov_shear = lambda x: at_shear[0] + 0*x gd_array = np.array([gd_flexure, gd_shear]) gcov_array = np.array([gcov_flexure, gcov_shear]) #plt.close('all') #plt.figure() #plt.plot(new_service_time, gt_flex, 'bo', # new_service_time, gt_shear, 'rs', # new_service_time, gd_flexure(new_service_time), 'b-', # new_service_time, gd_shear(new_service_time), 'r-') #plt.figure() #plt.plot(new_service_time, at_flex, 'bo', # new_service_time, at_shear, 'rs', # new_service_time, gcov_flexure(new_service_time), 'b-', # new_service_time, gcov_shear(new_service_time), 'r-') #plt.show() # initial peaks corrTarget = np.eye(2) cov_variable1 = Normal('cov1', mean=0., stdv=COVT0_COV) cov_rv1 = cov_variable1.rv cov_variable2 = Normal('cov2', mean=0., stdv=COVT0_COV) cov_rv2 = cov_variable2.rv rv_array = np.array([cov_rv1, cov_rv2]) peaks0 = getInitialPeaks(rv_array, NUM_COMPONENT, corrTarget=corrTarget, options={'disp':False, 'anneal':True}) weights0 = 1./NUM_COMPONENT * np.ones(NUM_COMPONENT) covar0 = getInitialCovar(rv_array, NUM_COMPONENT) # initial values gmdistr = GMM(n_components=NUM_COMPONENT , covariance_type='full') gmdistr.weights_ = weights0 gmdistr.means_ = peaks0 gmdistr.covars_ = covar0 # CE-based smpling nTimePoint = time_array.size #pfpre = np.zeros(nTimePoint) #pfmain = np.zeros(nTimePoint) Spf2pre = np.zeros(nTimePoint) Spf2main = np.zeros(nTimePoint) kopt = np.zeros(nTimePoint) #pf = np.zeros(nTimePoint) #Spf2 = np.zeros(nTimePoint) # preliminary sampling preSmp = PreSmp(analysis_type, NUM_ADAPTATION, NUM_PRE_SMP, gmdistr, rv_array, r_array, sl_array, rate_array, gd_array, gcov_array) return preSmp
def trainingGMMHMM( dataset, # training dataset. n_c, # number of hmm's components (ie. hidden states) n_m, # number of gmm's mixtures (ie. Gaussian model) start_prob_prior=None, # prior of start hidden states probabilities. trans_mat_prior=None, # prior of transition matrix. start_prob=None, # the start hidden states probabilities. trans_mat=None, # the transition matrix. gmms=None, # models' params of gmm covar_type='full', n_i=50): # Initiation of dataset. # d = Dataset(dataset) X = dataset.getDataset() # Initiation of GMM. _GMMs = [] if gmms is None: _GMMs = None else: for gmm in gmms: _GMM = GMM(n_components=n_m, covariance_type=covar_type) _GMM.covars_ = np.array(gmm["covars"]) _GMM.means_ = np.array(gmm["means"]) _GMM.weights_ = np.array(gmm["weights"]) _GMMs.append(_GMM) # Initiation of GMMHMM. model = GMMHMM(startprob_prior=np.array(start_prob_prior), transmat_prior=np.array(trans_mat_prior), startprob=np.array(start_prob), transmat=np.array(trans_mat), gmms=_GMMs, n_components=n_c, n_mix=n_m, covariance_type=covar_type, n_iter=n_i) # Training. model.fit(X) # The result. new_gmmhmm = { "nComponent": n_c, "nMix": n_m, "covarianceType": covar_type, "hmmParams": { "startProb": model.startprob_.tolist(), "transMat": model.transmat_.tolist() }, "gmmParams": { "nMix": n_m, "covarianceType": covar_type, "params": [] } } for i in range(0, n_m): gaussian_model = { "covars": model.gmms_[i].covars_.tolist(), "means": model.gmms_[i].means_.tolist(), "weights": model.gmms_[i].weights_.tolist() } new_gmmhmm["gmmParams"]["params"].append(gaussian_model) return new_gmmhmm