コード例 #1
0
 def init():
     offset = 2.0
     #if optimum[0] < np.inf:
     #    xmin = min(results['ADAM'][0][0], optimum[0]) - offset
     #    xmax = max(results['ADAM'][0][0], optimum[0]) + offset
     #else:
     xmin = domain[0, 0]
     xmax = domain[0, 1]
     #if optimum[1] < np.inf:
     #    ymin = min(results['ADAM'][1][0], optimum[1]) - offset
     #    ymax = max(results['ADAM'][1][0], optimum[1]) + offset
     #else:
     ymin = domain[1, 0]
     ymax = domain[1, 1]
     x = np.arange(xmin, xmax, 0.01)
     y = np.arange(ymin, ymax, 0.01)
     X, Y = np.meshgrid(x, y)
     Z = np.zeros(np.shape(Y))
     for a, _ in np.ndenumerate(Y):
         Z[a] = func(X[a], Y[a])
     level = fdict['level']
     if level is None:
         level = np.linspace(Z.min(), Z.max(), 20)
     else:
         if level[0] == 'normal':
             level = np.linspace(Z.min(), Z.max(), level[1])
         if level[0] == 'log':
             level = np.logspace(np.log(Z.min()), np.log(Z.max()), level[1])
     CF = ax[0].contour(X,Y,Z, levels=level)
     #plt.colorbar(CF, orientation='horizontal', format='%.2f')
     ax[0].grid()
     ax[0].plot(results['ADAM'][0][0], results['ADAM'][1][0], 
         'h', markersize=15, color = '0.75')
     if optimum[0] < np.inf and optimum[1] < np.inf:
         ax[0].plot(optimum[0], optimum[1], '*', markersize=40, 
             markeredgewidth = 2, alpha = 0.5, color = '0.75')
     ax[0].legend(loc='upper center', ncol=3, bbox_to_anchor=(0.5, 1.15))
     
     ax[1].plot(0, results['ADAM'][2][0], 'o')
     ax[1].axis([0, T, -0.5, max_err + 0.5])
     ax[1].set_xlabel('num. iteration')
     ax[1].set_ylabel('loss')
     
     line1.set_data([], [])
     line2.set_data([], [])
     line3.set_data([], [])
     line4.set_data([], [])
     line5.set_data([], [])
     
     err1.set_data([], [])
     err2.set_data([], [])
     err3.set_data([], [])
     err4.set_data([], [])
     err5.set_data([], [])
     
     return line1, line2, line3, line4, line5, \
         err1, err2, err3, err4, err5, 
コード例 #2
0
ファイル: fits_image.py プロジェクト: HIPS/DESI-MCMC
 def make_pixel_grid(self):
     """ makes a stack of points corresponding to each point in a pixel grid
         with input shape 
     """
     y_grid = np.arange(self.nelec.shape[0], dtype=np.float) + 1
     x_grid = np.arange(self.nelec.shape[1], dtype=np.float) + 1
     xx, yy = np.meshgrid(x_grid, y_grid, indexing='xy')
     # whenever we flatten and reshape use C ordering...
     return np.column_stack((xx.ravel(order='C'), yy.ravel(order='C')))
コード例 #3
0
ファイル: black_box_svi.py プロジェクト: AugustLONG/autograd
 def plot_isocontours(ax, func, xlimits=[-2, 2], ylimits=[-4, 2], numticks=101):
     x = np.linspace(*xlimits, num=numticks)
     y = np.linspace(*ylimits, num=numticks)
     X, Y = np.meshgrid(x, y)
     zs = func(np.concatenate([np.atleast_2d(X.ravel()), np.atleast_2d(Y.ravel())]).T)
     Z = zs.reshape(X.shape)
     plt.contour(X, Y, Z)
     ax.set_yticks([])
     ax.set_xticks([])
コード例 #4
0
ファイル: rj_proposals.py プロジェクト: HIPS/DESI-MCMC
    def create_pixel_grid(image, loc):
        v_s = image.equa2pixel(loc)
        bound = image.R
        minx_b, maxx_b = max(0, int(v_s[0] - bound)), min(int(v_s[0] + bound + 1), image.nelec.shape[1])
        miny_b, maxy_b = max(0, int(v_s[1] - bound)), min(int(v_s[1] + bound + 1), image.nelec.shape[0])
        y_grid = np.arange(miny_b, maxy_b, dtype=np.float)
        x_grid = np.arange(minx_b, maxx_b, dtype=np.float)
        xx, yy = np.meshgrid(x_grid, y_grid, indexing='xy')
        pixel_grid = np.column_stack((xx.ravel(order='C'), yy.ravel(order='C')))

        return xx.astype(int), yy.astype(int),pixel_grid
コード例 #5
0
ファイル: pylqr_trajctrl.py プロジェクト: navigator8972/pylqr
def PyLQR_TrajCtrl_GeneralTest():
    #build RBF basis
    rbf_basis = np.array([
        [-1.0, -1.0],
        [-1.0, 1.0],
        [1.0, -1.0],
        [1.0, 1.0]
        ])
    gamma = 1
    T = 100
    R = 1e-5
    # rbf_funcs = [lambda x, u, t, aux: np.exp(-gamma*np.linalg.norm(x[0:2]-basis)**2) + .01*np.linalg.norm(u)**2 for basis in rbf_basis]
    rbf_funcs = [
    lambda x, u, t, aux: -np.exp(-gamma*np.linalg.norm(x[0:2]-rbf_basis[0])**2) + R*np.linalg.norm(u)**2,
    lambda x, u, t, aux: -np.exp(-gamma*np.linalg.norm(x[0:2]-rbf_basis[1])**2) + R*np.linalg.norm(u)**2,
    lambda x, u, t, aux: -np.exp(-gamma*np.linalg.norm(x[0:2]-rbf_basis[2])**2) + R*np.linalg.norm(u)**2,
    lambda x, u, t, aux: -np.exp(-gamma*np.linalg.norm(x[0:2]-rbf_basis[3])**2) + R*np.linalg.norm(u)**2
    ]

    weights = np.array([.75, .5, .25, 1.])
    weights = weights / (np.sum(weights) + 1e-6)

    cost_func = lambda x, u, t, aux: np.sum(weights * np.array([basis_func(x, u, t, aux) for basis_func in rbf_funcs]))

    lqr_traj_ctrl = PyLQR_TrajCtrl(use_autograd=True)
    lqr_traj_ctrl.build_ilqr_general_solver(cost_func, n_dims=rbf_basis.shape[1], T=T)

    n_eval_pnts = 50
    coords = np.linspace(-2.5, 2.5, n_eval_pnts)
    xv, yv = np.meshgrid(coords, coords)

    z = [[cost_func(np.array([xv[i, j], yv[i, j]]), np.zeros(2), None, None) for j in range(yv.shape[1])] for i in range(len(xv))]

    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.hold(True)
    ax.contour(xv, yv, z)
    
    n_queries = 5
    u_array = np.random.rand(2, T-1).T * 2 - 1
    
    for i in range(n_queries):
        #start from a perturbed point
        x0 = np.random.rand(2) * 4 - 2
        syn_traj = lqr_traj_ctrl.synthesize_trajectory(x0, u_array)
        #plot it
        ax.plot([x0[0]], [x0[1]], 'k*', markersize=12.0)
        ax.plot(syn_traj[:, 0], syn_traj[:, 1], linewidth=3.5)

    plt.show()

    return
コード例 #6
0
def plot_error_surface(loss_fun, params, ax=None):
    if ax is None:
        fig = plt.figure()
        ax = fig.add_subplot(111)
    w0s = np.linspace(-2*params[0], 2*params[0], 10)
    w1s = np.linspace(-2*params[1], 2*params[1], 10)
    w0_grid, w1_grid = np.meshgrid(w0s, w1s)
    lossvec = np.vectorize(loss_fun)
    z = lossvec(w0_grid, w1_grid)
    cs = ax.contour(w0s, w1s, z)
    ax.clabel(cs)
    ax.plot(params[0], params[1], 'rx', markersize=14)
    return ax
コード例 #7
0
ファイル: rosen_demo.py プロジェクト: 9578577/pmtk3
def plot_trace(ps, ttl):
    x = np.linspace(-5, 5, 100)
    y = np.linspace(-5, 5, 100)
    X, Y = np.meshgrid(x, y)
    Z = rosen(np.vstack([X.ravel(), Y.ravel()])).reshape((100,100))
    ps = np.array(ps)
    plt.figure(figsize=(12,4))
    plt.subplot(121)
    plt.contour(X, Y, Z, np.arange(10)**5)
    plt.plot(ps[:, 0], ps[:, 1], '-o')
    plt.plot(1, 1, 'r*', markersize=12) # global minimum
    plt.subplot(122)
    plt.semilogy(range(len(ps)), rosen(ps.T))
    plt.title(ttl)
コード例 #8
0
ファイル: linreg_1d_plot_demo.py プロジェクト: 9578577/pmtk3
def plot_error_surface(xtrain, ytrain, model, ax=None):
    params = model.params
    if ax is None:
        fig = plt.figure()
        ax = fig.add_subplot(111)
    w0s = np.linspace(-2*params[0], 2*params[0], 10)
    w1s = np.linspace(-2*params[1], 2*params[1], 10)
    w0_grid, w1_grid = np.meshgrid(w0s, w1s)
    def loss(w0, w1):
        return model.objective([w0, w1], xtrain, ytrain)
    lossvec = np.vectorize(loss)
    z = lossvec(w0_grid, w1_grid)
    cs = ax.contour(w0s, w1s, z)
    ax.clabel(cs)
    ax.plot(params[0], params[1], 'rx', markersize=14)
コード例 #9
0
def plot_true_posterior():
    true_posterior_contour_levels = [0.01, 0.2, 1.0, 10.0]

    x = np.linspace(*xlimits, num=200)
    y = np.linspace(*ylimits, num=200)
    X, Y = np.meshgrid(x, y)

    fig = plt.figure(0); fig.clf()
    fig.set_size_inches((5,4))
    ax = fig.add_subplot(111)
    zs = np.array([nllfun(np.concatenate(([x],[y]))) for x,y in zip(np.ravel(X), np.ravel(Y))])
    Z = zs.reshape(X.shape)
    plt.contour(X, Y, np.exp(-Z), true_posterior_contour_levels, colors='k')
    ax.set_yticks([])
    ax.set_xticks([])
    return ax
コード例 #10
0
ファイル: fluidsim.py プロジェクト: shuangao/autograd
def advect(f, vx, vy):
    """Move field f according to x and y velocities (u and v)
       using an implicit Euler integrator."""
    rows, cols = f.shape
    cell_ys, cell_xs = np.meshgrid(np.arange(rows), np.arange(cols))
    center_xs = (cell_xs - vx).ravel()
    center_ys = (cell_ys - vy).ravel()

    # Compute indices of source cells.
    left_ix = np.floor(center_xs).astype(np.int)
    top_ix  = np.floor(center_ys).astype(np.int)
    rw = center_xs - left_ix              # Relative weight of right-hand cells.
    bw = center_ys - top_ix               # Relative weight of bottom cells.
    left_ix  = np.mod(left_ix,     rows)  # Wrap around edges of simulation.
    right_ix = np.mod(left_ix + 1, rows)
    top_ix   = np.mod(top_ix,      cols)
    bot_ix   = np.mod(top_ix  + 1, cols)

    # A linearly-weighted sum of the 4 surrounding cells.
    flat_f = (1 - rw) * ((1 - bw)*f[left_ix,  top_ix] + bw*f[left_ix,  bot_ix]) \
                 + rw * ((1 - bw)*f[right_ix, top_ix] + bw*f[right_ix, bot_ix])
    return np.reshape(flat_f, (rows, cols))
コード例 #11
0
ファイル: models.py プロジェクト: HIPS/DESI-MCMC
    def resample_photons(self, srcs, verbose=False):
        """resample photons - store source-specific images"""
        # first, clear out old sample images
        for src in srcs:
            src.clear_sample_images()

        # generate per-source sample image patch for each fits image in
        # this field.  keep track of photons due to noise
        noise_sums = {}
        for band, img in self.img_dict.iteritems():
            if verbose:
                print " ... resampling band %s " % band
            samp_imgs, noise_sum = \
                cel_mcmc.sample_source_photons_single_image_cython(
                    img, [s.params for s in srcs]
                )

            # tell each source to keep track of it's source-specific sampled
            # images (and the image it was stripped out of)
            for src, samp_img in zip(srcs, samp_imgs):
                if samp_img is not None:

                    # cache pixel grid for each sample image
                    y_grid = np.arange(samp_img.y0, samp_img.y1, dtype=np.float)
                    x_grid = np.arange(samp_img.x0, samp_img.x1, dtype=np.float)
                    xx, yy = np.meshgrid(x_grid, y_grid, indexing='xy')
                    pixel_grid = np.column_stack((xx.ravel(order='C'), yy.ravel(order='C')))
                    src.sample_image_list.append((samp_img, img, pixel_grid))

            # keep track of noise sums
            noise_sums[band] = noise_sum

        # resample noise parameter in each fits image
        for band, img in self.img_dict.iteritems():
            a_n         = self.a_0 + noise_sums[band]
            b_n         = self.b_0 + img.nelec.size
            eps_tmp     = img.epsilon
            img.epsilon = np.random.gamma(a_n, 1./b_n)
コード例 #12
0
ファイル: PDE.py プロジェクト: imyoungmin/DENN
    # Get maximum error.
    maxError = 0
    for xp in points:
        y_a = phi_a(xp)
        y_t = phi_t(xp, optimizedParams)
        error = np.abs(y_a - y_t)
        if error > maxError:
            maxError = error
    print("Max error:", maxError)

    # Plot solutions.

    # Make data.
    X = np.linspace(MinVal, MaxVal, 100)
    Y = np.linspace(MinVal, MaxVal, 100)
    X, Y = np.meshgrid(X, Y)
    Z_a = np.zeros(X.shape)  # Analytic solution.
    Z_t = np.zeros(X.shape)  # Trial solution with neural network.
    Z_e = np.zeros(X.shape)  # Error surface.
    d2Z_a_x12 = np.zeros(X.shape)  # Second derivatives of analytic solution.
    d2Z_a_x22 = np.zeros(X.shape)
    d2Z_t_x12 = np.zeros(X.shape)  # Second derivatives of trial solution.
    d2Z_t_x22 = np.zeros(X.shape)
    d2Z_e_x12 = np.zeros(
        X.shape)  # Error surface for second derivatives with respect to x_1.
    d2Z_e_x22 = np.zeros(
        X.shape)  # Error surface for second derivatives with respect to x_2.
    for ii in range(X.shape[0]):
        for jj in range(X.shape[1]):
            v = np.array([X[ii][jj], Y[ii][jj]])  # Input values.
            Z_a[ii][jj] = phi_a(v)  # Evalutating solutions.
コード例 #13
0
ファイル: synth.py プロジェクト: aasensio/DNHazel
    def __init__(self, NSIDE, npix, clv=True):
        """
        Args:
            NSIDE (int) : the healpix NSIDE parameter, must be a power of 2, less than 2**30
            npix (int) : number of pixel in the X and Y axis of the final projected map
            rot_velocity (float) : rotation velocity of the star in the equator in km/s
        
        Returns:
            None
        """
        self.NSIDE = int(NSIDE)
        self.npix = int(npix)
        self.hp_npix = hp.nside2npix(NSIDE)

        # self.rot_velocity = rot_velocity
        self.clv = clv

# Generate the indices of all healpix pixels
        self.indices = np.arange(hp.nside2npix(NSIDE), dtype='int')
        self.n_healpix_pxl = len(self.indices)

# Define the orthographic projector that generates the maps of the star on the plane of the sky
        self.projector = hp.projector.OrthographicProj(xsize=int(self.npix))

# This function returns the pixel associated with a vector (x,y,z). This is needed by the projector
        self.f_vec2pix = lambda x, y, z: hp.pixelfunc.vec2pix(int(self.NSIDE), x, y, z)

# Generate a mesh grid of X and Y points in the plane of the sky that covers only the observed hemisphere of the star
        x = np.linspace(-2.0,0.0,int(self.npix/2))
        y = np.linspace(-1.0,1.0,int(self.npix/2))
        X, Y = np.meshgrid(x,y)

# Rotational velocity vector (pointing in the z direction and unit vector)
        omega = np.array([0,0,1])

# Compute the radial vector at each position in the map and the projected velocity on the plane of the sky
        radial_vector = np.array(self.projector.xy2vec(X.flatten(), Y.flatten())).reshape((3,int(self.npix/2),int(self.npix/2)))        
        self.vel_projection = np.cross(omega[:,None,None], radial_vector, axisa=0, axisb=0)[:,:,0]

# Compute the mu angle (astrocentric angle)
        self.mu_angle = radial_vector[0,:,:]        
        
# Read all Kurucz models from the database. Hardwired temperature and mu angles
        print("Reading Kurucz spectra...")
        self.T = 3500 + 250 * np.arange(27)
        self.mus = np.array([1.0,0.9,0.8,0.7,0.6,0.5,0.4,0.3,0.2,0.1,0.05,0.02])[::-1]

        for i in tqdm(range(27)):
            f = 'kurucz_models/RESULTS/T_{0:d}_logg4.0_feh0.0.spec'.format(self.T[i])
            vel, _, spec = _read_kurucz_spec(f)

            if (i == 0):
                self.nlambda, self.nmus = spec.shape
                self.velocity = np.zeros((self.nlambda))
                self.spectrum = np.zeros((27,self.nmus,self.nlambda))

            self.velocity = vel
            self.spectrum[i,:,:] = spec[:,::-1].T

