Beispiel #1
0
def run_accelerated_comparison():
    # Repeatable testing of standard training model to compare Double DQN
    step_array = []
    reward_array = []
    random.seed(0)
    np.random.seed(0)
    tf.random.set_seed(0)
    for i in range(1):
        ins = wrapper(batch_size=512,
                      policy_type='epsilon_decay',
                      policy_param={
                          'eps': 0.0,
                          'eps_decay': 0.9999,
                          'min_eps': 0.002
                      },
                      accelerated=True,
                      Double_DQN_version=1)
        ins.train(10)
        steps, rewards, _, _ = ins.evaluate(100)
        step_array.append(steps)
        reward_array.append(rewards)
    print(
        f'Accelerated Rewards: {sum(reward_array)/len(reward_array)} Steps:{sum(step_array)/len(step_array)}'
    )
    helper_functions.print_integrated_times()
    step_array = []
    reward_array = []
    random.seed(0)
    np.random.seed(0)
    tf.random.set_seed(0)
    for i in range(1):
        ins = wrapper(batch_size=512,
                      policy_type='epsilon_decay',
                      policy_param={
                          'eps': 0.0,
                          'eps_decay': 0.9999,
                          'min_eps': 0.002
                      },
                      accelerated=False,
                      Double_DQN_version=1)
        ins.train(10)
        steps, rewards, _, _ = ins.evaluate(100)
        step_array.append(steps)
        reward_array.append(rewards)
    print(
        f'Standard Rewards: {sum(reward_array)/len(reward_array)} Steps:{sum(step_array)/len(step_array)}'
    )
    helper_functions.print_times()
Beispiel #2
0
    def __init__(self, worksheetName, outputDir, timestamp):
        print("Inside init")
        print(worksheetName)
        self.nsr_workbook_name = worksheetName
        print(" %s" % self.nsr_workbook_name)
        self.workbook = xlrd.open_workbook(self.nsr_workbook_name)
        self.outputDir = outputDir
        self.timestamp = timestamp
        ##############
        worksheet = self.workbook.sheet_by_name("Contents")

        num_rows = worksheet.nrows
        num_cols = worksheet.ncols
        initial_header_row = 2
        start_column_num = 1
        last_col_num = 2
        _inputFile_Address_Col = 10

        wrap = wrapper()
        ### Getting list of Object to be processed
        demoList = wrap.fetchingObect(worksheet, num_rows, initial_header_row,
                                      start_column_num, last_col_num)
        #logger.info("Mains-start---"+str(demoList))
        print(demoList)
        _obj_name_ = ""
        for _obj_name_ in demoList[:]:
            if ((_obj_name_ in skipWorksheet)
                    or (self.isMapSheet(_obj_name_) == True)):
                print("Skip %s" % _obj_name_)
            else:
                print("Creating SPML Query for %s" % _obj_name_)
                self.createSPMLGetQuery(_obj_name_, self.timestamp)
Beispiel #3
0
    def start_fs(self, alg_idx, tr_data, tr_ans, ts_data, ts_ans, calg_idx):
        if alg_idx == 0:

            return self.pcc(tr_data, tr_ans, ts_data)
        elif alg_idx == 1:
            return self.pca(tr_data, ts_data)
        elif alg_idx == 2:
            return self._filter(tr_data, ts_data)
        elif alg_idx == 3:
            return wp.wrapper(tr_data, tr_ans, ts_data, ts_ans)
Beispiel #4
0
def form_gov():
    """
    Collects the data from the government form and
    redirects them to the appropriate results page to report
    the final results
    """
    collected_data = []
    form_gov = InputData_gov()
    if request.method == "POST":
        try:
            collected_data.append("Government")
            collected_data.append(request.form.get("state"))
            collected_data.append(request.form.get("location"))
            collected_data.append(request.form.get("land_ava"))
            collected_data.append(request.form.get("goal_renewable"))
            collected_data.append(request.form.get("api_key"))
            # run "build_config.py" to build file for accessing NREL data
            build_hscfg.config_file(collected_data[5])
            # input data to wrapper function
            wtk = h5pyd.File("/nrel/wtk-us.h5", "r")
            if collected_data[4] == '':
                results = wrapper.wrapper(wtk, collected_data[2],
                                          collected_data[1],
                                          float(collected_data[3]))
            else:
                results = wrapper.wrapper(wtk,
                                          collected_data[2],
                                          collected_data[1],
                                          float(collected_data[3]),
                                          goal=int(collected_data[4]))
            return redirect(url_for("results_gov", gov_results=results))
        except IndexError:
            flash("ERROR: Check spelling of 'City/Town' or try a nearby city")
            return render_template("form_gov.html", form_gov=form_gov)
        except OSError:
            flash("ERROR: API key not accepted")
            return render_template("form_gov.html", form_gov=form_gov)
        except ValueError:
            flash("Error: Land available must be a number")
            return render_template("form_gov.html", form_gov=form_gov)
    return render_template("form_gov.html", form_gov=form_gov)
