示例#1
0
 def normalize_projection(self, choreography, state_to_node):
     removeIndirections(choreography, state_to_node)
     log.debug("-- after remove indirections --\n%s", choreography)
     removeForkJoin(self, choreography, state_to_node)
     log.debug("-- after remove fork/join --\n%s", choreography)
     minimize(choreography, state_to_node)
     log.debug("-- after minimize --\n%s", choreography)
     self.checkProjection(choreography, state_to_node)
示例#2
0
def check(B0, f, X, Y, eta, h, precision):
    print "starting at", B0
    (m, steps, trace) = minimize(f, B0, eta, h, precision)
    print "gradient descent gives ", m, "Cost", Cost(m)

    fit = np.polyfit(X, Y, 1)
    print "exact is               ", [fit[1], fit[0]]
    assert int(m[0] * 1000) == int(fit[1] * 1000)
def check(B0, f, X, Y, eta, h, precision):
    print "starting at", B0
    (m, steps, trace) = minimize(f, B0, eta, h, precision)
    print "gradient descent gives ", m, "Cost", Cost(m)

    fit = np.polyfit(X, Y, 1)
    print "exact is               ", [fit[1], fit[0]]
    assert int(m[0]*1000) == int(fit[1]*1000)
示例#4
0
def build(config_filename, output_filename, compress = True, **kwargs):
    config = mergejs.Config()
    config.read(config_filename)
    merged = mergejs.merge(kwargs.get('source_directory', source_directory), config)
    if compress:
        try:
            output = minimize(merged)
        except Exception, e:
            print "Neither mergejs nor minimize was found for compression.\
HOURLY_WAGE = [
    2.98, 3.09, 3.23, 3.33, 3.46, 3.6, 3.73, 2.91, 4.25, 4.47, 5.04, 5.47, 5.76
]
# Number of homicides per 100,000 people
MURDERS = [
    8.6, 8.9, 8.52, 8.89, 13.07, 14.57, 21.36, 28.03, 31.49, 37.39, 46.26,
    47.24, 52.33
]


def Cost(B, X=HOURLY_WAGE, Y=MURDERS):
    cost = 0.0
    for i in xrange(0, len(X)):
        ...
    return cost


def heatmap(f, trace=None):  # trace is a list of [b1, b2] pairs
    ...


random.seed(int(round(time.time() * 1000)))

begin = time.time()
(m, steps, trace) = minimize(Cost, B0, LEARNING_RATE, h, PRECISION)
end = time.time()

heatmap(Cost, trace)

show()
示例#6
0
                                params={
                                    'add': [
                                        np.round(np.linspace(-2, 0, 11),
                                                 decimals=6).tolist(),
                                        np.round(np.linspace(0, 60, 11),
                                                 decimals=6).tolist()
                                    ]
                                })

#'source':'/media/ivan/64E21891E2186A16/Users/vano2/Documents/LC_SK/new_spx/alt_bulk_from_rows/alt_20_bulk_19/1/best'}
#					   'double':False

for idx, Kv in enumerate(Klist, start=1):
    system, s, energy = minimize(ini=ini,
                                 J=J,
                                 D=D,
                                 Kbulk=np.power(D, 2) * Kv[0],
                                 Ksurf=np.power(D, 2) * Kv[1],
                                 precision=1e-5)
    container = magnes.io.container(
        str(
            directory.joinpath(state_name +
                               '_{:.5f}_{:.5f}.npz'.format(Kv[0], Kv[1]))))
    container.store_system(system)
    container['PATH'] = np.array([s])
    container['ENERGY'] = energy
    container.save(
        str(
            directory.joinpath(state_name +
                               '_{:.5f}_{:.5f}.npz'.format(Kv[0], Kv[1]))))

