예제 #1
0
파일: model.py 프로젝트: czczup/URST
    def __init__(self, model=None, fixed=False):
        super(Decoder3, self).__init__()
        self.fixed = fixed

        self.conv31 = nn.Conv2d(256, 128, 3, 1, 0)
        self.conv22 = nn.Conv2d(128, 128, 3, 1, 0)
        self.conv21 = nn.Conv2d(128, 64, 3, 1, 0)
        self.conv12 = nn.Conv2d(64, 64, 3, 1, 0)
        self.conv11 = nn.Conv2d(64, 3, 3, 1, 0)

        self.relu = nn.ReLU(inplace=True)
        self.unpool = nn.UpsamplingNearest2d(scale_factor=2)
        self.pad = nn.ReflectionPad2d((1, 1, 1, 1))

        if model:
            assert (os.path.splitext(model)[1] in {".t7", ".pth"})
            if model.endswith(".t7"):
                t7_model = torchfile.load(model)
                load_param(t7_model, 1, self.conv31)
                load_param(t7_model, 5, self.conv22)
                load_param(t7_model, 8, self.conv21)
                load_param(t7_model, 12, self.conv12)
                load_param(t7_model, 15, self.conv11)
            else:
                self.load_state_dict(
                    torch.load(model,
                               map_location=lambda storage, location: storage))

        if fixed:
            for param in self.parameters():
                param.requires_grad = False
예제 #2
0
파일: model.py 프로젝트: czczup/URST
    def __init__(self, model=None, fixed=False):
        super(Encoder1, self).__init__()
        self.fixed = fixed

        self.conv0 = nn.Conv2d(3, 3, 1, 1, 0)
        self.conv11 = nn.Conv2d(3, 64, 3, 1, 0, dilation=1)

        self.relu = nn.ReLU(inplace=True)
        self.pool = nn.MaxPool2d(kernel_size=2, stride=2, return_indices=False)
        self.pad = nn.ReflectionPad2d((1, 1, 1, 1))

        if model:
            assert (os.path.splitext(model)[1] in {".t7", ".pth"})
            if model.endswith(".t7"):
                t7_model = torchfile.load(model)
                load_param(t7_model, 0, self.conv0)
                load_param(t7_model, 2, self.conv11)
            else:
                self.load_state_dict(
                    torch.load(model,
                               map_location=lambda storage, location: storage))

        if fixed:
            for param in self.parameters():
                param.requires_grad = False
예제 #3
0
def get_rbf_mran_and_hist_len(study_folder, exe_mode):
    param_file = study_folder + '/model/param.yaml'
    train_file = study_folder + '/data/train.txt'
    with open(train_file, mode='r') as f:
        l = f.readlines()
    train_datas = [list(map(float, s.strip().split())) for s in l]

    param = load_param(param_file)
    hist_len = param['past_sys_output_num']
    rbf_mran = RBF_MRAN(
        nu=int(train_datas[2][0]),  # システム入力(制御入力)の次元
        ny=int(train_datas[1][0]),  # システム出力ベクトルの次元
        past_sys_input_num=param['past_sys_input_num'],  # 過去のシステム入力保存数
        past_sys_output_num=hist_len,  # 過去のシステム出力保存数
        # init_h=param['init_h'], # スタート時の隠れニューロン数
        init_h=0,  # スタート時の隠れニューロン数
        E1=param['E1'],
        E2=param['E2'],
        E3=param['E3'],
        E3_max=param['E3_max'],
        E3_min=param['E3_min'],
        gamma=param['gamma'],
        Nw=param['Nw'],
        Sw=param['Sw'],
        kappa=param['kappa'] if 'kappa' in param else 1.0,
        p0=param['p'] if 'p' in param else 1.0,
        q=param['q'] if 'q' in param else 0.1,
        study_folder=study_folder,
        use_exist_net=True,  # 既存のネットワークを使うかどうか
        exe_mode=exe_mode)
    return rbf_mran, hist_len