# Generate a fake temperature map in the star using spherical harmonics
        # self.temperature_map = 5000 * np.ones(self.npix)
        # self.temperature_map = 5000 + 250 * hp.sphtfunc.alm2map(np.ones(10,dtype='complex'),self.NSIDE) #np.random.rand(self.n_healpix_pxl) * 2000 + 5000 #

        self.temperature_map = 5000 * np.ones(self.hp_npix)
        self.coeffs = hp.sphtfunc.map2alm(self.temperature_map)

        self.velocity_per_pxl = self.velocity[1] - self.velocity[0]

        self.freq_grid = np.fft.fftfreq(self.nlambda)

        self.gradient = value_and_grad(self.loss)
    def static_N2_simple(self, w_best, runner, **kwargs):
        cost = runner.cost
        predict = runner.model
        full_predict = runner.full_model
        feat = runner.feature_transforms
        normalizer = runner.normalizer
        inverse_nornalizer = runner.inverse_normalizer
        x_train = inverse_nornalizer(runner.x_train).T
        y_train = runner.y_train

        x_test = inverse_nornalizer(runner.x_test).T
        y_test = runner.y_test

        # or just take last weights
        self.w = w_best

        # construct figure
        fig, axs = plt.subplots(1, 1, figsize=(9, 4))

        # create subplot with 2 panels
        gs = gridspec.GridSpec(1, 2, width_ratios=[3, 1])
        ax2 = plt.subplot(gs[0], aspect='equal')
        ax3 = plt.subplot(gs[1])
        ax3.axis('off')

        ### create boundary data ###
        xmin1 = np.min(self.x[:, 0])
        xmax1 = np.max(self.x[:, 0])
        xgap1 = (xmax1 - xmin1) * 0.05
        xmin1 -= xgap1
        xmax1 += xgap1

        xmin2 = np.min(self.x[:, 1])
        xmax2 = np.max(self.x[:, 1])
        xgap2 = (xmax2 - xmin2) * 0.05
        xmin2 -= xgap2
        xmax2 += xgap2

        # plot boundary for 2d plot
        r1 = np.linspace(xmin1, xmax1, 300)
        r2 = np.linspace(xmin2, xmax2, 300)
        s, t = np.meshgrid(r1, r2)
        s = np.reshape(s, (np.size(s), 1))
        t = np.reshape(t, (np.size(t), 1))
        h = np.concatenate((s, t), axis=1)

        # compute model on train data
        z1 = predict(normalizer(h.T), self.w)
        z1 = np.sign(z1)

        # reshape it
        s.shape = (np.size(r1), np.size(r2))
        t.shape = (np.size(r1), np.size(r2))
        z1.shape = (np.size(r1), np.size(r2))

        ### loop over two panels plotting each ###
        for ax in [ax2]:
            # plot training points
            ind0 = np.argwhere(y_train == +1)
            ind0 = [v[1] for v in ind0]
            ax.scatter(x_train[ind0, 0],
                       x_train[ind0, 1],
                       s=55,
                       color=self.colors[0],
                       edgecolor='k',
                       linewidth=2.5,
                       zorder=3)

            ind1 = np.argwhere(y_train == -1)
            ind1 = [v[1] for v in ind1]
            ax.scatter(x_train[ind1, 0],
                       x_train[ind1, 1],
                       s=55,
                       color=self.colors[1],
                       edgecolor='k',
                       linewidth=2.5,
                       zorder=3)

            # plot testing points
            ind0 = np.argwhere(y_test == +1)
            ind0 = [v[1] for v in ind0]
            ax.scatter(x_test[ind0, 0],
                       x_test[ind0, 1],
                       s=55,
                       color=self.colors[0],
                       edgecolor=[1, 0.8, 0.5],
                       linewidth=2.5,
                       zorder=3)

            ind1 = np.argwhere(y_test == -1)
            ind1 = [v[1] for v in ind1]
            ax.scatter(x_test[ind1, 0],
                       x_test[ind1, 1],
                       s=55,
                       color=self.colors[1],
                       edgecolor=[1, 0.8, 0.5],
                       linewidth=2.5,
                       zorder=3)

            #### plot contour, color regions ####
            ax.contour(s,
                       t,
                       z1,
                       colors='k',
                       linewidths=2.5,
                       levels=[0],
                       zorder=2)
            ax.contourf(s,
                        t,
                        z1,
                        colors=[self.colors[1], self.colors[0]],
                        alpha=0.15,
                        levels=range(-1, 2))

            # cleanup panel
            ax.set_xlabel(r'$x_1$', fontsize=15)
            ax.set_ylabel(r'$x_2$', fontsize=15, rotation=0, labelpad=20)
            ax.xaxis.set_major_formatter(FormatStrFormatter('%.1f'))
            ax.yaxis.set_major_formatter(FormatStrFormatter('%.1f'))
コード例 #15
0
def secant(func, anchor, tracer, ax):
    # evaluate function at anchor and tracer
    g_anchor = func(anchor)
    g_tracer = func(tracer)
    anchor_orig = copy.deepcopy(anchor)
    tracer_orig = copy.deepcopy(tracer)

    # determine non-zero component of tracer, compute slope of secant line
    anchor = anchor.flatten()
    tracer = tracer.flatten()
    ind = np.argwhere(tracer != 0)
    anchor = anchor[ind]
    tracer = tracer[ind]

    # plot secant plane
    color = 'lime'
    if abs(anchor - tracer) > 10**-4:
        # scatter tracer point
        ax.scatter(tracer_orig[0],
                   tracer_orig[1],
                   g_tracer,
                   s=50,
                   c='b',
                   edgecolor='k',
                   linewidth=1)

        # change color to red
        color = 'r'

        # plot visual guide for tracer
        w = np.linspace(0, g_tracer, 100)
        o = np.ones(100)
        ax.plot(o * tracer_orig[0],
                o * tracer_orig[1],
                w,
                linewidth=1.5,
                alpha=1,
                color='k',
                linestyle='--')

        w = np.linspace(0, g_anchor, 100)
        o = np.ones(100)
        ax.plot(o * anchor_orig[0],
                o * anchor_orig[1],
                w,
                linewidth=1.5,
                alpha=1,
                color='k',
                linestyle='--')

    # compute slope of secant plane
    slope = (g_anchor - g_tracer) / float(anchor - tracer)

    # create function for hyperplane connecting anchor to tracer
    w_tan = np.linspace(-2.5, 2.5, 200)

    w1tan_vals, w2tan_vals = np.meshgrid(w_tan, w_tan)
    w1tan_vals.shape = (len(w_tan)**2, 1)
    w2tan_vals.shape = (len(w_tan)**2, 1)
    wtan_vals = np.concatenate((w1tan_vals, w2tan_vals), axis=1).T

    # create tangent hyperplane formula, evaluate
    h = lambda w: g_anchor + slope * (w[ind] - anchor)
    h_vals = h(wtan_vals)

    # reshape everything and prep for plotting
    w1tan_vals.shape = (len(w_tan), len(w_tan))
    w2tan_vals.shape = (len(w_tan), len(w_tan))
    h_vals.shape = (len(w_tan), len(w_tan))

    # plot hyperplane and guides based on proximity of tracer to anchor
    ax.plot_surface(w1tan_vals,
                    w2tan_vals,
                    h_vals,
                    alpha=0.2,
                    color=color,
                    zorder=3,
                    rstride=50,
                    cstride=50,
                    linewidth=0.5,
                    edgecolor='k')
コード例 #16
0
    def show_complete_coloring(self, w_hist, **kwargs):
        '''
        # determine best set of weights from history
        cost_evals = []
        for i in range(len(w_hist)):
            W = w_hist[i]
            cost = self.counting_cost(W)
            cost_evals.append(cost)
        ind = np.argmin(cost_evals)
        '''

        # or just take last weights
        self.W = w_hist[-1]

        # initialize figure
        fig = plt.figure(figsize=(9, 4))

        show_cost = False
        if 'show_cost' in kwargs:
            show_cost = kwargs['show_cost']
        if show_cost == True:
            gs = gridspec.GridSpec(1,
                                   3,
                                   width_ratios=[1, 1, 1],
                                   height_ratios=[1])

            # create third panel for cost values
            ax3 = plt.subplot(gs[2], aspect='equal')

        else:
            gs = gridspec.GridSpec(1, 2, width_ratios=[1, 1])

        # setup current axis
        ax = plt.subplot(gs[0], aspect='equal')
        ax2 = plt.subplot(gs[1], aspect='equal')

        # generate input range for viewing range
        minx = min(min(self.x[0, :]), min(self.x[1, :]))
        maxx = max(max(self.x[0, :]), max(self.x[1, :]))
        gapx = (maxx - minx) * 0.1
        minx -= gapx
        maxx += gapx

        # plot panel with all data and separators
        self.plot_data(ax)
        self.plot_data(ax2)
        self.plot_all_separators(ax)

        ### draw multiclass boundary on right panel
        r = np.linspace(minx, maxx, 2000)
        w1_vals, w2_vals = np.meshgrid(r, r)
        w1_vals.shape = (len(r)**2, 1)
        w2_vals.shape = (len(r)**2, 1)
        o = np.ones((len(r)**2, 1))
        h = np.concatenate([o, w1_vals, w2_vals], axis=1)
        pts = np.dot(h, self.W)
        g_vals = np.argmax(pts, axis=1)

        # vals for cost surface
        w1_vals.shape = (len(r), len(r))
        w2_vals.shape = (len(r), len(r))
        g_vals.shape = (len(r), len(r))

        # plot contour
        C = len(np.unique(self.y))
        ax2.contour(w1_vals,
                    w2_vals,
                    g_vals,
                    colors='k',
                    levels=range(0, C + 1),
                    linewidths=2.75,
                    zorder=4)
        ax2.contourf(w1_vals,
                     w2_vals,
                     g_vals + 1,
                     colors=self.colors[:],
                     alpha=0.2,
                     levels=range(0, C + 1))
        ax.contourf(w1_vals,
                    w2_vals,
                    g_vals + 1,
                    colors=self.colors[:],
                    alpha=0.2,
                    levels=range(0, C + 1))

        # dress panel
        ax.set_xlim(minx, maxx)
        ax.set_ylim(minx, maxx)
        ax.set_xticks([])
        ax.set_yticks([])
        ax.set_yticklabels([])
        ax.set_xticklabels([])
        #ax.set_ylabel(r'$x_2$',rotation = 0,fontsize = 12,labelpad = 10)
        #ax.set_xlabel(r'$x_1$',fontsize = 12)

        ax2.set_xticks([])
        ax2.set_yticks([])
        ax2.set_yticklabels([])
        ax2.set_xticklabels([])
        ax2.set_xlim(minx, maxx)
        ax2.set_ylim(minx, maxx)
        #ax2.set_ylabel(r'$x_2$',rotation = 0,fontsize = 12,labelpad = 10)
        #ax2.set_xlabel(r'$x_1$',fontsize = 12)

        if show_cost == True:
            # compute cost eval history
            g = kwargs['cost']
            cost_evals = []
            for i in range(len(w_hist)):
                W = w_hist[i]
                cost = g(W)
                cost_evals.append(cost)

            # plot cost path - scale to fit inside same aspect as classification plots
            num_iterations = len(w_hist)
            s = np.linspace(minx + gapx, maxx - gapx, num_iterations)
            scaled_costs = [
                c / float(max(cost_evals)) * (maxx - gapx) - (minx + gapx)
                for c in cost_evals
            ]
            ax3.plot(s, scaled_costs, color='k', linewidth=1.5)
            ax3.set_xlabel('iteration', fontsize=12)
            ax3.set_title('cost function plot', fontsize=12)

            # rescale number of iterations and cost function value to fit same aspect ratio as other two subplots
            ax3.set_xlim(minx, maxx)
            ax3.set_ylim(minx, maxx)

            ### set tickmarks for both axes - requries re-scaling
            # x axis
            marks = range(0, num_iterations, round(num_iterations / 5.0))
            ax3.set_xticks(s[marks])
            labels = [item.get_text() for item in ax3.get_xticklabels()]
            ax3.set_xticklabels(marks)

            ### y axis
            r = (max(scaled_costs) - min(scaled_costs)) / 5.0
            marks = [min(scaled_costs) + m * r for m in range(6)]
            ax3.set_yticks(marks)
            labels = [item.get_text() for item in ax3.get_yticklabels()]

            r = (max(cost_evals) - min(cost_evals)) / 5.0
            marks = [int(min(cost_evals) + m * r) for m in range(6)]
            ax3.set_yticklabels(marks)
コード例 #17
0
def compare_2d3d(func1, func2, **kwargs):
    # input arguments
    view = [20, -65]
    if 'view' in kwargs:
        view = kwargs['view']

    # define input space
    w = np.linspace(-3, 3, 200)  # input range for original function
    if 'w' in kwargs:
        w = kwargs['w']

    # define pts
    pt1 = 0
    if 'pt1' in kwargs:
        pt1 = kwargs['pt1']

    pt2 = [0, 0]
    if 'pt2' in kwargs:
        pt2 = kwargs['pt2']

    # construct figure
    fig = plt.figure(figsize=(6, 3))

    # remove whitespace from figure
    fig.subplots_adjust(left=0, right=1, bottom=0, top=1)  # remove whitespace
    fig.subplots_adjust(wspace=0.01, hspace=0.01)

    # create subplot with 3 panels, plot input function in center plot
    gs = gridspec.GridSpec(1, 2, width_ratios=[1, 2])

    ### draw 2d version ###
    ax1 = plt.subplot(gs[0])
    grad = compute_grad(func1)

    # generate a range of values over which to plot input function, and derivatives
    g_plot = func1(w)
    g_range = max(g_plot) - min(g_plot)  # used for cleaning up final plot
    ggap = g_range * 0.2

    # grab the next input/output tangency pair, the center of the next approximation(s)
    pt1 = float(pt1)
    g_val = func1(pt1)

    # plot original function
    ax1.plot(w, g_plot, color='k', zorder=1, linewidth=2)

    # plot the input/output tangency point
    ax1.scatter(pt1,
                g_val,
                s=60,
                c='lime',
                edgecolor='k',
                linewidth=2,
                zorder=3)  # plot point of tangency

    #### plot first order approximation ####
    # plug input into the first derivative
    g_grad_val = grad(pt1)

    # compute first order approximation
    w1 = pt1 - 3
    w2 = pt1 + 3
    wrange = np.linspace(w1, w2, 100)
    h = g_val + g_grad_val * (wrange - pt1)

    # plot the first order approximation
    ax1.plot(wrange, h, color='lime', alpha=0.5, linewidth=3,
             zorder=2)  # plot approx

    # make new x-axis
    ax1.plot(w, g_plot * 0, linewidth=3, color='k')

    #### clean up panel ####
    # fix viewing limits on panel
    ax1.set_xlim([min(w), max(w)])
    ax1.set_ylim([min(min(g_plot) - ggap, -4), max(max(g_plot) + ggap, 0.5)])

    # label axes
    ax1.set_xlabel('$w$', fontsize=12, labelpad=-50)
    ax1.set_ylabel('$g(w)$', fontsize=25, rotation=0, labelpad=50)

    ax1.grid(False)
    ax1.yaxis.set_visible(False)
    ax1.spines['right'].set_visible(False)
    ax1.spines['top'].set_visible(False)
    ax1.spines['left'].set_visible(False)

    ### draw 3d version ###
    ax2 = plt.subplot(gs[1], projection='3d')
    grad = compute_grad(func2)
    w_val = [float(0), float(0)]

    # define input space
    w1_vals, w2_vals = np.meshgrid(w, w)
    w1_vals.shape = (len(w)**2, 1)
    w2_vals.shape = (len(w)**2, 1)
    w_vals = np.concatenate((w1_vals, w2_vals), axis=1).T
    g_vals = func2(w_vals)

    # evaluation points
    w_val = np.array([float(pt2[0]), float(pt2[1])])
    w_val.shape = (2, 1)
    g_val = func2(w_val)
    grad_val = grad(w_val)
    grad_val.shape = (2, 1)

    # create and evaluate tangent hyperplane
    w1tan_vals, w2tan_vals = np.meshgrid(w, w)
    w1tan_vals.shape = (len(w)**2, 1)
    w2tan_vals.shape = (len(w)**2, 1)
    wtan_vals = np.concatenate((w1tan_vals, w2tan_vals), axis=1).T

    #h = lambda weh: g_val +  np.dot( (weh - w_val).T,grad_val)
    h = lambda weh: g_val + (weh[0] - w_val[0]) * grad_val[0] + (weh[
        1] - w_val[1]) * grad_val[1]
    h_vals = h(wtan_vals + w_val)

    # vals for cost surface, reshape for plot_surface function
    w1_vals.shape = (len(w), len(w))
    w2_vals.shape = (len(w), len(w))
    g_vals.shape = (len(w), len(w))
    w1tan_vals += w_val[0]
    w2tan_vals += w_val[1]
    w1tan_vals.shape = (len(w), len(w))
    w2tan_vals.shape = (len(w), len(w))
    h_vals.shape = (len(w), len(w))

    ### plot function ###
    ax2.plot_surface(w1_vals,
                     w2_vals,
                     g_vals,
                     alpha=0.5,
                     color='w',
                     rstride=25,
                     cstride=25,
                     linewidth=1,
                     edgecolor='k',
                     zorder=2)

    ### plot z=0 plane ###
    ax2.plot_surface(w1_vals,
                     w2_vals,
                     g_vals * 0,
                     alpha=0.1,
                     color='w',
                     zorder=1,
                     rstride=25,
                     cstride=25,
                     linewidth=0.3,
                     edgecolor='k')

    ### plot tangent plane ###
    ax2.plot_surface(w1tan_vals,
                     w2tan_vals,
                     h_vals,
                     alpha=0.4,
                     color='lime',
                     zorder=1,
                     rstride=50,
                     cstride=50,
                     linewidth=1,
                     edgecolor='k')

    # scatter tangency
    ax2.scatter(w_val[0],
                w_val[1],
                g_val,
                s=70,
                c='lime',
                edgecolor='k',
                linewidth=2)

    ### clean up plot ###
    # plot x and y axes, and clean up
    ax2.xaxis.pane.fill = False
    ax2.yaxis.pane.fill = False
    ax2.zaxis.pane.fill = False

    #ax2.xaxis.pane.set_edgecolor('white')
    ax2.yaxis.pane.set_edgecolor('white')
    ax2.zaxis.pane.set_edgecolor('white')

    # remove axes lines and tickmarks
    ax2.w_zaxis.line.set_lw(0.)
    ax2.set_zticks([])
    ax2.w_xaxis.line.set_lw(0.)
    ax2.set_xticks([])
    ax2.w_yaxis.line.set_lw(0.)
    ax2.set_yticks([])

    # set viewing angle
    ax2.view_init(view[0], view[1])

    # set vewing limits
    wgap = (max(w) - min(w)) * 0.4
    y = max(w) + wgap
    ax2.set_xlim([-y, y])
    ax2.set_ylim([-y, y])

    zmin = min(np.min(g_vals), -0.5)
    zmax = max(np.max(g_vals), +0.5)
    ax2.set_zlim([zmin, zmax])

    # label plot
    fontsize = 12
    ax2.set_xlabel(r'$w_1$', fontsize=fontsize, labelpad=-30)
    ax2.set_ylabel(r'$w_2$', fontsize=fontsize, rotation=0, labelpad=-30)

    plt.show()
    def show_individual_classifiers(self, run, w, **kwargs):
        model = run.model
        normalizer = run.normalizer
        feat = run.feature_transforms

        # grab args
        view = [20, -70]
        if 'view' in kwargs:
            view = kwargs['view']

        ### plot all input data ###
        # generate input range for functions
        minx = min(min(self.x[:, 0]), min(self.x[:, 1]))
        maxx = max(max(self.x[:, 0]), max(self.x[:, 1]))
        gapx = (maxx - minx) * 0.1
        minx -= gapx
        maxx += gapx

        r = np.linspace(minx, maxx, 600)
        w1_vals, w2_vals = np.meshgrid(r, r)
        w1_vals.shape = (len(r)**2, 1)
        w2_vals.shape = (len(r)**2, 1)
        h = np.concatenate([w1_vals, w2_vals], axis=1).T

        g_vals = model(normalizer(h), w)
        g_vals = np.asarray(g_vals)
        g_new = copy.deepcopy(g_vals).T
        g_vals = np.argmax(g_vals, axis=0)

        # vals for cost surface
        w1_vals.shape = (len(r), len(r))
        w2_vals.shape = (len(r), len(r))
        g_vals.shape = (len(r), len(r))

        # count points
        class_nums = np.unique(self.y)
        C = int(len(class_nums))

        fig = plt.figure(figsize=(10, 7))
        gs = gridspec.GridSpec(2, C)

        #### left plot - data and fit in original space ####
        # setup current axis
        ax1 = plt.subplot(gs[C], projection='3d')
        ax2 = plt.subplot(gs[C + 1], aspect='equal')
        fig.subplots_adjust(left=0, right=1, bottom=0,
                            top=1)  # remove whitespace around 3d figure

        ##### plot top panels ####
        for d in range(C):
            # create panel
            ax = plt.subplot(gs[d], aspect='equal')

            for c in range(C):
                # plot points
                ind = np.argwhere(self.y == class_nums[c])
                ind = [v[0] for v in ind]
                ax.scatter(self.x[ind, 0],
                           self.x[ind, 1],
                           s=50,
                           color=self.colors[c],
                           edgecolor='k',
                           linewidth=2)

            g_2 = np.sign(g_new[:, d])
            g_2.shape = (len(r), len(r))

            # plot separator curve
            ax.contour(w1_vals,
                       w2_vals,
                       g_2 + 1,
                       colors='k',
                       levels=[-1, 1],
                       linewidths=4.5,
                       zorder=1,
                       linestyle='-')
            ax.contour(w1_vals,
                       w2_vals,
                       g_2 + 1,
                       colors=self.colors[d],
                       levels=[-1, 1],
                       linewidths=2.5,
                       zorder=1,
                       linestyle='-')

            ax.set_xlabel(r'$x_1$', fontsize=18, labelpad=10)
            ax.set_ylabel(r'$x_2$', rotation=0, fontsize=18, labelpad=15)

        ##### plot bottom panels ###
        # scatter points in both bottom panels
        for c in range(C):
            ind = np.argwhere(self.y == class_nums[c])
            ind = [v[0] for v in ind]
            ax1.scatter(self.x[ind, 0],
                        self.x[ind, 1],
                        self.y[ind],
                        s=50,
                        color=self.colors[c],
                        edgecolor='k',
                        linewidth=1.5)
            ax2.scatter(self.x[ind, 0],
                        self.x[ind, 1],
                        s=50,
                        color=self.colors[c],
                        edgecolor='k',
                        linewidth=2)

        ax1.plot_surface(w1_vals,
                         w2_vals,
                         g_vals,
                         alpha=0.1,
                         color='w',
                         rstride=45,
                         cstride=45,
                         linewidth=0.25,
                         edgecolor='k')

        for c in range(C):
            # plot separator curve in left plot z plane
            ax1.contour(w1_vals,
                        w2_vals,
                        g_vals - c,
                        colors='k',
                        levels=[0],
                        linewidths=3,
                        zorder=1)

            # color parts of plane with correct colors
            ax1.contourf(w1_vals,
                         w2_vals,
                         g_vals - c + 0.5,
                         colors=self.colors[c],
                         alpha=0.4,
                         levels=[0, 1])

        # plot separator in right plot
        ax2.contour(w1_vals,
                    w2_vals,
                    g_vals,
                    colors='k',
                    levels=range(0, C + 1),
                    linewidths=3,
                    zorder=1)

        # adjust height of regressor to plot filled contours
        ax2.contourf(w1_vals,
                     w2_vals,
                     g_vals + 0.5,
                     colors=self.colors[:],
                     alpha=0.2,
                     levels=range(0, C + 1))

        ### clean up panels
        # set viewing limits on vertical dimension for 3d plot
        minz = 0
        maxz = max(copy.deepcopy(self.y))
        gapz = (maxz - minz) * 0.1
        minz -= gapz
        maxz += gapz
        ax1.set_zlim([minz, maxz])

        ax1.view_init(view[0], view[1])

        # clean up panel
        ax1.xaxis.pane.fill = False
        ax1.yaxis.pane.fill = False
        ax1.zaxis.pane.fill = False

        ax1.xaxis.pane.set_edgecolor('white')
        ax1.yaxis.pane.set_edgecolor('white')
        ax1.zaxis.pane.set_edgecolor('white')

        ax1.xaxis._axinfo["grid"]['color'] = (1, 1, 1, 0)
        ax1.yaxis._axinfo["grid"]['color'] = (1, 1, 1, 0)
        ax1.zaxis._axinfo["grid"]['color'] = (1, 1, 1, 0)

        self.move_axis_left(ax1)
        ax1.set_xlabel(r'$x_1$', fontsize=16, labelpad=5)
        ax1.set_ylabel(r'$x_2$', rotation=0, fontsize=16, labelpad=5)
        ax1.set_zlabel(r'$y$', rotation=0, fontsize=16, labelpad=5)

        ax2.set_xlabel(r'$x_1$', fontsize=18, labelpad=10)
        ax2.set_ylabel(r'$x_2$', rotation=0, fontsize=18, labelpad=15)
