Пример #1
0
def plot_topo(fnm, fnm2=None, show=True, savefig=False):

    params, par_names = get_params()

    data = np.loadtxt(fnm)
    print np.shape(data)
    if fnm2 is not None:
        data2 = np.loadtxt(fnm2)
        print np.shape(data2)

    mpl.rcParams['figure.figsize'] = 15, 12
    mpl.rcParams['font.size'] = 10
    n = len(par_names)
    f, axarr = plt.subplots(n, n)
    fitness = data[:, 0]
    print np.min(fitness), np.max(fitness)
    fitness = np.log(fitness[~np.isnan(fitness)])
    print np.shape(fitness)
    print np.min(fitness), np.max(fitness)
    j = 0
    for v in par_names:
        i = 0
        for p in par_names:
            if (j < i):
                px = data[:, i + 1]
                py = data[:, j + 1]
                px = px[~np.isnan(fitness)]
                py = py[~np.isnan(fitness)]
                xi = np.linspace(min(px), max(px), 100)
                yi = np.linspace(min(py), max(py), 100)
                zi = griddata(px, py, fitness, xi, yi)
                p = axarr[i, j].pcolormesh(xi, yi, zi, cmap='Paired')
                if fnm2 is not None:
                    px2 = data2[:, i + 2]
                    py2 = data2[:, j + 2]
                    axarr[i, j].plot(px2, py2, 'ko-', alpha=0.5, mew=0.1)
                    axarr[i, j].plot(px2[-1], py2[-1], 'rs', markersize=7)
                axarr[i, j].set_xlabel(par_names[i])
                axarr[i, j].set_ylabel(par_names[j])
                axarr[i, j].set_xlim(float(params[i][1]), float(params[i][2]))
                axarr[i, j].set_ylim(float(params[j][1]), float(params[j][2]))
                if i == 1:
                    divider = make_axes_locatable(axarr[0, 1])
                    cax = divider.append_axes("left", size="10%", pad=0.1)
                    cb = plt.colorbar(p, cax=cax)
                    cb.set_label("Fitness", fontsize=12)
            else:
                f.delaxes(axarr[i, j])
            plt.setp(axarr[i, j].get_xticklabels(), rotation=30)
            i += 1
        j += 1
    f.set_tight_layout(True)
    if show: plt.show()
    if savefig:
        f.savefig('parameter_matrix.pdf',
                  type='PDF',
                  bbox_inches='tight',
                  pad_inches=0.0)
Пример #2
0
def plot_topo(fnm,fnm2=None,show=True,savefig=False):
    
    params, par_names = get_params()

    data = np.loadtxt(fnm)
    print np.shape(data)
    if fnm2 is not None:
        data2 = np.loadtxt(fnm2)
        print np.shape(data2)
    
    mpl.rcParams['figure.figsize'] = 15,12
    mpl.rcParams['font.size'] = 10
    n = len(par_names)
    f, axarr = plt.subplots(n,n)
    fitness = data[:,0]
    print np.min(fitness), np.max(fitness)
    fitness = np.log(fitness[~np.isnan(fitness)])
    print np.shape(fitness)
    print np.min(fitness), np.max(fitness)
    j = 0
    for v in par_names:
        i = 0
        for p in par_names:
            if (j < i):
                px = data[:,i+1]
                py = data[:,j+1]
                px = px[~np.isnan(fitness)]
                py = py[~np.isnan(fitness)]
                xi = np.linspace(min(px), max(px),100)
                yi = np.linspace(min(py), max(py),100)
                zi = griddata(px, py, fitness, xi, yi)
                p = axarr[i,j].pcolormesh(xi,yi,zi,cmap='Paired')
                if fnm2 is not None:
                    px2 = data2[:,i+2]
                    py2 = data2[:,j+2]
                    axarr[i,j].plot(px2,py2,'ko-',alpha=0.5,mew=0.1)
                    axarr[i,j].plot(px2[-1],py2[-1],'rs',markersize=7)
                axarr[i,j].set_xlabel(par_names[i])
                axarr[i,j].set_ylabel(par_names[j])
                axarr[i,j].set_xlim(float(params[i][1]),float(params[i][2]))
                axarr[i,j].set_ylim(float(params[j][1]),float(params[j][2]))
                if i == 1 :
                    divider = make_axes_locatable(axarr[0,1])
                    cax = divider.append_axes("left", size="10%", pad=0.1)
                    cb = plt.colorbar(p,cax=cax)
                    cb.set_label("Fitness",fontsize=12)
            else:
                f.delaxes(axarr[i,j])
            plt.setp(axarr[i,j].get_xticklabels(), rotation=30)
            i += 1
        j += 1
    f.set_tight_layout(True)
    if show: plt.show()
    if savefig: f.savefig('parameter_matrix.pdf',type='PDF',bbox_inches='tight', pad_inches = 0.0)
