示例#1
0
文件: plot.py 项目: mschachter/prorn
def plot_fmc_by_perf(pdata, perf_attr='logit_perf', use_dlyap=False, npts=15, weight=False, vmax=None):
    
    num_plots = 25
    
    indx_off = [0, len(pdata)-num_plots]
    weights = [[], []]
    inputs = [[], []]
    fmcs = [[], []]
    fmc_max = -np.Inf
    for k,offset in enumerate(indx_off):
        pend = offset + num_plots
        for m,p in enumerate(pdata[offset:pend]):
            weights[k].append(p.net.W)
            inputs[k].append(p.net.Win)
            J = fisher_memory_matrix(p.net.W, p.net.Win, use_dlyap=use_dlyap, npts=npts)
            fmc = np.diag(J)
            fmc[fmc < 0.0] = 0.0
            fmcs[k].append(fmc)
            fmc_max = max(fmc_max, fmc.max())
    
    perrow = int(np.sqrt(num_plots))
    percol = perrow
    
    for j,offset in enumerate(indx_off):
        fig = plt.figure()
        fig.subplots_adjust(wspace=0.1, hspace=0.1)    
        for k in range(num_plots):
            W = weights[j][k]
            v = inputs[j][k]
            fmc = fmcs[j][k]
            
            if vmax is None:
                vmax = fmc_max
            
            if weight:
                indx = np.arange(npts) + 2.0
                w = np.log2(indx)
                jsum = (w * np.abs(fmc)).sum()
            else:
                jsum = fmc.sum()
            
            ax = fig.add_subplot(perrow, percol, k)
            ax.plot(fmc, 'k-')
            ax.set_ylim([0, vmax])
            ax.set_title('%0.3f' % jsum)            
            plt.xticks([], [])
            plt.yticks([], [])
            
            p = pdata[offset + k]
            
        if offset == 0:
            plt.suptitle('Fisher Memory Curves of Top %d Networks' % num_plots)
        else:
            plt.suptitle('Fisher Memory Curves of Bottom %d Networks' % num_plots)
            
    plt.show()    
示例#2
0
文件: plot.py 项目: mschachter/prorn
def plot_fmm_by_perf(pdata, perf_attr='logit_perf', use_dlyap=False, npts=15, num_plots=25, vbounds=None):
    
    num_plots = num_plots
    
    indx_off = [0, len(pdata)-num_plots]
    weights = [[], []]
    inputs = [[], []]
    perfs = [[], []] 
    fmms = [[], []]
    max_fmm = -np.Inf    
    min_fmm = np.Inf
    for k,offset in enumerate(indx_off):
        pend = offset + num_plots
        for m,p in enumerate(pdata[offset:pend]):
            weights[k].append(p.W)
            inputs[k].append(p.Win)
            perfs[k].append(p.logit_perf)
            J = fisher_memory_matrix(p.net.W, p.net.Win, npts=npts, use_dlyap=use_dlyap)
            fmms[k].append(J)
            max_fmm = max(max_fmm, J.max())
            min_fmm = min(min_fmm, J.min())            
    
    perrow = int(np.sqrt(num_plots))
    percol = perrow
    
    for j,offset in enumerate(indx_off):
        fig = plt.figure()
        fig.subplots_adjust(wspace=0.1, hspace=0.1)    
        for k in range(num_plots):            
            J = fmms[j][k]
            
            if vbounds is None:
                vbounds = [min_fmm, max_fmm]
            ax = fig.add_subplot(perrow, percol, k)
            res = ax.imshow(J, interpolation='nearest', cmap=cm.jet, vmin=vbounds[0], vmax=vbounds[1])
            #fig.colorbar(res)
            #ax.set_title('%0.2f' % perfs[j][k])            
            plt.xticks([], [])
            plt.yticks([], [])
            
            p = pdata[offset + k]
            
        if offset == 0:
            plt.suptitle('Fisher Memory Matricies of Top %d Networks' % num_plots)
        else:
            plt.suptitle('Fisher Memory Matricies of Bottom %d Networks' % num_plots)
            
    plt.show()    
示例#3
0
文件: plot.py 项目: mschachter/prorn
def plot_fmm_single(perf_data, npts=50):
    
    J = fisher_memory_matrix(perf_data.net.W, perf_data.net.Win, npts=npts, use_dlyap=False)
    plt.figure()
    fig = plt.gcf()
    ax = fig.add_subplot(1, 1, 1)
    res = ax.imshow(J, interpolation='nearest', cmap=cm.jet)
    fig.colorbar(res)
    ax.set_xlabel('Timestep')
    ax.set_ylabel('Timestep')
    #plt.xticks([], [])
    #plt.yticks([], [])
    
    fmc = np.diag(J)
    fmc[fmc < 0.0] = 0.0
    t = np.arange(len(fmc))
    plt.figure()
    plt.plot(t, fmc, 'k-', linewidth=3.0)
    ax.set_xlabel('Timestep')
    ax.set_ylabel('Memory')
    plt.axis('tight')