예제 #4
0
def load_data(**kwargs):
    """
    - **kwargs will add/overwrite any set parameters in param.
    """
    param = utils.load_param(**kwargs)

    if param['device'] == "cuda":
        # Find optimal device and use it
        sleeptime = random.randint(0, 3)
        time.sleep(sleeptime)
        if os.environ.get(
                'interactive_v2'
        ) == "true":  #only run this in interactive as production pods doesnt support it
            param['device'] = utils.get_best_cuda()
            torch.cuda.set_device(param['device'])
        torch.set_default_tensor_type('torch.cuda.FloatTensor')

    ind2val, itemattr, dataloaders = dataset.load_dataloaders(
        data_dir=param['data_dir'],
        batch_size=param['batch_size'],
        split_trainvalid=param['split_trainvalid'],
        t_testsplit=param['t_testsplit'],
        sample_uniform_action=param.get('sample_uniform_action', False))

    noclick_rate = (dataloaders['train'].dataset.data['click'] == 1).float(
    ).sum() / (dataloaders['train'].dataset.data['click'] > 0).float().sum()
    logging.info(f"Loaded dataset has {noclick_rate*100:.1f}% of noClick.")

    param['num_items'] = len(ind2val['itemId'])
    param['num_groups'] = len(np.unique(itemattr['category']))
    param['num_users'], param['maxlen_time'], _ = dataloaders[
        'train'].dataset.data['action'].size()
    param['num_users'] = dataloaders['train'].dataset.data['userId'].max() + 1
    return param, ind2val, itemattr, dataloaders
예제 #5
0
def def_optimizer_param(**kw):
    optimizer_type = "tp"
    lr = 1.0e-2
    lr_decoder = 10 * lr
    sub_optimizer_type = "sgd"

    lr_decay_method = "linear"
    milestones = [[0.50, 1.0],[0.70, 1.0e-1], [0.85, 1.0e-2], [0.95, 1.0e-3]]
    return load_param(locals())
예제 #6
0
def def_model_param(**kw): # define necessary model parameters here.
    model_type = "RSLP_EI"
    model_name = "RSLP_EI"
    data_set = "cifar10"
    batch_size = 64
    optimize_method = "tp"

    E_num = 400
    I_num = 100
    iter_time = 10
    
    init_weight = {
        "i":["input", 1.0e-3],
        "f":["input", 1.0e-3],
        "r":["input", 1.0e-3],
    }

    #loss coeffients
    act_coeff = 0.0e-4
    class_coeff = 1.00
    class_loss_func = "CEL"
    weight_coeff = 1.0e-5
    hebb_coeff = 0.00 #Hebb loss
    stab_coeff = 0.00 #loss that punishes divergence of neural activity along time.
    noise_coeff = 0.0 #noise coefficient

    separate_ei = True
    cons_method = "abs" # methd to limit weight, which is used to implement Dale's Law.
    class_loss_func = "CEL" # CEL: Cross Entropy Loss, MSE: Mean Square Error
    if separate_ei:
        act_func_e = ["relu", 1.0]
        act_func_i = ["relu", 1.0]
        time_const_e = 0.2
        time_const_i = 0.4 # fast-spiking inhibitory neurons
        Dale = ["r", "f"] # weights that conform to Dale's Law  
    else:
        act_func = ["relu", 0.0]
        time_const = 0.2
    bias = False
    return load_param(locals())