Beispiel #5
0
def wrapper_method(x, y):
	'''
	apply wrapper model to select features
	
	:x : features in transet
	:y : labels in transet
	
	:return : columns' indexes that should be reserved
	'''
	clf = RandomForestClassifier(n_estimators=20)
	wrp = wrapper.wrapper(x, y, clf, ga.ga)
	best_ind = wrp.select_features()
	# print best_ind
	cols_inx = [i for i in range(len(best_ind)) if best_ind[i] == 1]
	return cols_inx
Beispiel #6
0
def wrapper_method(x, y):
    '''
	apply wrapper model to select features
	
	:x : features in transet
	:y : labels in transet
	
	:return : columns' indexes that should be reserved
	'''
    clf = RandomForestClassifier(n_estimators=20)
    wrp = wrapper.wrapper(x, y, clf, ga.ga)
    best_ind = wrp.select_features()
    # print best_ind
    cols_inx = [i for i in range(len(best_ind)) if best_ind[i] == 1]
    return cols_inx
Beispiel #7
0
def run_test():
    random.seed(0)
    np.random.seed(0)
    tf.random.set_seed(0)
    ins = wrapper(batch_size=512,
                  mem_size=5000,
                  policy_type='epsilon_decay',
                  policy_param={
                      'eps': 0.1,
                      'eps_decay': 0.9999,
                      'min_eps': 0.002
                  },
                  accelerated=True,
                  Double_DQN_version=0,
                  optimizer='adadelta')
    ins.train(5)
    helper_functions.reset_times()
    ins.train(10)
    helper_functions.print_integrated_times()
Beispiel #8
0
def compile(funs, sigs = None, debug = False):
  if not isinstance(funs, list):
    return compile([funs], [sigs], debug)

  if len(funs) == 0:
    return None

  if sigs is None:
    sigs = [None] * len(funs)

  (pySources, sigs) = zip(*map(getSource, funs, sigs))
  pySource = "\n\n".join(pySources)

  (funExt, sigExt) = zip(*[(fun, sig) for (fun, sig) in zip(funs, sigs) if sig[0] is Void])
  funNames = [fun.__name__ for fun in funExt]
  debugOut = funNames[0] if len(funNames) > 0 else 'module'
  cudaSource = py2cuda(pySource, sigs, output = debugOut + ".cu" if debug else None)
  cudaCalls = compileCuda(cudaSource, sigExt, ["__call" + f for f in funNames])

  return wrapper(cudaCalls[0], sigExt[0], funNames[0])
Beispiel #9
0
def run_double_DQN_comparison():
    # Repeatable testing of standard training model to compare Double DQN
    for version in range(3):
        step_array = []
        reward_array = []
        for i in range(3):
            ins = wrapper(batch_size=512,
                          policy_type='epsilon_decay',
                          policy_param={
                              'eps': 0.1,
                              'eps_decay': 0.9999,
                              'min_eps': 0.002
                          },
                          accelerated=True,
                          Double_DQN_version=version,
                          plot_frequency=10)
            ins.train(500)
            steps, rewards, _, _ = ins.evaluate(100)
            step_array.append(steps)
            reward_array.append(rewards)
        print(
            f'Version {version} Rewards: {sum(reward_array)/len(reward_array)} Steps:{sum(step_array)/len(step_array)}'
        )
        helper_functions.print_times()
os.makedirs(exp_path, exist_ok=True)

print(f"CAM no distillation MAG Student:{args.s_arch} [{exp_path}]")

logger = SummaryWriter(osp.join(exp_path, 'events'), flush_secs=10)

