Beispiel #1
0
    def plotDebug(self, grid, alpha, candidates, gpi, gpj, ans):
        # -----------------------------------------------------------------
        # plot result
        fig = plt.figure()

        plotGrid2d(grid, alpha)

        for gp in list(candidates.values()):
            p = DataVector(gp.getDimension())
            gp.getStandardCoords(p)
            plt.plot(p[0], p[1], "x ", color="green")

        for gp in list(ans.values()):
            p = DataVector(gp.getDimension())
            gp.getStandardCoords(p)
            plt.plot(p[0], p[1], "o ", color="green")

        p = DataVector(grid.getStorage().getDimension())
        gpi.getStandardCoords(p)
        plt.plot(p[0], p[1], "^ ", color="orange")
        gpj.getStandardCoords(p)
        plt.plot(p[0], p[1], "^ ", color="orange")

        plt.xlim(0, 1)
        plt.ylim(0, 1)
        plt.title("# new = %i, overall = %i" % (len(candidates), len(ans)))

        fig.show()
        plt.show()
Beispiel #2
0
    def plotDebug(self, grid, alpha, candidates, localFullGrid, ans,
                  allCounts):
        currentCnt, allCnt = allCounts
        # -----------------------------------------------------------------
        # plot result
        fig = plt.figure()

        plotGrid2d(grid, alpha)

        for gp in list(candidates.values()):
            p = DataVector(gp.getDimension())
            gp.getStandardCoordinates(p)
            plt.plot(p[0], p[1], "o ", color="lightgreen")

        for gp in list(ans.values()):
            p = DataVector(gp.getDimension())
            gp.getStandardCoordinates(p)
            plt.plot(p[0], p[1], "o ", color="yellow")

        plt.xlim(0, 1)
        plt.ylim(0, 1)
        plt.title("%i/%i: # new = %i, overall = %i" %
                  (currentCnt, allCnt, len(candidates), len(ans)))

        fig.show()
        plt.show()
Beispiel #3
0
    def getLocalFullGridLevel(self, levels, indices, grid, gpk=None, gpl=None):
        localMaxLevels = np.zeros(self.numDims,
                                  dtype="int")  # + self.maxLevel - levels + 1
        if False and self.numDims == 2 and self.plot:
            levelouter, indexouter = self.findOuterIntersection(gpk, gpl)
            levelinner, indexinner = self.findInnerIntersection(gpk, gpl)
            fig = plt.figure()
            plotGrid2d(grid)
            if gpk is not None:
                plt.plot(gpk.getCoord(0), gpk.getCoord(1), "v", color="orange")
            if gpl is not None:
                plt.plot(gpl.getCoord(0), gpl.getCoord(1), "v", color="orange")
            plt.plot(2**-levelinner[0] * indexinner[0],
                     2**-levelinner[1] * indexinner[1],
                     "o ",
                     color="yellow")
            plt.plot(2**-levelouter[0] * indexouter[0],
                     2**-levelouter[1] * indexouter[1],
                     "o ",
                     color="yellow")
            plt.xlim(0, 1)
            plt.ylim(0, 1)
            fig.show()

        gpInnerIntersection = HashGridPoint(self.numDims)
        gpi = HashGridPoint(self.numDims)
        gpj = HashGridPoint(self.numDims)
        gs = grid.getStorage()

        for idim, jdim in combinations(list(range(self.numDims)), 2):
            # find neighbors in direction idim
            iright, ileft = getGridPointsOnBoundary(levels[idim],
                                                    indices[idim])
            # find neighbors in direction idim
            jright, jleft = getGridPointsOnBoundary(levels[jdim],
                                                    indices[jdim])

            for left, right in product((iright, ileft), (jright, jleft)):
                if left is not None and right is not None:
                    (llevel, lindex), (rlevel, rindex) = left, right
                    for i in range(self.numDims):
                        # compute intersection i
                        if i == idim:
                            gpi.set(i, int(llevel), int(lindex))
                        else:
                            gpi.set(i, levels[i], indices[i])

                        # compute intersection j
                        if i == jdim:
                            gpj.set(i, int(rlevel), int(rindex))
                        else:
                            gpj.set(i, levels[i], indices[i])

                    # compute inner intersection
                    levelInner, indexInner = self.findInnerIntersection(
                        gpi, gpj)
                    for i in range(self.numDims):
                        gpInnerIntersection.set(i, levelInner[i],
                                                indexInner[i])

                    if gs.isContaining(gpj):
                        localMaxLevels[idim] = max(
                            localMaxLevels[idim],
                            self.getMaxLevelOfChildrenUpToMaxLevel(
                                gpj, grid, idim))
                    if gs.isContaining(gpi):
                        localMaxLevels[jdim] = max(
                            localMaxLevels[jdim],
                            self.getMaxLevelOfChildrenUpToMaxLevel(
                                gpi, grid, jdim))
                    if gs.isContaining(gpInnerIntersection):
                        xdim = np.array([
                            i for i in range(self.numDims)
                            if i != idim and i != jdim
                        ],
                                        dtype="int")
                        for i in xdim:
                            localMaxLevels[i] = max(
                                localMaxLevels[i],
                                self.getMaxLevelOfChildrenUpToMaxLevel(
                                    gpInnerIntersection, grid, i))

                    # plot result
                    if False and self.plot:
                        levelouter, indexouter = self.findOuterIntersection(
                            gpi, gpj)
                        fig = plt.figure()
                        plotGrid2d(grid)

                        plt.plot(gpi.getCoord(0),
                                 gpi.getCoord(1),
                                 "v",
                                 color="orange")
                        plt.plot(gpj.getCoord(0),
                                 gpj.getCoord(1),
                                 "v",
                                 color="orange")
                        plt.plot(gpInnerIntersection.getCoord(0),
                                 gpInnerIntersection.getCoord(1),
                                 "v ",
                                 color="red")
                        plt.plot(2**-levelouter[0] * indexouter[0],
                                 2**-levelouter[1] * indexouter[1],
                                 "o ",
                                 color="yellow")
                        plt.xlim(0, 1)
                        plt.ylim(0, 1)
                        plt.title(localMaxLevels)
                        fig.show()

        if False and self.plot:
            plt.show()

        return localMaxLevels + 1
Beispiel #4
0
alpha = alpha.array()
sgdeDist = SGDEdist(grid,
                    alpha,
                    trainData=trainSamples,
                    bounds=dist.getBounds())
print(
    "l=%i: (gs=%i) -> %g (%g, %g)," %
    (level, sgdeDist.grid.getSize(), dist.klDivergence(sgdeDist, testSamples),
     sgdeDist.crossEntropy(testSamples), sgdeDist.vol))
print("-" * 80)

if numDims == 2 and plot:
    # plot the result
    fig = plt.figure()
    plotGrid2d(grid, alpha, show_numbers=False)
    #     plt.title("neg: #gp = %i, kldivergence = %g, log = %g" % (grid.getStorage().getSize(),
    #                                                               dist.klDivergence(sgdeDist, testSamples),
    #                                                               dist.crossEntropy(testSamples)))
    fig.show()

    fig, ax, _ = plotSG3d(grid, sgdeDist.alpha)
    ax.set_title("negative")
    fig.show()

C = 0
M = np.sum([1 for i in range(len(alpha)) if alpha[i] < 0])
for d in range(2, numDims + 1):
    C += binom(M, d)
print("predicted comparison costs = %i" % C)
print("full grid                  = %i" % ((2**level - 1)**numDims, ))