コード例 #19
0
    def static_N2_img(self,w_best,cost,predict,f1,f2,**kwargs):
        # or just take last weights
        self.w = w_best
        
        zplane = 'on'
        if 'zplane' in kwargs:
            zplane = kwargs['zplane']
        view1 = [20,45]
        if 'view1' in kwargs:
            view1 = kwargs['view1']
        view2 = [20,30]
        if 'view2' in kwargs:
            view2 = kwargs['view2']  
            
        # initialize figure
        fig = plt.figure(figsize = (10,9))
        gs = gridspec.GridSpec(2, 2,width_ratios = [1,1]) 

        #### left plot - data and fit in original space ####
        # setup current axis
        ax = plt.subplot(gs[0],aspect = 'equal');
        ax2 = plt.subplot(gs[1],aspect = 'equal');
        ax3 = plt.subplot(gs[2],projection = '3d');
        ax4 = plt.subplot(gs[3],projection = '3d');
        
        ### cleanup left plots, create max view ranges ###
        xmin1 = min(self.x[:,0])
        xmax1 = max(self.x[:,0])
        xgap1 = (xmax1 - xmin1)*0.05
        xmin1 -= xgap1
        xmax1 += xgap1
        ax.set_xlim([xmin1,xmax1])
        ax3.set_xlim([xmin1,xmax1])

        xmin2 = min(self.x[:,1])
        xmax2 = max(self.x[:,1])
        xgap2 = (xmax2 - xmin2)*0.05
        xmin2 -= xgap2
        xmax2 += xgap2
        ax.set_ylim([xmin2,xmax2])
        ax3.set_ylim([xmin2,xmax2])

        ymin = min(self.y)
        ymax = max(self.y)
        ygap = (ymax - ymin)*0.05
        ymin -= ygap
        ymax += ygap
        ax3.set_zlim([ymin,ymax])
        
        ax3.axis('off')
        ax3.view_init(view1[0],view1[1])

        ax.set_yticklabels([])
        ax.set_xticklabels([])
        ax.set_xticks([])
        ax.set_yticks([])
        ax.set_xlabel(r'$x_1$',fontsize = 15)
        ax.set_ylabel(r'$x_2$',fontsize = 15,rotation = 0,labelpad = 20)
            
        #### plot left panels ####
        # plot points in 2d and 3d
        ind0 = np.argwhere(self.y == +1)
        ax.scatter(self.x[ind0,0],self.x[ind0,1],s = 55, color = self.colors[0], edgecolor = 'k')
        ax3.scatter(self.x[ind0,0],self.x[ind0,1],self.y[ind0],s = 55, color = self.colors[0], edgecolor = 'k')

        ind1 = np.argwhere(self.y == -1)
        ax.scatter(self.x[ind1,0],self.x[ind1,1],s = 55, color = self.colors[1], edgecolor = 'k')
        ax3.scatter(self.x[ind1,0],self.x[ind1,1],self.y[ind1],s = 55, color = self.colors[1], edgecolor = 'k')
       
        # plot boundary for 2d plot
        r1 = np.linspace(xmin1,xmax1,100)
        r2 = np.linspace(xmin2,xmax2,100)
        s,t = np.meshgrid(r1,r2)
        s = np.reshape(s,(np.size(s),1))
        t = np.reshape(t,(np.size(t),1))
        h = np.concatenate((s,t),axis = 1)
        z = []
        for j in range(len(h)):
            a = predict(h[j,:],self.w)
            z.append(a)
        z = np.asarray(z)
        z = np.tanh(z)
        
        # reshape it
        s.shape = (np.size(r1),np.size(r2))
        t.shape = (np.size(r1),np.size(r2))     
        z.shape = (np.size(r1),np.size(r2))
        
        #### plot contour, color regions ####
        ax.contour(s,t,z,colors='k', linewidths=2.5,levels = [0],zorder = 2)
        ax.contourf(s,t,z,colors = [self.colors[1],self.colors[0]],alpha = 0.15,levels = range(-1,2))
        ax3.plot_surface(s,t,z,alpha = 0.1,color = 'w',rstride=10, cstride=10,linewidth=0.5,edgecolor = 'k')

        # plot zplane = 0 in left 3d panel - showing intersection of regressor with z = 0 (i.e., its contour, the separator, in the 3d plot too)?
        if zplane == 'on':
            # plot zplane
            ax3.plot_surface(s,t,z*0,alpha = 0.1,rstride=20, cstride=20,linewidth=0.15,color = 'w',edgecolor = 'k') 
            
            # plot separator curve in left plot
            ax3.contour(s,t,z,colors = 'k',levels = [0],linewidths = 3,zorder = 1)
            ax3.contourf(s,t,z,colors = self.colors[1],levels = [0,1],zorder = 1,alpha = 0.1)
            ax3.contourf(s,t,z+1,colors = self.colors[0],levels = [0,1],zorder = 1,alpha = 0.1)
        
        #### plot right panel scatter ####
        # transform data
        x1 = [f1(e) for e in self.x]
        x2 = [f2(e) for e in self.x]
        ind0 = [v[0] for v in ind0]
        ind1 = [v[0] for v in ind1]

        # plot points on desired panel
        v1 = [x1[e] for e in ind0]
        v2 = [x2[e] for e in ind0]
        ax2.scatter(v1,v2,s = 55, color = self.colors[0], edgecolor = 'k')
        ax4.scatter(v1,v2,self.y[ind0],s = 55, color = self.colors[0], edgecolor = 'k')

        v1 = [x1[e] for e in ind1]
        v2 = [x2[e] for e in ind1]        
        ax2.scatter(v1,v2,s = 55, color = self.colors[1], edgecolor = 'k')
        ax4.scatter(v1,v2,self.y[ind1],s = 55, color = self.colors[1], edgecolor = 'k')
        
        ### cleanup right panels - making max viewing ranges ###
        xmin1 = min(v1)
        xmax1 = max(v1)
        xgap1 = (xmax1 - xmin1)*0.1
        xmin1 -= xgap1
        xmax1 += xgap1
        ax2.set_xlim([xmin1,xmax1])
        ax4.set_xlim([xmin1,xmax1])

        xmin2 = min(v2)
        xmax2 = max(v2)
        xgap2 = (xmax2 - xmin2)*0.1
        xmin2 -= xgap2
        xmax2 += xgap2
        ax2.set_ylim([xmin2,xmax2])
        ax4.set_ylim([xmin2,xmax2])

        ymin = min(self.y)
        ymax = max(self.y)
        ygap = (ymax - ymin)*0.05
        ymin -= ygap
        ymax += ygap
        ax4.set_zlim([ymin,ymax])
        
        ax4.axis('off')
        ax4.view_init(view2[0],view2[1])

        ax2.set_yticklabels([])
        ax2.set_xticklabels([])
        ax2.set_xticks([])
        ax2.set_yticks([])
        ax2.set_xlabel(r'$f\,_1\left(x_1,x_2\right)$',fontsize = 15)
        ax2.set_ylabel(r'$f\,_2\left(x_1,x_2\right)$',fontsize = 15)        
        
        ### plot right panel 3d scatter ###
        #### make right plot contour ####
        r1 = np.linspace(xmin1,xmax1,100)
        r2 = np.linspace(xmin2,xmax2,100)
        s,t = np.meshgrid(r1,r2)
        z = self.w[0] + self.w[1]*s + self.w[2]*t 
        z = np.tanh(np.asarray(z))
        z.shape = (np.size(r1),np.size(r2))
        ax2.contour(s,t,z,colors='k', linewidths=2.5,levels = [0],zorder = 2)
        ax2.contourf(s,t,z,colors = [self.colors[1],self.colors[0]],alpha = 0.15,levels = range(-1,2))
        
        #### plot right surface plot ####
        # plot regression surface
        ax4.plot_surface(s,t,z,alpha = 0.1,color = 'w',rstride=10, cstride=10,linewidth=0.5,edgecolor = 'k')
            
        # plot zplane = 0 in left 3d panel - showing intersection of regressor with z = 0 (i.e., its contour, the separator, in the 3d plot too)?
        if zplane == 'on':
            # plot zplane
            ax4.plot_surface(s,t,z*0,alpha = 0.1,rstride=20, cstride=20,linewidth=0.15,color = 'w',edgecolor = 'k') 
            
            # plot separator curve in left plot
            ax4.contour(s,t,z,colors = 'k',levels = [0],linewidths = 3,zorder = 1)
            ax4.contourf(s,t,z,colors = self.colors[0],levels = [0,1],zorder = 1,alpha = 0.1)
            ax4.contourf(s,t,z+1,colors = self.colors[1],levels = [0,1],zorder = 1,alpha = 0.1)
   
        plt.show()
コード例 #20
0
def stop_condition(grad, eps):
    return np.linalg.norm(grad) <= eps


if __name__ == "__main__":
    n = 2
    iteration = 0
    epsilon = 0.1
    verbose = 1
    color = 'black'
    H_f = jacobian(egrad(f))

    if n == 2:  # if dimension = 2 we could plot function
        x_line = np.arange(-10, 10, 0.05)
        y_line = np.arange(-10, 10, 0.05)
        x_grid, y_grid = np.meshgrid(x_line, y_line, sparse=True)
        function_values = f([x_grid, y_grid])

        plt.pcolormesh(x_line,
                       y_line,
                       function_values,
                       cmap=cm.get_cmap('inferno_r'),
                       alpha=0.8)

    x = np.array([-8, -8], dtype=np.float32)
    if verbose == 1:
        print('Starting...')
        print('Starting point x_0:', x)

    while True:
        iteration += 1
コード例 #21
0
    def draw_panel(self,ax,title,**kwargs):
        # set viewing limits on contour plot
        xvals = [self.w_hist[s][0] for s in range(len(self.w_hist))]
        xvals.append(self.w_init[0])
        yvals = [self.w_hist[s][1] for s in range(len(self.w_hist))]
        yvals.append(self.w_init[1])
        xmax = max(xvals)
        xmin = min(xvals)
        xgap = (xmax - xmin)*0.1
        ymax = max(yvals)
        ymin = min(yvals)
        ygap = (ymax - ymin)*0.1
        xmin -= xgap
        xmax += xgap
        ymin -= ygap
        ymax += ygap

        if 'xmin' in kwargs:
            xmin = kwargs['xmin']
        if 'xmax' in kwargs:
            xmax = kwargs['xmax']
        if 'ymin' in kwargs:
            ymin = kwargs['ymin']
        if 'ymax' in kwargs:
            ymax = kwargs['ymax'] 
        axes = False
        if 'axes' in kwargs:
            axes = kwargs['ymax']
        pts = False
        if 'pts' in kwargs:
            pts = kwargs['pts']  
        
        pts = False
        if 'pts' in kwargs:
            pts = kwargs['pts']  
            
        linewidth = 2.5
        if 'linewidth' in kwargs:
            linewidth = kwargs['linewidth']
            
        #### define input space for function and evaluate ####
        w1 = np.linspace(xmin,xmax,400)
        w2 = np.linspace(ymin,ymax,400)
        w1_vals, w2_vals = np.meshgrid(w1,w2)
        w1_vals.shape = (len(w1)**2,1)
        w2_vals.shape = (len(w2)**2,1)
        h = np.concatenate((w1_vals,w2_vals),axis=1)
        func_vals = np.asarray([self.g(s) for s in h])
        w1_vals.shape = (len(w1),len(w1))
        w2_vals.shape = (len(w2),len(w2))
        func_vals.shape = (len(w1),len(w2)) 

        ### make contour right plot - as well as horizontal and vertical axes ###
        # set level ridges
        num_contours = kwargs['num_contours']
        levelmin = min(func_vals.flatten())
        levelmax = max(func_vals.flatten())
        cutoff = 0.5
        cutoff = (levelmax - levelmin)*cutoff
        numper = 3
        levels1 = np.linspace(cutoff,levelmax,numper)
        num_contours -= numper

        levels2 = np.linspace(levelmin,cutoff,min(num_contours,numper))
        levels = np.unique(np.append(levels1,levels2))
        num_contours -= numper
        while num_contours > 0:
            cutoff = levels[1]
            levels2 = np.linspace(levelmin,cutoff,min(num_contours,numper))
            levels = np.unique(np.append(levels2,levels))
            num_contours -= numper

        a = ax.contour(w1_vals, w2_vals, func_vals,levels = levels,colors = 'k')
        ax.contourf(w1_vals, w2_vals, func_vals,levels = levels,cmap = 'Blues')

        if axes == True:
            ax.axhline(linestyle = '--', color = 'k',linewidth = 1)
            ax.axvline(linestyle = '--', color = 'k',linewidth = 1)

        # colors for points
        s = np.linspace(0,1,len(self.w_hist[:round(len(self.w_hist)/2)]))
        s.shape = (len(s),1)
        t = np.ones(len(self.w_hist[round(len(self.w_hist)/2):]))
        t.shape = (len(t),1)
        s = np.vstack((s,t))
        colorspec = []
        colorspec = np.concatenate((s,np.flipud(s)),1)
        colorspec = np.concatenate((colorspec,np.zeros((len(s),1))),1)

        ### plot function decrease plot in right panel
        for j in range(len(self.w_hist)):  
            w_val = self.w_hist[j]
            g_val = self.g(w_val)

            # plot in left panel
            if pts == 'True':
                ax.scatter(w_val[0],w_val[1],s = 30,c = colorspec[j],edgecolor = 'k',linewidth = 1.5*math.sqrt((1/(float(j) + 1))),zorder = 3)

            # plot connector between points for visualization purposes
            if j > 0:
                w_old = self.w_hist[j-1]
                w_new = self.w_hist[j]    
                
                ax.plot([w_old[0],w_new[0]],[w_old[1],w_new[1]],color = colorspec[j],linewidth = linewidth,alpha = 1,zorder = 2)      # plot approx
                ax.plot([w_old[0],w_new[0]],[w_old[1],w_new[1]],color = 'k',linewidth = linewidth + 0.4,alpha = 1,zorder = 1)      # plot approx
                
        # clean panel
        ax.set_title(title,fontsize = 12)
        ax.set_xlabel('$w_1$',fontsize = 12)
        ax.set_ylabel('$w_2$',fontsize = 12,rotation = 0)
        ax.axhline(y=0, color='k',zorder = 0,linewidth = 0.5)
        ax.axvline(x=0, color='k',zorder = 0,linewidth = 0.5)               
        ax.set_xlim([xmin,xmax])
        ax.set_ylim([ymin,ymax])
        ax.set_xticks(np.arange(round(xmin), round(xmax) + 1, 1.0))
        ax.set_yticks(np.arange(round(ymin), round(ymax) + 1, 1.0))
            