예제 #7
0
def main(**kwargs):
    param = utils.load_param()

    # Overwrite param with whatever is in kwargs:
    try:
        for key, val in kwargs.items():
            logging.info(f"Overwriting parameter {key} to {val}.")
            param[key] = val
    except:
        logging.info("ERROR: Did no overwrite of default param.")

    if param['device'] == "cuda":
        torch.set_default_tensor_type('torch.cuda.FloatTensor')

    if param.get('real_data'):
        logging.info("Loading real data")

        ind2val, itemattr, dataloaders = prepare.load_dataloaders(
            data_dir="data_real",
            data_type="lake-noclickrate-0.2",
            batch_size=param['batch_size'],
            split_trainvalid=param['split_trainvalid'],
            num_workers=0,
            override_candidate_sampler="actual",
            t_testsplit=param['t_testsplit'])

    else:
        sim_param = utils.load_sim_param()
        #%% Place all items in a group:
        item_group = 1 + (torch.arange(sim_param['num_items']) //
                          (sim_param['num_items'] /
                           (sim_param['num_groups'] - 1))).long()
        item_group[:3] = 0  # first three items are special group
        itemattr = {'category': item_group.cpu().numpy()}

        # %% TRAIN: MODEL+CALLBACKS+TRAINER
        pyro.clear_param_store()
        env = models.PyroRecommender(**sim_param,
                                     item_group=torch.tensor(
                                         itemattr['category']))
        env.init_set_of_real_parameters()
        sim = simulator.Simulator(**sim_param, env=env)
        ind2val, itemattr, dataloaders, sim = simulator.collect_simulated_data(
            sim,
            policy_epsilon=sim_param['collect_data_randompct'],
            **sim_param)

    param['num_items'] = len(ind2val['itemId'])
    param['num_groups'] = len(np.unique(itemattr['category']))
    param['num_users'], param['maxlen_time'], _ = dataloaders[
        'train'].dataset.data['action'].size()
    param['num_users'] = param['num_users'] + 1
    #param['num_displayTypes'] = 3
    # Move data to device
    #for key, val in dataloaders['train'].dataset.data.items():
    #    dataloaders['train'].dataset.data[key] = val.to(param['device'])
    #%%
    pyro.clear_param_store()
    pyro.validation_enabled(True)
    torch.manual_seed(param['train_seed'])
    import pyrotrainer
    dummybatch = next(iter(dataloaders['train']))
    dummybatch['phase_mask'] = dummybatch['mask_train']
    dummybatch = {
        key: val.long().to(param.get("device"))
        for key, val in dummybatch.items()
    }

    model = models.PyroRecommender(**param,
                                   item_group=torch.tensor(
                                       itemattr['category']).long())
    guide = models.MeanFieldGuide(model=model, batch=dummybatch, **param)

    #%% START WITH TRUE PARAMETERS IF THIS IS TRUE:
    if param.get("start_true"):
        logging.info(f"Starting in true mean parameters...:")
        pyro.clear_param_store()
        for key, val in env.par_real.items():
            pyro.param(f"{key}-mean", val)
            pyro.param(f"{key}-scale", torch.zeros_like(val) + 1e-5)
            print(key)

    #%% Define callbacks:

    # Common callbacks:
    optim = pyrotrainer.SviStep(model=model, guide=guide, **param)

    step_callbacks = [optim, pyrotrainer.calc_batch_stats]

    phase_end_callbacks = [
        pyrotrainer.report_phase_end,
        pyrotrainer.ReportPyroParameters(),
        pyrotrainer.EarlyStoppingAndCheckpoint(
            stopping_criteria=param['stopping_criteria'],
            patience=param['patience'],
            name=param['name'])
    ]

    after_training_callbacks = []

    if param['real_data']:
        plot_finn_ads = pyrotrainer.PlotFinnAdsRecommended(ind2val,
                                                           epoch_interval=3)
        phase_end_callbacks.append(plot_finn_ads)
        after_training_callbacks.append(pyrotrainer.VisualizeEmbeddings())
    else:
        test_sim = simulator.Simulator(**param, env=env)
        step_callbacks.append(pyrotrainer.Simulator_batch_stats(test_sim))
        after_training_callbacks.append(
            pyrotrainer.VisualizeEmbeddings(sim=test_sim))
        after_training_callbacks.append(
            pyrotrainer.RewardComputation(param, test_sim))

    after_training_callbacks.append(pyrotrainer.ReportHparam(param))
    #%%
    trainer = pyrotrainer.PyroTrainer(
        model,
        guide,
        dataloaders,
        before_training_callbacks=[pyrotrainer.checksum_data],
        after_training_callbacks=after_training_callbacks,
        step_callbacks=step_callbacks,
        phase_end_callbacks=phase_end_callbacks,
        max_epoch=param['max_epochs'],
        **param)
    return param, ind2val, trainer
