def run_freq(self, Vf, Wf=None, Zf=None, Hf=None, niter=5): self.freq_analyzer.V = Vf if Wf is None or Zf is None or Hf is None: initW, initZ, initH = self.freq_analyzer.initialize() if Wf is None: Wf = initW if Zf is None: Zf = initZ if Hf is None: Hf = initH for iter in xrange(niter): logprob, WZH = self.freq_analyzer.do_estep(Wf, Zf, Hf) logger.info('Iteration f%d: logprob = %f', iter, logprob) Wf, Zf, Hf = self.freq_analyzer.do_mstep(iter) plt.clf() plt.plot(Wf[...,0]) plt.draw() # Hf : (rank, F, rank*T) # Hf_sum : (rank, F, T) meta_Hf = self.sum_pieces(Hf) meta_logprob, meta_WZH = self.meta_freq_analyzer.do_estep( Wf, Zf, meta_Hf) logger.info('Meta f%d: logprob = %f', iter, logprob) meta_Wf, meta_Zf, meta_Hf = self.meta_freq_analyzer.do_mstep(0) return Wf, Zf, Hf, meta_Hf
def run_time(self, Vt, Wt=None, Zt=None, Ht=None, niter=5): self.time_analyzer.V = Vt if Wt is None or Zt is None or Ht is None: initW, initZ, initH = self.time_analyzer.initialize() if Wt is None: Wt = initW if Zt is None: Zt = initZ if Ht is None: Ht = initH for iter in xrange(niter): logprob, WZH = self.time_analyzer.do_estep(Wt, Zt, Ht) logger.info('Iteration t%d: logprob = %f', iter, logprob) Wt, Zt, Ht = self.time_analyzer.do_mstep(iter) assert Wt.ndim == 3 assert Zt.ndim == 1 assert Ht.ndim == 2 # Ht : (rank, rank*F*T) # Ht_sum : (rank, F*T) meta_Ht = self.sum_pieces(Ht) Htt = meta_Ht.reshape(self.rank, self.f_steps, self.t_steps) Hmax = np.max(Htt) plt.clf() plt.imshow(np.rollaxis(Htt[0:3]/Hmax*2, 0, 3), origin='lower', aspect='auto', interpolation='nearest') plt.draw() meta_logprob, meta_WZH = self.meta_time_analyzer.do_estep(Wt, Zt, meta_Ht) logger.info('Meta t%d: logprob = %f', iter, logprob) meta_Wt, meta_Zt, meta_Ht = self.meta_time_analyzer.do_mstep(0) return Wt, Zt, Ht, meta_Ht
def plot(self, V, Wf, Wt, Z, H, curriter=-1): rank = len(Z) nrows = rank + 2 WZH = self.reconstruct(Wf, Wt, Z, H, circular=self.circular) W = Basilica.w_product(Wf, Wt) plottools.plotall([V, WZH] + [SIPLCA2.reconstruct(W[:,z,:], Z[z], H[z,:], circular=self.circular) for z in xrange(len(Z))], title=['V (Iteration %d)' % curriter, 'Reconstruction'] + ['Basis %d reconstruction' % x for x in xrange(len(Z))], colorbar=False, grid=False, cmap=plt.cm.hot, subplot=(nrows, 2), order='c', align='xy') plottools.plotall([None] + [Z], subplot=(nrows, 2), clf=False, plotfun=lambda x: plt.bar(np.arange(len(x)) - 0.4, x), xticks=[[], range(rank)], grid=False, colorbar=False, title='Z') plots = [None] * (3*nrows + 2) titles = plots + ['W%d' % x for x in range(rank)] wxticks = [[]] * (3*nrows + rank + 1) + [range(0, W.shape[2], 10)] plots.extend(W.transpose((1, 0, 2))) plottools.plotall(plots, subplot=(nrows, 6), clf=False, order='c', align='xy', cmap=plt.cm.hot, colorbar=False, ylabel=r'$\parallel$', grid=False, title=titles, yticks=[[]], xticks=wxticks) plots = [None] * (2*nrows + 2) titles=plots + ['H%d' % x for x in range(rank)] if np.squeeze(H).ndim < 4: plotH = np.squeeze(H) else: plotH = H.sum(2) if rank == 1: plotH = [plotH] plots.extend(plotH) plottools.plotall(plots, subplot=(nrows, 3), order='c', align='xy', grid=False, clf=False, title=titles, yticks=[[]], colorbar=False, cmap=plt.cm.hot, ylabel=r'$*$', xticks=[[]]*(3*nrows-1) + [range(0, V.shape[1], 100)]) plt.draw()