コード例 #22
0
    def region_coloring(self, region, ax):
        #### color first regions  ####
        # generate input range for functions
        minx = min(min(self.x[:, 0]), min(self.x[:, 1]))
        maxx = max(max(self.x[:, 0]), max(self.x[:, 1]))
        gapx = (maxx - minx) * 0.1
        minx -= gapx
        maxx += gapx

        # plot over range
        r = np.linspace(minx, maxx, 200)
        x1_vals, x2_vals = np.meshgrid(r, r)
        x1_vals.shape = (len(r)**2, 1)
        x2_vals.shape = (len(r)**2, 1)
        o = np.ones((len(r)**2, 1))
        x = np.concatenate([o, x1_vals, x2_vals], axis=1)

        ### for region 1, determine points that are uniquely positive for each classifier ###
        ind_set = []
        y = np.dot(self.W, x.T)
        num_classes = np.size(np.unique(self.y))

        if region == 1 or region == 'all':
            for i in range(0, num_classes):
                class_inds = np.arange(num_classes)
                class_inds = np.delete(class_inds, (i), axis=0)

                # loop over non-current classifier
                ind = np.argwhere(y[class_inds[0]] < 0).tolist()
                ind = [s[0] for s in ind]
                for j in range(1, len(class_inds)):
                    c_ind = class_inds[j]
                    ind2 = np.argwhere(y[c_ind] < 0).tolist()
                    ind2 = [s[0] for s in ind2]
                    ind = [s for s in ind if s in ind2]

                ind2 = np.argwhere(y[i] > 0).tolist()
                ind2 = [s[0] for s in ind2]
                ind = [s for s in ind if s in ind2]

                # plot polygon over region defined by ind
                x1_ins = np.asarray([x1_vals[s] for s in ind])
                x1_ins.shape = (len(x1_ins), 1)
                x2_ins = np.asarray([x2_vals[s] for s in ind])
                x2_ins.shape = (len(x2_ins), 1)
                h = np.concatenate((x1_ins, x2_ins), axis=1)
                vertices = ConvexHull(h).vertices
                poly = [h[v] for v in vertices]
                polygon = Polygon(poly, True)
                patches = []
                patches.append(polygon)

                p = PatchCollection(patches, alpha=0.2, color=self.colors[i])
                ax.add_collection(p)

        if region == 2 or region == 'all':
            for i in range(0, num_classes):
                class_inds = np.arange(num_classes)
                class_inds = np.delete(class_inds, (i), axis=0)

                # loop over non-current classifier
                ind = np.argwhere(y[class_inds[0]] > 0).tolist()
                ind = [s[0] for s in ind]
                for j in range(1, len(class_inds)):
                    c_ind = class_inds[j]
                    ind2 = np.argwhere(y[c_ind] > 0).tolist()
                    ind2 = [s[0] for s in ind2]
                    ind = [s for s in ind if s in ind2]

                ind2 = np.argwhere(y[i] < 0).tolist()
                ind2 = [s[0] for s in ind2]
                ind = [s for s in ind if s in ind2]

                # plot polygon over region defined by ind
                x1_ins = np.asarray([x1_vals[s] for s in ind])
                x1_ins.shape = (len(x1_ins), 1)
                x2_ins = np.asarray([x2_vals[s] for s in ind])
                x2_ins.shape = (len(x2_ins), 1)
                o = np.ones((len(x2_ins), 1))
                h = np.concatenate((o, x1_ins, x2_ins), axis=1)

                # determine regions dominated by one classifier or the other
                vals = []
                for c in class_inds:
                    w = self.W[int(c)]
                    nv = np.dot(w, h.T)
                    vals.append(nv)
                vals = np.asarray(vals)
                vals.shape = (len(class_inds), len(h))
                ind = np.argmax(vals, axis=0)

                for j in range(len(class_inds)):
                    # make polygon for each subregion
                    ind1 = np.argwhere(ind == j)
                    x1_ins2 = np.asarray([x1_ins[s] for s in ind1])
                    x1_ins2.shape = (len(x1_ins2), 1)
                    x2_ins2 = np.asarray([x2_ins[s] for s in ind1])
                    x2_ins2.shape = (len(x2_ins2), 1)
                    h = np.concatenate((x1_ins2, x2_ins2), axis=1)

                    # find convex hull of points
                    vertices = ConvexHull(h).vertices
                    poly = [h[v] for v in vertices]
                    polygon = Polygon(poly, True)
                    patches = []
                    patches.append(polygon)
                    c = class_inds[j]
                    p = PatchCollection(patches,
                                        alpha=0.2,
                                        color=self.colors[c])
                    ax.add_collection(p)

        if region == 3 or region == 'all':
            # find negative zone of all classifiers
            ind = np.argwhere(y[0] < 0).tolist()
            ind = [s[0] for s in ind]
            for i in range(1, num_classes):
                ind2 = np.argwhere(y[i] < 0).tolist()
                ind2 = [s[0] for s in ind2]
                ind = [s for s in ind if s in ind2]

            # loop over negative zone, find max area of each classifier
            x1_ins = np.asarray([x1_vals[s] for s in ind])
            x1_ins.shape = (len(x1_ins), 1)
            x2_ins = np.asarray([x2_vals[s] for s in ind])
            x2_ins.shape = (len(x2_ins), 1)
            o = np.ones((len(x2_ins), 1))
            h = np.concatenate((o, x1_ins, x2_ins), axis=1)

            # determine regions dominated by one classifier or the other
            vals = []
            for c in range(num_classes):
                w = self.W[c]
                nv = np.dot(w, h.T)
                vals.append(nv)
            vals = np.asarray(vals)
            vals.shape = (num_classes, len(h))
            ind = np.argmax(vals, axis=0)

            # loop over each class, construct polygon region for each
            for c in range(num_classes):
                # make polygon for each subregion
                ind1 = np.argwhere(ind == c)
                x1_ins2 = np.asarray([x1_ins[s] for s in ind1])
                x1_ins2.shape = (len(x1_ins2), 1)
                x2_ins2 = np.asarray([x2_ins[s] for s in ind1])
                x2_ins2.shape = (len(x2_ins2), 1)
                h = np.concatenate((x1_ins2, x2_ins2), axis=1)

                # find convex hull of points
                vertices = ConvexHull(h).vertices
                poly = [h[v] for v in vertices]
                polygon = Polygon(poly, True)
                patches = []
                patches.append(polygon)
                p = PatchCollection(patches, alpha=0.2, color=self.colors[c])
                ax.add_collection(p)
コード例 #23
0
    def show_complete_coloring(self):
        # generate input range for viewing range
        minx = min(min(self.x[:, 0]), min(self.x[:, 1]))
        maxx = max(max(self.x[:, 0]), max(self.x[:, 1]))
        gapx = (maxx - minx) * 0.1
        minx -= gapx
        maxx += gapx

        # initialize figure
        fig = plt.figure(figsize=(8, 4))
        gs = gridspec.GridSpec(1, 2, width_ratios=[1, 1])

        # setup current axis
        ax = plt.subplot(gs[0], aspect='equal')
        ax2 = plt.subplot(gs[1], aspect='equal')

        # plot panel with all data and separators
        self.plot_data(ax)
        self.plot_data(ax2)
        self.plot_all_separators(ax)

        ### draw multiclass boundary on right panel
        r = np.linspace(minx, maxx, 2000)
        w1_vals, w2_vals = np.meshgrid(r, r)
        w1_vals.shape = (len(r)**2, 1)
        w2_vals.shape = (len(r)**2, 1)
        o = np.ones((len(r)**2, 1))
        h = np.concatenate([o, w1_vals, w2_vals], axis=1)
        pts = np.dot(self.W, h.T)
        g_vals = np.argmax(pts, axis=0)

        # vals for cost surface
        w1_vals.shape = (len(r), len(r))
        w2_vals.shape = (len(r), len(r))
        g_vals.shape = (len(r), len(r))

        # plot contour
        C = len(np.unique(self.y))
        ax2.contour(w1_vals,
                    w2_vals,
                    g_vals,
                    colors='k',
                    levels=range(0, C + 1),
                    linewidths=2.75,
                    zorder=4)
        ax2.contourf(w1_vals,
                     w2_vals,
                     g_vals + 1,
                     colors=self.colors[:],
                     alpha=0.2,
                     levels=range(0, C + 1))
        ax.contourf(w1_vals,
                    w2_vals,
                    g_vals + 1,
                    colors=self.colors[:],
                    alpha=0.2,
                    levels=range(0, C + 1))

        # dress panel
        ax.set_xlim(minx, maxx)
        ax.set_ylim(minx, maxx)
        ax.axis('off')

        ax2.set_xlim(minx, maxx)
        ax2.set_ylim(minx, maxx)
        ax2.axis('off')
コード例 #24
0
    def draw_surface(self, g, ax, **kwargs):
        xmin = -3.1
        xmax = 3.1
        ymin = -3.1
        ymax = 3.1
        if 'xmin' in kwargs:
            xmin = kwargs['xmin']
        if 'xmax' in kwargs:
            xmax = kwargs['xmax']
        if 'ymin' in kwargs:
            ymin = kwargs['ymin']
        if 'ymax' in kwargs:
            ymax = kwargs['ymax']

        #### define input space for function and evaluate ####
        w1 = np.linspace(xmin, xmax, 200)
        w2 = np.linspace(ymin, ymax, 200)
        w1_vals, w2_vals = np.meshgrid(w1, w2)
        w1_vals.shape = (len(w1)**2, 1)
        w2_vals.shape = (len(w2)**2, 1)
        h = np.concatenate((w1_vals, w2_vals), axis=1)
        func_vals = np.asarray([g(np.reshape(s, (2, 1))) for s in h])

        ### plot function as surface ###
        w1_vals.shape = (len(w1), len(w2))
        w2_vals.shape = (len(w1), len(w2))
        func_vals.shape = (len(w1), len(w2))
        ax.plot_surface(w1_vals,
                        w2_vals,
                        func_vals,
                        alpha=0.1,
                        color='w',
                        rstride=25,
                        cstride=25,
                        linewidth=1,
                        edgecolor='k',
                        zorder=2)

        # plot z=0 plane
        ax.plot_surface(w1_vals,
                        w2_vals,
                        func_vals * 0,
                        alpha=0.1,
                        color='w',
                        zorder=1,
                        rstride=25,
                        cstride=25,
                        linewidth=0.3,
                        edgecolor='k')

        # clean up axis
        ax.xaxis.pane.fill = False
        ax.yaxis.pane.fill = False
        ax.zaxis.pane.fill = False

        ax.xaxis.pane.set_edgecolor('white')
        ax.yaxis.pane.set_edgecolor('white')
        ax.zaxis.pane.set_edgecolor('white')

        ax.xaxis._axinfo["grid"]['color'] = (1, 1, 1, 0)
        ax.yaxis._axinfo["grid"]['color'] = (1, 1, 1, 0)
        ax.zaxis._axinfo["grid"]['color'] = (1, 1, 1, 0)

        ax.set_xlabel('$w_0$', fontsize=14)
        ax.set_ylabel('$w_1$', fontsize=14, rotation=0)
        ax.set_title('$g(w_0,w_1)$', fontsize=14)
コード例 #25
0
def plot_regressions(x, y, predictor, view1, view2):
    # import all the requisite libs
    # construct panels
    fig = plt.figure(figsize=(9, 4))
    ax0 = plt.subplot(121, projection='3d')
    ax0.view_init(view1[0], view1[1])
    ax0.axis('off')

    ax1 = plt.subplot(122, projection='3d')
    ax1.view_init(view2[0], view2[1])
    ax1.axis('off')

    # scatter plot data in each panel
    ax0.scatter(x[0, :],
                x[1, :],
                y[0, :],
                c='k',
                edgecolor='w',
                linewidth=1,
                s=60)
    ax1.scatter(x[0, :],
                x[1, :],
                y[1, :],
                c='k',
                edgecolor='w',
                linewidth=1,
                s=60)

    # construct input for each model fit
    a_ = np.linspace(0, 1, 15)
    a, b = np.meshgrid(a_, a_)
    a = a.flatten()[np.newaxis, :]
    b = b.flatten()[np.newaxis, :]
    c = np.vstack((a, b))

    # evaluate model
    p = predictor(c)
    m1 = p[0, :]
    m2 = p[1, :]

    # plot each as surface
    a.shape = (a_.size, a_.size)
    b.shape = (a_.size, a_.size)
    m1.shape = (a_.size, a_.size)
    m2.shape = (a_.size, a_.size)

    ax0.plot_surface(a,
                     b,
                     m1,
                     alpha=0.25,
                     color='lime',
                     cstride=2,
                     rstride=2,
                     linewidth=1,
                     edgecolor='k')
    ax1.plot_surface(a,
                     b,
                     m2,
                     alpha=0.25,
                     color='lime',
                     cstride=2,
                     rstride=2,
                     linewidth=1,
                     edgecolor='k')

    plt.show()
    def static_N2_simple(self, w_best, runner, **kwargs):
        cost = runner.cost
        predict = runner.model
        feat = runner.feature_transforms
        normalizer = runner.normalizer

        # count parameter layers of input to feature transform
        sig = signature(feat)
        sig = len(sig.parameters)

        # or just take last weights
        self.w = w_best

        # construct figure
        fig, axs = plt.subplots(1, 2, figsize=(9, 4))

        # create subplot with 2 panels
        gs = gridspec.GridSpec(1, 2)
        ax2 = plt.subplot(gs[1], aspect='equal')
        ax1 = plt.subplot(gs[0], projection='3d')

        # scatter points
        self.scatter_pts(ax1, self.x)

        ### from above
        ax2.set_xlabel(r'$x_1$', fontsize=15)
        ax2.set_ylabel(r'$x_2$', fontsize=15, rotation=0, labelpad=20)
        ax2.xaxis.set_major_formatter(FormatStrFormatter('%.1f'))
        ax2.yaxis.set_major_formatter(FormatStrFormatter('%.1f'))

        # plot points in 2d and 3d
        C = len(np.unique(self.y))
        if C == 2:
            ind0 = np.argwhere(self.y == +1)
            ax2.scatter(self.x[ind0, 0],
                        self.x[ind0, 1],
                        s=55,
                        color=self.colors[0],
                        edgecolor='k')

            ind1 = np.argwhere(self.y == -1)
            ax2.scatter(self.x[ind1, 0],
                        self.x[ind1, 1],
                        s=55,
                        color=self.colors[1],
                        edgecolor='k')
        else:
            for c in range(C):
                ind0 = np.argwhere(self.y == c)
                ax2.scatter(self.x[ind0, 0],
                            self.x[ind0, 1],
                            s=55,
                            color=self.colors[c],
                            edgecolor='k')

        self.move_axis_left(ax1)
        ax1.set_xlabel(r'$x_1$', fontsize=12, labelpad=5)
        ax1.set_ylabel(r'$x_2$', rotation=0, fontsize=12, labelpad=5)
        ax1.set_zlabel(r'$y$', rotation=0, fontsize=12, labelpad=-3)

        ### create surface and boundary plot ###
        xmin1 = np.min(self.x[:, 0])
        xmax1 = np.max(self.x[:, 0])
        xgap1 = (xmax1 - xmin1) * 0.05
        xmin1 -= xgap1
        xmax1 += xgap1

        xmin2 = np.min(self.x[:, 1])
        xmax2 = np.max(self.x[:, 1])
        xgap2 = (xmax2 - xmin2) * 0.05
        xmin2 -= xgap2
        xmax2 += xgap2
        if 'view' in kwargs:
            view = kwargs['view']
            ax1.view_init(view[0], view[1])

        # plot boundary for 2d plot
        r1 = np.linspace(xmin1, xmax1, 300)
        r2 = np.linspace(xmin2, xmax2, 300)
        s, t = np.meshgrid(r1, r2)
        s = np.reshape(s, (np.size(s), 1))
        t = np.reshape(t, (np.size(t), 1))
        h = np.concatenate((s, t), axis=1)
        z = predict(normalizer(h.T), self.w)
        z = np.sign(z)

        # reshape it
        s.shape = (np.size(r1), np.size(r2))
        t.shape = (np.size(r1), np.size(r2))
        z.shape = (np.size(r1), np.size(r2))

        #### plot contour, color regions ####
        ax2.contour(s, t, z, colors='k', linewidths=2.5, levels=[0], zorder=2)
        ax2.contourf(s,
                     t,
                     z,
                     colors=[self.colors[1], self.colors[0]],
                     alpha=0.15,
                     levels=range(-1, 2))
        ax1.plot_surface(s,
                         t,
                         z,
                         alpha=0.1,
                         color='w',
                         rstride=30,
                         cstride=30,
                         linewidth=0.5,
                         edgecolor='k')
コード例 #27
0
ファイル: demo9_3.py プロジェクト: motein/Pocketin
# Compute the gradient of the loss function
gradient_loss = grad(loss)

# Set the initial weights
weights = np.array([1.0, 1.0])

# Steepest Descent
loss_values = []
learning_rate = 0.001
for i in range(100):
    loss_values.append(loss(weights))
    step = gradient_loss(weights)
    weights -= step * learning_rate

# Plot the decision boundary
x_min, x_max = train_X[:, 0].min() - 0.5, train_X[:, 0].max() + 0.5
y_min, y_max = train_X[:, 1].min() - 0.5, train_X[:, 1].max() + 0.5
x_mesh, y_mesh = np.meshgrid(np.arange(x_min, x_max, 0.01),
                             np.arange(y_min, y_max, 0.01))
Z = predict(weights, np.c_[x_mesh.ravel(), y_mesh.ravel()])
Z = Z.reshape(x_mesh.shape)
cs = pylab.contourf(x_mesh, y_mesh, Z, cmap=pylab.cm.Spectral)
pylab.scatter(train_X[:, 0], train_X[:, 1], c=train_y, cmap=pylab.cm.Spectral)
# pylab.colorbar(cs)

# Plot the loss over each step
pylab.figure()
# pylab.plot(loss_values)
pylab.xlabel("Steps")
pylab.ylabel("Loss")
pylab.show()
コード例 #28
0
    if np.all(abs(x_i - x_ip1) < 1e-6):
        break

    # Update x and p
    p_i = p_ip1
    x_i = x_ip1
    # Save results
    F = np.append(F, f(x_i))
    X = np.append(X, np.reshape(x_i, (1, -1)), axis=0)

print("Plotting results")
xmin, xmax, xstep = -4.5, 4.5, .2
ymin, ymax, ystep = -4.5, 4.5, .2

x, y = np.meshgrid(np.arange(xmin, xmax + xstep, xstep),
                   np.arange(ymin, ymax + ystep, ystep))
z = f([x, y])
minima = np.array([0, 0])
minima_ = minima.reshape(-1, 1)

fig, ax = plt.subplots(figsize=(10, 10))

ax.contour(x,
           y,
           z,
           levels=np.logspace(0, 5, 35),
           norm=LogNorm(),
           cmap=plt.cm.jet)