示例#4
0
文件: plot.py 项目: mschachter/prorn
def plot_perf_by_jsum(pdata, perf_attr='logit_perf', use_dlyap=False, use_abs=True, npts=25):
    
    perfs = []
    jtots = []
    for m,p in enumerate(pdata):
        W = p.net.W
        Win = p.net.Win
        v = Win.squeeze()
        J = fisher_memory_matrix(W, v, use_dlyap=use_dlyap, npts=npts)
        jt_vals = J[np.triu_indices(len(J))] 
        if use_abs:
            jt_vals = np.abs(jt_vals)
        jusum = jt_vals.sum()
        perfs.append(getattr(p, perf_attr))
        jtots.append(jusum)
    
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    ax.plot(jtots, perfs, 'ko')
    ax.set_title('Upper Triangular Sum of J vs Performance')
    plt.xlabel('UT Sum')
    plt.ylabel('Performance')
    plt.show()
示例#5
0
文件: plot.py 项目: mschachter/prorn
def plot_perf_by_jtot(pdata, perf_attr='logit_perf', use_dlyap=False, npts=15, weight=False):
    
    perfs = []
    jtots = []
    for m,p in enumerate(pdata):
        J = fisher_memory_matrix(p.W, p.Win, use_dlyap=use_dlyap, npts=npts)
        fmc = np.diag(J)
        fmc[fmc < 0.0] = 0.0
        perfs.append(getattr(p, perf_attr))
        if weight:
            indx = np.arange(npts) + 1.0
            w = np.log2(indx)
            jsum = (w * fmc).sum()
        else:
            jsum = fmc.sum() 
        jtots.append(jsum)
    
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    ax.plot(jtots, perfs, 'ko')
    ax.set_title('Jtot vs Performance')
    plt.xlabel('Jtot')
    plt.ylabel('Performance')
    plt.show()
示例#6
0
def compute_net_metrics(pdata, output_file):
    
    npts = 40
    
    data = []
    
    for p in pdata:
        J = fisher_memory_matrix(p.net.W, p.net.Win, use_dlyap=False, npts=npts)
        
        feature_names = []
        features = []
        
        #compute weighted jtot
        fmc = np.diag(J)
        fmc[fmc < 0.0] = 0.0
        indx = np.arange(len(fmc)) + 1.0
        w = np.log2(indx)
        wjtot = (w * fmc).sum()
        feature_names.append('wjtot')
        features.append(wjtot)
        
        #compute upper-triangular abs sum
        jt_vals = np.abs(J[np.triu_indices(len(J))]) 
        fmm_sum = jt_vals.sum()
        feature_names.append('fmm_sum')
        features.append(fmm_sum)
        
        #compute arc length of convex hull around pseudospectra
        bounds=[-3, 3, -3, 3]
        for eps in [0.5, 0.1]:
            (X, Y, Z, smin) = compute_pseudospectra(p.net.W, bounds=bounds, npts=75, invert=False)
            arclen = np.NAN
            if np.sum(smin < eps) > 0:
                arclen = 0.0                
                xvals = X[smin < eps].ravel()
                yvals = Y[smin < eps].ravel()
                pnts = zip(xvals, yvals)
                ch = np.array(convex_hull(pnts))
                
                for m in range(ch.shape[0]):
                    if m == 0:
                        p1 = ch[-1, :]
                    else:
                        p1 = ch[m-1, :]
                    p2 = ch[m, :]
                    arclen += np.linalg.norm(p2 - p1)
            feature_names.append('arclen_%0.2f' % eps)
            features.append(arclen)
        
        #compute eigenvalues/schur decomposition
        (T, U, sdim) = scipy.linalg.schur(p.net.W, 'complex', sort='rhp')
        
        evals = np.diag(T)
        for k,ev in enumerate(evals):
            feature_names.append('ev%d_real' % k)
            features.append(ev.real)
            feature_names.append('ev%d_imag' % k)
            features.append(ev.imag)
        
        for i in range(p.net.W.shape[0]):
            for j in range(i):
                if i != j:
                    od = T[j, i]
                    feature_names.append('od%d%d_real' % (j, i))
                    features.append(od.real)
                    feature_names.append('od%d%d_imag' % (j, i))
                    features.append(od.imag)
        
        feature_names.append('perf')
        features.append(p.logit_perf)
        
        data.append(features)
    
    f = open(output_file, 'w')
    f.write('%s\n' % ','.join(feature_names))
    for row in data:
        fstr = ['%0.12f' % x for x in row]
        f.write('%s\n' % ','.join(fstr))
    f.close()