def random_attack( sentence, y_true, net, vocab, tokenizer, maxlen, verbose=False, sub_rate_limit=None): doc = nlp(sentence) if sub_rate_limit: sub_rate_limit = int(sub_rate_limit * len(doc)) else: sub_rate_limit = len(doc) def halt_conditionh_func(perturbed_text): perturbed_vector = str2seq(perturbed_text, vocab, tokenizer, maxlen).to(config_device) predict = net.predict_class(perturbed_vector)[0] return predict != y_true candidates_list = [] for idx, token in enumerate(doc): if idx >= maxlen: break candidates = _generate_synonym_candidates(token=token, token_position=idx, rank_fn=None) if len(candidates) > 0: candidates_list.append((idx, candidates)) upper = min(len(candidates_list), sub_rate_limit) lower = upper // 3 sub_num = get_random(lower, upper) sub_pos = random.sample(candidates_list, sub_num) change_tuple_list = [] accepted_candidates = [] for token_pos, candidates in sub_pos: substitution = random.sample(candidates, 1)[0] accepted_candidates.append(substitution) change_tuple_list.append((token_pos, substitution.original_token, substitution.candidate_word, None, 'tagNONE')) perturbed_text = ' '.join( _compile_perturbed_tokens(doc, accepted_candidates)) if halt_conditionh_func(perturbed_text): break if verbose: print(f'origin token pos {token_pos}, origin token {substitution.original_token}, candidate token {substitution.candidate_word}') perturbed_text = ' '.join( _compile_perturbed_tokens(doc, accepted_candidates)) sub_rate = len(change_tuple_list) / len(doc) ne_rate = 0.0 adv_vec = str2seq(perturbed_text, vocab, tokenizer, maxlen).to(config_device) adv_y = net.predict_class(adv_vec)[0] return perturbed_text, adv_y, sub_rate, ne_rate, change_tuple_list
def build_word_index_map(self, vocab): assert len(self.syn_dict) > 0 assert len(vocab.word_dict) > 0 self.syn_index_dict[0] = set() for key, value in self.syn_dict.items(): res = {vocab.get_index(v) for v in value} if get_random(0, 1) == 1: res.add(0) idx = vocab.get_index(key) if idx == 0: self.syn_index_dict[idx] |= res else: self.syn_index_dict[idx] = res unknowns = set(random.sample(self.syn_index_dict.keys(), 10)) unknowns.add(0) self.syn_index_dict[0] |= set(unknowns)
def random_mask(self, X: torch.Tensor, mask_low=1, mask_rate=0.15): limit = int(X.size()[-1] * mask_rate) limit = get_random(mask_low, limit) temp = [i for i in range(X.size()[-1])] count = 0 replaced = set() loop = 0 while count < limit and loop < 5: loop += 1 pos = random.sample(temp, limit - count) for p in pos: if count == limit: break if p in replaced: continue ori_word = X[p].item() res = self.get_syn_words_index(ori_word) if len(res) > 0: s = random.sample(res, 1) X[p] = torch.tensor(s) count += 1 # print(f'{count} sub with {p} {s}') replaced.add(p) flag = True if count > 0 else False return X, flag
# Loop; break the loop with a keystroke while(keep_running): light = T = C = dC = L = Z = 0 T_correction = -5.1 # difference between on-wire and on-board temperature sensors # measure sensors if (full_functional): light = drdaq.get_light() # light intensity [%] T = drdaq.get_ext1() # temperature [degC] #T = max(drdaq.get_ext1(), drdaq.get_temperature()+T_correction) # temperature [degC] #C = fdc2214.read_ch1() # capacitance [pF] #dC = fdc2214.read_ch10() # capacitance diffrence [pF] L = ldc1000.read_inductance() # inductance [uH] Z = ldc1000.read_impedance() # impedance [kOhm] else: light = tools.get_random() T = tools.get_random() C = tools.get_random() dC = tools.get_random() L = tools.get_random() Z = tools.get_random() # encode temperature, inductance and impedance into LED RGB values red = min(255, max(0, math.exp(T * 0.22) - 210)) #green = min(255, math.exp(L * 0.26)) green = 255 * light / 100 #blue = min(255, math.exp(Z * 0.44)) blue = 0 drdaq.set_led(int(red), int(green), int(blue)) # compose string to the console
ssh = paramiko.SSHClient() cmd_to_execute = "python3 /home/robot/motor_movement/angle_example.py" ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(server, username=username, password=password) ''' imax = 30 L_min = math.inf L_max = -math.inf print("Calibrating for " + str(imax*0.1) + " seconds, please wait...") for i in range(imax): # measure sensors if (full_functional): L = ldc1000.read_inductance() # inductance [uH] else: L = tools.get_random() if (L < L_min): L_min = L if (L > L_max): L_max = L time.sleep(0.1) L_0 = (L_max + L_min)/2 L_step = L_max - L_min print("Calibration ready: L_0 = " + format_d(L_0) + ", L_step = " + format_d(L_step)) time.sleep(0.5) print("L[uH], Z[kOHm]") # Loop; break the loop with a keystroke while(keep_running): # measure sensors
while (keep_running): #light = T = C = dC = L = Z = 0 T_correction = -5.1 # difference between on-wire and on-board temperature sensors # measure sensors if (full_functional): #light = drdaq.get_light() # light intensity [%] #T = drdaq.get_ext1() # temperature [degC] #T = max(drdaq.get_ext1(), drdaq.get_temperature()+T_correction) # temperature [degC] C = fdc2214.read_ch0() # capacitance [pF] #dC = fdc2214.read_ch10() # capacitance diffrence [pF] #L = ldc1000.read_inductance() # inductance [uH] #Z = ldc1000.read_impedance() # impedance [kOhm] else: #light = tools.get_random() T = tools.get_random() C = tools.get_random() dC = tools.get_random() #L = tools.get_random() #Z = tools.get_random() # encode temperature, inductance and impedance into LED RGB values red = min(255, max(0, math.exp(T * 0.22) - 210)) #green = min(255, math.exp(L * 0.26)) green = 255 * light / 100 #blue = min(255, math.exp(Z * 0.44)) blue = 0 drdaq.set_led(int(red), int(green), int(blue)) # compose string to the console results = ""