예제 #8
0
def def_trainer_param(**kw):    
    epoch_num = 120
    save_model = True
    save_model_interval = 10
    save_model_path = "../saved_models/"
    return load_param(locals())
예제 #9
0
def def_data_loader_param(**kw):
    data_set = "cifar10"
    batch_size = 64
    return load_param(locals())
예제 #10
0
파일: model.py 프로젝트: czczup/URST
    def __init__(self, model=None, fixed=False):
        super(Encoder5, self).__init__()
        self.fixed = fixed

        self.conv0 = nn.Conv2d(3, 3, 1, 1, 0)
        self.conv0.weight = nn.Parameter(
            torch.from_numpy(
                np.array([[[[0]], [[0]], [[255]]], [[[0]], [[255]], [[0]]],
                          [[[255]], [[0]], [[0]]]])).float())
        self.conv0.bias = nn.Parameter(
            torch.from_numpy(np.array([-103.939, -116.779, -123.68])).float())
        self.conv11 = nn.Conv2d(3, 64, 3, 1, 0)  # conv1_1
        self.conv12 = nn.Conv2d(64, 64, 3, 1, 0)  # conv1_2
        self.conv21 = nn.Conv2d(64, 128, 3, 1, 0)  # conv2_1
        self.conv22 = nn.Conv2d(128, 128, 3, 1, 0)  # conv2_2
        self.conv31 = nn.Conv2d(128, 256, 3, 1, 0)  # conv3_1
        self.conv32 = nn.Conv2d(256, 256, 3, 1, 0)  # conv3_2
        self.conv33 = nn.Conv2d(256, 256, 3, 1, 0)  # conv3_3
        self.conv34 = nn.Conv2d(256, 256, 3, 1, 0)  # conv3_4
        self.conv41 = nn.Conv2d(256, 512, 3, 1, 0)  # conv4_1
        self.conv42 = nn.Conv2d(512, 512, 3, 1, 0)  # conv4_2
        self.conv43 = nn.Conv2d(512, 512, 3, 1, 0)  # conv4_3
        self.conv44 = nn.Conv2d(512, 512, 3, 1, 0)  # conv4_4
        self.conv51 = nn.Conv2d(512, 512, 3, 1, 0)  # conv5_1

        self.relu = nn.ReLU(inplace=True)
        self.pool = nn.MaxPool2d(kernel_size=2, stride=2, return_indices=False)
        self.pad = nn.ReflectionPad2d((1, 1, 1, 1))

        if model:
            assert (os.path.splitext(model)[1] in {".t7", ".pth"})
            if model.endswith(".t7"):
                t7_model = torchfile.load(model)
                ## load from my normalised t7 model, which has no conv0 layer.
                # load_param(t7_model, 0,  self.conv11)
                # load_param(t7_model, 2,  self.conv12)
                # load_param(t7_model, 5,  self.conv21)
                # load_param(t7_model, 7,  self.conv22)
                # load_param(t7_model, 10, self.conv31)
                # load_param(t7_model, 12, self.conv32)
                # load_param(t7_model, 14, self.conv33)
                # load_param(t7_model, 16, self.conv34)
                # load_param(t7_model, 19, self.conv41)
                # load_param(t7_model, 21, self.conv42)
                # load_param(t7_model, 23, self.conv43)
                # load_param(t7_model, 25, self.conv44)
                # load_param(t7_model, 28, self.conv51)
                # load_param(t7_model, 30, self.conv52)
                load_param(t7_model, 0, self.conv0)
                load_param(t7_model, 2, self.conv11)
                load_param(t7_model, 5, self.conv12)
                load_param(t7_model, 9, self.conv21)
                load_param(t7_model, 12, self.conv22)
                load_param(t7_model, 16, self.conv31)
                load_param(t7_model, 19, self.conv32)
                load_param(t7_model, 22, self.conv33)
                load_param(t7_model, 25, self.conv34)
                load_param(t7_model, 29, self.conv41)
                load_param(t7_model, 32, self.conv42)
                load_param(t7_model, 35, self.conv43)
                load_param(t7_model, 38, self.conv44)
                load_param(t7_model, 42, self.conv51)
            else:
                self.load_state_dict(
                    torch.load(model,
                               map_location=lambda storage, location: storage))

        if fixed:
            for param in self.parameters():
                param.requires_grad = False