train_loader, val_loader, n_data = get_cifar100_dataloaders(
    root=args.root,
    batch_size=args.batch_size,
    num_workers=4,
    is_instance=True)
args.n_data = n_data

# student model definition
s_model = model_dict[args.s_arch](num_classes=100)
s_model = wrapper(module=s_model, cfg=args).cuda()

# ----------------  start distillation ! -------------------
print("-------------start distillation ! -------------")

# construct kd loss and optimizer

s_optimizer = optim.SGD(chain(s_model.parameters()),
                        lr=args.lr,
                        momentum=args.momentum,
                        weight_decay=args.weight_decay)
s_scheduler = MultiStepLR(s_optimizer, args.milestones, args.gamma)

best_acc = -1
best_ak_acc = -1
best_dk_acc = -1
Beispiel #11
0
# -*- coding: utf-8 -*-
"""
Created on Fri Jun 13 16:13:58 2014

@author: pablo
"""

import wrapper as s

sim = s.wrapper(file_dir = '/home/pablo/codes/files_test_runs/')
rec = sim.run_simulation(max_limit=10000,learning_set=3000,beginning=127000,
                         running_set = 0.3,mode='platform',learning_rounds_per_unit=30)
import torch.nn.functional as F
from wrapper import wrapper

parser = argparse.ArgumentParser(description='KL and CE analysis on cifar100')
parser.add_argument('--encoder', type=int, nargs='+', default=[64, 256])
parser.add_argument('--t_arch', type=str, required=True)
parser.add_argument('--s_arch', type=str, required=True)
parser.add_argument('--t_weight', type=str, required=True)
parser.add_argument('--s_weight', type=str, required=True)
parser.add_argument('--root', type=str, default='./data')
parser.add_argument('--device', type=str, default='cuda:0')

args = parser.parse_args()

test_transform = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize((0.5071, 0.4867, 0.4408), (0.2675, 0.2565, 0.2761)),
])

test_set = datasets.CIFAR100(root=args.root,
                             download=True,
                             train=False,
                             transform=test_transform)
test_loader = DataLoader(test_set, batch_size=64, shuffle=False, num_workers=4)

# teacher and student model
teacher = model_dict[args.t_arch](num_classes=100).to(args.device)
teacher.load_state_dict(torch.load(args.t_weight))
student = model_dict[args.s_arch](num_classes=100)
student = wrapper(student, args).to(args.device)
student.load_state_dict(torch.load(args.s_weight))
#Y is the second protein
Y = Seq("TWIWDLVVVDSSGVAEVGVRVPDTITEWKAGAFCLSDDTGIGLSLPTTLQAFQPFFVELTMPYSVIRGEAFTLKATVLNYLPNCIRVRVHL", IUPAC.protein)

#z is an int
#it contains the information of the phase you want your proteins in
#it can vary from -len(y) to len(x)
z = 0

#antisens allows you to decide if you want a Same Strand overlap or no
#antisens = False -> same strand overlap
#By default, antisens is True

#q is a float
#it allows you to decide if you want a more fidel X' or Y'
#a high q will get a more fidel Y'
#if you want an equal fidelity, take q = 2.5 (experimental)
q = 1.

(Xprime,Yprime,DNA)=wrapper(X,Y,z,q,antisens=True)

print("X protein")
print(X)
print("X' protein")
print(Xprime)
print("Y protein")
print(Y)
print("Y' protein")
print(Yprime)
print("final DNA")
print(DNA)
Beispiel #14
0
import numpy as np
import random

seed = 1234
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
np.random.seed(seed)
random.seed(seed)

if __name__ == '__main__':
    tokenizer = BertTokenizer.from_pretrained(config.BertPath)
    with open(config.RelPath, 'r', encoding='utf-8') as f:
        rel_list = [line.strip() for line in f.readlines()]
    test_dataloader = CBTDataSet(config.TestPath, tokenizer, rel_list,
                                 config.batch_size)
    model = wrapper(rel_list)
    model_path = config.ModelPath

    if not os.path.exists(model_path):
        train_dataloader = CBTDataSet(config.TrainPath, tokenizer, rel_list,
                                      config.batch_size)
        dev_dataloader = CBTDataSet(config.DevPath, tokenizer, rel_list,
                                    config.batch_size)
        model.train(train_dataloader, dev_dataloader)
        model.save_model(model_path)
    else:
        model.load_model(model_path)

    model.test(test_dataloader)
    data = get_data(config.TestPath)
    model.extract_triple(data, tokenizer, config.ResultPath)