map_info.map_info(directory)
示例#7
0
def omgp(covfunc, M, X, Y, Xs):
    """
    One possible way to initialize and optimize hyperparameters:

    Uses omgpEinc to perform the update of qZ and omgpbound to update
    hyperparameters.
    """
    if (np.argsort(X).sum() != 0) or X.shape[1] != 1:
        warnings.warn(
            'Your *inputs* are multidimensional or not sorted. Optimization may have trouble converging! (Multidimensional outputs are OK).'
        )

    # Initialization
    [N, oD] = Y.shape
    maxiter = 100

    #Assume zero-mean functions, substract mean
    meany = np.mean(Y, axis=0)
    stdy = np.std(
        Y, axis=0, ddof=1
    )  # Setting the ddof (degrees of freedom) param  to 1 to get the same value as Matlab's

    Y = np.divide(Y - np.matmul(np.ones((N, 1)), np.matrix((meany))),
                  np.matmul(np.ones((N, 1)), np.matrix((stdy))) + 1e-6)

    # Independent or shared hyperparameters
    if len(covfunc) == M:
        print('Using a different set of hyperparameters for each component')
    else:
        print('Using shared hyperparameters for all components')

    # Initial hyperparameters setting
    lengthscale = np.log(
        np.mean((X.max() - X.min()).conj().transpose() / 2 / 5))
    lengthscale = np.maximum(lengthscale,
                             -100)  # minimum value of lengthscale is -100
    covpower = 0.5 * np.log(1)
    noisepower = 0.5 * np.log(1 / 8) * np.ones((M, 1))

    loghyper = np.array(())
    if covfunc.shape != (1, 1):
        covfunc_array = np.squeeze(np.asarray(covfunc))
    else:
        covfunc_array = covfunc
    for function in covfunc_array:
        if function == 'covNoise':
            loghyper = loghyper + covpower
        elif function == 'covSEiso':
            loghyper = np.append(loghyper, lengthscale)
            loghyper = np.append(loghyper, covpower)
        else:
            raise Warning('Covariance type not (yet) supported')
    # Add responsibilities
    qZ = np.random.rand(N, M) + 10

    qZ = np.divide(qZ, npm.repmat(qZ.sum(axis=1), M, 1).conj().transpose())
    logqZ = np.log(qZ)

    logqZ = logqZ - np.matmul(
        np.matrix((logqZ[:, 0])).conj().transpose(), np.ones((1, M)))
    logqZ = logqZ[:, 1:]

    loghyper = np.append(loghyper, np.zeros((M - 1, 1)))
    loghyper = np.append(loghyper, noisepower)
    loghyper = np.append(loghyper, logqZ.flatten('F').conj().transpose())

    # Iterate EM updates
    F_old = np.inf

    convergence = []
    for iter_variable in range(maxiter):
        [loghyper, conv1] = omgpEinc(loghyper, covfunc, M, X, Y)
        print('\nBound after E-step is %.4f' % (conv1[-1]))
        [loghyper, conv2] = minimize(loghyper, 'omgpbound', 10, 'learnhyp',
                                     covfunc, M, X, Y)

        convergence = np.concatenate((conv1, conv2))
        F = convergence[-1]
        if np.abs(F - F_old) < np.abs(F_old) * (1e-6):
            break

        F_old = F
    if iter_variable is maxiter:
        print('Maximum number of iterations exceeded')
    print('\n')

    # Final pass, also updating pi0
    [loghyper, conv] = minimize(loghyper, 'omgpbound', 20, 'learnall', covfunc,
                                M, X, Y)

    print('\n\n')

    F = conv[-1]

    loghyperinit = np.concatenate((loghyper[:-N * (M - 1) - 2 * M + 1],
                                   loghyper[-N * (M - 1) - M:-N * (M - 1)]))

    # Compute qZ and pi0
    logqZ = np.concatenate((np.zeros(
        (N, 1)), np.reshape(loghyper[-N * (M - 1):], (N, M - 1), order='F')),
                           axis=1)
    qZ = np.exp(logqZ - np.matrix((logqZ.max(axis=1))).conj().transpose() *
                np.ones((1, M)))
    qZ = np.divide(qZ, (qZ.sum(axis=1) * np.ones((1, M))))

    logpZ = np.concatenate(
        ([0], loghyper[-N * (M - 1) - 2 * M + 1:-N * (M - 1) - M]))
    logpZ = logpZ - logpZ.max()
    logpZ = logpZ - np.log(np.exp(logpZ).sum())

    pi0 = np.exp(logpZ)

    Ntst = Xs.shape[0]
    [mu, C] = omgpboundB(loghyper, 'learnall', covfunc, M, X, Y, Xs)

    mu1 = np.ones((Ntst, oD, M))
    mu2 = np.ones((Ntst, oD, M))
    C1 = np.ones((Ntst, oD, M))
    for i in range(M):
        mu1[:, :, i] = np.multiply(mu1[:, :, i],
                                   np.kron(np.ones((Ntst, 1)), meany))
        mu2[:, :, i] = np.multiply(mu2[:, :, i],
                                   np.kron(np.ones((Ntst, 1)), (stdy + 1e-6)))
        C1[:, :, i] = np.multiply(
            C1[:, :, i], np.kron(np.ones((Ntst, 1)), np.power((stdy + 1e-6),
                                                              2)))

    mu = mu1 + np.multiply(mu, mu2)
    C = np.multiply(C, C1)

    return [F, qZ, loghyperinit, mu, C, pi0]
        )

LEARNING_RATE = [80, 40]
h = 0.00001
PRECISION = 0.00000000000001
f, ax = plt.subplots(2, sharex=True)

for k in range(2): # We need to generate 2 plot

    B0 = [0, 0]
    random.seed(int(round(time.time() * 1000)))
    B0[0] = random.randrange(-50, -20)
    B0[1] = random.randrange(10, 20)

    begin = time.time()
    (m, steps, trace) = minimize(Cost, B0, LEARNING_RATE, h, PRECISION)
    end = time.time()

    heatmap(HOURLY_WAGE, MURDERS, Cost, k, trace)
    ax[k].set_title("Trace in heat map %i" %(k+1))
    ax[k].text(-59.5, 21.5, "Steps: %i" %steps)
    ax[k].text(-59.5, 8, "Start B0: %i Start B1: %i" %(B0[0], B0[1]))
    for i in range(len(trace)):
        if i == 0:
            ax[k].plot(trace[i][0], trace[i][1], "ko", markersize=6, color='red')
            ax[k].text(trace[i][0] - 1.3, trace[i][1] + 0.7, "Start")
        elif i == len(trace) - 1:
            ax[k].plot(trace[i][0], trace[i][1], "ko", markersize=6, color='red')
            ax[k].text(trace[i][0] - 1, trace[i][1] + 0.7, "Stop")
        else:
            ax[k].plot(trace[i][0], trace[i][1], "ko", markersize=2)