예제 #11
0
파일: model.py 프로젝트: czczup/URST
    def __init__(self, model=None, fixed=False):
        super(Encoder4, self).__init__()
        self.fixed = fixed

        self.conv0 = nn.Conv2d(3, 3, 1, 1, 0)
        self.conv11 = nn.Conv2d(3, 64, 3, 1, 0)  # conv1_1
        self.conv12 = nn.Conv2d(64, 64, 3, 1, 0)  # conv1_2
        self.conv21 = nn.Conv2d(64, 128, 3, 1, 0)  # conv2_1
        self.conv22 = nn.Conv2d(128, 128, 3, 1, 0)  # conv2_2
        self.conv31 = nn.Conv2d(128, 256, 3, 1, 0)  # conv3_1
        self.conv32 = nn.Conv2d(256, 256, 3, 1, 0)  # conv3_2
        self.conv33 = nn.Conv2d(256, 256, 3, 1, 0)  # conv3_3
        self.conv34 = nn.Conv2d(256, 256, 3, 1, 0)  # conv3_4
        self.conv41 = nn.Conv2d(256, 512, 3, 1, 0)  # conv4_1

        self.relu = nn.ReLU(inplace=True)
        self.pool = nn.MaxPool2d(kernel_size=2, stride=2, return_indices=False)
        self.pad = nn.ReflectionPad2d((1, 1, 1, 1))

        if model:
            assert (os.path.splitext(model)[1] in {".t7", ".pth"})
            if model.endswith(".t7"):
                t7_model = torchfile.load(model)
                load_param(t7_model, 0, self.conv0)
                load_param(t7_model, 2, self.conv11)
                load_param(t7_model, 5, self.conv12)
                load_param(t7_model, 9, self.conv21)
                load_param(t7_model, 12, self.conv22)
                load_param(t7_model, 16, self.conv31)
                load_param(t7_model, 19, self.conv32)
                load_param(t7_model, 22, self.conv33)
                load_param(t7_model, 25, self.conv34)
                load_param(t7_model, 29, self.conv41)
            else:
                self.load_state_dict(
                    torch.load(model,
                               map_location=lambda storage, location: storage))

        if fixed:
            for param in self.parameters():
                param.requires_grad = False
