def Mutate(self, instance): ("\t\tinput " + instance.ToString()) instance = inst(copy.deepcopy(instance.val), self.gen.consts) Hoef = np.zeros(self.nActions) for a in range(self.nActions): Hoef[a] = self.empiricalMeans[a] + np.sqrt( (2.0 * np.log(self.n + 1.0)) / self.N[a]) self.lastAction = -1 maxVal = np.NINF for a in range(self.nActions): if Hoef[a] > maxVal: self.lastAction, maxVal = a, Hoef[a] oper = self.ops[self.lastAction] LogPrint("\t\taction = " + oper.name, verbose=3) done = False c = 0 while not done and c < 50: [instance, done] = replace_fixed_op_randomly(instance, self.ops[self.lastAction], self.gen) c += 1 LogPrint("\t\treturn " + instance.ToString(), verbose=3) return instance
def Mutate(self, instance): LogPrint("\t\tinput " + instance.ToString(), verbose=3) instance = inst(copy.deepcopy(instance.val), self.gen.consts) #Sample R = np.zeros([self.nActions, self.k]) for a in range(self.nActions): for i in range(self.k): R[a][i] = np.random.beta(self.alphaBetaPairs[a][0], self.alphaBetaPairs[a][1]) for a in range(self.nActions): sum = 0.0 for i in range(self.k): sum += R[a][i] self.empiricalMeans[a] = 1.0 / self.k * sum self.lastAction = -1 val = np.NINF for a in range(self.nActions): if self.empiricalMeans[a] > val: self.lastAction, val = a, self.empiricalMeans[a] oper = self.ops[self.lastAction] LogPrint("\t\taction = " + oper.name, verbose=3) done = False c = 0 while not done and c < 50: [instance, done] = replace_fixed_op_randomly(instance, self.ops[self.lastAction], self.gen) c += 1 LogPrint("\t\treturn " + instance.ToString(), verbose=3) return instance
def Mutate(self, instance): LogPrint("\t\tinput " + instance.ToString(), verbose=3) instance = inst(copy.deepcopy(instance.val), self.gen.consts) self.lastAction = -1 if np.random.rand(1)[0] < self.epsilon: self.lastAction = np.random.randint(0, self.nActions) else: val = np.NINF for a in range(self.nActions): if self.empiricalMeans[a] > val: self.lastAction, val = a, self.empiricalMeans[a] oper = self.ops[self.lastAction] LogPrint("\t\taction = " + oper.name, verbose=3) done = False c = 0 while not done and c < 50: [instance, done] = replace_fixed_op_randomly(instance, self.ops[self.lastAction], self.gen) c += 1 LogPrint("\t\treturn " + instance.ToString(), verbose=3) return instance
def ReadModel(self): try: with open("models/" + self.name + str(self.id) + ".model", 'r') as file: lines = file.readlines() self.N = [] lines[0] = lines[0].split(" ") while lines[0][-1] == "" or lines[0][-1] == "\n": lines[0].pop() for i in range(len(lines[0])): self.N.append(int(lines[0][i])) self.empiricalMeans = [] lines[1] = lines[1].split(" ") while lines[1][-1] == "" or lines[1][-1] == "\n": lines[1].pop() for i in range(len(lines[1])): self.empiricalMeans.append(float(lines[1][i])) self.n = int(lines[2]) self.nIter = int(lines[3]) except IOError as e: LogPrint("Couldn't open model.", verbose=3)
def WriteModel(self): try: with open("models/" + self.name + str(self.id) + ".model", 'w') as file: file.write(str(self.nIter) + "\n") except IOError as e: LogPrint("Couldn't save file.", verbose=3)
def ReadModel(self): try: with open("models/" + self.name + str(self.id) + ".model", 'r') as file: lines = file.readlines() self.nIter = int(lines[0]) except IOError as e: LogPrint("Couldn't open model.", verbose=3)
def Reward(self, rewardVal): if rewardVal >= 0: self.alphaBetaPairs[self.lastAction][0] += 1 rewardVal = 1.0 else: self.alphaBetaPairs[self.lastAction][1] += 1 rewardVal = 0.0 LogPrint("\t\tempirical means = " + str(self.empiricalMeans), verbose=3) with open("models/" + self.name + str(self.id) + ".rewards", "a") as myfile: myfile.write(str(rewardVal) + "\n") self.nIter += 1
def WriteModel(self): try: with open("models/" + self.name + str(self.id) + ".model", 'w') as file: file.write(str(self.k) + "\n") for i in range(len(self.alphaBetaPairs)): file.write( str(self.alphaBetaPairs[i][0]) + " " + str(self.alphaBetaPairs[i][1]) + " ") file.write("\n") file.write(str(self.nIter) + "\n") except IOError as e: LogPrint("Couldn't save file.", verbose=3)
def WriteModel(self): try: with open("models/" + self.name + str(self.id) + ".model", 'w') as file: for i in range(len(self.N)): file.write(str(self.N[i]) + " ") file.write("\n") for i in range(len(self.empiricalMeans)): file.write(str(self.empiricalMeans[i]) + " ") file.write("\n") file.write(str(self.n) + "\n") file.write(str(self.nIter) + "\n") except IOError as e: LogPrint("Couldn't save file.", verbose=3)
def Reward(self, rewardVal): if rewardVal >= 0: rewardVal = 1.0 else: rewardVal = 0.0 self.empiricalMeans[self.lastAction] = ( self.N[self.lastAction] * self.empiricalMeans[self.lastAction] + rewardVal) / (self.N[self.lastAction] + 1) self.N[self.lastAction] += 1 self.n += 1 LogPrint("\t\tempirical means = " + str(self.empiricalMeans), verbose=3) with open("models/" + self.name + str(self.id) + ".rewards", "a") as myfile: myfile.write(str(rewardVal) + "\n") self.nIter += 1
def ReadModel(self): try: with open("models/" + self.name + str(self.id) + ".model", 'r') as file: lines = file.readlines() self.k = int(lines[0]) self.alphaBetaPairs = [] lines[1] = lines[1].split(" ") while lines[1][-1] == "" or lines[1][-1] == "\n": lines[1].pop() for i in range(self.nActions): self.alphaBetaPairs.append( [int(lines[1][2 * i]), int(lines[1][2 * i + 1])]) self.nIter = int(lines[2]) except IOError as e: LogPrint("Couldn't open model.", verbose=3)
def FuzzerLoop(self): LogPrint("Fuzzer Start") hardnessLog = [] population = [] ret = [] log = open( "tmpdata/run" + self.logName + str(Settings.PythonRandomSeed) + self.mutater.name + ".txt", "w") instanceSet = set() for iter in range(self.nIter): LogPrint("----------------------------------------------------") LogPrint("Starting Generation #" + str(iter)) if iter != 0 and hardnessLog[ -1] >= Settings.SolverTimeout and Settings.FuzzerOverrideTerminationOnMaxScore == False: LogPrint("Achieved expected score, Fuzzing Complete.") while len(hardnessLog) < self.nIter: hardnessLog.append(hardnessLog[-1]) log.write(str(hardnessLog[-1]) + "\n") log.flush() break LogPrint("Solving.") if iter == 0: for i in range(self.startPop): inst = self.gen.gen() while inst.ToString() in instanceSet: inst = self.gen.gen() instanceSet.add(inst.ToString()) population.append(inst) for i in range(self.startPop): for j in range(len(self.solvers)): self.solvers[j].Solve(population[i], self.gen.consts) LogPrint("\t(" + str(i + 1) + "/" + str(self.startPop) + ")\t" + "Score = " + str(population[i].Score()) + "\tTime = " + str(population[i].times) + "\tIsSat = " + str(population[i].stdout)) else: assert len(population) == self.nKeepBest, "error" n = 0 for i in range(self.nKeepBest): LogPrint("\t(" + str(n + 1) + "/" + str(self.nPop) + ")\t Kept Inst\t" + "Score = " + str(population[i].Score()) + "\tTime = " + str(population[i].times) + "\tIsSat = " + str(population[i].stdout)) n += 1 for imut in range(self.nMutations): inst = self.mutater.Mutate(population[i]) mutFail = False if inst.ToString() in instanceSet: mutFail = True while inst.ToString() in instanceSet: inst = self.gen.gen() LogPrint("\t Mutation Failed.") instanceSet.add(inst.ToString()) population.append(inst) for k in range(len(self.solvers)): self.solvers[k].Solve(population[-1], self.gen.consts) if Settings.BanditTrainingMode and not mutFail: self.mutater.Reward(population[0].Score() - population[imut].Score()) LogPrint("\t(" + str(n + 1) + "/" + str(self.nPop) + ")\t Mutated Inst\t" + "Score = " + str(population[-1].Score()) + "\tTime = " + str(population[-1].times) + "\tIsSat = " + str(population[-1].stdout)) n += 1 if Settings.BanditTrainingMode: if self.mutater.nIter >= Settings.BanditNumberTrainingIterations: sys.exit(1) for i in range(self.nRandom): inst = self.mutater.Mutate(population[i]) if inst.ToString() in instanceSet: while inst.ToString() in instanceSet: inst = self.gen.gen() population.append(inst) for j in range(len(self.solvers)): self.solvers[j].Solve(population[-1], self.gen.consts) LogPrint("\t(" + str(n + 1) + "/" + str(self.nPop) + ")\tRand Inst\t" + "Score = " + str(population[-1].Score()) + "\tTime = " + str(population[-1].times) + "\tIsSat = " + str(population[-1].stdout)) n += 1 self.mutater.WriteModel() population.sort() hardestSum = 0.0 LogPrint( "Testing Finished, analyzing and creating next generation.") ret = [] for i in range(self.nKeepBest): hardestSum += population[-1 - i].Score() ret.append(population[-1 - i]) LogPrint(" Hardest Average = " + str(hardestSum / self.nKeepBest)) hardnessLog.append(hardestSum / self.nKeepBest) log.write(str(hardestSum / self.nKeepBest) + "\n") log.flush() population = ret population[0].ToFile("tmpdata/final/" + "run" + self.logName + str(Settings.PythonRandomSeed) + self.mutater.name) self.mutater.WriteModel() if len(hardnessLog) == self.nIter: LogPrint("Ranout of iterations.") return ret
if args.e1 == None: if "CIFAR" in args.dataset: args.e1 = pretrained_be_path["CIFAR10"] elif args.dataset == "MNIST": key = "MNIST" + args.which_lenet args.e1 = pretrained_be_path[key] args.e1 = check_path(args.e1) args.e2 = check_path(args.e2) args.pretrained_dir = check_path(args.pretrained_dir) args.adv_train = int(args.mode[-1]) num_channel = 1 if args.dataset == "MNIST" else 3 # Set up directories and logs, etc. TimeID, ExpID, rec_img_path, weights_path, log = set_up_dir( args.project_name, args.resume, args.CodeID) logprint = LogPrint(log) args.ExpID = ExpID if __name__ == "__main__": # Set up model AE = AutoEncoders[args.mode] ae = AE(args).cuda() # Set up exponential moving average ema_dec = [] ema_se = [] for di in range(1, args.num_dec + 1): ema_dec.append(EMA(args.ema_factor)) dec = eval("ae.d%s" % di) for name, param in dec.named_parameters(): if param.requires_grad: