Beispiel #1
0
    def init_params(self, configtag):

        params = load_params('./config.yaml', configtag)
        logging.info(str(params))
        self.dataname = params['dataname']
        self.valname = params['valname']
        self.img_dim = params['img_dim']
        self.noise_vect_len = params['noise_vect_len']
        self.nlayers = params['nlayers']
        self.convkern = params['convkern']
        self.convstride = params['convstride']
        self.nconvfilters = params['nconvfilters']
        self.ndeconvfilters = params['ndeconvfilters']
        self.specnorm = params['specnorm']
        self.label_flip = params['label_flip']
        self.batchsize = params['batchsize']
        self.print_batch = params['print_batch']
        self.checkpt_batch = params['checkpt_batch']
        self.cscale = params['cscale']
        self.datascale = params['datascale']
        self.G_lr, self.D_lr = params['learn_rate']
        self.DG_update_ratio = params['DG_update_ratio']
        self.resume = params['resume']
        self.Nepochs = params['Nepochs']
        self.loss = params['loss']
        self.weight_hists = params['weight_hists']
        self.grad_hists = params['grad_hists']
        self.sigma_plot = params['sigma_plot']
        self.doubleconv = params['doubleconv']
        self.datafmt = params['datafmt']
        self.alpha = 0.2
        self.start = 0
        self.bestchi = np.inf
        if self.datafmt == 'channels_last':
            self.bn_axis = -1
            self.imshape = (self.img_dim, self.img_dim, 1)
        else:
            self.bn_axis = 1
            self.imshape = (1, self.img_dim, self.img_dim)
        self.real = 1
        if self.loss == 'hinge':  # hinge loss is untested
            self.fake = -1
        else:
            self.fake = 0
 def init_params(self, configtag):
     params = load_params('./config.yaml', configtag)
     self.dataname = params['dataname']
     self.expDir = params['expDir']
     self.img_dim = params['img_dim']
     self.noise_vect_len = params['noise_vect_len']
     self.nlayers = params['nlayers']
     self.convkern = params['convkern']
     self.convstride = params['convstride']
     self.nconvfilters = params['nconvfilters']
     self.ndeconvfilters = params['ndeconvfilters']
     self.label_flip = params['label_flip']
     self.batchsize = params['batchsize']
     self.print_batch = params['print_batch']
     self.print_epoch = params['print_epoch']
     self.histwindow = params['histwindow']
     self.cscale = params['cscale']
     self.alpha = 0.2
     self.start = 0
     self.datafmt = 'channels_first'
Beispiel #3
0
 def init_params(self,configtag):
     
     params = load_params('./config.yaml', configtag)
     logging.info(str(params))
     self.dataname = params['dataname']
     self.train_fname=params['train_file']
     self.val_fname=params['val_file']
     self.img_dim = params['img_dim']
     self.noise_vect_len = params['noise_vect_len']
     self.nlayers = params['nlayers']
     self.convkern = params['convkern']
     self.convstride = params['convstride']
     self.nconvfilters = params['nconvfilters']
     self.ndeconvfilters = params['ndeconvfilters']
     self.label_flip = params['label_flip']
     self.batchsize = params['batchsize'] 
     self.print_batch = params['print_batch']
     self.checkpt_batch = params['checkpt_batch']
     self.cscale = params['cscale']
     self.datascale = params['datascale']
     self.G_lr, self.D_lr = params['learn_rate']
     self.DG_update_ratio = params['DG_update_ratio']
     self.Nepochs = params['Nepochs']
     self.datafmt = params['datafmt']
     nchannels = 1
     self.multichannel = params['multichannel']
     if self.multichannel:
         self.linear_scaler = 1000.
         nchannels = 2
     self.alpha = 0.2
     self.start = 0
     self.bestchi = np.inf
     self.bestchi_pspect = np.inf
     if self.datafmt == 'channels_last':
         self.C_axis = -1
         self.imshape = (self.img_dim, self.img_dim, nchannels)
     else:
         self.C_axis = 1
         self.imshape = (nchannels, self.img_dim, self.img_dim)
Beispiel #4
0
def replace_with_train_params(params):
    """Replace some parameters with the training parameters.
    Returns the updated params.
    """
    params = params._replace(eval=True)
    train_params = parameters.load_params(params.train_dir)
    if not train_params:
        deploy.log_fn('WARNING: no training parameters file found.')
        return params

    p = params
    p = p._replace(eval_dir=os.path.join(params.train_dir, 'eval'))
    p = p._replace(eval_interval_secs=train_params.save_model_secs)

    p = p._replace(model=train_params.model)
    p = p._replace(data_dir=train_params.data_dir)
    p = p._replace(data_name=train_params.data_name)
    # Dataset subset: set validation by default if nothing specified.
    # Good also for backward compatibility.
    if p.data_subset == 'train':
        p = p._replace(data_subset='validation')

    p = p._replace(batch_size=4)
    p = p._replace(num_gpus=train_params.num_gpus)
    p = p._replace(gpu_memory_frac_for_testing=0.1)
    p = p._replace(num_intra_threads=0)
    p = p._replace(num_inter_threads=0)
    p = p._replace(summary_verbosity=1)
    p = p._replace(print_training_accuracy=True)
    p = p._replace(variable_update='parameter_server')
    p = p._replace(local_parameter_device='cpu')

    p = p._replace(data_format=train_params.data_format)
    p = p._replace(label_smoothing=train_params.label_smoothing)
    p = p._replace(weight_decay=train_params.weight_decay)
    params = p
    return params