예제 #12
0
파일: init.py 프로젝트: pythings/PythingsOS
def start():

    # Get Pythings version
    env.pythings_version = common.get_pythings_version()

    print('\n|--------------------------|')
    print('|  Starting PythingsOS :)  |')
    print('|--------------------------|')
    print(' Version: {}'.format(env.pythings_version))
    print(' Platform: {}'.format(platform))
    try: print(' Thing ID: {}\n'.format(hal.get_tid()))
    except: pass

    # Init hardware and platform
    hal.init()
    pal.init()
    
    # Start setup  mode if required
    if hal.HW_SUPPORTS_RESETCAUSE and hal.HW_SUPPORTS_WLAN and hal.get_reset_cause() in hal.WEBSETUP_RESETCAUSES:
        setup_timeout = int(load_param('setup_timeout', 60))
        if setup_timeout:
            if hal.HW_SUPPORTS_LED: hal.LED.on()
            from websetup import websetup
            gc.collect()
            websetup(timeout_s=setup_timeout)
            if hal.HW_SUPPORTS_LED: hal.LED.off()
            logger.info('Resetting...')
            hal.reboot()

    # Disable AP mode, Enable and configure STA mode 
    if hal.HW_SUPPORTS_WLAN:
        hal.WLAN.ap_active(False)
        hal.WLAN.sta_active(True)

    # Start loading settings and parameters
    from utils import load_settings
    env.settings = load_settings()
    env.payload_encrypter = None # Initalization

    # Load backend: the local param wins 
    env.backend = load_param('backend', None)

    if not env.backend:
        backend_overrided = False
        if 'backend' in env.settings and env.settings['backend']:
            env.backend = env.settings['backend']
        else:
            env.backend = 'backend.pythings.io'
    else:
        backend_overrided = True

    # Load aid and tid: only local param or default
    env.aid = load_param('aid', None)
    if env.aid is None:
        logger.critical('AID not provided, stopping here. Please set it up!')
        import time
        while True:
            time.sleep(1)
    env.tid = load_param('tid', None)
    if env.tid is None: env.tid = hal.get_tid()

    # Load pool: the local param wins 
    env.pool = load_param('pool', None)
    if not env.pool:
        if 'pool' in env.settings and env.settings['pool']:
            env.pool = env.settings['pool']
      
    env.frozen = hal.is_frozen()

    # Tasks placeholders
    env.app_workerTask = None
    env.app_managementTask = None
      
    # Report
    logger.info('Running with backend="{}" and aid="{}"'.format(env.backend, env.aid))

    # Get app version:
    env.app_version = common.get_app_version()

    # Register and perform the first management task call on "safe" backend, if not overrided
    if not backend_overrided:
        backend_set = env.backend
        env.backend ='backend.pythings.io'
    
    # Pre-register if payload encryption activated
    use_payload_encryption = env.settings['payload_encryption'] if 'payload_encryption' in env.settings else hal.USE_ENCRYPTION_DEFAULT
    if use_payload_encryption and hal.HW_SUPPORTS_ENCRYPTION and pal.get_payload_encrypter():
        logger.info('Enabling Payload Encryption and preregistering')
        env.payload_encrypter = pal.get_payload_encrypter()(comp_mode=True)
        from preregister import preregister
        token = preregister()
        env.token = token
        logger.info('Got token: {}'.format(env.token))
        del preregister
        gc.collect()
        
    # Register yourself, and start a new session
    from register import register
    token, epoch = register()
    if not env.payload_encrypter:
        env.token = token
        logger.info('Got token: {}'.format(env.token))
    del register
    gc.collect()
    
    # Sync time and add to env
    chronos = hal.Chronos(epoch)
    env.chronos = chronos

    # Call system management (will update App/Pythings versions  and settings if required)
    logger.info('Calling system management (preloop)')
    from management import system_management_task
    system_management_task(chronos)
    del system_management_task
    gc.collect()
    
    # Set back host to the proper one
    if not backend_overrided:
        env.backend=backend_set
        del backend_set
    gc.collect()

    # Init app
    try:
        from worker_task import WorkerTask
        env.app_workerTask = WorkerTask()
    except Exception as e:
        logger.error('Error in importing/loading app\'s worker tasks: {} {}'.format(e.__class__.__name__, e))
        logger.debug(pal.get_traceback(e))
        from api import report
        common.run_controlled(2,report,what='worker', status='KO', message='{} {} ({})'.format(e.__class__.__name__, e, pal.get_traceback(e)))

    try:
        from management_task import ManagementTask
        env.app_managementTask = ManagementTask()
    except Exception as e:
        logger.error('Error in importing/loading  app\'s management tasks: {} {}'.format(e.__class__.__name__, e))
        logger.debug(pal.get_traceback(e))
        from api import report
        common.run_controlled(2,report,what='management', status='KO', message='{} {} ({})'.format(e.__class__.__name__, e, pal.get_traceback(e)))

    # Setup intervals
    worker_interval = int(env.settings['worker_interval']) if 'worker_interval' in env.settings else 300
    management_interval = int(env.settings['management_interval']) if 'management_interval' in env.settings else 60

    # Start main loop
    loop_count = 0
    while True:
        
        if loop_count % management_interval == 0:
            logger.info('Calling management (loop={})'.format(loop_count))
            if hal.HW_SUPPORTS_LED:
                hal.LED.on(); sleep(0.05); hal.LED.off()
            from management import system_management_task
            system_management_task(chronos)
            del system_management_task
            gc.collect()
            logger.info('Done management')

        if loop_count % worker_interval == 0:
            logger.info('Calling worker (loop={})'.format(loop_count))
            from worker import system_worker_task
            system_worker_task(chronos)
            del system_worker_task
            gc.collect()
            logger.info('Done worker')
            
        loop_count+=1
        sleep(1)