Пример #3
0
def plot_costs(fnm, show=True, savefig=False):

    params, par_names = get_params()
    print params

    data = np.loadtxt(fnm, unpack=True)

    mpl.rcParams['figure.figsize'] = 15, 12
    mpl.rcParams['font.size'] = 10
    n = len(par_names)
    f, axarr = plt.subplots(n, n)
    j = 0
    for v in par_names:
        i = 0
        for p in par_names:
            if (j < i):
                px = data[i + 2, :]
                py = data[j + 2, :]
                axarr[i, j].plot(px, py, 'ko-', alpha=0.1, mew=0)
                axarr[i, j].plot(px[-1],
                                 py[-1],
                                 'rs',
                                 alpha=0.5,
                                 markersize=7,
                                 mew=0)
                axarr[i, j].set_xlabel(par_names[i])
                axarr[i, j].set_ylabel(par_names[j])
                axarr[i, j].set_xlim(float(params[i][1]), float(params[i][2]))
                axarr[i, j].set_ylim(float(params[j][1]), float(params[j][2]))
            else:
                f.delaxes(axarr[i, j])
            plt.setp(axarr[i, j].get_xticklabels(), rotation=30)
            i += 1
        j += 1
    f.set_tight_layout(True)
    if show: plt.show()
    if savefig:
        f.savefig('parameter_matrix.pdf',
                  type='PDF',
                  bbox_inches='tight',
                  pad_inches=0.0)
Пример #4
0
    # mse is mean_squared_error, mpe is mean_absolute_percentage_error
    model.compile(loss='mean_squared_error', optimizer='adam', metrics=['mse','mape'])
    # Here we're starting our training
    history=model.fit(train_x, train_y, verbose=2, epochs=epochs, batch_size=batches)
    return model, name, mparams, history

if __name__ == '__main__':
    # Getting data formatted as a supervised problem
    years, past_values, values=get_data()
    X, Y = past_values, values
    # Split data into two parts: one for training, one for testing
    # Test part won't be seen by a model during training so it will
    # give us some idea how our model performs on a unseen data.
    train_x, train_y, test_x, test_y = train_test_split(X, Y)
    # Getting our command line parameters
    name, epochs, batches, plot=get_params()
    # Do the training
    model, name, mp, history=train_model(name, train_x, train_y, epochs, batches)
    # Save models and the training history for later use
    mname='models/model-%s-%d-%d' % (name, epochs, batches)
    model.save(mname+'.h5')
    with open(mname+'-history.pickle', 'wb') as ms:
        pickle.dump(history.history, ms)
    print()
    print('Model and its history saved in %s*' % mname)
    title='%s (epochs=%d, batch_size=%d)' % (name, epochs, batches)
    # Test our model on both data that has been seen
    # (training data set) and unseen (test data set)
    print('Scores for %s' % title)
    train_score = model.evaluate(train_x, train_y, verbose=0)
    trscore='RMSE: %s MAPE: %.0f%%' % ("{:,.0f}".format(math.sqrt(train_score[0])), train_score[2])
Пример #5
0
        logging.info("Epoch {} took {}".format(e, end - start_epoch))
        logging.info("Epoch:{} ValidAccPi    loss:{}".format(
            e, acc_ploss / it))
        logging.info("Epoch:{} ValidAccValue loss: {}".format(
            e, acc_vloss / it))
        logging.info("Epoch:{} ValidTotal    loss:{}".format(e, acc_loss / it))
        logging.info("Epoch:{} checkpoint".format(new_param_file))
        torch.save({
            'state_dict': model.state_dict(),
        }, new_param_file)

    # saving new parameters
    logging.info("Training took {}".format(end - start_train))
    logging.info("Saving model to {}".format(new_param_file))
    torch.save({
        'state_dict': model.state_dict(),
    }, new_param_file)
    logging.info("####################END#################")
    return True


if __name__ == "__main__":
    param_file = tools.get_params()
    new_param_file = tools.get_new_params()
    data_files = tools.get_data()
    if train(param_file, new_param_file, data_files):
        print("New model parameters were saved to {}".format(new_param_file))
    else:
        print("Training failed, check for errors {}/train_{}.log".format(
            LOG_PATH, new_param_file))