Beispiel #15
0
    lr = args.t_lr

    num_decays = len(t_lr_decay_epochs)
    for decay_i in range(num_decays):
        if epoch >= t_lr_decay_epochs[decay_i]:
            lr = lr * args.t_lr_decay_factor
        #end if epoch
    #end for decay_i
    for param_group in optimizer.param_groups:
        param_group['lr'] = lr

## load teacher
t_model = model_dict[args.t_arch](num_classes=args.num_classes).cuda()
state_dict = torch.load(args.t_path)['model']
t_model.load_state_dict(state_dict)
t_model = wrapper(module=t_model).cuda()

t_optimizer = optim.SGD([{'params':t_model.backbone.parameters(), 'lr':0.0},
                        {'params':t_model.proj_head.parameters(), 'lr':args.t_lr}],
                        momentum=args.momentum, weight_decay=args.weight_decay)
t_model.eval()

## pretrained teacher model's test accuracy
t_acc_record = AverageMeter()
t_loss_record = AverageMeter()
start = time.time()
for x, target in val_loader:

    x = x[:,0,:,:,:].cuda()
    target = target.long().cuda()
    with torch.no_grad():
Beispiel #16
0
    logger.info("input data found for customer " + customer +
                " excel path is " + inputExcel)
    wb = xlrd.open_workbook(inputExcel)
    config.customer = customer

    worksheet = wb.sheet_by_name("Contents")

    num_rows = worksheet.nrows
    num_cols = worksheet.ncols
    initial_header_row = 2
    start_column_num = 1
    last_col_num = 2
    _inputFile_Address_Col = 10

    wrap = wrapper()
    ### Getting list of Object to be processed
    demoList = wrap.fetchingObect(worksheet, num_rows, initial_header_row,
                                  start_column_num, last_col_num)
    logger.info("Mains-start---" + str(demoList))
    try:
        for _obj_name_ in demoList[:]:
            logger.info("config.project_path---" + config.project_path)
            d = config.project_path + "\\log\\" + customer + "\\" + _obj_name_
            if not os.path.exists(d):
                os.makedirs(d)
            logger.info(_obj_name_)
            if (_obj_name_ == "BearerCapabilitySet" or "OCsi" or "TCsi"
                    or "SMSCsi" or "QualityOfServiceProfile" or "UgCsi"
                    or "PDPContext" or "RoamingArea" or "RoamPlan"):
                logger.info(_obj_name_)
Beispiel #17
0
        self.move_cursor_absolute(y - 1, 0)
        self.stdscr.addch(v)
        self.restore_cursor_position()

    def print_char(self, ch):
        self.stdscr.addch(ch)

    def main_loop(self):
        self.draw_help_box(5, 120, 4, 10)
        self.print_to_help_box('q - quit')
        while not self.x_pressed():
            self.k = self.read_char()
            self.print_char(self.k)

    def start(self):
        self.initialize_cursor()
        self.main_loop()


def main(stdscr):
    try:
        editor = Editor(stdscr)
        editor.start()
    except Exception as ex:
        print(ex)
        pass


if __name__ == '__main__':
    my.wrapper(main)