'''
import numpy as np
import cv2
import tensorflow as tf
import time
import TF_utils_cjy as tu
import sys
import argparse
import env_way
import thread
import worker
import parameters
import env_atari
from preprocessing import preprocess

params = parameters.load_params()

ap = argparse.ArgumentParser()
ap.add_argument("-log", "--log_name", required=False, help="log file name")
ap.add_argument("-net",
                "--net_type",
                required=False,
                help="network type('A3C' or 'AnDQN')")
ap.add_argument("-LSTM", "--LSTM", required=False, help="LSTM (True or False)")
ap.add_argument("-show_eval",
                "--show_eval",
                required=False,
                help="show evaluation screen? (True or False)")
ap.add_argument("-eval_mode",
                "--eval_mode",
                required=False,
scenario = "Ectogrid_min"

# Choose scenario
#scenario = "typedays_1bal"                              # Typedays, single thermal balance for BU design, TES in buildings
#scenario = "typedays_absC"                              # Clustering, seperated heating and cooling balance for BU design, TES in buildings
#scenario = "typedays_1bal_noBldgTES"                    # Clustering, single thermal balance for BU design, no building storages
#scenario = "typedays_absC_noBldgTES"                    # Clustering, seperated heating and cooling balance for BU design, no building storages
#scenario = "typedays_standalone"
#scenario = "1bal_noBldgTES"

# Define paths
path_file = str(os.path.dirname(os.path.realpath(__file__)))
dir_results = path_file + "\\Results\\" + str(
    datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')) + "_" + scenario

if not os.path.exists(dir_results):
    os.makedirs(dir_results)

# Maximum number of typedays
N_max = 60
tac_opt = np.zeros(N_max)

for N in np.arange(1, N_max + 1):

    ## Load parameters
    nodes, param, devs, devs_dom = parameters.load_params(
        use_case, path_file, scenario, N)

    # Run device optimization
    tac_opt[N - 1] = opt.run_optim(nodes, param, devs, devs_dom, dir_results)
Beispiel #7
0
def simulate(verbose=False, save_files=False, init=None, **kwargs):
    """

    :param verbose:
    :param save_files:
    :param kwargs:
    :return:
    """
    # parameters
    global_params = load_params(**kwargs)

    Nx = global_params["Nx"]
    Ny = global_params["Ny"]
    dx = global_params["dx"]
    dy = global_params["dy"]
    dt = global_params["dt"]
    Tmax = global_params["Tmax"]
    Toutput = global_params["Toutput"]
    C = global_params["C"]
    V = global_params["V"]

    shape = (Ny, Nx)
    rhs = np.zeros(shape)
    # RK Fields
    k1 = np.zeros(shape)
    k2 = np.zeros(shape)
    k3 = np.zeros(shape)
    k4 = np.zeros(shape)
    y1 = np.zeros(shape)
    y2 = np.zeros(shape)
    y3 = np.zeros(shape)

    # init fields and constants
    Y, X = np.meshgrid(dx * np.linspace(0, Nx, num=Nx),
                       dy * np.linspace(0, Ny, num=Ny))

    if init is None:
        Field_p = initfield_2D(X, Y)
    else:
        Field_p = init(X, Y)

    if verbose:
        print("stability numbers : ")
        print("advection in x direction: {0}".format(np.abs(V) * dt / dx))
        print("advection in y direction: {0}".format(np.abs(V) * dt / dy))
        print("diffusion in x direction: {0}".format(C * dt / dx**2))
        print("diffusion in y direction: {0}".format(C * dt / dy**2))

    mat2D = LinearMatrix2D(*shape)
    mat2D.init(-C * dt, dx, dy)

    # main loop
    t = 0
    tlast = 0
    Frames = []
    Frames.append(Field_p.copy())

    iterations = 0
    run_number = get_run_number(prefix='h2d_', as_text=True)

    if save_files:
        save_outputs(Field_p, run_number, as_text=True, prefix='advdiff_')
    with Timer() as tf:
        while t < Tmax:

            if global_params["scheme"] == "eule":
                operators.eule(rhs, Field_p, **global_params)
            elif global_params["scheme"] == "euli":
                operators.euli(mat2D, rhs, Field_p, **global_params)
            elif global_params["scheme"] == "RK2":
                operators.RK2(k1, k2, y1, Field_p, **global_params)
            elif global_params["scheme"] == "RK4":
                operators.RK4(k1, k2, k3, k4, y1, y2, y3, Field_p,
                              **global_params)
            elif global_params["scheme"] == "CN":
                # operators.CranckN(Mat, k1, Field_w, **global_params)
                raise ValueError("CN scheme not implemented in 2D")
            else:
                raise ValueError("scheme not specified")

            if (t - tlast) > Toutput:
                if verbose:
                    print('processing.... {0}%'.format(int(100. * t / Tmax)))
                if save_files:
                    save_outputs(Field_p,
                                 run_number,
                                 as_text=True,
                                 prefix='h2d_')
                tlast += Toutput
                Frames.append(np.array(Field_p))
            t += dt
            iterations += 1

    print("number of frames {0}".format(len(Frames)))
    print("loop time  in µs {0}".format(tf.interval))
    print("used time for 1 time step in µs: {0:.2f}".format(tf.interval /
                                                            iterations))
    # disable pickel for better compatibility
    np.save("data_2D.npy", Frames, allow_pickle=False)
    print("Data saved to file : ./data_2D.npy")
    return Frames
Beispiel #8
0
def simulate(verbose=False, save_files=False, **kwargs):
    """

    :param verbose: use verbose mode for outputs
    :param save_files: save snapshot in files
    :param kwargs: dict containing parameters used in simulation
    :return: list of snapshots
    """
    global_params = load_params(**kwargs)
    Nx = global_params["Nx"]
    dx = global_params["dx"]
    dt = global_params["dt"]
    Tmax = global_params["Tmax"]
    Toutput = global_params["Toutput"]
    C = global_params["C"]
    V = global_params["V"]

    # init fields and constants
    X = dx * np.linspace(0, Nx - 1, num=Nx)

    Field_p_init = initfield_1D(X)

    # RK Fields
    k1 = np.zeros(Nx)
    k2 = np.zeros(Nx)
    k3 = np.zeros(Nx)
    k4 = np.zeros(Nx)
    y1 = np.zeros(Nx)
    y2 = np.zeros(Nx)
    y3 = np.zeros(Nx)

    # matrix for implicit schemes
    Mat = matrix.linearmatrix(Nx)

    # init fields and constants
    if verbose:
        print("stability numbers : ")
        print("advection : {0}".format(np.abs(V) * dt / dx))
        print("diffusion : {0}".format(C * dt / dx**2))

    t = 0
    tlast = 0
    global_params["C"] *= dt
    global_params["V"] *= dt

    Field_p = Field_p_init.copy()
    Frames = [Field_p_init.copy()]

    # generate matrix for implicit part I + Vdu/dx - C d2u/dx2
    scheme = global_params["scheme"]
    if scheme == "CN":
        global_params["V"] *= 0.5
        global_params["C"] *= 0.5
    C = global_params["C"]
    V = global_params["V"]
    Mat.add_advection(V, dx)
    Mat.add_diffusion(-C, dx)
    Mat.add_diag_constant(1)
    Mat.initInvMat1D()

    iterations = 0
    run_number = get_run_number(prefix='advdiff_', as_text=True)
    if save_files:
        save_outputs(Field_p, run_number, as_text=True, prefix='advdiff_')
    with Timer() as tf:
        while t < Tmax:
            # uncomment line depending on choosen scheme
            if global_params["scheme"] == "eule":
                operators.eule(k1, Field_p, **global_params)
            elif global_params["scheme"] == "euli":
                operators.euli(Mat, k1, Field_p, **global_params)
            elif global_params["scheme"] == "RK2":
                operators.RK2(k1, k2, y1, Field_p, **global_params)
            elif global_params["scheme"] == "RK4":
                operators.RK4(k1, k2, k3, k4, y1, y2, y3, Field_p,
                              **global_params)
            elif global_params["scheme"] == "CN":
                operators.CranckN(Mat, k1, Field_p, **global_params)
            else:
                raise ValueError("scheme not specified")

            if (t - tlast) > Toutput:
                if verbose:
                    print('processing.... {0}%'.format(int(100. * t / Tmax)))
                if save_files:
                    save_outputs(Field_p,
                                 run_number,
                                 as_text=True,
                                 prefix='advdiff_')
                tlast += Toutput
                Frames.append(np.array(Field_p))
            t += dt
            iterations += 1
    print("total execution time in µs : {0}".format(tf.interval))
    print("number of snapshots : {0}".format(len(Frames)))
    print("used time for 1 time step : {0:.2f} µs".format(tf.interval /
                                                          iterations))

    return Frames
Beispiel #9
0
import parameters
import datetime

path_file = str(os.path.dirname(os.path.realpath(__file__)))
dir_results = path_file + "\\Results\\" + str(datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S'))

if not os.path.exists(dir_results):
    os.makedirs(dir_results)


# Choose use case
use_case = "FZJ"


# Load parameters
nodes, param = parameters.load_params(use_case, path_file)




typedays = []



z = param["day_matrix"]


for d in range(365):
    if any(z[d]):
        typedays.append(d)
        
Beispiel #10
0
def simulate(verbose=False, save_files=False, **kwargs):
    """

    :param verbose:
    :param save_files:
    :param kwargs:
    :return:
    """
    # parameters
    global_params = load_params(**kwargs)

    Nx = global_params["Nx"]
    Nm = global_params["Nm"]
    dx = global_params["dx"]

    ky = global_params["ky"]
    dy = global_params["dy"]

    dt = global_params["dt"]
    Tmax = global_params["Tmax"]
    Toutput = global_params["Toutput"]

    # RK Fields
    shape = (Nm, Nx)
    k1 = np.zeros(shape, dtype=np.complex128)
    k2 = np.zeros(shape, dtype=np.complex128)
    k3 = np.zeros(shape, dtype=np.complex128)
    k4 = np.zeros(shape, dtype=np.complex128)
    y1 = np.zeros(shape, dtype=np.complex128)
    y2 = np.zeros(shape, dtype=np.complex128)
    y3 = np.zeros(shape, dtype=np.complex128)
    rhs = np.zeros(shape, dtype=np.complex128)
    Field_p = np.zeros(shape, dtype=np.complex128)

    # init fields and constants
    Y, X = np.meshgrid(dx * np.linspace(0, Nx, num=Nx),
                       dy * np.linspace(0, Nm, num=2 * Nm - 1))

    U0 = initfield_2D(X, Y)

    # compute fourier represention of U(x,y,t=0)
    operators.compute_spectral_field(1, 1, U0, Field_p)

    # print stability condition
    C = global_params["C"]
    V = np.abs(global_params["V"])
    print("stability numbers : ")
    print("advection in x direction: {0}".format(V * dt / dx))
    print("advection in y direction: {0}".format(V * dt / dy))
    print("diffusion in x direction: {0}".format(C * dt / dx**2))
    print("diffusion in y direction: {0}".format(C * dt / dy**2))

    # main loop
    t = 0
    tlast = 0
    Field_p = Field_p.astype(np.complex)
    Frames = [
        Field_p.copy(),
    ]

    iterations = 0
    run_number = get_run_number(prefix='h2dspec_', as_text=True)

    if save_files:
        save_outputs(U0, run_number, as_text=True, prefix='advdiff_')
    with Timer() as tf:
        while t < Tmax:
            if global_params["scheme"] == "eule":
                operators.eule(rhs, Field_p, **global_params)
            elif global_params["scheme"] == "euli":
                raise ValueError("euli scheme not implemented in 2D")
                # operators.euli(mat2D, rhs, Field_w, **global_params)
            elif global_params["scheme"] == "RK2":
                operators.RK2(k1, k2, y1, Field_p, **global_params)
            elif global_params["scheme"] == "RK4":
                operators.RK4(k1, k2, k3, k4, y1, y2, y3, Field_p,
                              **global_params)
            elif global_params["scheme"] == "CN":
                #     operators.CranckN(Mat, k1, Field_w, **global_params)
                raise ValueError("CN scheme not implemented in 2D")
            else:
                raise ValueError("scheme not specified")

            if (t - tlast) > Toutput:
                if verbose:
                    print('processing.... {0}%'.format(int(100. * t / Tmax)))

                tlast += Toutput
                Frames.append(np.array(Field_p))
            t += dt
            iterations += 1

    print("number of frames {0}".format(len(Frames)))
    print("loop time  in µs {0}".format(tf.interval))
    print("used time for 1 time step : {0:.2f}".format(tf.interval /
                                                       iterations))

    Frames_real = []
    real_data = np.zeros((2 * (Nm - 1), Nx), dtype=np.double)
    if verbose:
        print('converting snapshots from Fourier to real space')
    for slices in Frames:
        operators.compute_real_field(1, 1, slices, real_data)
        if save_files:
            save_outputs(real_data,
                         run_number,
                         as_text=True,
                         prefix='h2dspec_')
        Frames_real.append(np.array(real_data))

    np.save("data_2D.npy", Frames_real, allow_pickle=False)
    print("Data saved to file : ./data_2D.npy")
    return Frames_real