#ax.quiver(X.T[0,:-1], X.T[1,:-1], X.T[0,1:]-X.T[0,:-1], X.T[1,1:]-X.T[1,:-1], scale_units='xy', angles='xy', scale=1, color='k')
ax.scatter(X.T[0], X.T[1], s=5)
ax.plot(*minima_, 'r*', markersize=5)
コード例 #29
0
    def static_N1_img(self,w_best,cost,predict,**kwargs):
        # or just take last weights
        self.w = w_best
        
        # initialize figure
        fig = plt.figure(figsize = (9,4))
        
        show_cost = False
        if show_cost == True:   
            gs = gridspec.GridSpec(1, 3,width_ratios = [1,1,1],height_ratios = [1]) 
            
            # create third panel for cost values
            ax3 = plt.subplot(gs[2],aspect = 'equal')
            
        else:
            gs = gridspec.GridSpec(1, 2,width_ratios = [1,1]) 

        #### left plot - data and fit in original space ####
        # setup current axis
        ax = plt.subplot(gs[0]);
        ax2 = plt.subplot(gs[1],aspect = 'equal');
        
        # scatter original points
        self.scatter_pts(ax,self.x)
        ax.set_xlabel(r'$x$', fontsize = 14,labelpad = 10)
        ax.set_ylabel(r'$y$', rotation = 0,fontsize = 14,labelpad = 10)
        
        # create fit
        gapx = (max(self.x) - min(self.x))*0.1
        s = np.linspace(min(self.x) - gapx,max(self.x) + gapx,100)
        t = [np.tanh(predict(np.asarray([v]),self.w)) for v in s]
        
        # plot fit
        ax.plot(s,t,c = 'lime')
        ax.axhline(linewidth=0.5, color='k',zorder = 1)

        #### plot data in new space in middle panel (or right panel if cost function decrease plot shown ) #####
        if 'f_x' in kwargs:
            f_x = kwargs['f_x']

            # scatter points
            self.scatter_pts(ax2,f_x)

            # create and plot fit
            s = np.linspace(min(f_x) - 0.1,max(f_x) + 0.1,100)
            t = np.tanh(self.w[0] + self.w[1]*s)
            ax2.plot(s,t,c = 'lime')
            ax2.set_xlabel(r'$f\,(x)$', fontsize = 14,labelpad = 10)
            ax2.set_ylabel(r'$y$', rotation = 0,fontsize = 14,labelpad = 10)
        
        if 'f2_x' in kwargs:
            ax2 = plt.subplot(gs[1],projection = '3d');   
            view = kwargs['view']
            
            # get input
            f1_x = kwargs['f1_x']
            f2_x = kwargs['f2_x']

            # scatter points
            f1_x = np.asarray(f1_x)
            f1_x.shape = (len(f1_x),1)
            f2_x = np.asarray(f2_x)
            f2_x.shape = (len(f2_x),1)
            xtran = np.concatenate((f1_x,f2_x),axis = 1)
            self.scatter_pts(ax2,xtran)

            # create and plot fit
            s1 = np.linspace(min(f1_x) - 0.1,max(f1_x) + 0.1,100)
            s2 = np.linspace(min(f2_x) - 0.1,max(f2_x) + 0.1,100)
            t1,t2 = np.meshgrid(s1,s2)
            
            # compute fitting hyperplane
            t1.shape = (len(s1)**2,1)
            t2.shape = (len(s2)**2,1)
            r = np.tanh(self.w[0] + self.w[1]*t1 + self.w[2]*t2)
            
            # reshape for plotting
            t1.shape = (len(s1),len(s1))
            t2.shape = (len(s2),len(s2))
            r.shape = (len(s1),len(s2))
            ax2.plot_surface(t1,t2,r,alpha = 0.1,color = 'lime',rstride=10, cstride=10,linewidth=0.5,edgecolor = 'k')
                
            # label axes
            self.move_axis_left(ax2)
            ax2.set_xlabel(r'$f_1(x)$', fontsize = 12,labelpad = 5)
            ax2.set_ylabel(r'$f_2(x)$', rotation = 0,fontsize = 12,labelpad = 5)
            ax2.set_zlabel(r'$y$', rotation = 0,fontsize = 12,labelpad = -3)
            ax2.view_init(view[0],view[1])
            
        # plot cost function decrease
        if  show_cost == True: 
            # compute cost eval history
            g = cost
            cost_evals = []
            for i in range(len(w_hist)):
                W = w_hist[i]
                cost = g(W)
                cost_evals.append(cost)
     
            # plot cost path - scale to fit inside same aspect as classification plots
            num_iterations = len(w_hist)
            minx = min(self.x)
            maxx = max(self.x)
            gapx = (maxx - minx)*0.1
            minc = min(cost_evals)
            maxc = max(cost_evals)
            gapc = (maxc - minc)*0.1
            minc -= gapc
            maxc += gapc
            
            s = np.linspace(minx + gapx,maxx - gapx,num_iterations)
            scaled_costs = [c/float(max(cost_evals))*(maxx-gapx) - (minx+gapx) for c in cost_evals]
            ax3.plot(s,scaled_costs,color = 'k',linewidth = 1.5)
            ax3.set_xlabel('iteration',fontsize = 12)
            ax3.set_title('cost function plot',fontsize = 12)
            
            # rescale number of iterations and cost function value to fit same aspect ratio as other two subplots
            ax3.set_xlim(minx,maxx)
            #ax3.set_ylim(minc,maxc)
            
            ### set tickmarks for both axes - requries re-scaling   
            # x axis
            marks = range(0,num_iterations,round(num_iterations/5.0))
            ax3.set_xticks(s[marks])
            labels = [item.get_text() for item in ax3.get_xticklabels()]
            ax3.set_xticklabels(marks)
            
            ### y axis
            r = (max(scaled_costs) - min(scaled_costs))/5.0
            marks = [min(scaled_costs) + m*r for m in range(6)]
            ax3.set_yticks(marks)
            labels = [item.get_text() for item in ax3.get_yticklabels()]
            
            r = (max(cost_evals) - min(cost_evals))/5.0
            marks = [int(min(cost_evals) + m*r) for m in range(6)]
            ax3.set_yticklabels(marks)
コード例 #30
0
ファイル: run_animators.py プロジェクト: hasnank/tugasakhir
    def multiclass_plot(self, ax1, ax2, run, w, view, **kwargs):
        model = run.model
        normalizer = run.normalizer

        ### plot all input data ###
        # generate input range for functions
        minx = min(min(self.x[:, 0]), min(self.x[:, 1]))
        maxx = max(max(self.x[:, 0]), max(self.x[:, 1]))
        gapx = (maxx - minx) * 0.1
        minx -= gapx
        maxx += gapx

        r = np.linspace(minx, maxx, 600)
        w1_vals, w2_vals = np.meshgrid(r, r)
        w1_vals.shape = (len(r)**2, 1)
        w2_vals.shape = (len(r)**2, 1)
        h = np.concatenate([w1_vals, w2_vals], axis=1).T

        g_vals = model(normalizer(h), w)
        g_vals = np.asarray(g_vals)
        g_vals = np.argmax(g_vals, axis=0)

        # vals for cost surface
        w1_vals.shape = (len(r), len(r))
        w2_vals.shape = (len(r), len(r))
        g_vals.shape = (len(r), len(r))

        # scatter points in both panels
        class_nums = np.unique(self.y)
        C = len(class_nums)
        for c in range(C):
            ind = np.argwhere(self.y == class_nums[c])
            ind = [v[0] for v in ind]
            ax1.scatter(self.x[ind, 0],
                        self.x[ind, 1],
                        self.y[ind],
                        s=80,
                        color=self.colors[c],
                        edgecolor='k',
                        linewidth=1.5)
            ax2.scatter(self.x[ind, 0],
                        self.x[ind, 1],
                        s=110,
                        color=self.colors[c],
                        edgecolor='k',
                        linewidth=2)

        # switch for 2class / multiclass view
        if C == 2:
            # plot regression surface
            ax1.plot_surface(w1_vals,
                             w2_vals,
                             g_vals,
                             alpha=0.1,
                             color='k',
                             rstride=20,
                             cstride=20,
                             linewidth=0,
                             edgecolor='k')

            # plot zplane = 0 in left 3d panel - showing intersection of regressor with z = 0 (i.e., its contour, the separator, in the 3d plot too)?
            ax1.plot_surface(w1_vals,
                             w2_vals,
                             g_vals * 0,
                             alpha=0.1,
                             rstride=20,
                             cstride=20,
                             linewidth=0.15,
                             color='k',
                             edgecolor='k')

            # plot separator in left plot z plane
            ax1.contour(w1_vals,
                        w2_vals,
                        g_vals,
                        colors='k',
                        levels=[0],
                        linewidths=3,
                        zorder=1)

            # color parts of plane with correct colors
            ax1.contourf(w1_vals,
                         w2_vals,
                         g_vals + 1,
                         colors=self.colors[:],
                         alpha=0.1,
                         levels=range(0, 2))
            ax1.contourf(w1_vals,
                         w2_vals,
                         -g_vals + 1,
                         colors=self.colors[1:],
                         alpha=0.1,
                         levels=range(0, 2))

            # plot separator in right plot
            ax2.contour(w1_vals,
                        w2_vals,
                        g_vals,
                        colors='k',
                        levels=[0],
                        linewidths=3,
                        zorder=1)

            # adjust height of regressor to plot filled contours
            ax2.contourf(w1_vals,
                         w2_vals,
                         g_vals + 1,
                         colors=self.colors[:],
                         alpha=0.1,
                         levels=range(0, C + 1))

            ### clean up panels
            # set viewing limits on vertical dimension for 3d plot
            minz = min(copy.deepcopy(self.y))
            maxz = max(copy.deepcopy(self.y))

            gapz = (maxz - minz) * 0.1
            minz -= gapz
            maxz += gapz

        # multiclass view
        else:
            ax1.plot_surface(w1_vals,
                             w2_vals,
                             g_vals,
                             alpha=0.2,
                             color='w',
                             rstride=45,
                             cstride=45,
                             linewidth=2,
                             edgecolor='k')

            for c in range(C):
                # plot separator curve in left plot z plane
                ax1.contour(w1_vals,
                            w2_vals,
                            g_vals - c,
                            colors='k',
                            levels=[0],
                            linewidths=3,
                            zorder=1)

                # color parts of plane with correct colors
                ax1.contourf(w1_vals,
                             w2_vals,
                             g_vals - c + 0.5,
                             colors=self.colors[c],
                             alpha=0.4,
                             levels=[0, 1])

            # plot separator in right plot
            ax2.contour(w1_vals,
                        w2_vals,
                        g_vals,
                        colors='k',
                        levels=range(0, C + 1),
                        linewidths=3,
                        zorder=1)

            # adjust height of regressor to plot filled contours
            ax2.contourf(w1_vals,
                         w2_vals,
                         g_vals + 0.5,
                         colors=self.colors[:],
                         alpha=0.2,
                         levels=range(0, C + 1))

            ### clean up panels
            # set viewing limits on vertical dimension for 3d plot
            minz = 0
            maxz = max(copy.deepcopy(self.y))
            gapz = (maxz - minz) * 0.1
            minz -= gapz
            maxz += gapz
            ax1.set_zlim([minz, maxz])

            ax1.view_init(view[0], view[1])

        # clean up panel
        ax1.xaxis.pane.fill = False
        ax1.yaxis.pane.fill = False
        ax1.zaxis.pane.fill = False

        ax1.xaxis.pane.set_edgecolor('white')
        ax1.yaxis.pane.set_edgecolor('white')
        ax1.zaxis.pane.set_edgecolor('white')

        ax1.xaxis._axinfo["grid"]['color'] = (1, 1, 1, 0)
        ax1.yaxis._axinfo["grid"]['color'] = (1, 1, 1, 0)
        ax1.zaxis._axinfo["grid"]['color'] = (1, 1, 1, 0)

        ax1.set_xlabel(r'$x_1$', fontsize=16, labelpad=5)
        ax1.set_ylabel(r'$x_2$', rotation=0, fontsize=16, labelpad=5)
        ax1.set_zlabel(r'$y$', rotation=0, fontsize=16, labelpad=5)

        ax2.set_xlabel(r'$x_1$', fontsize=18, labelpad=10)
        ax2.set_ylabel(r'$x_2$', rotation=0, fontsize=18, labelpad=15)
コード例 #31
0
    def show_surface_fit(self, w_hist, view, **kwargs):
        '''
        # determine best set of weights from history
        cost_evals = []
        for i in range(len(w_hist)):
            W = w_hist[i]
            cost = self.counting_cost(W)
            cost_evals.append(cost)
        ind = np.argmin(cost_evals)
        '''

        # or just take last weights
        self.W = w_hist[-1]

        # initialize figure
        fig = plt.figure(figsize=(9, 4))
        gs = gridspec.GridSpec(1, 2, width_ratios=[1.5, 1])

        # setup current axis
        ax = plt.subplot(gs[0], projection='3d')
        ax2 = plt.subplot(gs[1], aspect='equal')

        # load in args
        zplane = 'on'
        if 'zplane' in kwargs:
            zplane = kwargs['zplane']

        # generate input range for viewing range
        minx = min(min(self.x[0, :]), min(self.x[1, :]))
        maxx = max(max(self.x[0, :]), max(self.x[1, :]))
        gapx = (maxx - minx) * 0.1
        minx -= gapx
        maxx += gapx

        # plot panel with all data and separators
        # scatter points in both panels
        class_nums = np.unique(self.y)
        C = len(class_nums)

        # plot data in right panel from above
        self.plot_data(ax2)

        ### draw multiclass boundary on right panel
        r = np.linspace(minx, maxx, 1000)
        w1_vals, w2_vals = np.meshgrid(r, r)
        w1_vals.shape = (len(r)**2, 1)
        w2_vals.shape = (len(r)**2, 1)
        o = np.ones((len(r)**2, 1))
        h = np.concatenate([o, w1_vals, w2_vals], axis=1)
        pts = np.dot(h, self.W)
        g_vals = np.argmax(pts, axis=1)

        # vals for cost surface
        w1_vals.shape = (len(r), len(r))
        w2_vals.shape = (len(r), len(r))
        g_vals.shape = (len(r), len(r))

        # plot contour in right panel
        C = len(np.unique(self.y))
        ax2.contour(w1_vals,
                    w2_vals,
                    g_vals,
                    colors='k',
                    levels=range(-1, C),
                    linewidths=2.75,
                    zorder=4)
        ax2.contourf(w1_vals,
                     w2_vals,
                     g_vals,
                     colors=self.colors[:],
                     alpha=0.2,
                     levels=range(-1, C))

        # plot surface and z-plane contour in left panel
        ax.plot_surface(w1_vals,
                        w2_vals,
                        g_vals,
                        alpha=0.3,
                        color='w',
                        rstride=50,
                        cstride=50,
                        linewidth=0.5,
                        edgecolor='k',
                        zorder=0)

        # plot zplane = 0 in left 3d panel - showing intersection of regressor with z = 0 (i.e., its contour, the separator, in the 3d plot too)?
        if zplane == 'on':
            g_vals += 1
            #ax.plot_surface(w1_vals,w2_vals,g_vals*0-1,alpha = 0.1)

            # loop over each class and color in z-plane
            for c in class_nums:
                # plot separator curve in left plot z plane
                ax.contour(w1_vals,
                           w2_vals,
                           g_vals - c,
                           colors='k',
                           levels=[0],
                           linewidths=3,
                           zorder=1)

                # color parts of plane with correct colors
                ax.contourf(w1_vals,
                            w2_vals,
                            g_vals - 0.5 - c,
                            colors=self.colors[(int(c)):],
                            alpha=0.1,
                            levels=range(0, 2))

        # scatter points in 3d
        for c in range(C):
            ind = np.argwhere(self.y == class_nums[c])
            ind = [v[0] for v in ind]
            ax.scatter(self.x[0, ind],
                       self.x[1, ind],
                       self.y[ind],
                       s=80,
                       color=self.colors[c],
                       edgecolor='k',
                       linewidth=1.5,
                       zorder=3)

        # dress panel
        ax.view_init(view[0], view[1])
        ax.axis('off')
        ax.set_xlim(minx, maxx)
        ax.set_ylim(minx, maxx)
        ax.set_zlim(-0.5, C + 0.5)

        ax2.set_xticks([])
        ax2.set_yticks([])
        ax2.set_yticklabels([])
        ax2.set_xticklabels([])
        ax2.set_xlim(minx, maxx)
        ax2.set_ylim(minx, maxx)
コード例 #32
0
lmb = 0.001

for i in range(100):
    loss_grad = grad(loss_fun)(X, Y, w)
    w[0] = w[0] - lmb * loss_grad[0]
    w[1] = w[1] - lmb * loss_grad[1]

surface2 = np.zeros((nx, ny))

for i, x in enumerate(X):
    for j, y in enumerate(Y):
        surface2[i][j] = psy_trial(x, y, w)

fig = plt.figure()
ax = fig.gca(projection='3d')
x, y = np.meshgrid(X, Y)
surf = ax.plot_surface(x,
                       y,
                       surface,
                       rstride=1,
                       cstride=1,
                       cmap=cm.viridis,
                       linewidth=0,
                       antialiased=False)

ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.set_zlim(0, 2)

ax.set_xlabel('$x$')
ax.set_ylabel('$y$')
コード例 #33
0
    return activation(np.dot(inputs, weights))
def loss(weights):
    preds = predict(weights, train_X)
    label_probabilities = preds * train_y + (1 - preds) * (1 - train_y)
    return -np.sum(np.log(label_probabilities))
# Compute the gradient of the loss function
gradient_loss = grad(loss)
# Set the initial weights
weights = np.array([1.0, 1.0])
# Steepest Descent
loss_values = []
learning_rate = 0.001
for i in range(100):
    loss_values.append(loss(weights))
    step = gradient_loss(weights)
    weights -= step * learning_rate
# Plot the decision boundary
x_min, x_max = train_X[:, 0].min() - 0.5, train_X[:, 0].max() + 0.5
y_min, y_max = train_X[:, 1].min() - 0.5, train_X[:, 1].max() + 0.5
x_mesh, y_mesh = np.meshgrid(np.arange(x_min, x_max, 0.01), np.arange(y_min, y_max, 0.01))
Z = predict(weights, np.c_[x_mesh.ravel(), y_mesh.ravel()])
Z = Z.reshape(x_mesh.shape)
cs = pylab.contourf(x_mesh, y_mesh, Z, cmap=pylab.cm.Spectral)
pylab.scatter(train_X[:, 0], train_X[:, 1], c=train_y, cmap=pylab.cm.Spectral)
pylab.colorbar(cs)
# Plot the loss over each step
pylab.figure()
pylab.plot(loss_values)
pylab.xlabel("Steps")
pylab.ylabel("Loss")
pylab.show()
コード例 #34
0
ファイル: plot3d.py プロジェクト: TCoherence/EECS-475-ML
    def draw_2d(self, g, **kwargs):
        self.g = g  # input function
        wmin = -5.1
        wmax = 5.1
        view = [50, 50]
        num_contours = 100
        if 'wmin' in kwargs:
            wmin = kwargs['wmin']
        if 'wmax' in kwargs:
            wmax = kwargs['wmax']
        if 'view' in kwargs:
            view = kwargs['view']
        if 'num_contours' in kwargs:
            num_contours = kwargs['num_contours']

        ##### construct figure with panels #####
        # construct figure
        fig = plt.figure(figsize=(9, 3))

        # remove whitespace from figure
        fig.subplots_adjust(left=0, right=1, bottom=0,
                            top=1)  # remove whitespace

        # create subplot with 3 panels, plot input function in center plot
        gs = gridspec.GridSpec(1, 2, width_ratios=[1, 2])
        ax = plt.subplot(gs[0], projection='3d')
        ax2 = plt.subplot(gs[1], aspect='equal')

        #### define input space for function and evaluate ####
        w = np.linspace(-wmax, wmax, 200)
        w1_vals, w2_vals = np.meshgrid(w, w)
        w1_vals.shape = (len(w)**2, 1)
        w2_vals.shape = (len(w)**2, 1)
        h = np.concatenate((w1_vals, w2_vals), axis=1)
        func_vals = np.asarray([g(s) for s in h])
        w1_vals.shape = (len(w), len(w))
        w2_vals.shape = (len(w), len(w))
        func_vals.shape = (len(w), len(w))

        ### plot function as surface ###
        ax.plot_surface(w1_vals,
                        w2_vals,
                        func_vals,
                        alpha=0.1,
                        color='w',
                        rstride=25,
                        cstride=25,
                        linewidth=1,
                        edgecolor='k',
                        zorder=2)

        # plot z=0 plane
        ax.plot_surface(w1_vals,
                        w2_vals,
                        func_vals * 0,
                        alpha=0.1,
                        color='w',
                        zorder=1,
                        rstride=25,
                        cstride=25,
                        linewidth=0.3,
                        edgecolor='k')

        ### plot function as contours ###
        # set level ridges
        levelmin = min(func_vals.flatten())
        levelmax = max(func_vals.flatten())
        cutoff = 0.5
        cutoff = (levelmax - levelmin) * cutoff
        numper = 3
        levels1 = np.linspace(cutoff, levelmax, numper)
        num_contours -= numper

        levels2 = np.linspace(levelmin, cutoff, min(num_contours, numper))
        levels = np.unique(np.append(levels1, levels2))
        num_contours -= numper
        while num_contours > 0:
            cutoff = levels[1]
            levels2 = np.linspace(levelmin, cutoff, min(num_contours, numper))
            levels = np.unique(np.append(levels2, levels))
            num_contours -= numper

        ax2.contour(w1_vals, w2_vals, func_vals, levels=levels, colors='k')
        ax2.contourf(w1_vals, w2_vals, func_vals, levels=levels, cmap='Blues')

        ### cleanup panels ###
        ax.set_xlabel('$w_1$', fontsize=12)
        ax.set_ylabel('$w_2$', fontsize=12, rotation=0)
        ax.set_title('$g(w_1,w_2)$', fontsize=12)
        ax.view_init(view[0], view[1])

        ax2.set_xlabel('$w_1$', fontsize=12)
        ax2.set_ylabel('$w_2$', fontsize=12, rotation=0)
        ax2.axhline(y=0, color='k', zorder=0, linewidth=0.5)
        ax2.axvline(x=0, color='k', zorder=0, linewidth=0.5)
        ax2.set_xticks(np.arange(-round(wmax), round(wmax) + 1))
        ax2.set_yticks(np.arange(-round(wmax), round(wmax) + 1))

        # clean up axis
        ax.xaxis.pane.fill = False
        ax.yaxis.pane.fill = False
        ax.zaxis.pane.fill = False

        ax.xaxis.pane.set_edgecolor('white')
        ax.yaxis.pane.set_edgecolor('white')
        ax.zaxis.pane.set_edgecolor('white')

        ax.xaxis._axinfo["grid"]['color'] = (1, 1, 1, 0)
        ax.yaxis._axinfo["grid"]['color'] = (1, 1, 1, 0)
        ax.zaxis._axinfo["grid"]['color'] = (1, 1, 1, 0)

        # plot
        plt.show()