Beispiel #18
0
def compare_accelerated(seed, DoDQN, epochs, done=2):
    """Compare the performance of the accelerated and normal models.
  Trains both models for number of epochs, picks a experience tuple, and prints
  Q values as well as loss function for debug purposes.  Both models should give
  same results."""
    global experience
    helper_functions.set_seed(seed)
    ins = wrapper(
        batch_size=1,
        policy_type='epsilon_decay',
        policy_param={
            'eps': 0,
            'eps_decay': 0.9999,
            'min_eps': 0.002
        },
        accelerated=True,
        Double_DQN_version=DoDQN,
        optimizer='adadelta',
        # games_per_epoch = 1,
        hidden_layers=[100, 100, 100, 100, 100])
    ins.train(epochs)
    if epochs > 0:
        experience = ins.player[0].get_memory_batch(1)
        from_obs, ard, to_obs = zip(*experience)
        action = ard[0][0]
        reward = ard[0][1]
        done = ard[0][2]
        state = np.array(from_obs)
        state2 = np.array(to_obs)
        print(f'Done {done}, Action {action}, Reward {reward}')
    else:
        from_obs, _, _, _ = ins.env.reset()
        to_obs, _, _, _ = ins.env.reset()
        state = np.array([from_obs])
        state2 = np.array([to_obs])
        action = 0
        reward = 2
    print(ins.online_model.predict(state))
    print(ins.online_model.predict(state2))
    # print([state, np.array([[action,reward,done]]), state2], [0])
    loss = ins.training_model.evaluate(
        [state, np.array([[action, reward, done]]), state2], np.zeros(1))
    print(loss)
    helper_functions.set_seed(seed)
    ins = wrapper(
        batch_size=1,
        policy_type='epsilon_decay',
        policy_param={
            'eps': 0,
            'eps_decay': 0.9999,
            'min_eps': 0.002
        },
        accelerated=False,
        Double_DQN_version=DoDQN,
        optimizer='adadelta',
        # games_per_epoch = 1,
        hidden_layers=[100, 100, 100, 100, 100])
    ins.train(epochs)
    print(ins.online_model.predict(state))
    print(ins.online_model.predict(state2))
    from_obs_array = state
    to_obs_array = state2
    alpha = 1
    gamma = 0.9
    if DoDQN != 1:
        online_next_Q_values = np.array(
            ins.online_model.predict_on_batch(to_obs_array))
    if DoDQN > 0:
        target_Q_values = np.array(
            ins.target_model.predict_on_batch(to_obs_array))
    Q_values = np.array(ins.online_model.predict_on_batch(from_obs_array))
    if DoDQN == 2:
        argmax = np.expand_dims(np.argmax(online_next_Q_values, axis=1),
                                axis=-1)
        max_for_next_obs = np.take_along_axis(target_Q_values, argmax, axis=1)
    elif DoDQN == 1:
        max_for_next_obs = np.amax(target_Q_values, axis=1)
    else:
        max_for_next_obs = np.amax(online_next_Q_values, axis=1)

    Q_values[0][action] *= 1 - alpha
    if done:
        Q_values[0][action] = reward
    else:
        calc_action_value = alpha * (reward + gamma * max_for_next_obs[0])
        Q_values[0][action] += calc_action_value
    # print(from_obs_array, Q_values)
    loss = ins.online_model.evaluate(from_obs_array, Q_values)
    print(loss)
Beispiel #19
0
def test_accelerated():
    random.seed(0)
    np.random.seed(0)
    tf.random.set_seed(0)
    ins = wrapper(
        batch_size=64,
        policy_type='epsilon_decay',
        policy_param={
            'eps': 0,
            'eps_decay': 0.9999,
            'min_eps': 0.002
        },
        accelerated=True,
        Double_DQN_version=0,
        optimizer='adadelta',
        # games_per_epoch = 1,
        hidden_layers=[100, 100, 100, 100, 100])
    ins.train(1)
    experience = ins.player[0].get_memory_batch(1)
    from_obs, ard, to_obs = zip(*experience)
    action = ard[0][0]
    reward = ard[0][1]
    done = ard[0][2]
    state = np.array(from_obs)
    state2 = np.array(to_obs)
    # print([state, np.array([[action,reward,done]]), state2], [0])
    loss = ins.training_model.evaluate(
        [state, np.array([[action, reward, done]]), state2], np.zeros(1))
    assert 0.05058867111802101 == loss, "Invalid loss for GPU acceleration"
    ins = wrapper(
        batch_size=64,
        policy_type='epsilon_decay',
        policy_param={
            'eps': 0,
            'eps_decay': 0.9999,
            'min_eps': 0.002
        },
        accelerated=True,
        Double_DQN_version=0,
        optimizer='adadelta',
        # games_per_epoch = 1,
        hidden_layers=[100, 100, 100, 100, 100])
    ins.train(1)
    loss = ins.training_model.evaluate(
        [state, np.array([[action, reward, done]]), state2], np.zeros(1))
    assert 0.09761060774326324 == loss, "Invalid loss for GPU acceleration DDQN 1"
    ins = wrapper(
        batch_size=64,
        policy_type='epsilon_decay',
        policy_param={
            'eps': 0,
            'eps_decay': 0.9999,
            'min_eps': 0.002
        },
        accelerated=True,
        Double_DQN_version=0,
        optimizer='adadelta',
        # games_per_epoch = 1,
        hidden_layers=[100, 100, 100, 100, 100])
    ins.train(1)
    loss = ins.training_model.evaluate(
        [state, np.array([[action, reward, done]]), state2], np.zeros(1))
    assert 0.0347185917198658 == loss, "Invalid loss for GPU acceleration DDQN 2"
    print('Test passed!')