Пример #6
0
			recv_chips = np.zeros(2*RECV_CHIPS_I.shape[0], dtype=np.float)
			recv_chips[::2]  = RECV_CHIPS_I[:,phi_idx]
			recv_chips[1::2] = RECV_CHIPS_Q[:,phi_idx]

			# slice bits to simulate hard decision decoder
			if decision in ('hard',):
				recv_chips = np.sign(recv_chips)

			recv_syms = pt.detect_syms_corrcoef(recv_chips)[1:-1]
	        
			ser_s = np.append(ser_s, np.sum(recv_syms != send_syms_s) / (1.0*len(recv_syms)))
	    
		if SER_S is None:
			SER_S = ser_s
		else:
			SER_S = np.vstack((SER_S, ser_s))

		print 'PRR = ', np.mean((1-ser_s)**pktlen)

	np.save('data/ser_s_%s_%s_%s.npy'%(content, decision, mode), SER_S)

if __name__ == '__main__':
	input_params = (('content', ('same', 'unif'), 'Data content'), ('decision', ('hard', 'soft'), 'Bit decision'))
	content, decision = tools.get_params(input_params)

	if not tools.overwrite_ok('data/ser_s_%s_%s_n.npy'%(content, decision)):
		sys.exit()

	gen_n_interf(mode='n')
	gen_n_interf(mode='1')
Пример #7
0
import numpy as np
import matplotlib.pyplot as plt
from tools import get_params, get_filenames, sort_key

Tc = 2 / np.log(1 + np.sqrt(2))
lw = 3
c = ['indianred', 'orange', 'forestgreen', 'royalblue']

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

for filename in get_filenames('test_scalar'):

    b, L, K = get_params(filename)
    results = np.loadtxt(filename, unpack=True)

    temp = results[1]
    mse = results[6]

    i = int(np.log(L) / np.log(b) - 3)
    ax.semilogy(temp / Tc, mse, label="L=" + str(L), color=c[i], linewidth=lw)

# sort legend
handles, labels = ax.get_legend_handles_labels()
labels, handles = zip(*sorted(zip(labels, handles), key=sort_key))
ax.legend(handles, labels)

# fig params
plt.ylabel('Mean Squared Error')
plt.xlim(temp[0] / Tc, temp[-1] / Tc)
plt.xlabel(r'$T/T_c$')
plt.title('Testing MSE of Outputs From Large and Small Lattices')
Пример #8
0
            break

        mcts_second.simulate(sims_second)
        pi = mcts_second.get_prob()
        move = pi.argmax()
        mcts_first.make_move(move)
        mcts_second.make_move(move)

        if mcts_first.winner != -1:
            logging.info("Game ended in {} moves".format(i))
            break
    return


if __name__ == "__main__":
    param_best = tools.get_params()
    param_latest = tools.get_versus()
    if param_best == None:
        print("Parameters were not found")
        sys.exit(1)
    if param_latest == None:
        print("Parameters were not found")
        sys.exit(1)
    print(param_best, param_latest)
    if param_best == param_latest:
        print("Best and latest are the same")
        sys.exit(1)

    rc = True
    try:
        rc = eval_models(param_best, param_latest)
Пример #9
0
#import conf
conf = tf.compat.v1.ConfigProto()

from keras.models import Sequential, load_model
from keras.layers import Dense

from prep import get_data
from plot import plotpred
from tools import get_params
from train import confs
import numpy as np

import os

if __name__ == '__main__':
    name, epochs, batches, _ = get_params(script='predict.py')
    model, _ = confs[name]
    mname = 'models/model-%s-%d-%d.h5' % (name, epochs, batches)
    # Loading the model.
    if os.path.exists(mname):
        model = load_model(mname)
        print('Model loaded!')
    else:
        print("Can't find %s model, train it first using 'train.py %s %d %d'" %
              (mname, name, epochs, batches))
    years, _, values = get_data()
    # We're using the last value of our dataset
    # as a base for prediction.
    values, years = list(values), list(years)
    predict_on_value = values[-1:]
    predict_for_year = years[-1] + 'next'
Пример #10
0
    df = format_cols(df)

    # Round the ESS to the nearest 10
    df["n_eff"] = df["n_eff"].astype(int) // 100 * 100

    # Tidy up column and row names for tex
    df = rename_cols(df)
    df = rename_rows(df)

    df = df.rename_axis('{Parameter}', axis=1)

    # Column formatting. The S column type is provided by siunitx
    col_format = "@{}cS[table-format=2.2]S[table-format=2.2]S[table-format=2.2]r@{}"
    # Print dataframe to latex
    tex = df.to_latex(escape=False, column_format=col_format)

    save_path = path.join(proj_dir, 'out', '{}'.format(model_name))

    with open(save_path + '.tab', "w") as file:
        file.write(tex)