コード例 #35
0
    loss_grad = grad(loss_function)(W, x_space, y_space)
    W[0] = W[0] - lmb * loss_grad[0]
    W[1] = W[1] - lmb * loss_grad[1]

print(loss_function(W, x_space, y_space))

surface2 = np.zeros((ny, nx))

for i, x in enumerate(x_space):
    for j, y in enumerate(y_space):
        net_outt = neural_network(W, [x, y])[0]
        surface2[i][j] = psy_trial([x, y], net_outt)

fig = plt.figure()
ax = fig.gca(projection='3d')
X, Y = np.meshgrid(x_space, y_space)
surf = ax.plot_surface(X,
                       Y,
                       surface2,
                       rstride=1,
                       cstride=1,
                       cmap=cm.viridis,
                       linewidth=0,
                       antialiased=False)

ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.set_zlim(0, 1)

ax.set_xlabel('$x$')
ax.set_ylabel('$y$')
コード例 #36
0
#number density of obj in 1d

sig_psf = 0.1  # psf width
sig_noise = 0.02  # noise level

#these are values for the power law function for sampling intensities
w_interval = (1, 2)
w_lin = np.linspace(1, 2, 100)
alpha_true = 2
w_norm = (50**(alpha_true + 1) -
          w_interval[0]**(alpha_true + 1)) / (alpha_true + 1)
w_func = np.power(w_lin, alpha_true) / w_norm
w_true = w_norm * np.random.choice(w_func, Ndata)

mid = int(n_grid / 2)
x, y = np.meshgrid(pix_1d, pix_1d)
psf = np.exp(-((y - pix_1d[mid])**2 + (x - pix_1d[mid])**2) / 2 / sig_psf**2)
#keep in mind difference between x and y position and indices! Here, you are given indices, but meshgrid is in x-y coords


def psi(pos):
    ''' measurement model, which in our case is just a 1d gaussian of width 
    sigma (PSF) written out to a meshgrid created by pix1d 
    '''
    x, y = np.meshgrid(pix_1d, pix_1d)
    return np.exp(-((y - pix_1d[pos[0]])**2 + (x - pix_1d[pos[1]])**2) / 2 /
                  sig_psf**2)
    #keep in mind difference between x and y position and indices! Here, you are given indices, but meshgrid is in x-y coords


def gaussian(x, loc=None, scale=None):
コード例 #37
0
    def static_N2_simple(self, ax, w_best, runner, train_valid):
        cost = runner.cost
        predict = runner.model
        feat = runner.feature_transforms
        normalizer = runner.normalizer
        inverse_nornalizer = runner.inverse_normalizer

        # or just take last weights
        self.w = w_best

        ### create boundary data ###
        xmin1 = np.min(self.x[:, 0])
        xmax1 = np.max(self.x[:, 0])
        xgap1 = (xmax1 - xmin1) * 0.05
        xmin1 -= xgap1
        xmax1 += xgap1

        xmin2 = np.min(self.x[:, 1])
        xmax2 = np.max(self.x[:, 1])
        xgap2 = (xmax2 - xmin2) * 0.05
        xmin2 -= xgap2
        xmax2 += xgap2

        # plot boundary for 2d plot
        r1 = np.linspace(xmin1, xmax1, 300)
        r2 = np.linspace(xmin2, xmax2, 300)
        s, t = np.meshgrid(r1, r2)
        s = np.reshape(s, (np.size(s), 1))
        t = np.reshape(t, (np.size(t), 1))
        h = np.concatenate((s, t), axis=1)

        # compute model on train data
        z1 = predict(normalizer(h.T), self.w)
        z1 = np.sign(z1)

        # reshape it
        s.shape = (np.size(r1), np.size(r2))
        t.shape = (np.size(r1), np.size(r2))
        z1.shape = (np.size(r1), np.size(r2))

        ### loop over two panels plotting each ###
        # plot training points
        if train_valid == 'train':
            # reverse normalize data
            x_train = inverse_nornalizer(runner.x_train).T
            y_train = runner.y_train

            # plot data
            ind0 = np.argwhere(y_train == +1)
            ind0 = [v[1] for v in ind0]
            ax.scatter(x_train[ind0, 0],
                       x_train[ind0, 1],
                       s=45,
                       color=self.colors[0],
                       edgecolor=[0, 0.7, 1],
                       linewidth=1,
                       zorder=3)

            ind1 = np.argwhere(y_train == -1)
            ind1 = [v[1] for v in ind1]
            ax.scatter(x_train[ind1, 0],
                       x_train[ind1, 1],
                       s=45,
                       color=self.colors[1],
                       edgecolor=[0, 0.7, 1],
                       linewidth=1,
                       zorder=3)
            ax.set_title('training data', fontsize=15)

        if train_valid == 'validate':
            # reverse normalize data
            x_valid = inverse_nornalizer(runner.x_valid).T
            y_valid = runner.y_valid

            # plot testing points
            ind0 = np.argwhere(y_valid == +1)
            ind0 = [v[1] for v in ind0]
            ax.scatter(x_valid[ind0, 0],
                       x_valid[ind0, 1],
                       s=45,
                       color=self.colors[0],
                       edgecolor=[1, 0.8, 0.5],
                       linewidth=1,
                       zorder=3)

            ind1 = np.argwhere(y_valid == -1)
            ind1 = [v[1] for v in ind1]
            ax.scatter(x_valid[ind1, 0],
                       x_valid[ind1, 1],
                       s=45,
                       color=self.colors[1],
                       edgecolor=[1, 0.8, 0.5],
                       linewidth=1,
                       zorder=3)
            ax.set_title('validation data', fontsize=15)

        if train_valid == 'original':
            # plot all points
            ind0 = np.argwhere(self.y == +1)
            ax.scatter(self.x[ind0, 0],
                       self.x[ind0, 1],
                       s=55,
                       color=self.colors[0],
                       edgecolor='k',
                       linewidth=1,
                       zorder=3)

            ind1 = np.argwhere(self.y == -1)
            ax.scatter(self.x[ind1, 0],
                       self.x[ind1, 1],
                       s=55,
                       color=self.colors[1],
                       edgecolor='k',
                       linewidth=1,
                       zorder=3)
            ax.set_title('original data', fontsize=15)

        #### plot contour, color regions ####
        ax.contour(s, t, z1, colors='k', linewidths=2.5, levels=[0], zorder=2)
        ax.contourf(s,
                    t,
                    z1,
                    colors=[self.colors[1], self.colors[0]],
                    alpha=0.15,
                    levels=range(-1, 2))

        # cleanup panel
        ax.set_xlabel(r'$x_1$', fontsize=15)
        ax.set_ylabel(r'$x_2$', fontsize=15, rotation=0, labelpad=20)
        ax.xaxis.set_major_formatter(FormatStrFormatter('%.1f'))
        ax.yaxis.set_major_formatter(FormatStrFormatter('%.1f'))