Beispiel #20
0
def form_res():
    """
    Collects the data from the resident form and
    redirects the user to the appropriate page to report
    the final results based off of the provided information
    """
    collected_data = []
    form_res = InputData_res()
    if request.method == "POST":
        try:
            # collect input data to use in functions
            collected_data.append("Resident")
            collected_data.append(request.form.get("state"))
            collected_data.append(request.form.get("location"))
            collected_data.append(request.form.get("household"))
            collected_data.append(request.form.get("energy_bill"))
            collected_data.append(request.form.get("goal_renewable"))
            collected_data.append(request.form.get("api_key"))
            # run "build_config.py" to build file for accessing NREL data
            build_hscfg.config_file(collected_data[6])

            # input data to wrapper function
            wtk = h5pyd.File("/nrel/wtk-us.h5", "r")
            if collected_data[4] == '' and collected_data[5] == '':
                results = wrapper.wrapper(wtk,
                                          collected_data[2],
                                          collected_data[1],
                                          0,
                                          residential=True,
                                          household_size=int(
                                              collected_data[3]))
            elif collected_data[4] == '' and collected_data[5] != '':
                results = wrapper.wrapper(wtk,
                                          collected_data[2],
                                          collected_data[1],
                                          0,
                                          goal=int(collected_data[5]),
                                          residential=True,
                                          household_size=int(
                                              collected_data[3]))
            elif collected_data[5] == '' and collected_data[4] != '':
                results = wrapper.wrapper(wtk,
                                          collected_data[2],
                                          collected_data[1],
                                          0,
                                          residential=True,
                                          energy_bill=float(collected_data[4]),
                                          household_size=int(
                                              collected_data[3]))
            else:
                results = wrapper.wrapper(wtk,
                                          collected_data[2],
                                          collected_data[1],
                                          0,
                                          residential=True,
                                          energy_bill=float(collected_data[4]),
                                          goal=int(collected_data[5]),
                                          household_size=int(
                                              collected_data[3]))
            return redirect(url_for("results_res", res_results=results))
        except IndexError:
            flash("ERROR: Check spelling of 'City/Town' or try a nearby city")
            return render_template("form_res.html", form_res=form_res)
        except OSError:
            flash("ERROR: API key not accepted")
            return render_template("form_res.html", form_res=form_res)
    return render_template("form_res.html", form_res=form_res)
    n1 = n2 = 0
    while n1 == n2:
      n1 = self._rand.getBool()
      n2 = self._rand.getBool()
    self._lastBytes = n1, n2
    if n1 == 1:
      self._incOne()
      return 1
    else:
      self._incZero()
      return 0
  
  def getLastBytes(self):
    return self._lastBytes

def main(screen):
  rand  = Urandom()
  white = Whitening(rand)
  ui = Uicli(screen)

  zeroCount = 0
  oneCount = 0
  while ui.isRunning():
    byte = white.getBool()
    ui.refresh(byte, rand, white)
    sleep(0.05)

if __name__ == "__main__":
  wrapper.wrapper(main)

Beispiel #22
0
print(exp_path)

if args.dataset == 'stl10':
    num_class = 10
    train_loader, val_loader = get_stl_dataloaders(root=args.root, batch_size=args.batch_size, num_workers=4)
elif args.dataset == 'tinyimagenet':
    num_class = 200
    args.batch_size = 128
    args.lr = 2 * args.lr
    train_loader, val_loader = get_imagenet_dataloader(root=args.root, batch_size=args.batch_size, num_workers=4)
else:
    raise NotImplementedError