if __name__ == "__main__":

    model_name = input("Name of model to fit (neg_bin or norm): ")
    param_names, plot_labels = tools.get_params(model_name)

    model, fit = tools.load_model_fit(model_name, proj_dir)

    fit2latex(fit, model_name, param_names, proj_dir)
Пример #11
0
            del memory[-1]
            f.write("%i %.6g " % (iter, best["fitness"]))
            [f.write("%.6g "%p) for p in best["pos"]]
    	    f.write("\n")
            print " > iteration %i, fitness=%.4g" % (iter, best["fitness"])
            print [names[i] + ": %.6g " % p for i,p in enumerate(best["pos"])]
    except KeyboardInterrupt:
        print "\033[91mInterruption by user. Exiting...\033[0m"
    	pass
    f.close()
    return best

if __name__ == "__main__":
    

    params, names = get_params()

    #
    # Parsing command line arguments
    #

    search_space = [[float(x) for x in p[1:]] for p in params]

    def plot(args,fnm):
        if args.plot: pc.plot_costs(fnm) 
        if args.save and args.plot: pc.plot_costs(fnm,savefig=True) 
        if args.save and not args.plot: pc.plot_costs(fnm,show=False,savefig=True) 


    def run_es(args):
        max_gens = args.max_gens
Пример #12
0
        p["pos"] = sample[i]
    return population


def search(search_space, pop_size, prefix):
    f, tmpdir = init_search(prefix,names)
    #pop = init_population_random(search_space, pop_size)
    pop = init_population_lhs(search_space, pop_size)
    try:
        cost = run_particles(pop,tmpdir+'/'+prefix)
        for i,candidate in enumerate(pop):
            candidate["cost"] = cost[candidate["id"]]
            f.write("%.6g " % candidate["cost"])
            [f.write("%.6g "%p) for p in candidate["pos"]]
            f.write("\n")
            #print candidate["pos"]
    except KeyboardInterrupt:
        print "\033[91mInterruption by user. Exiting...\033[0m"
    	pass
    f.close()


if __name__ == "__main__":
    
    #Get information on parameters and ranges from user file
    params, names = get_params()
    
    search_space = [[float(x) for x in p[1:]] for p in params]

    search(search_space, 1000, 'topo_')
Пример #13
0
def main():
    global args
    args = get_args()

    print("DBG:Play: starting server")
    server = Server(args.ip, args.port)
    mcts = cmcts.mcts()
    param_file = tools.get_params()
    params = torch.load(param_file, map_location='cpu')
    model_class = getattr(model_module, MODEL_CLASS)
    model = model_class()
    model.load_state_dict(params['state_dict'])
    print(param_file)
    jit_model_name = "{}/tmp_{}.pt".format(
        os.path.dirname(os.path.realpath(__file__)),
        os.path.basename(param_file).replace(".pyt", ''))
    example = torch.rand(1, 2, SHAPE, SHAPE)
    model.eval()
    with torch.no_grad():
        traced_script_module = torch.jit.trace(model, example)

    traced_script_module.save(jit_model_name)
    mcts = cmcts.mcts(cpuct=CPUCT)
    mcts.set_alpha_default()
    mcts.set_threads(THREADS)
    mcts.set_params(jit_model_name)

    while True:
        print("DBG:Play: waiting for connection")
        server.connect()

        while True:
            cmd, move = server.get_cmd()

            if cmd == cmds.OP_MOVE:
                print("Opponents move")
                sys.stdout.flush()
                mcts.make_movexy(x=move[0], y=move[1])
            elif cmd == cmds.MAKE_MOVE:
                print("My move sims {}".format(SIMS))
                sys.stdout.flush()
                mcts.simulate(SIMS)
                print("get")
                pi = mcts.get_prob()
                mcts.print_node([])
                move = np.argmax(pi)
                mcts.make_move(move)
                print("made move")
                server.make_move((move % SHAPE, move // SHAPE))
            elif cmd == cmds.LOAD_MOVE:
                print("Loading move")
                sys.stdout.flush()
                raise NotImplementedError
                #game.set_player()
                #game.make_move(move)
                #game.set_player(1)
            elif cmd == cmds.INITIALIZE:
                print("Initializing")
                sys.stdout.flush()
                print(move)
                #assert move == SHAPE
                mcts.clear()
            elif cmd == cmds.END:
                print("End")
                sys.stdout.flush()
                break
            else:
                print('Error: unknow command: ' + cmd)
                sys.stdout.flush()
                raise NotImplementedError

    return