コード例 #38
0
def reconstruct_fullfield(fname,
                          theta_st=0,
                          theta_end=PI,
                          n_epochs='auto',
                          crit_conv_rate=0.03,
                          max_nepochs=200,
                          alpha=1e-7,
                          alpha_d=None,
                          alpha_b=None,
                          gamma=1e-6,
                          learning_rate=1.0,
                          output_folder=None,
                          minibatch_size=None,
                          save_intermediate=False,
                          full_intermediate=False,
                          energy_ev=5000,
                          psize_cm=1e-7,
                          n_epochs_mask_release=None,
                          cpu_only=False,
                          save_path='.',
                          shrink_cycle=10,
                          core_parallelization=True,
                          free_prop_cm=None,
                          multiscale_level=1,
                          n_epoch_final_pass=None,
                          initial_guess=None,
                          n_batch_per_update=5,
                          dynamic_rate=True,
                          probe_type='plane',
                          probe_initial=None,
                          probe_learning_rate=1e-3,
                          pupil_function=None,
                          theta_downsample=None,
                          forward_algorithm='fresnel',
                          random_theta=True,
                          object_type='normal',
                          fresnel_approx=True,
                          shared_file_object=False,
                          reweighted_l1=False,
                          **kwargs):
    """
    Reconstruct a beyond depth-of-focus object.
    :param fname: Filename and path of raw data file. Must be in HDF5 format.
    :param theta_st: Starting rotation angle.
    :param theta_end: Ending rotation angle.
    :param n_epochs: Number of epochs to be executed. If given 'auto', optimizer will stop
                     when reduction rate of loss function goes below crit_conv_rate.
    :param crit_conv_rate: Reduction rate of loss function below which the optimizer should
                           stop.
    :param max_nepochs: The maximum number of epochs to be executed if n_epochs is 'auto'.
    :param alpha: Weighting coefficient for both delta and beta regularizer. Should be None
                  if alpha_d and alpha_b are specified.
    :param alpha_d: Weighting coefficient for delta regularizer.
    :param alpha_b: Weighting coefficient for beta regularizer.
    :param gamma: Weighting coefficient for TV regularizer.
    :param learning_rate: Learning rate of ADAM.
    :param output_folder: Name of output folder. Put None for auto-generated pattern.
    :param downsample: Downsampling (not implemented yet).
    :param minibatch_size: Size of minibatch.
    :param save_intermediate: Whether to save the object after each epoch.
    :param energy_ev: Beam energy in eV.
    :param psize_cm: Pixel size in cm.
    :param n_epochs_mask_release: The number of epochs after which the finite support mask
                                  is released. Put None to disable this feature.
    :param cpu_only: Whether to disable GPU.
    :param save_path: The location of finite support mask, the prefix of output_folder and
                      other metadata.
    :param shrink_cycle: Shrink-wrap is executed per every this number of epochs.
    :param core_parallelization: Whether to use Horovod for parallelized computation within
                                 this function.
    :param free_prop_cm: The distance to propagate the wavefront in free space after exiting
                         the sample, in cm.
    :param multiscale_level: The level of multiscale processing. When this number is m and
                             m > 1, m - 1 low-resolution reconstructions will be performed
                             before reconstructing with the original resolution. The downsampling
                             factor for these coarse reconstructions will be [2^(m - 1),
                             2^(m - 2), ..., 2^1].
    :param n_epoch_final_pass: specify a number of iterations for the final pass if multiscale
                               is activated. If None, it will be the same as n_epoch.
    :param initial_guess: supply an initial guess. If None, object will be initialized with noises.
    :param n_batch_per_update: number of minibatches during which gradients are accumulated, after
                               which obj is updated.
    :param dynamic_rate: when n_batch_per_update > 1, adjust learning rate dynamically to allow it
                         to decrease with epoch number
    :param probe_type: type of wavefront. Can be 'plane', '  fixed', or 'optimizable'. If 'optimizable',
                           the probe function will be optimized along with the object.
    :param probe_initial: can be provided for 'optimizable' probe_type, and must be provided for
                              'fixed'.
    """
    def calculate_loss(obj_delta, obj_beta, this_ind_batch, this_prj_batch):

        if not shared_file_object:
            obj_stack = np.stack([obj_delta, obj_beta], axis=3)
            obj_rot_batch = []
            for i in range(minibatch_size):
                obj_rot_batch.append(
                    apply_rotation(
                        obj_stack, coord_ls[this_ind_batch[i]],
                        'arrsize_{}_{}_{}_ntheta_{}'.format(
                            dim_y, dim_x, dim_x, n_theta)))
            obj_rot_batch = np.stack(obj_rot_batch)

            exiting_batch = multislice_propagate_batch_numpy(
                obj_rot_batch[:, :, :, :, 0],
                obj_rot_batch[:, :, :, :, 1],
                probe_real,
                probe_imag,
                energy_ev,
                psize_cm * ds_level,
                free_prop_cm=free_prop_cm,
                obj_batch_shape=[minibatch_size, *this_obj_size],
                kernel=h,
                fresnel_approx=fresnel_approx)
            loss = np.mean((np.abs(exiting_batch) - np.abs(this_prj_batch))**2)

        else:
            exiting_batch = multislice_propagate_batch_numpy(
                obj_delta,
                obj_beta,
                probe_real,
                probe_imag,
                energy_ev,
                psize_cm * ds_level,
                free_prop_cm=free_prop_cm,
                obj_batch_shape=obj_delta.shape,
                kernel=h,
                fresnel_approx=fresnel_approx)
            exiting_batch = exiting_batch[:, safe_zone_width:exiting_batch.
                                          shape[1] - safe_zone_width,
                                          safe_zone_width:exiting_batch.
                                          shape[2] - safe_zone_width]
            loss = np.mean((np.abs(exiting_batch) - np.abs(this_prj_batch))**2)

        dxchange.write_tiff(
            np.squeeze(abs(exiting_batch._value)),
            'cone_256_foam/test_shared_file_object/current/exit_{}'.format(
                rank),
            dtype='float32',
            overwrite=True)
        dxchange.write_tiff(
            np.squeeze(abs(this_prj_batch)),
            'cone_256_foam/test_shared_file_object/current/prj_{}'.format(
                rank),
            dtype='float32',
            overwrite=True)

        reg_term = 0
        if reweighted_l1:
            if alpha_d not in [None, 0]:
                reg_term = reg_term + alpha_d * np.mean(
                    weight_l1 * np.abs(obj_delta))
                loss = loss + reg_term
            if alpha_b not in [None, 0]:
                reg_term = reg_term + alpha_b * np.mean(
                    weight_l1 * np.abs(obj_beta))
                loss = loss + reg_term
        else:
            if alpha_d not in [None, 0]:
                reg_term = reg_term + alpha_d * np.mean(np.abs(obj_delta))
                loss = loss + reg_term
            if alpha_b not in [None, 0]:
                reg_term = reg_term + alpha_b * np.mean(np.abs(obj_beta))
                loss = loss + reg_term
        if gamma not in [None, 0]:
            if shared_file_object:
                reg_term = reg_term + gamma * total_variation_3d(obj_delta,
                                                                 axis_offset=1)
            else:
                reg_term = reg_term + gamma * total_variation_3d(obj_delta,
                                                                 axis_offset=0)
            loss = loss + reg_term

        print('Loss:', loss._value, 'Regularization term:',
              reg_term._value if reg_term != 0 else 0)

        # if alpha_d is None:
        #     reg_term = alpha * (np.sum(np.abs(obj_delta)) + np.sum(np.abs(obj_delta))) + gamma * total_variation_3d(
        #         obj_delta)
        # else:
        #     if gamma == 0:
        #         reg_term = alpha_d * np.sum(np.abs(obj_delta)) + alpha_b * np.sum(np.abs(obj_beta))
        #     else:
        #         reg_term = alpha_d * np.sum(np.abs(obj_delta)) + alpha_b * np.sum(
        #             np.abs(obj_beta)) + gamma * total_variation_3d(obj_delta)
        # loss = loss + reg_term

        # Write convergence data
        f_conv.write('{},{},{},'.format(i_epoch, i_batch, loss._value))
        f_conv.flush()

        return loss

    comm = MPI.COMM_WORLD
    n_ranks = comm.Get_size()
    rank = comm.Get_rank()
    t_zero = time.time()

    # read data
    t0 = time.time()
    print_flush('Reading data...', 0, rank)
    f = h5py.File(os.path.join(save_path, fname), 'r')
    prj_0 = f['exchange/data']
    theta = -np.linspace(theta_st, theta_end, prj_0.shape[0], dtype='float32')
    n_theta = len(theta)
    prj_theta_ind = np.arange(n_theta, dtype=int)
    if theta_downsample is not None:
        prj_0 = prj_0[::theta_downsample]
        theta = theta[::theta_downsample]
        prj_theta_ind = prj_theta_ind[::theta_downsample]
        n_theta = len(theta)
    original_shape = prj_0.shape
    comm.Barrier()
    print_flush('Data reading: {} s'.format(time.time() - t0), 0, rank)
    print_flush('Data shape: {}'.format(original_shape), 0, rank)
    comm.Barrier()

    if output_folder is None:
        output_folder = 'recon_360_minibatch_{}_' \
                        'mskrls_{}_' \
                        'shrink_{}_' \
                        'iter_{}_' \
                        'alphad_{}_' \
                        'alphab_{}_' \
                        'gamma_{}_' \
                        'rate_{}_' \
                        'energy_{}_' \
                        'size_{}_' \
                        'ntheta_{}_' \
                        'prop_{}_' \
                        'ms_{}_' \
                        'cpu_{}' \
            .format(minibatch_size, n_epochs_mask_release, shrink_cycle,
                    n_epochs, alpha_d, alpha_b,
                    gamma, learning_rate, energy_ev,
                    prj_0.shape[-1], prj_0.shape[0], free_prop_cm,
                    multiscale_level, cpu_only)
        if abs(PI - theta_end) < 1e-3:
            output_folder += '_180'

    if save_path != '.':
        output_folder = os.path.join(save_path, output_folder)

    for ds_level in range(multiscale_level - 1, -1, -1):

        initializer_flag = False if ds_level == range(multiscale_level -
                                                      1, -1, -1)[0] else True

        ds_level = 2**ds_level
        print_flush('Multiscale downsampling level: {}'.format(ds_level), 0,
                    rank)
        comm.Barrier()

        # Physical metadata
        voxel_nm = np.array([psize_cm] * 3) * 1.e7 * ds_level
        lmbda_nm = 1240. / energy_ev
        delta_nm = voxel_nm[-1]

        # downsample data
        prj = prj_0
        # prj = np.copy(prj_0)
        # if ds_level > 1:
        #     prj = prj[:, ::ds_level, ::ds_level]
        #     prj = prj.astype('complex64')
        # comm.Barrier()

        dim_y, dim_x = prj.shape[-2] // ds_level, prj.shape[-1] // ds_level
        this_obj_size = [dim_y, dim_x, dim_x]
        comm.Barrier()

        if shared_file_object:
            # Create parallel npy
            if rank == 0:
                try:
                    os.makedirs(os.path.join(output_folder))
                except:
                    print('Target folder {} exists.'.format(output_folder))
                np.save(os.path.join(output_folder, 'intermediate_obj.npy'),
                        np.zeros([*this_obj_size, 2]))
                np.save(os.path.join(output_folder, 'intermediate_m.npy'),
                        np.zeros([*this_obj_size, 2]))
                np.save(os.path.join(output_folder, 'intermediate_v.npy'),
                        np.zeros([*this_obj_size, 2]))
            comm.Barrier()

            # Create memmap pointer on each rank
            dset = np.load(os.path.join(output_folder, 'intermediate_obj.npy'),
                           mmap_mode='r+',
                           allow_pickle=True)
            dset_m = np.load(os.path.join(output_folder, 'intermediate_m.npy'),
                             mmap_mode='r+',
                             allow_pickle=True)
            dset_v = np.load(os.path.join(output_folder, 'intermediate_v.npy'),
                             mmap_mode='r+',
                             allow_pickle=True)

            # Get block allocation
            n_blocks_y, n_blocks_x, n_blocks, block_size = get_block_division(
                this_obj_size, n_ranks)
            print_flush('Number of blocks in y: {}'.format(n_blocks_y), 0,
                        rank)
            print_flush('Number of blocks in x: {}'.format(n_blocks_x), 0,
                        rank)
            print_flush('Block size: {}'.format(block_size), 0, rank)
            probe_pos = []
            # probe_pos is a list of tuples of (line_st, line_end, px_st, ps_end).
            for i_pos in range(n_blocks):
                probe_pos.append(
                    get_block_range(i_pos, n_blocks_x, block_size)[:4])
            probe_pos = np.array(probe_pos)
            if free_prop_cm not in [0, None]:
                safe_zone_width = ceil(4.0 * np.sqrt(
                    (delta_nm * dim_x + free_prop_cm * 1e7) * lmbda_nm) /
                                       (voxel_nm[0]))
            else:
                safe_zone_width = ceil(4.0 * np.sqrt(
                    (delta_nm * dim_x) * lmbda_nm) / (voxel_nm[0]))
            print_flush('safe zone: {}'.format(safe_zone_width), 0, rank)

        # read rotation data
        try:
            coord_ls = read_all_origin_coords(
                'arrsize_{}_{}_{}_ntheta_{}'.format(dim_y, dim_x, dim_x,
                                                    n_theta), n_theta)
        except:
            save_rotation_lookup([dim_y, dim_x, dim_x], n_theta)
            coord_ls = read_all_origin_coords(
                'arrsize_{}_{}_{}_ntheta_{}'.format(dim_y, dim_x, dim_x,
                                                    n_theta), n_theta)

        if minibatch_size is None:
            minibatch_size = n_theta

        if n_epochs_mask_release is None:
            n_epochs_mask_release = np.inf

        if (not shared_file_object) or (shared_file_object and rank == 0):
            try:
                mask = dxchange.read_tiff_stack(
                    os.path.join(save_path, 'fin_sup_mask', 'mask_00000.tiff'),
                    range(prj_0.shape[1]))
            except:
                try:
                    mask = dxchange.read_tiff(
                        os.path.join(save_path, 'fin_sup_mask', 'mask.tiff'))
                except:
                    obj_pr = dxchange.read_tiff_stack(
                        os.path.join(save_path,
                                     'paganin_obj/recon_00000.tiff'),
                        range(prj_0.shape[1]), 5)
                    obj_pr = gaussian_filter(np.abs(obj_pr),
                                             sigma=3,
                                             mode='constant')
                    mask = np.zeros_like(obj_pr)
                    mask[obj_pr > 1e-5] = 1
                    dxchange.write_tiff_stack(mask,
                                              os.path.join(
                                                  save_path,
                                                  'fin_sup_mask/mask'),
                                              dtype='float32',
                                              overwrite=True)
            if ds_level > 1:
                mask = mask[::ds_level, ::ds_level, ::ds_level]
            if shared_file_object:
                np.save(os.path.join(output_folder, 'intermediate_mask.npy'),
                        mask)
        comm.Barrier()

        if shared_file_object:
            dset_mask = np.load(os.path.join(output_folder,
                                             'intermediate_mask.npy'),
                                mmap_mode='r+',
                                allow_pickle=True)

        # unify random seed for all threads
        comm.Barrier()
        seed = int(time.time() / 60)
        np.random.seed(seed)
        comm.Barrier()

        if rank == 0:
            if initializer_flag == False:
                if initial_guess is None:
                    print_flush('Initializing with Gaussian random.', 0, rank)
                    obj_delta = np.random.normal(size=[dim_y, dim_x, dim_x],
                                                 loc=8.7e-7,
                                                 scale=1e-7) * mask
                    obj_beta = np.random.normal(size=[dim_y, dim_x, dim_x],
                                                loc=5.1e-8,
                                                scale=1e-8) * mask
                    obj_delta[obj_delta < 0] = 0
                    obj_beta[obj_beta < 0] = 0
                else:
                    print_flush('Using supplied initial guess.', 0, rank)
                    sys.stdout.flush()
                    obj_delta = initial_guess[0]
                    obj_beta = initial_guess[1]
            else:
                print_flush('Initializing previous pass outcomes.', 0, rank)
                obj_delta = dxchange.read_tiff(
                    os.path.join(output_folder,
                                 'delta_ds_{}.tiff'.format(ds_level * 2)))
                obj_beta = dxchange.read_tiff(
                    os.path.join(output_folder,
                                 'beta_ds_{}.tiff'.format(ds_level * 2)))
                obj_delta = upsample_2x(obj_delta)
                obj_beta = upsample_2x(obj_beta)
                obj_delta += np.random.normal(
                    size=[dim_y, dim_x, dim_x], loc=8.7e-7, scale=1e-7) * mask
                obj_beta += np.random.normal(
                    size=[dim_y, dim_x, dim_x], loc=5.1e-8, scale=1e-8) * mask
                obj_delta[obj_delta < 0] = 0
                obj_beta[obj_beta < 0] = 0
            obj_size = obj_delta.shape
            if object_type == 'phase_only':
                obj_beta[...] = 0
            elif object_type == 'absorption_only':
                obj_delta[...] = 0
            if not shared_file_object:
                np.save('init_delta_temp.npy', obj_delta)
                np.save('init_beta_temp.npy', obj_beta)
            else:
                dset[:, :, :, 0] = obj_delta
                dset[:, :, :, 1] = obj_beta
                dset_m[...] = 0
                dset_v[...] = 0
        comm.Barrier()

        if not shared_file_object:
            obj_delta = np.zeros(this_obj_size)
            obj_beta = np.zeros(this_obj_size)
            obj_delta[:, :, :] = np.load('init_delta_temp.npy',
                                         allow_pickle=True)
            obj_beta[:, :, :] = np.load('init_beta_temp.npy',
                                        allow_pickle=True)
            comm.Barrier()
            if rank == 0:
                os.remove('init_delta_temp.npy')
                os.remove('init_beta_temp.npy')
            comm.Barrier()

        print_flush('Initialzing probe...', 0, rank)
        if not shared_file_object:
            if probe_type == 'plane':
                probe_real = np.ones([dim_y, dim_x])
                probe_imag = np.zeros([dim_y, dim_x])
            elif probe_type == 'optimizable':
                if probe_initial is not None:
                    probe_mag, probe_phase = probe_initial
                    probe_real, probe_imag = mag_phase_to_real_imag(
                        probe_mag, probe_phase)
                else:
                    # probe_mag = np.ones([dim_y, dim_x])
                    # probe_phase = np.zeros([dim_y, dim_x])
                    back_prop_cm = (free_prop_cm + (psize_cm * obj_size[2])
                                    ) if free_prop_cm is not None else (
                                        psize_cm * obj_size[2])
                    probe_init = create_probe_initial_guess(
                        os.path.join(save_path, fname), back_prop_cm * 1.e7,
                        energy_ev, psize_cm * 1.e7)
                    probe_real = probe_init.real
                    probe_imag = probe_init.imag
                if pupil_function is not None:
                    probe_real = probe_real * pupil_function
                    probe_imag = probe_imag * pupil_function
            elif probe_type == 'fixed':
                probe_mag, probe_phase = probe_initial
                probe_real, probe_imag = mag_phase_to_real_imag(
                    probe_mag, probe_phase)
            elif probe_type == 'point':
                # this should be in spherical coordinates
                probe_real = np.ones([dim_y, dim_x])
                probe_imag = np.zeros([dim_y, dim_x])
            elif probe_type == 'gaussian':
                probe_mag_sigma = kwargs['probe_mag_sigma']
                probe_phase_sigma = kwargs['probe_phase_sigma']
                probe_phase_max = kwargs['probe_phase_max']
                py = np.arange(obj_size[0]) - (obj_size[0] - 1.) / 2
                px = np.arange(obj_size[1]) - (obj_size[1] - 1.) / 2
                pxx, pyy = np.meshgrid(px, py)
                probe_mag = np.exp(-(pxx**2 + pyy**2) /
                                   (2 * probe_mag_sigma**2))
                probe_phase = probe_phase_max * np.exp(
                    -(pxx**2 + pyy**2) / (2 * probe_phase_sigma**2))
                probe_real, probe_imag = mag_phase_to_real_imag(
                    probe_mag, probe_phase)
            else:
                raise ValueError(
                    'Invalid wavefront type. Choose from \'plane\', \'fixed\', \'optimizable\'.'
                )
        else:
            if probe_type == 'plane':
                probe_real = np.ones([block_size + 2 * safe_zone_width] * 2)
                probe_imag = np.zeros([block_size + 2 * safe_zone_width] * 2)
            else:
                raise ValueError(
                    'probe_type other than plane is not yet supported with shared file object.'
                )

        # =============finite support===================
        if not shared_file_object:
            obj_delta = obj_delta * mask
            obj_beta = obj_beta * mask
            obj_delta = np.clip(obj_delta, 0, None)
            obj_beta = np.clip(obj_beta, 0, None)
        # ==============================================

        # generate Fresnel kernel
        if not shared_file_object:
            h = get_kernel(delta_nm,
                           lmbda_nm,
                           voxel_nm, [dim_y, dim_y, dim_x],
                           fresnel_approx=fresnel_approx)
        else:
            h = get_kernel(delta_nm,
                           lmbda_nm,
                           voxel_nm, [
                               block_size + safe_zone_width * 2,
                               block_size + safe_zone_width * 2, dim_x
                           ],
                           fresnel_approx=fresnel_approx)

        loss_grad = grad(calculate_loss, [0, 1])

        # Save convergence data
        try:
            os.makedirs(os.path.join(output_folder, 'convergence'))
        except:
            pass
        f_conv = open(
            os.path.join(output_folder, 'convergence',
                         'loss_rank_{}.txt'.format(rank)), 'w')
        f_conv.write('i_epoch,i_batch,loss,time\n')

        print_flush('Optimizer started.', 0, rank)
        if rank == 0:
            create_summary(output_folder, locals(), preset='fullfield')

        cont = True
        i_epoch = 0
        while cont:
            if shared_file_object:
                # Do a ptychography-like allocation.
                n_pos = len(probe_pos)
                n_spots = n_theta * n_pos
                n_tot_per_batch = minibatch_size * n_ranks
                n_batch = int(np.ceil(float(n_spots) / n_tot_per_batch))
                spots_ls = range(n_spots)
                ind_list_rand = []

                theta_ls = np.arange(n_theta)
                np.random.shuffle(theta_ls)

                for i, i_theta in enumerate(theta_ls):
                    spots_ls = range(n_pos)
                    if n_pos % minibatch_size != 0:
                        # Append randomly selected diffraction spots if necessary, so that a rank won't be given
                        # spots from different angles in one batch.
                        spots_ls = np.append(
                            spots_ls,
                            np.random.choice(
                                spots_ls[:-(n_pos % minibatch_size)],
                                minibatch_size - (n_pos % minibatch_size),
                                replace=False))
                    if i == 0:
                        ind_list_rand = np.vstack(
                            [np.array([i_theta] * len(spots_ls)),
                             spots_ls]).transpose()
                    else:
                        ind_list_rand = np.concatenate([
                            ind_list_rand,
                            np.vstack([
                                np.array([i_theta] * len(spots_ls)), spots_ls
                            ]).transpose()
                        ],
                                                       axis=0)
                ind_list_rand = split_tasks(ind_list_rand, n_tot_per_batch)
                probe_size_half = block_size // 2 + safe_zone_width
            else:
                ind_list_rand = np.arange(n_theta)
                np.random.shuffle(ind_list_rand)
                n_tot_per_batch = n_ranks * minibatch_size
                if n_theta % n_tot_per_batch > 0:
                    ind_list_rand = np.concatenate([
                        ind_list_rand, ind_list_rand[:n_tot_per_batch -
                                                     n_theta % n_tot_per_batch]
                    ])
                ind_list_rand = split_tasks(ind_list_rand, n_tot_per_batch)
                ind_list_rand = [np.sort(x) for x in ind_list_rand]

            m, v = (None, None)
            t0 = time.time()
            for i_batch in range(len(ind_list_rand)):

                t00 = time.time()
                if not shared_file_object:
                    this_ind_batch = ind_list_rand[i_batch][rank *
                                                            minibatch_size:
                                                            (rank + 1) *
                                                            minibatch_size]
                    this_prj_batch = prj[
                        this_ind_batch, ::ds_level, ::ds_level]
                else:
                    if len(ind_list_rand[i_batch]) < n_tot_per_batch:
                        n_supp = n_tot_per_batch - len(ind_list_rand[i_batch])
                        ind_list_rand[i_batch] = np.concatenate([
                            ind_list_rand[i_batch], ind_list_rand[0][:n_supp]
                        ])

                    this_ind_batch = ind_list_rand[i_batch]
                    this_i_theta = this_ind_batch[rank * minibatch_size, 0]
                    this_ind_rank = this_ind_batch[rank *
                                                   minibatch_size:(rank + 1) *
                                                   minibatch_size, 1]

                    this_prj_batch = []
                    for i_pos in this_ind_rank:
                        line_st, line_end, px_st, px_end = probe_pos[i_pos]
                        line_st_0 = max([0, line_st])
                        line_end_0 = min([dim_y, line_end])
                        px_st_0 = max([0, px_st])
                        px_end_0 = min([dim_x, px_end])
                        patch = prj[this_i_theta, ::ds_level, ::ds_level][
                            line_st_0:line_end_0, px_st_0:px_end_0]
                        if line_st < 0:
                            patch = np.pad(patch, [[-line_st, 0], [0, 0]],
                                           mode='constant')
                        if line_end > dim_y:
                            patch = np.pad(patch,
                                           [[0, line_end - dim_y], [0, 0]],
                                           mode='constant')
                        if px_st < 0:
                            patch = np.pad(patch, [[0, 0], [-px_st, 0]],
                                           mode='constant')
                        if px_end > dim_x:
                            patch = np.pad(patch,
                                           [[0, 0], [0, px_end - dim_x]],
                                           mode='constant')
                        this_prj_batch.append(patch)
                    this_prj_batch = np.array(this_prj_batch)
                    this_pos_batch = probe_pos[this_ind_rank]
                    this_pos_batch_safe = this_pos_batch + np.array([
                        -safe_zone_width, safe_zone_width, -safe_zone_width,
                        safe_zone_width
                    ])
                    # if ds_level > 1:
                    #     this_prj_batch = this_prj_batch[:, :, ::ds_level, ::ds_level]
                    comm.Barrier()

                    # Get values for local chunks of object_delta and beta; interpolate and read directly from HDF5
                    obj = get_rotated_subblocks(dset, this_pos_batch_safe,
                                                coord_ls[this_i_theta], None)
                    obj_delta = np.array(obj[:, :, :, :, 0])
                    obj_beta = np.array(obj[:, :, :, :, 1])
                    m = get_rotated_subblocks(dset_m, this_pos_batch,
                                              coord_ls[this_i_theta], None)
                    m = np.array([m[:, :, :, :, 0], m[:, :, :, :, 1]])
                    m_0 = np.copy(m)
                    v = get_rotated_subblocks(dset_v, this_pos_batch,
                                              coord_ls[this_i_theta], None)
                    v = np.array([v[:, :, :, :, 0], v[:, :, :, :, 1]])
                    v_0 = np.copy(v)
                    mask = get_rotated_subblocks(dset_mask,
                                                 this_pos_batch,
                                                 coord_ls[this_i_theta],
                                                 None,
                                                 monochannel=True)

                    mask_0 = np.copy(mask)

                # Update weight for reweighted L1
                if i_batch % 10 == 0 and i_epoch >= 1:
                    weight_l1 = np.max(obj_delta) / (abs(obj_delta) + 1e-8)
                else:
                    weight_l1 = np.ones_like(obj_delta)

                grads = loss_grad(obj_delta, obj_beta, this_ind_batch,
                                  this_prj_batch)
                if not shared_file_object:
                    this_grads = np.array(grads)
                    grads = np.zeros_like(this_grads)
                    comm.Allreduce(this_grads, grads)
                # grads = comm.allreduce(this_grads)
                grads = np.array(grads)
                grads = grads / n_ranks

                if shared_file_object:
                    grads = grads[:, :,
                                  safe_zone_width:safe_zone_width + block_size,
                                  safe_zone_width:safe_zone_width +
                                  block_size, :]
                    obj_delta = obj_delta[:,
                                          safe_zone_width:obj_delta.shape[1] -
                                          safe_zone_width,
                                          safe_zone_width:obj_delta.shape[2] -
                                          safe_zone_width, :]
                    obj_beta = obj_beta[:, safe_zone_width:obj_beta.shape[1] -
                                        safe_zone_width,
                                        safe_zone_width:obj_beta.shape[2] -
                                        safe_zone_width, :]

                (obj_delta, obj_beta), m, v = apply_gradient_adam(
                    np.array([obj_delta, obj_beta]),
                    grads,
                    i_batch,
                    m,
                    v,
                    step_size=learning_rate)

                # finite support
                obj_delta = obj_delta * mask
                obj_beta = obj_beta * mask
                obj_delta = np.clip(obj_delta, 0, None)
                obj_beta = np.clip(obj_beta, 0, None)

                # shrink wrap

                if shrink_cycle is not None:
                    if i_batch % shrink_cycle == 0 and i_batch > 0:
                        boolean = obj_delta > 1e-12
                        boolean = boolean.astype('float')
                        if not shared_file_object:
                            mask = mask * boolean.astype('float')
                        if shared_file_object:
                            write_subblocks_to_file(dset_mask,
                                                    this_pos_batch,
                                                    boolean,
                                                    None,
                                                    coord_ls[this_i_theta],
                                                    probe_size_half,
                                                    mask=True)

                if shared_file_object:
                    obj = obj[:,
                              safe_zone_width:obj.shape[1] - safe_zone_width,
                              safe_zone_width:obj.shape[2] -
                              safe_zone_width, :, :]
                    obj_delta = obj_delta - obj[:, :, :, :, 0]
                    obj_beta = obj_beta - obj[:, :, :, :, 1]
                    obj_delta = obj_delta / n_ranks
                    obj_beta = obj_beta / n_ranks
                    write_subblocks_to_file(dset, this_pos_batch, obj_delta,
                                            obj_beta, coord_ls[this_i_theta],
                                            probe_size_half)
                    m = m - m_0
                    m /= n_ranks
                    write_subblocks_to_file(dset_m, this_pos_batch, m[0], m[1],
                                            coord_ls[this_i_theta],
                                            probe_size_half)
                    v = v - v_0
                    v /= n_ranks
                    write_subblocks_to_file(dset_v, this_pos_batch, v[0], v[1],
                                            coord_ls[this_i_theta],
                                            probe_size_half)

                if rank == 0:
                    if shared_file_object:
                        # dxchange.write_tiff(dset[:, :, :, 0],
                        #                     fname=os.path.join(output_folder, 'intermediate', 'current'.format(ds_level)),
                        #                     dtype='float32', overwrite=True)
                        dxchange.write_tiff(
                            dset[:, :, :, 0],
                            fname=os.path.join(
                                output_folder,
                                'current/delta_{}'.format(i_batch)),
                            dtype='float32',
                            overwrite=True)
                    else:
                        dxchange.write_tiff(obj_delta,
                                            fname=os.path.join(
                                                output_folder, 'intermediate',
                                                'current'.format(ds_level)),
                                            dtype='float32',
                                            overwrite=True)

                print_flush('Minibatch done in {} s (rank {})'.format(
                    time.time() - t00, rank))

                f_conv.write('{}\n'.format(time.time() - t_zero))
                f_conv.flush()

            if n_epochs == 'auto':
                pass
            else:
                if i_epoch == n_epochs - 1: cont = False
            i_epoch = i_epoch + 1

            # print_flush(
            #     'Epoch {} (rank {}); loss = {}; Delta-t = {} s; current time = {}.'.format(i_epoch, rank,
            #                                                         calculate_loss(obj_delta, obj_beta, this_ind_batch,
            #                                                                        this_prj_batch),
            #                                                         time.time() - t0, time.time() - t_zero))
            if rank == 0:
                if shared_file_object:
                    dxchange.write_tiff(dset[:, :, :, 0],
                                        fname=os.path.join(
                                            output_folder,
                                            'delta_ds_{}'.format(ds_level)),
                                        dtype='float32',
                                        overwrite=True)
                    dxchange.write_tiff(dset[:, :, :, 1],
                                        fname=os.path.join(
                                            output_folder,
                                            'beta_ds_{}'.format(ds_level)),
                                        dtype='float32',
                                        overwrite=True)
                else:
                    dxchange.write_tiff(obj_delta,
                                        fname=os.path.join(
                                            output_folder,
                                            'delta_ds_{}'.format(ds_level)),
                                        dtype='float32',
                                        overwrite=True)
                    dxchange.write_tiff(obj_beta,
                                        fname=os.path.join(
                                            output_folder,
                                            'beta_ds_{}'.format(ds_level)),
                                        dtype='float32',
                                        overwrite=True)

        print_flush('Current iteration finished.', 0, rank)