model = model_dict[args.arch](num_classes=100)
model = wrapper(module=model, cfg=args)
model.load_state_dict(torch.load(args.pretrained_path)['state_dict'])
model = model.backbone
feat_dim = list(model.children())[-1].in_features

try:
    model.fc = nn.Linear(feat_dim, num_class)
    optimizer = optim.SGD(model.fc.parameters(), lr=args.lr, momentum=args.momentum,
                          weight_decay=args.weight_decay)
except:
    try:
        model.linear = nn.Linear(feat_dim, num_class)
        optimizer = optim.SGD(model.linear.parameters(), lr=args.lr, momentum=args.momentum,
                              weight_decay=args.weight_decay)
    except:
        model.classifer = nn.Linear(feat_dim, num_class)
Beispiel #23
0
 def wrapper(self, tr_data, tr_ans, ts_data, ts_ans):
     return wp.wrapper(tr_data, tr_ans, ts_data, ts_ans)
Beispiel #24
0
        while n1 == n2:
            n1 = self._rand.getBool()
            n2 = self._rand.getBool()
        self._lastBytes = n1, n2
        if n1 == 1:
            self._incOne()
            return 1
        else:
            self._incZero()
            return 0

    def getLastBytes(self):
        return self._lastBytes


def main(screen):
    rand = Urandom()
    white = Whitening(rand)
    ui = Uicli(screen)

    zeroCount = 0
    oneCount = 0
    while ui.isRunning():
        byte = white.getBool()
        ui.refresh(byte, rand, white)
        sleep(0.05)


if __name__ == "__main__":
    wrapper.wrapper(main)
Beispiel #25
0
def main():
    # Control de argumentos de línea de comandos:
    if len(sys.argv) != 4:
        print("Uso: {} ficheroEntrada, ficheroSalida, pdfLogFile".format(
            sys.argv[0]))
        sys.exit(0)

    #reading fEntrada
    try:
        f = open(sys.argv[1])
        lista = f.readlines()
    except:
        print("No se encuentra el archivo de Entrada")
        sys.exit(-1)

    #parsing fEntrada, check for incorrect values
    try:
        for number in lista:
            N = int(number)
            if not (0 <= N <= 99999):
                raise ValueError()
    except:
        print("All values must be a int value between 0 and 99999")
        sys.exit(-1)

    #inicializamos marcadores de tiempo
    timeOption1 = []
    timeOption2 = []
    timeOption3 = []

    #Creamos las soluciones
    for i in range(2000, 200000, 2000):
        #Método 1, usando el metodo set de python
        t0_sol1 = time.time_ns()
        sol1 = set(lista[0:i])
        texec_sol1 = (time.time_ns() - t0_sol1) / 1.0e9

        #Método 2, usando diccionarios en python
        aux = 0
        t0_sol2 = time.time_ns()
        sol2 = list(dict.fromkeys(lista[0:i], 0))
        texec_sol2 = (time.time_ns() - t0_sol2) / 1.0e9

        #Método 3, definido en c y llamado a través del wrapper
        listTarget = lista[0:i].copy()
        listInt = [int(i) for i in listTarget]

        #creamos e inicializamos las estructuras de datos a utilizar durante la llamada en c
        salida = (ctypes.c_int * i)()
        entrada = (ctypes.c_int * i)(*listInt)

        t0_sol3 = time.time_ns()
        sol3 = wrapper(i, entrada, salida)
        texec_sol3 = (time.time_ns() - t0_sol3) / 1.0e9

        #Comprobamos que los 3 metodos den la misma solucion
        try:
            if not (len(sol1) == len(sol2) == len(sol3)):
                raise ValueError()
        except:
            print("Los métodos difieren en el resultado")
            sys.exit(-1)

        #Guardamos los datos para esta iteracion, para poder dibujarlos con matplotlib
        timeOption1.append(texec_sol1)
        timeOption2.append(texec_sol2)
        timeOption3.append(texec_sol3)

    #Creamos el archivo solucion
    try:
        name = sys.argv[2]
        file = open(name, "w")

    except:
        print("Can't create file")
        sys.exit(-1)

    for num in sol3:

        file.write(str(num))
        file.write("\n")

    # Guardamos los graficos en el pdf de salida marcado

    plot_values(timeOption1, timeOption2, timeOption3, sys.argv[3])

    show_plot_and_wait_for_key()