예제 #13
0
파일: init.py 프로젝트: pythings/PythingsOS
#  Imports
from time import sleep
import gc
import env
import common
import hal
import pal
from utils import load_param
from platform import platform

# Logger
import logger
logger.level = int(load_param('loglevel', logger.INFO))

# Start
def start():

    # Get Pythings version
    env.pythings_version = common.get_pythings_version()

    print('\n|--------------------------|')
    print('|  Starting PythingsOS :)  |')
    print('|--------------------------|')
    print(' Version: {}'.format(env.pythings_version))
    print(' Platform: {}'.format(platform))
    try: print(' Thing ID: {}\n'.format(hal.get_tid()))
    except: pass

    # Init hardware and platform
    hal.init()
예제 #14
0
파일: example.py 프로젝트: KoenHan/rbf-mran
        gen_res = gen_data(
            sys_type=args.sys, train_file=train_file, test_file=test_file,
            data_len=args.data_len, ncx=args.n_change_x, ncu=args.n_change_u,
            xb=args.x_before, xa=args.x_after, ub=args.u_before, ua=args.u_after)
        if gen_res < 0 :
            exit()

    assert os.path.isfile(train_file), 'No train.txt!!!! exit.'
    assert os.path.isfile(test_file), 'No test.txt!!! exit.'

    # パラメータファイル生成or読み込み
    param_file = study_folder+'/model/'+args.param_file
    if args.param_file == 'param.yaml' and not os.path.isfile(param_file) :
        gen_param_file_from_cmd(param_file)

    param = load_param(param_file)
    with open(train_file, mode='r') as f:
        l = f.readlines()
    datas = [list(map(float, s.strip().split())) for s in l]

    rbf_mran = RBF_MRAN(
        nu=int(datas[2][0]), # システム入力(制御入力)の次元
        ny=int(datas[1][0]), # システム出力ベクトルの次元
        past_sys_input_num=param['past_sys_input_num'], # 過去のシステム入力保存数
        past_sys_output_num=param['past_sys_output_num'], # 過去のシステム出力保存数
        # init_h=param['init_h'], # スタート時の隠れニューロン数
        init_h=args.init_h, # スタート時の隠れニューロン数
        E1=param['E1'],
        E2=param['E2'],
        E3=param['E3'],
        E3_max=param['E3_max'],
예제 #15
0
import pyro
import logging
import simulator
import torch
import matplotlib.pyplot as plt
from torch.nn import functional as F
import pyro
import pyro.distributions as dist
import models
import numpy as np
logging.basicConfig(format='%(asctime)s %(message)s', level='INFO')
import prepare
from prepare import SequentialDataset

# %%
param = utils.load_param()

# Overwrite param with whatever is in kwargs:
try:
    for key, va.l in kwargs.items():
        logging.info(f"Overwriting parameter {key} to {val}.")
        param[key] = val
except:
    logging.info("Did no overwrite of default param.")

if param['device'] == "cuda":
    torch.set_default_tensor_type('torch.cuda.FloatTensor')
if param.get('real_data'):
    logging.info("Loading real data")

    ind2val, itemattr, dataloaders = prepare.load_dataloaders(
예제 #16
0
def at_post(url, data, dest=None):

    # Do we have the Modem ready?
    try:
        env.modem
    except AttributeError:
        # Do we have to load custom pins?
        modem_pins = load_param('modem_pins', None)
        if modem_pins:
            MODEM_PWKEY_PIN, MODEM_RST_PIN, MODEM_POWER_ON_PIN, MODEM_TX_PIN, MODEM_RX_PIN = [
                int(pin) if pin != 'None' else None
                for pin in modem_pins.split(',')
            ]

        # Create new modem object on the right Pins
        if platform.startswith('esp8266'):

            from machine import SOFTUART
            uart = Uart(
                SOFTUART(tx=MODEM_TX_PIN if modem_pins else hal.MODEM_TX_PIN,
                         rx=MODEM_RX_PIN if modem_pins else hal.MODEM_RX_PIN,
                         baudrate=9600))
            env.modem = Modem(uart)

        else:
            env.modem = Modem(
                MODEM_PWKEY_PIN=MODEM_PWKEY_PIN
                if modem_pins else hal.MODEM_PWKEY_PIN,
                MODEM_RST_PIN=MODEM_RST_PIN
                if modem_pins else hal.MODEM_RST_PIN,
                MODEM_POWER_ON_PIN=MODEM_POWER_ON_PIN
                if modem_pins else hal.MODEM_POWER_ON_PIN,
                MODEM_TX_PIN=MODEM_TX_PIN if modem_pins else hal.MODEM_TX_PIN,
                MODEM_RX_PIN=MODEM_RX_PIN if modem_pins else hal.MODEM_RX_PIN)
        env.modem.initialize()

    # Always connect, as if it is already connected won't do anything
    env.modem.connect(apn=env.apn)

    #----------------------
    #  Post start
    #----------------------

    try:
        token = env.token
    except AttributeError:
        token = None
    if token: data['token'] = token
    logger.debug('Calling POST "{}" with data'.format(url), data)

    # If POST fails do not have an exception when closing file in the finally
    if dest: f = None

    try:
        if dest: f = open(dest, 'w')

        # Execute POST
        response = env.modem.http_request('http://' + url, 'POST',
                                          json.dumps(data), 'application/json')
        logger.debug('Received status code', response.status_code)
        logger.debug('Received content', response.content)
        status = response.status_code
        content = response.content

        if dest and status == 200:
            f.write(content)

        else:
            if content is None:
                content = ''

        return {
            'version': None,
            'status': status,
            'msg': None,
            'content': content
        }
    except:
        raise
    finally:
        if dest and f:
            f.close()
예제 #17
0
    def readline(self):
        line = bytes()
        while True:
            char = chr(self.softuart.get())
            if char == '\x00':
                return line
            line += char.encode('utf8')
            if char == '\n':
                return line


# Do we have a modem configured?
try:
    env.apn
except AttributeError:
    env.apn = load_param('apn', None)

# Should we use AT-based, modem POST?
if (not network.WLAN(network.STA_IF).isconnected()) and env.apn:
    logger.debug('Using AT post')
    use_at_post = True
else:
    logger.debug('Using socket post')
    use_at_post = False


# Define AT-based post for AT modems
def at_post(url, data, dest=None):

    # Do we have the Modem ready?
    try:
예제 #18
0
Vocabulary = freq_of_words.most_common(vocabulary_size-1)

#Make Index - to - Word Vector
idx_to_wd = [v[0] for v in Vocabulary]
idx_to_wd.append(not_known)

#Make Word - to - Index Vector
wd_to_idx = dict([(word, idx) for idx, word in enumerate(idx_to_wd)])

print "The current length of vocab = %d." % vocabulary_size
print "The least frequent word = %s." % Vocabulary[-1][0]
print "The number of times of appearance of the least frequent word = %d." % Vocabulary[-1][1]

#The words not present in our vocab are changed to 'not_known' token
for i, s in enumerate(sen_token):
    sen_token[i] = [Word if Word in wd_to_idx else not_known for Word in s]
    
train_data = np.asarray([[wd_to_idx[Word] for Word in s[:-1]] for s in sen_token])
train_label = np.asarray([[wd_to_idx[Word] for Word in s[1:]] for s in sen_token])


model = RNNLanguageModel(vocabulary_size, hidden_size=_Hidden)
samay1 = time.time()
model.grad_descent(train_data[10], train_label[10], _Alpha)
samay2 = time.time()
print "Time of Gradient Descent step = %f ms" % ((samay2 - samay1) * 1000.)

if _Model != None:
    utils.load_param(_Model, model)

train(model, train_data, train_label, epochs = _Epochs, alpha = _Alpha)