def visualize3d(func,pt_history,eval_history,**kwargs):
    ### input arguments ###        
    wmax = 1
    if 'wmax' in kwargs:
        wmax = kwargs['wmax'] + 0.5
        
    view = [20,-50]
    if 'view' in kwargs:
        view = kwargs['view']
        
    axes = False
    if 'axes' in kwargs:
        axes = kwargs['axes']
       
    plot_final = False
    if 'plot_final' in kwargs:
        plot_final = kwargs['plot_final']
      
    num_contours = 10
    if 'num_contours' in kwargs:
        num_contours = kwargs['num_contours']
        
    pt = [0,0]
    if 'pt' in kwargs:
        pt = kwargs['pt']
    pt = np.asarray(pt)
    pt.shape = (2,1)
     
    max_steps = 10
    if 'max_steps' in kwargs:
        max_steps = kwargs['max_steps']
    num_samples = 10
    if 'num_samples' in kwargs:
        num_samples = kwargs['num_samples'] 
    steplength = 1
    if 'steplength' in kwargs:
        steplength = kwargs['steplength']     
        
    ##### construct figure with panels #####
    # construct figure
    fig = plt.figure(figsize = (9,3))
          
    # remove whitespace from figure
    fig.subplots_adjust(left=0, right=1, bottom=0, top=1) # remove whitespace
        
    # create subplot with 3 panels, plot input function in center plot
    gs = gridspec.GridSpec(1, 2, width_ratios=[1,2]) 
    ax = plt.subplot(gs[0],projection='3d'); 
    ax2 = plt.subplot(gs[1],aspect='equal'); 
    
    #### define input space for function and evaluate ####
    w = np.linspace(-wmax,wmax,200)
    w1_vals, w2_vals = np.meshgrid(w,w)
    w1_vals.shape = (len(w)**2,1)
    w2_vals.shape = (len(w)**2,1)
    h = np.concatenate((w1_vals,w2_vals),axis=1)
    func_vals = np.asarray([func(s) for s in h])
    w1_vals.shape = (len(w),len(w))
    w2_vals.shape = (len(w),len(w))
    func_vals.shape = (len(w),len(w))
    
    # plot function 
    ax.plot_surface(w1_vals, w2_vals, func_vals, alpha = 0.1,color = 'w',rstride=25, cstride=25,linewidth=1,edgecolor = 'k',zorder = 2)

    # plot z=0 plane 
    ax.plot_surface(w1_vals, w2_vals, func_vals*0, alpha = 0.1,color = 'w',zorder = 1,rstride=25, cstride=25,linewidth=0.3,edgecolor = 'k') 
    
    ### make contour right plot - as well as horizontal and vertical axes ###
    ax2.contour(w1_vals, w2_vals, func_vals,num_contours,colors = 'k')
    if axes == True:
        ax2.axhline(linestyle = '--', color = 'k',linewidth = 1)
        ax2.axvline(linestyle = '--', color = 'k',linewidth = 1)
    
    ### plot circle on which point lies, as well as step length circle - used only for simple quadratic
    if plot_final == True:
        # plot contour of quadratic on which final point was plotted
        f = pt_history[-1]
        val = np.linalg.norm(f)
        theta = np.linspace(0,1,400)
        x = val*np.cos(2*np.pi*theta) 
        y = val*np.sin(2*np.pi*theta) 
        ax2.plot(x,y,color = 'r',linestyle = '--',linewidth = 1)

        # plot direction sampling circle centered at final point
        x = steplength*np.cos(2*np.pi*theta) + f[0]
        y = steplength*np.sin(2*np.pi*theta) + f[1]
        ax2.plot(x,y,color = 'b',linewidth = 1)    
    
    # colors for points
    s = np.linspace(0,1,len(eval_history[:round(len(eval_history)/2)]))
    s.shape = (len(s),1)
    t = np.ones(len(eval_history[round(len(eval_history)/2):]))
    t.shape = (len(t),1)
    s = np.vstack((s,t))
    colorspec = []
    colorspec = np.concatenate((s,np.flipud(s)),1)
    colorspec = np.concatenate((colorspec,np.zeros((len(s),1))),1)
    
    #### scatter path points ####
    for k in range(len(eval_history)):
        ax.scatter(pt_history[k][0],pt_history[k][1],0,s = 60,c = colorspec[k],edgecolor = 'k',linewidth = 0.5*math.sqrt((1/(float(k) + 1))),zorder = 3)
        
        ax2.scatter(pt_history[k][0],pt_history[k][1],s = 60,c = colorspec[k],edgecolor = 'k',linewidth = 1.5*math.sqrt((1/(float(k) + 1))),zorder = 3)

    #### connect points with arrows ####
    for i in range(len(eval_history)-1):
        pt1 = pt_history[i]
        pt2 = pt_history[i+1]

        if np.linalg.norm(pt1 - pt2) > 0.5:
            # draw arrow in left plot
            a = Arrow3D([pt1[0],pt2[0]], [pt1[1],pt2[1]], [0, 0], mutation_scale=10, lw=2, arrowstyle="-|>", color="k")
            ax.add_artist(a)

            # draw 2d arrow in right plot
            ax2.arrow(pt1[0],pt1[1],(pt2[0] - pt1[0])*0.78,(pt2[1] - pt1[1])*0.78, head_width=0.1, head_length=0.1, fc='k', ec='k',linewidth=3,zorder = 2,length_includes_head=True)

    ### cleanup panels ###
    ax.set_xlabel('$w_0$',fontsize = 12)
    ax.set_ylabel('$w_1$',fontsize = 12,rotation = 0)
    ax.set_title('$g(w_0,w_1)$',fontsize = 12)
    ax.view_init(view[0],view[1])
    
    ax2.set_xlabel('$w_0$',fontsize = 12)
    ax2.set_ylabel('$w_1$',fontsize = 12,rotation = 0)
    
    # clean up axis
    ax.xaxis.pane.fill = False
    ax.yaxis.pane.fill = False
    ax.zaxis.pane.fill = False

    ax.xaxis.pane.set_edgecolor('white')
    ax.yaxis.pane.set_edgecolor('white')
    ax.zaxis.pane.set_edgecolor('white')
    
    ax.xaxis._axinfo["grid"]['color'] =  (1,1,1,0)
    ax.yaxis._axinfo["grid"]['color'] =  (1,1,1,0)
    ax.zaxis._axinfo["grid"]['color'] =  (1,1,1,0)
    
    # plot
    plt.show()
コード例 #40
0
#create true values - assign to grid
#x_true = np.abs(np.random.rand(Ndata)) # location of sources
#y_true = np.abs(np.random.rand(Ndata));
x_true = [0.5]
y_true = [0.5]
w_true = np.ones(Ndata) * 5
print(x_true, y_true, w_true)

#true grid needs to be set up with noise
w_true_grid = np.zeros((n_grid, n_grid))
for x, y, w in zip(x_true, y_true, w_true):
    w_true_grid[(np.abs(grid1d - x)).argmin(),
                (np.abs(grid1d - y)).argmin()] = w

mid = int(n_grid / 2)
x, y = np.meshgrid(grid1d, grid1d)
psf = np.exp(-((y - grid1d[mid])**2 + (x - grid1d[mid])**2) / 2. / sig_psf**2)

xx = np.linspace(-0.5, 0.5, n_grid)
bump = np.exp(-0.5 * xx**2 / sig_psf**2)
#bump /= np.trapz(bump) # normalize the integral to 1
_psf = bump[:, np.newaxis] * bump[np.newaxis, :]

fig, ax = plt.subplots(1, 2)
ax[0].imshow(psf)
ax[0].set_title('psf')
ax[1].imshow(_psf)
ax[1].set_title('psf')
plt.savefig('test0.png')

data = np.real(fft.ifft2(fft.fft2(w_true_grid) * fft.fft2(_psf)))
コード例 #41
0
ファイル: celeste.py プロジェクト: HIPS/DESI-MCMC
def gen_point_source_psf_image(
        u,                         # source location in equatorial coordinates
        image,                     # FitsImage object
        xlim          = None,      # compute model image only on patch defined
        ylim          = None,      #   by xlim ylimcompute only for this patch
        check_overlap = True,      # speedup to check overlap before computing
        return_patch  = True,      # return the small patch as opposed to large patch (memory/speed purposes)
        psf_grid      = None,      # cached PSF grid to be filled out
        pixel_grid    = None       # Nx2 matrix of discrete pixel values to evaluate mog at
        ):
    """ generates a PSF image (assigns density values to pixels) """
    # compute pixel space location of source
    # returns the X,Y = Width, Height pixel coordinate corresponding to u

    # compute pixel space location, v_{n,s}
    v_s = image.equa2pixel(u)
    does_not_overlap = check_overlap and \
                       (v_s[0] < -50 or v_s[0] > 2*image.nelec.shape[0] or
                       v_s[1] < -50 or v_s[0] > 2*image.nelec.shape[1])
    if does_not_overlap:
        return None, None, None

    # create sub-image - make sure it doesn't go outside of field pixels
    if xlim is None and ylim is None:
        bound = image.R
        minx_b, maxx_b = max(0, int(v_s[0] - bound)), min(int(v_s[0] + bound + 1), image.nelec.shape[1])
        miny_b, maxy_b = max(0, int(v_s[1] - bound)), min(int(v_s[1] + bound + 1), image.nelec.shape[0])
        y_grid = np.arange(miny_b, maxy_b, dtype=np.float)
        x_grid = np.arange(minx_b, maxx_b, dtype=np.float)
        xx, yy = np.meshgrid(x_grid, y_grid, indexing='xy')
        pixel_grid = np.column_stack((xx.ravel(order='C'), yy.ravel(order='C')))
    else:
        miny_b, maxy_b = ylim
        minx_b, maxx_b = xlim
        if pixel_grid is None:
            y_grid = np.arange(miny_b, maxy_b, dtype=np.float)
            x_grid = np.arange(minx_b, maxx_b, dtype=np.float)
            xx, yy = np.meshgrid(x_grid, y_grid, indexing='xy')
            pixel_grid = np.column_stack((xx.ravel(order='C'), yy.ravel(order='C')))
    grid_shape = (maxy_b-miny_b, maxx_b-minx_b)
    #psf_grid_small = gmm_like_2d(x       = pixel_grid,
    #                             ws      = image.weights,
    ##                             mus     = image.means + v_s,
    #                             sigs    = image.covars)
    psf_grid_small = np.exp(mog_funs.mog_loglike(pixel_grid,
                                            means = image.means+v_s,
                                            icovs = image.invcovars,
                                            dets  = np.exp(image.logdets),
                                            pis   = image.weights))

    # return the small patch and it's bounding box in the bigger fits_image
    if return_patch:
        return psf_grid_small.reshape(grid_shape, order='C'), \
               (miny_b, maxy_b), (minx_b, maxx_b)

    # instantiate a PSF grid 
    if psf_grid is None:
        psf_grid = np.zeros(image.nelec.shape, dtype=np.float)

    # create full field grid
    psf_grid[miny_b:maxy_b, minx_b:maxx_b] = \
        psf_grid_small.reshape(xx.shape, order='C')
    return psf_grid, (0, psf_grid.shape[0]), (0, psf_grid.shape[1])
コード例 #42
0
    def surface_plot(self, g, ax, wmax, view):
        ##### Produce cost function surface #####
        r = np.linspace(-wmax, wmax, 300)

        # create grid from plotting range
        w1_vals, w2_vals = np.meshgrid(r, r)
        w1_vals.shape = (len(r)**2, 1)
        w2_vals.shape = (len(r)**2, 1)
        w_ = np.concatenate((w1_vals, w2_vals), axis=1)
        g_vals = []
        for i in range(len(r)**2):
            g_vals.append(g(w_[i, :]))
        g_vals = np.asarray(g_vals)

        w1_vals.shape = (np.size(r), np.size(r))
        w2_vals.shape = (np.size(r), np.size(r))

        ### is this a counting cost?  if so re-calculate ###
        levels = np.unique(g_vals)
        if np.size(levels) < 30:
            # plot each level of the counting cost
            levels = np.unique(g_vals)
            for u in levels:
                # make copy of cost and nan out all non level entries
                z = g_vals.copy()
                ind = np.argwhere(z != u)
                ind = [v[0] for v in ind]
                z[ind] = np.nan

                # plot the current level
                z.shape = (len(r), len(r))
                ax.plot_surface(w1_vals,
                                w2_vals,
                                z,
                                alpha=0.4,
                                color='#696969',
                                zorder=0,
                                shade=True,
                                linewidth=0)

        else:  # smooth cost function, plot usual
            # reshape and plot the surface, as well as where the zero-plane is
            g_vals.shape = (np.size(r), np.size(r))

            # plot cost surface
            ax.plot_surface(w1_vals,
                            w2_vals,
                            g_vals,
                            alpha=0.1,
                            color='w',
                            rstride=25,
                            cstride=25,
                            linewidth=1,
                            edgecolor='k',
                            zorder=2)

        ### clean up panel ###
        ax.xaxis.pane.fill = False
        ax.yaxis.pane.fill = False
        ax.zaxis.pane.fill = False

        ax.xaxis.pane.set_edgecolor('white')
        ax.yaxis.pane.set_edgecolor('white')
        ax.zaxis.pane.set_edgecolor('white')

        ax.xaxis._axinfo["grid"]['color'] = (1, 1, 1, 0)
        ax.yaxis._axinfo["grid"]['color'] = (1, 1, 1, 0)
        ax.zaxis._axinfo["grid"]['color'] = (1, 1, 1, 0)

        ax.set_xlabel(r'$w_0$', fontsize=12)
        ax.set_ylabel(r'$w_1$', fontsize=12, rotation=0)

        ax.view_init(view[0], view[1])
コード例 #43
0
# GD ALPHA
GD_alpha = 0.0001

# CGD ALPHA
CGD_alpha = 0.0001

# BFGS alpha - set to (1,1,max_iter) to force step size 1
#BFGS_alpha = alpha = np.linspace(0.1, 1.0, max_iter)
BFGS_alpha = alpha = np.linspace(1, 1, max_iter)
##############################################


# Design variables at mesh points
i1 = np.arange(xmin, xmax, step)
i2 = np.arange(ymin, ymax, step)
x1_mesh, x2_mesh = np.meshgrid(i1, i2)

# Create a contour plot

fig, ax = plt.subplots()

plt.ylim(ymin, ymax)
plt.xlim(xmin, xmax)
v_func = np.vectorize(f_sep)
ax.contour(x1_mesh, x2_mesh, v_func(x1_mesh, x2_mesh))

# Add some text to the plot
ax.set_title(title)
ax.set_xlabel('x1')
ax.set_ylabel('x2')
コード例 #44
0

if __name__ == '__main__':

    m = 4
    n = 3

    initial_path = '../initial_airfoil/naca0012.dat'
    airfoil0_true = np.loadtxt(initial_path, skiprows=1)

    x_min = np.min(airfoil0_true[:, 0])
    x_max = np.max(airfoil0_true[:, 0])
    z_min = np.min(airfoil0_true[:, 1])
    z_max = np.max(airfoil0_true[:, 1])
    Px = np.linspace(x_min, x_max, m, endpoint=True)
    Py = np.linspace(z_min, z_max, n, endpoint=True)
    x, y = np.meshgrid(Px, Py)
    P0 = np.stack((x, y), axis=-1)
    Px = P0[:, :, 0]
    alpha0 = P0[:, :, 1].flatten()

    airfoil0 = synthesize(alpha0, airfoil0_true, m, n, Px)

    import matplotlib.pyplot as plt
    plt.figure()
    plt.plot(airfoil0[:, 0], airfoil0[:, 1], 'o-')
    plt.plot(airfoil0_true[:, 0], airfoil0_true[:, 1], 'r-')
    plt.plot(P0[:, :, 0].flatten(), P0[:, :, 1].flatten(), 'rs')
    plt.axis('equal')
    plt.show()