def simulate_pulses(voltage, pulses, output_to_csv=False): full_cycle_pulses = [x for x in range(1, pulses + 1)] device = memristor.Memristor() resistance = [] # Perform the experiment. for _ in full_cycle_pulses: device.apply_voltage(voltage) resistance.append(device.read_resistance()) # Store the data into a data frame. data = pd.DataFrame() data["voltage"] = [voltage] * len(full_cycle_pulses) data["resistance"] = resistance data["pulse_number"] = full_cycle_pulses if output_to_csv: utility.save_data(data, "./output", "simulate-pulses") # Plot the results. ax = plt.subplots(figsize=(10, 7))[1] data.plot(x="pulse_number", y="resistance", ax=ax) plt.legend(data["voltage"].unique(), title="Voltage (V)") plt.xlabel("Pulse number") plt.ylabel("Resistance (Ω)") plt.show()
def main(): revision = 1 print("Loading the classifier") classifier = utility.load_model("train_rtext_rev{}".format(revision)) print("Reading in the training data") train = utility.load_data("training", "rtext") print("Predicting the rest of the training data") pred = np.ravel(classifier.predict(list(train['rtext_bcat']))) score = utility.rmsle_log(pred, train['votes_useful_log']) print "Score:", score print("Writing out new training data") del train['rtext_bcat'] train['votes_useful_log_rtextpred_sgd'] = pd.Series(pred, index=train.index) utility.save_data(train, "training", "rtext_sgd_rev{}".format(revision)) print("Reading in the test data") test = utility.load_data("test", "rtext") tepred = np.ravel(classifier.predict(list(test['rtext_bcat']))) print("Writing out new test data") del test['rtext_bcat'] test['votes_useful_log_rtextpred_sgd'] = pd.Series(tepred, index=test.index) utility.save_data(test, "test", "rtext_sgd_rev{}".format(revision)) test['votes'] = pd.Series(np.exp(tepred) + 1, index=test.index) print("Writing out a new submission file") utility.write_submission(test, "rtextsgd_sub_rev{}".format(revision))
def do_scrape( download_dir="download", download_processes_num=8, except_log_file_name="request_exceptions_discoverireland.log" ): global img_dir global processes_num processes_num = download_processes_num img_dir = site_subdir_creator(img_dir)(download_dir) except_log_file = open(except_log_file_name, "w") start_time = time.time() discoverireland_data = process_site_layers(site_layers_description_list, except_log_file) print "discoverireland.ie scrapping time: ", str(time.time() - start_time) save_data(discoverireland_data, "discoverireland.dat") except_log_file.close() discoverireland_data = get_saved_data("discoverireland.dat") to_csv(discoverireland_data)
def main(): revision = 4 print("Loading the classifier") classifier = utility.load_model("train_rtext_rev{}".format(revision)) print("Reading in the training data") train = utility.load_data("training", "rtext") print("Predicting the rest of the training data") bunch = 50000 pred = np.zeros(len(train)) for ibunch in range(int(len(train) / bunch)) : beg = ibunch * bunch end = (ibunch + 1) * 50000 mtrain = train.ix[beg:end - 1] mpred = np.ravel(classifier.predict(list(mtrain['rtext_bcat']))) pred[beg:end] = mpred beg = int(len(train) / bunch) * bunch mtrain = train.ix[beg:] mpred = np.ravel(classifier.predict(list(mtrain['rtext_bcat']))) pred[beg:] = mpred score = utility.rmsle_log(pred, train['votes_useful_log']) print "Score:", score print("Writing out new training data") del train['rtext_bcat'] train['votes_useful_log_rtextpred'] = pd.Series(pred, index=train.index) utility.save_data(train, "training", "rtext_rev{}".format(revision)) print("Reading in the test data") test = utility.load_data("test", "rtext") tepred = np.ravel(classifier.predict(list(test['rtext_bcat']))) print("Writing out new test data") del test['rtext_bcat'] test['votes_useful_log_rtextpred'] = pd.Series(tepred, index=test.index) utility.save_data(test, "test", "rtext_rev{}".format(revision)) test['votes'] = pd.Series(np.exp(tepred) + 1, index=test.index) print("Writing out a new submission file") utility.write_submission(test, "rtextrf_sub_rev{}.csv".format(revision))
def simulate_pulsing_experiment(output_to_csv=False): half_cycle_pulses = 10 full_cycle_pulses = [x for x in range(1, half_cycle_pulses * 2 + 1)] positive_bias = [x * 0.1 for x in range(10, 0, -1)] negative_bias = -4 lrs_set_voltage = 1 device = memristor.Memristor() resistance = [] # Perform the experiment. for voltage in positive_bias: device.set_resistance(lrs_set_voltage) for _ in full_cycle_pulses[1: half_cycle_pulses + 1]: device.apply_voltage(negative_bias) resistance.append(device.read_resistance()) for _ in full_cycle_pulses[half_cycle_pulses:]: device.apply_voltage(voltage) resistance.append(device.read_resistance()) # Store the data into a data frame. data = pd.DataFrame() data["positive_bias"] = (positive_bias * len(full_cycle_pulses)) data.sort_values(by="positive_bias", ascending=False, inplace=True) data.reset_index(inplace=True, drop=True) data["resistance"] = resistance data["pulse_number"] = full_cycle_pulses * len(positive_bias) if output_to_csv: utility.save_data(data, "./output", "simulate-pulsing-experiment") # Plot the results. ax = plt.subplots(figsize=(10, 7))[1] data.groupby(["positive_bias"]).plot(x="pulse_number", y="resistance", ax=ax) plt.legend(data["positive_bias"].unique()[::-1], title="Positive bias (V)") plt.xlabel("Pulse number") plt.ylabel("Resistance (Ω)") plt.show()
def main(): trabus = utility.load_data("training", "business") tesbus = utility.load_data("test", "business") bus = pd.concat((trabus, tesbus)) for cat in delbuscats : if hasattr(bus, cat) : del bus[cat] bus['procbcat'] = pd.Series(map(process_bcat, bus['categories']), bus.index) del bus['categories'] for s in ["training", "test"] : rev = utility.load_data(s, "review") for cat in delrevcats : if hasattr(rev, cat) : del rev[cat] if hasattr(rev, 'votes_useful') : rev['votes_useful_log'] = np.log(rev.votes_useful + 1) rev = pd.merge(rev, bus, 'inner') rev['rtext_bcat'] = rev['text'] + rev['procbcat'] del rev['procbcat'] del rev['text'] utility.save_data(rev, s, 'rtext')
def save(self): utility.save_data("insults", self.insults) utility.save_data("insult_votes", self.votes)
def save_games(self): utility.save_data("games", self.games)
def mask_save(self): utility.save_data("urlmasks", self.url_masks)
def save(self): utility.save_data("insults", self.insults)
def main(): usr = pd.concat((utility.load_data('training', 'user'), utility.load_data('test', 'user'))) for cat in usrdelcats : if hasattr(usr, cat) : del usr[cat] usr = usr.rename(columns={'average_stars' : 'user_average_stars', 'review_count' : 'user_review_count'}) bus = pd.concat((utility.load_data('training', 'business'), utility.load_data('test', 'business'))) for cat in busdelcats : if hasattr(bus, cat) : del bus[cat] bus = bus.rename(columns={'stars' : 'business_average_stars', 'review_count' : 'business_review_count',}) rtxttag = 'rtext_rev{}'.format(rtext_rev) rtxt_tr = utility.load_data('training', rtxttag) rtxt_te = utility.load_data('test', rtxttag) rtxt_te.index = rtxt_te.index + len(rtxt_tr) rtxt = pd.concat((rtxt_tr, rtxt_te)) sgdtag = 'rtext_sgd_rev{}'.format(rtext_sgd_rev) sgdtxt_tr = utility.load_data('training', sgdtag) sgdtxt_te = utility.load_data('test', sgdtag) sgdtxt_te.index = sgdtxt_te.index + len(sgdtxt_tr) sgdtxt = pd.concat((sgdtxt_tr, sgdtxt_te)) tesrev = utility.load_data('test', 'review') trarev = utility.load_data('training', 'review') revlist = [trarev, tesrev] for i in range(len(revlist)) : revlength = revlist[i]['text'].apply(lambda t : len(t.split())) revlist[i]['review_length'] = revlength for col in revlist[i].columns : if not col in ['review_id', 'stars', 'date', 'review_length'] : del revlist[i][col] revlist[i] = revlist[i].rename(columns={'stars' : 'review_stars'}) revlist[i] = pd.merge(revlist[i], rtxt, 'left') revlist[i] = pd.merge(revlist[i], sgdtxt, 'left') revlist[i] = pd.merge(revlist[i], usr, 'left') revlist[i] = pd.merge(revlist[i], bus, 'left') revlist[i] = revlist[i].fillna(-1) for c in normedcols : norm_col(revlist, c) dates = [pd.to_datetime('2013-01-19'), pd.to_datetime('2013-03-12')] for i in range(len(revlist)) : ddiff = dates[i] - revlist[i]['date'] ddiff = ddiff.apply(lambda x: x / np.timedelta64(1, 'D')) revlist[i]['datediff'] = ddiff norm_col(revlist, 'datediff') for i in range(len(revlist)) : for c in delcols : if hasattr(revlist[i], c) : del revlist[i][c] utility.save_data(revlist[0], 'training', 'finalinput') utility.save_data(revlist[1], 'test', 'finalinput')
def set_location(self, value): self.location = value utility.save_data('festern_bbq', self.location)
global img_dir global processes_num processes_num = download_processes_num img_dir = site_subdir_creator(img_dir)(download_dir) except_log_file = open(except_log_file_name, "w") start_time = time.time() discoverireland_data = process_site_layers(site_layers_description_list, except_log_file) print "discoverireland.ie scrapping time: ", str(time.time() - start_time) save_data(discoverireland_data, "discoverireland.dat") except_log_file.close() discoverireland_data = get_saved_data("discoverireland.dat") to_csv(discoverireland_data) if __name__ == "__main__": create_dirs(img_dir) exceptions_log_file = open("discoverireland_exceptions.log", "w") start_time = time.time() discoverireland_data = process_site_layers(site_layers_description_list, exceptions_log_file) print "discoverireland.ie scrapping time: ", str(time.time() - start_time) save_data(discoverireland_data, "discoverireland.dat") exceptions_log_file.close() discoverireland_data = get_saved_data("discoverireland.dat") to_csv(discoverireland_data) # exceptions_log_file = open('discoverireland_exceptions.log', 'w') # first_layer_processor(site_layers_description_list[0], exceptions_log_file) # exceptions_log_file.close()
def set_notes(self, nick, text): self.notebook[nick] = text utility.save_data('notes', self.notebook)
def save(self): utility.save_data("rss_watch_list", self.watch_list)
def solve_xor_snn(learning_pulses, epochs, learn_hidden_synapses=False, output_to_csv=False): hrs_set_voltage = -4 positive_bias = 1 fixing_pulses = 37 * 10 ** 4 current_application_time = 500 spike = 1 true = 20 false = 0 training_voltage = 1 input_spikes_1 = [false, false, true, true] input_spikes_2 = [false, true, false, true] targets = [false, true, true, false] # Create a network with 2 input neurons, 2 hidden layer neurons # and 1 output neuron. The input neurons are simulated by their spikes. hidden_neuron_1 = spiking_neuron.SpikingNeuron() hidden_neuron_2 = spiking_neuron.SpikingNeuron() output_neuron = spiking_neuron.SpikingNeuron() # Create 6 synapses with input_synapse_ij representing the connection # between the input neuron i and the hidden neuron j, and hidden_synapse_k # representing the connection between the hidden neuron k and the output neuron. # The inhibitory synapse is hidden_synapse_2. input_synapse_11 = memristor.Memristor() input_synapse_12 = memristor.Memristor() input_synapse_21 = memristor.Memristor() input_synapse_22 = memristor.Memristor() hidden_synapse_1 = memristor.Memristor() hidden_synapse_2 = memristor.Memristor() # Set all the synapses to a high resistance state. input_synapse_11.set_resistance(hrs_set_voltage) input_synapse_12.set_resistance(hrs_set_voltage) input_synapse_21.set_resistance(hrs_set_voltage) input_synapse_22.set_resistance(hrs_set_voltage) hidden_synapse_1.set_resistance(hrs_set_voltage) hidden_synapse_2.set_resistance(hrs_set_voltage) # If the resistance values at the hidden synapses are not going # to be learned, then fix them by applying consecutive positive # bias voltage pulses. if not learn_hidden_synapses: for _ in range(fixing_pulses): hidden_synapse_1.apply_voltage(positive_bias) hidden_synapse_2.apply_voltage(positive_bias) c_11 = [] c_12 = [] c_21 = [] c_22 = [] hidden_spikes_1 = [] hidden_spikes_2 = [] z_1 = [] z_2 = [] output_spikes = [] errors = [] squared_errors = [] epoch_numbers = [] for epoch_number in range(1, epochs + 1): for spikes_1, spikes_2, target in zip(input_spikes_1, input_spikes_2, targets): epoch_numbers.append(epoch_number) # The normalized current c_ij passes through the input_synapse_ij. # The input voltage is 1 V if the input neuron fired as many pulses # as the encoding of TRUE or more, else it is 0 V. c_11.append(utility.normalize(utility.burst(spikes_1, true) / input_synapse_11.read_resistance())) c_12.append(utility.normalize(utility.burst(spikes_1, true) / input_synapse_12.read_resistance())) c_21.append(utility.normalize(utility.burst(spikes_2, true) / input_synapse_21.read_resistance())) c_22.append(utility.normalize(utility.burst(spikes_2, true) / input_synapse_22.read_resistance())) hidden_spikes_1.append(hidden_neuron_1.apply_current(c_11[-1] + c_21[-1], current_application_time)[0].count(spike)) hidden_spikes_2.append(hidden_neuron_2.apply_current(c_12[-1] + c_22[-1], current_application_time)[0].count(spike)) # The normalized current z_i passes through the hidden_synapse_i. # The current in the inhibitory synapse is inverted. The input # voltage is 1 V if the hidden neuron fired as many pulses as # the encoding of TRUE or more, else it is 0 V. z_1.append(utility.normalize(utility.burst(hidden_spikes_1[-1], true) / hidden_synapse_1.read_resistance())) z_2.append(utility.invert(utility.normalize(utility.burst(hidden_spikes_2[-1], true) / hidden_synapse_2.read_resistance()))) output_spikes.append(output_neuron.apply_current(z_1[-1] + z_2[-1], current_application_time)[0].count(spike)) errors.append(target - output_spikes[-1]) squared_errors.append(errors[-1] ** 2) # Update the synapses based on the error. # If the output neuron should have fired more times and it did not, # then strengthen hidden synapse 1 if hidden neuron 1 fired as many # pulses as the encoding of TRUE or more and if the resistance values # at the hidden synapses are to be learned. Otherwise strengthen the # input synapses of the input neurons that fired as many pulses as the # encoding of TRUE or more and that are connected to hidden neuron 1. if utility.is_false_negative(errors[-1]): if utility.burst(hidden_spikes_1[-1], true) and learn_hidden_synapses: for _ in range(learning_pulses): hidden_synapse_1.apply_voltage(training_voltage) else: if utility.burst(spikes_1, true): for _ in range(learning_pulses): input_synapse_11.apply_voltage(training_voltage) if utility.burst(spikes_2, true): for _ in range(learning_pulses): input_synapse_21.apply_voltage(training_voltage) # If the output neuron should not have fired and it did, then do the # equivalent update for hidden synapse 2 or for the input synapses that # are connected to hidden neuron 2. elif utility.is_false_positive(errors[-1]): if utility.burst(hidden_spikes_2[-1], true) and learn_hidden_synapses: for _ in range(learning_pulses): hidden_synapse_2.apply_voltage(training_voltage) else: if utility.burst(spikes_1, true): for _ in range(learning_pulses): input_synapse_12.apply_voltage(training_voltage) if utility.burst(spikes_2, true): for _ in range(learning_pulses): input_synapse_22.apply_voltage(training_voltage) # The neurons are resting until the next input. hidden_neuron_1.rest() hidden_neuron_2.rest() output_neuron.rest() # Store the data into a data frame. data = pd.DataFrame() data["input_spikes_1"] = input_spikes_1 * epochs data["input_spikes_2"] = input_spikes_2 * epochs data["c_11"] = c_11 data["c_12"] = c_12 data["c_21"] = c_21 data["c_22"] = c_22 data["hidden_spikes_1"] = hidden_spikes_1 data["hidden_spikes_2"] = hidden_spikes_2 data["z_1"] = z_1 data["z_2"] = z_2 data["output_spikes"] = output_spikes data["target"] = targets * epochs data["error"] = errors data["squared_error"] = squared_errors data["epoch"] = epoch_numbers data["learning_pulses"] = [learning_pulses] * epochs * 4 data["training_voltage"] = [training_voltage] * epochs * 4 data["learn_hidden_synapses"] = [learn_hidden_synapses] * epochs * 4 data["current_application_time"] = [current_application_time] * epochs * 4 if output_to_csv: utility.save_data(data, "./output", "solve-xor-snn") # Plot the MSE as a function of epoch. utility.plot_mse(data)
def measure_IP1(SMB_RF=SMB_RF, NRP2=NRP2, SAB=SAB, synthetizer_state=synthetizer_state, synthetizer_frequency=synthetizer_frequency, synthetizer_fix_power=synthetizer_fix_power, # dBm synthetizer_level=synthetizer_level, # dBm power_meter_make_zero=power_meter_make_zero, power_meter_make_zero_delay=power_meter_make_zero_delay, power_meter_misure_number=power_meter_misure_number, power_meter_misure_delay=power_meter_misure_delay, # ,milliseconds SAB_state=SAB_state, SAB_attenuation_level=SAB_attenuation_level, SAB_attenuation_delay=SAB_attenuation_delay, SAB_switch_01_delay=SAB_switch_01_delay, SAB_switch_02_delay=SAB_switch_02_delay, calibration_cable_file_name=calibration_cable_file_name, result_file_name=result_file_name, createprogressdialog=None ): dialog = createprogressdialog values = [] # open calibration file calibration_LO = readcalibrationfile(calibration_cable_file_name) calibration_function_LO, calibration_function_LO_unit = calibrationfilefunction(calibration_LO) # reset the synthetizer SMB100A SMB_RF.write("*rst") # imposta la modalita'� di funzionamento del SMB100A SMB_RF.write("FREQ:MODE FIX") SMB_RF.write("POW:MODE FIX") if str(SAB_state) == "OFF": SAB_attenuation_level_min = 0 SAB_attenuation_level_max = 0 SAB_attenuation_level_step = 1 # synthetizer_frequency_min = unit.convertion_to_base(synthetizer_frequency_min, synthetizer_frequency_unit) frequency_LO_range = synthetizer_frequency.return_range() level_LO_range = synthetizer_level.return_arange() attenuation_range = SAB_attenuation_level.return_arange() maxcount = len(frequency_LO_range) * len(level_LO_range) for s in attenuation_range: count = 0 if SAB_state == True: # set stepattenuator value SAB.write("ATT " + str(s)) # print("Attenuation {att} dB".format(att = str(s))) time.sleep(SAB_attenuation_delay) s_value = str(s) else: s_value = "OFF" for f in frequency_LO_range: # set SMB100A frequency if synthetizer_state == True: f_value = str(f) current_frequency = f_value + unit.return_unit_str(unit.Hz) current_frequency_human_readable = unit.return_human_readable_str(f) command = "FREQ " + current_frequency # Ex. FREQ 500kHz SMB_RF.write(command) for l in level_LO_range: current_LO_level, calibration_LO_result = calibrate(l, f, unit.Hz, calibration_LO, calibration_function=calibration_function_LO, calibration_function_unit=calibration_function_LO_unit) # set SMB100A level # current_level = str(l) current_level = str(current_LO_level) command = "POW " + current_level SMB_RF.write(command) # turn on RF if f == frequency_LO_range[0]: SMB_RF.write("OUTP ON") values.append([f_value, str(l), current_level, calibration_LO_result, s_value] + readNRP2(SAB, NRP2, power_meter_misure_number, power_meter_misure_delay, f, unit.Hz, SAB_switch_01_delay, make_zero=True)) count += 1 if not createprogressdialog is None: import wx wx.MicroSleep(500) message = "RF {lo_freq} {lo_pow}dB".format(lo_freq=current_frequency_human_readable, lo_pow=current_level) newvalue = min([int(count / maxcount * 100), 100]) if newvalue == 100: createprogressdialog = False # dialog.Update(newvalue, message) # wx.MicroSleep(500) dialog.Close() else: dialog.Update(newvalue, message) if f == frequency_LO_range[-1] and l == level_LO_range[-1] and s == attenuation_range[-1]: # safety turn Off # dialog.Update(100, "Measure completed) dialog.Destroy() else: l = 0 f_value = "OFF" current_level = "OFF" calibration_LO_result = "OFF" values.append([f_value, str(l), current_level, calibration_LO_result, s_value] + readNRP2(SAB, NRP2, power_meter_misure_number, power_meter_misure_delay, f, unit.Hz, SAB_switch_01_delay, make_zero=True)) # if stepattenuator_state == "ON": # #create step attenuator object # STA.closeport() # turn off RF SMB_RF.write("OUTP OFF") print("Misure completed\n") save_data("txt", result_file_name, values, power_meter_misure_number, unit.return_unit_str(unit.Hz)) save_data("csv", result_file_name, values, power_meter_misure_number, unit.return_unit_str(unit.Hz)) return result_file_name
def save(self): utility.save_data("places", self.places)
stats4 = player_data.find_all("div", {"class": "image-container-item"}) player_items = [stat_item.find("a").get('href').split("/")[2] for stat_item in stats4] temp_player_dict["items"] = player_items players_list.append(temp_player_dict) else: print("Not a known player for the match...") if match_counter >= 3: print(" {} People in party ".format(str(match_counter))) match_dict[match] = players_list # Not to get banned time.sleep(2) extraction_data["matches"] = match_dict return extraction_data else: return #print(main_process()) # Main execution if __name__ == "__main__": #print(main_process()) #Extract player data player_infos = player_info() # Save it as pickle utility.save_data("player_data", player_infos) #For loading the player infos #utility.load_data("player_data")
def save(self): utility.save_data("compliments", self.compliments)
# S = Society(delta=delta, beta=beta, tau=0.1, **config["society"]) mc = MCMC(system=S, **config["mcmc"]) r = mc.sample() # print("accptance ratio at {} was {}".format( # (tau,beta,delta), # (1 - mc.rejected/(S.N*mc.max_sweeps)) # )) return idx, (tau, delta, beta), r # result = map(run,args[:3]) pool = mp.Pool() t0 = time.time() print(40 * "=") print("Passed:", json.dumps(config, indent=4), sep="\n") print("Starting at: ", time.asctime()) result = pool.map(run, args) pool.close() pool.join() result.sort() t = time.time() print("pool.map(run, args) took %s" % (timedelta(seconds=(t - t0)))) t0_save = time.time() save_data(result, config) t_save = time.time() print("save_data took %s" % (timedelta(seconds=(t_save - t0_save)))) t_final = time.time() print("Total time spent: %s" % (timedelta(seconds=(t_save - t0)))) print("Finished at: ", time.asctime()) print(40 * "=")
def save(self): utility.save_data("postnr_addresses", self.places)
def save_urls(self): utility.save_data("urls", self.url_lists)
def save_refs(self): utility.save_data("spotify", self.references)
def solve_xor(learning_pulses, epochs, learn_hidden_synapses=False, output_to_csv=False): lrs_set_voltage = 1 hrs_set_voltage = -4 spike = 1 false_negative = 1 false_positive = -1 training_voltage = 1 input_spikes_1 = [0, 0, 1, 1] input_spikes_2 = [0, 1, 0, 1] targets = [0, 1, 1, 0] # Create a network with 2 input neurons, 2 hidden layer neurons # and 1 output neuron. The input neurons are simulated by their spikes. hidden_neuron_1 = neuron.Neuron() hidden_neuron_2 = neuron.Neuron() output_neuron = neuron.Neuron() # Create 6 synapses with input_synapse_ij representing the connection # between the input neuron i and the hidden neuron j, and hidden_synapse_k # representing the connection between the hidden neuron k and the output neuron. # The inhibitory synapse is hidden_synapse_2. input_synapse_11 = memristor.Memristor() input_synapse_12 = memristor.Memristor() input_synapse_21 = memristor.Memristor() input_synapse_22 = memristor.Memristor() hidden_synapse_1 = memristor.Memristor() hidden_synapse_2 = memristor.Memristor() # Set all the input synapses to a high resistance state. input_synapse_11.set_resistance(hrs_set_voltage) input_synapse_12.set_resistance(hrs_set_voltage) input_synapse_21.set_resistance(hrs_set_voltage) input_synapse_22.set_resistance(hrs_set_voltage) # If the resistance values at the hidden synapses are going to be learned, # then set all the hidden synapses to a high resistance state. Otherwise set # all the hidden synapses to a low resistance state. if learn_hidden_synapses: hidden_synapse_1.set_resistance(hrs_set_voltage) hidden_synapse_2.set_resistance(hrs_set_voltage) else: hidden_synapse_1.set_resistance(lrs_set_voltage) hidden_synapse_2.set_resistance(lrs_set_voltage) c_11 = [] c_12 = [] c_21 = [] c_22 = [] hidden_spikes_1 = [] hidden_spikes_2 = [] z_1 = [] z_2 = [] output_spikes = [] errors = [] squared_errors = [] epoch_numbers = [] for epoch_number in range(1, epochs + 1): for spike_1, spike_2, target in zip(input_spikes_1, input_spikes_2, targets): epoch_numbers.append(epoch_number) # The current c_ij passes through the input_synapse_ij. # The input voltage is 1 V if the input neuron fired, else it is 0 V. c_11.append(spike_1 / input_synapse_11.read_resistance()) c_12.append(spike_1 / input_synapse_12.read_resistance()) c_21.append(spike_2 / input_synapse_21.read_resistance()) c_22.append(spike_2 / input_synapse_22.read_resistance()) hidden_spikes_1.append( hidden_neuron_1.apply_current(c_11[-1] + c_21[-1])) hidden_spikes_2.append( hidden_neuron_2.apply_current(c_12[-1] + c_22[-1])) # The curent z_i passes through the hidden_synapse_i. # The current in the inhibitory synapse is inverted. # The input voltage is 1 V if the hidden neuron # fired, else it is 0 V. z_1.append(hidden_spikes_1[-1] / hidden_synapse_1.read_resistance()) z_2.append( utility.invert(hidden_spikes_2[-1] / hidden_synapse_2.read_resistance())) output_spikes.append(output_neuron.apply_current(z_1[-1] + z_2[-1])) errors.append(target - output_spikes[-1]) squared_errors.append(errors[-1]**2) # Update the synapses based on the error. # If the output neuron should have fired and it did not, # then strengthen hidden synapse 1 if hidden neuron 1 fired # and if the resistance values at the hidden synapses are to be learned. # Otherwise strengthen the input synapses of the input neurons that fired # and that are connected to hidden neuron 1. if errors[-1] == false_negative: if hidden_spikes_1[-1] == spike and learn_hidden_synapses: for _ in range(learning_pulses): hidden_synapse_1.apply_voltage(training_voltage) else: if spike_1 == spike: for _ in range(learning_pulses): input_synapse_11.apply_voltage(training_voltage) if spike_2 == spike: for _ in range(learning_pulses): input_synapse_21.apply_voltage(training_voltage) # If the output neuron should not have fired and it did, # then do the equivalent update for hidden synapse 2 or # for the input synapses that are connected to hidden neuron 2. elif errors[-1] == false_positive: if hidden_spikes_2[-1] == spike and learn_hidden_synapses: for _ in range(learning_pulses): hidden_synapse_2.apply_voltage(training_voltage) else: if spike_1 == spike: for _ in range(learning_pulses): input_synapse_12.apply_voltage(training_voltage) if spike_2 == spike: for _ in range(learning_pulses): input_synapse_22.apply_voltage(training_voltage) # Store the data into a data frame. data = pd.DataFrame() data["input_spike_1"] = input_spikes_1 * epochs data["input_spike_2"] = input_spikes_2 * epochs data["c_11"] = c_11 data["c_12"] = c_12 data["c_21"] = c_21 data["c_22"] = c_22 data["hidden_spike_1"] = hidden_spikes_1 data["hidden_spike_2"] = hidden_spikes_2 data["z_1"] = z_1 data["z_2"] = z_2 data["output_spike"] = output_spikes data["target"] = targets * epochs data["error"] = errors data["squared_error"] = squared_errors data["epoch"] = epoch_numbers data["learning_pulses"] = [learning_pulses] * epochs * 4 data["training_voltage"] = [training_voltage] * epochs * 4 data["learn_hidden_synapses"] = [learn_hidden_synapses] * epochs * 4 if output_to_csv: utility.save_data(data, "./output", "solve-xor") # Plot the MSE as a function of epoch. utility.plot_mse(data)
def save_urls(self): utility.save_data("urls", self.url_list)
def save(self): utility.save_data("favorites", self.favorites)
def save(self): utility.save_data("reminders", self.reminders)
def save(self): utility.save_data('schema_id', self.id_directory) utility.save_data('schema_fav', self.id_presets)
def solve_xor_complex_noisy_snn(learning_pulses, epochs, output_to_csv=False): hrs_set_voltage = -4 current_application_time = 500 spike = 1 training_voltage = 1 true = 20 false = 0 input_spikes_1 = [false, false, true, true] input_spikes_2 = [false, true, false, true] targets = [false, true, true, false] # Create a network with 2 input neurons, 2 hidden layer neurons # and 1 output neuron. The input neurons are simulated by their spikes. hidden_neuron_1 = spiking_neuron.SpikingNeuron() hidden_neuron_2 = spiking_neuron.SpikingNeuron() output_neuron = spiking_neuron.SpikingNeuron() # Create 6 synapses with pos_input_synapse_ij representing the excitatory # connection between the input neuron i and the hidden neuron j, and # pos_hidden_synapse_k representing the excitatory connection between # the hidden neuron k and the output neuron. pos_input_synapse_11 = memristor.Memristor() pos_input_synapse_12 = memristor.Memristor() pos_input_synapse_21 = memristor.Memristor() pos_input_synapse_22 = memristor.Memristor() pos_hidden_synapse_1 = memristor.Memristor() pos_hidden_synapse_2 = memristor.Memristor() # Create 6 synapses with neg_input_synapse_ij representing the inhibitory # connection between the input neuron i and the hidden neuron j, and # neg_hidden_synapse_k representing the inhibitory connection between # the hidden neuron k and the output neuron. neg_input_synapse_11 = memristor.Memristor() neg_input_synapse_12 = memristor.Memristor() neg_input_synapse_21 = memristor.Memristor() neg_input_synapse_22 = memristor.Memristor() neg_hidden_synapse_1 = memristor.Memristor() neg_hidden_synapse_2 = memristor.Memristor() # Set all the synapses to a high resistance state. pos_input_synapse_11.set_resistance(hrs_set_voltage) pos_input_synapse_12.set_resistance(hrs_set_voltage) pos_input_synapse_21.set_resistance(hrs_set_voltage) pos_input_synapse_22.set_resistance(hrs_set_voltage) pos_hidden_synapse_1.set_resistance(hrs_set_voltage) pos_hidden_synapse_2.set_resistance(hrs_set_voltage) neg_input_synapse_11.set_resistance(hrs_set_voltage) neg_input_synapse_12.set_resistance(hrs_set_voltage) neg_input_synapse_21.set_resistance(hrs_set_voltage) neg_input_synapse_22.set_resistance(hrs_set_voltage) neg_hidden_synapse_1.set_resistance(hrs_set_voltage) neg_hidden_synapse_2.set_resistance(hrs_set_voltage) c_11 = [] c_12 = [] c_21 = [] c_22 = [] hidden_spikes_1 = [] hidden_spikes_2 = [] z_1 = [] z_2 = [] output_spikes = [] errors = [] squared_errors = [] epoch_numbers = [] for epoch_number in range(1, epochs + 1): for spikes_1, spikes_2, target in zip(input_spikes_1, input_spikes_2, targets): epoch_numbers.append(epoch_number) # The normalized noisy current c_ij passes through the input_synapse_ij. The input # voltage is 1 V if the input neuron fired as many pulses as the encoding of # TRUE or more, else it is 0 V. pos_c_11 = utility.add_noise(utility.normalize(utility.burst(spikes_1, true) / pos_input_synapse_11.read_resistance())) pos_c_12 = utility.add_noise(utility.normalize(utility.burst(spikes_1, true) / pos_input_synapse_12.read_resistance())) pos_c_21 = utility.add_noise(utility.normalize(utility.burst(spikes_2, true) / pos_input_synapse_21.read_resistance())) pos_c_22 = utility.add_noise(utility.normalize(utility.burst(spikes_2, true) / pos_input_synapse_22.read_resistance())) # The normalized noisy current neg_c_ij passes through the neg_input_synapse_ij. # The current is inverted to represent the inhibitory connection. The input # voltage is 1 V if the input neuron fired as many pulses as the encoding # of TRUE or more, else it is 0 V. neg_c_11 = utility.invert(utility.add_noise(utility.normalize(utility.burst(spikes_1, true) / neg_input_synapse_11.read_resistance()))) neg_c_12 = utility.invert(utility.add_noise(utility.normalize(utility.burst(spikes_1, true) / neg_input_synapse_12.read_resistance()))) neg_c_21 = utility.invert(utility.add_noise(utility.normalize(utility.burst(spikes_2, true) / neg_input_synapse_21.read_resistance()))) neg_c_22 = utility.invert(utility.add_noise(utility.normalize(utility.burst(spikes_2, true) / neg_input_synapse_22.read_resistance()))) c_11.append(pos_c_11 + neg_c_11) c_12.append(pos_c_12 + neg_c_12) c_21.append(pos_c_21 + neg_c_21) c_22.append(pos_c_22 + neg_c_22) hidden_spikes_1.append(hidden_neuron_1.apply_current(c_11[-1] + c_21[-1], current_application_time)[0].count(spike)) hidden_spikes_2.append(hidden_neuron_2.apply_current(c_12[-1] + c_22[-1], current_application_time)[0].count(spike)) # The normalized noisy curent pos_z_i passes through the pos_hidden_synapse_i. # The input voltage is 1 V if the hidden neuron fired as many pulses as # the encoding of TRUE or more, else it is 0 V. pos_z_1 = utility.add_noise(utility.normalize(utility.burst(hidden_spikes_1[-1], true) / pos_hidden_synapse_1.read_resistance())) pos_z_2 = utility.add_noise(utility.normalize(utility.burst(hidden_spikes_2[-1], true) / pos_hidden_synapse_2.read_resistance())) # The normalized noisy curent neg_z_i passes through the neg_hidden_synapse_i. # The current is inverted to represent the inhibitory connection. The # input voltage is 1 V if the hidden neuron fired as many pulses as the # encoding of TRUE or more, else it is 0 V. neg_z_1 = utility.invert(utility.add_noise(utility.normalize(utility.burst(hidden_spikes_1[-1], true) / neg_hidden_synapse_1.read_resistance()))) neg_z_2 = utility.invert(utility.add_noise(utility.normalize(utility.burst(hidden_spikes_2[-1], true) / neg_hidden_synapse_2.read_resistance()))) z_1.append(pos_z_1 + neg_z_1) z_2.append(pos_z_2 + neg_z_2) output_spikes.append(output_neuron.apply_current(z_1[-1] + z_2[-1], current_application_time)[0].count(spike)) errors.append(target - output_spikes[-1]) squared_errors.append(errors[-1] ** 2) # Update the synapses based on the error. # If the output neuron should have fired more times and it did not, # then strengthen the excitatory hidden synapses of the hidden neurons # that fired as many pulses as the encoding of TRUE or more. Otherwise, # do the equivalent update for the input synapses of the input neurons # that fired as many pulses as the encoding of TRUE or more. if utility.is_false_negative(errors[-1]): if utility.burst(hidden_spikes_1[-1], true) or utility.burst(hidden_spikes_2[-1], true): if utility.burst(hidden_spikes_1[-1], true): for _ in range(learning_pulses): pos_hidden_synapse_1.apply_voltage(training_voltage) if utility.burst(hidden_spikes_2[-1], true): for _ in range(learning_pulses): pos_hidden_synapse_2.apply_voltage(training_voltage) else: if utility.burst(spikes_1, true): for _ in range(learning_pulses): pos_input_synapse_11.apply_voltage(training_voltage) pos_input_synapse_12.apply_voltage(training_voltage) if utility.burst(spikes_2, true): for _ in range(learning_pulses): pos_input_synapse_21.apply_voltage(training_voltage) pos_input_synapse_22.apply_voltage(training_voltage) # If the output neuron should not have fired and it did, then strengthen # the inhibitory hidden synapses of the hidden neurons that fired as many # pulses as the encoding of TRUE or more. elif utility.is_false_positive(errors[-1]): if utility.burst(hidden_spikes_1[-1], true): for _ in range(learning_pulses): neg_hidden_synapse_1.apply_voltage(training_voltage) if utility.burst(hidden_spikes_2[-1], true): for _ in range(learning_pulses): neg_hidden_synapse_2.apply_voltage(training_voltage) # The neurons are resting until the next input. hidden_neuron_1.rest() hidden_neuron_2.rest() output_neuron.rest() # Store the data into a data frame. data = pd.DataFrame() data["input_spikes_1"] = input_spikes_1 * epochs data["input_spikes_2"] = input_spikes_2 * epochs data["c_11"] = c_11 data["c_12"] = c_12 data["c_21"] = c_21 data["c_22"] = c_22 data["hidden_spikes_1"] = hidden_spikes_1 data["hidden_spikes_2"] = hidden_spikes_2 data["z_1"] = z_1 data["z_2"] = z_2 data["output_spikes"] = output_spikes data["target"] = targets * epochs data["error"] = errors data["squared_error"] = squared_errors data["epoch"] = epoch_numbers data["learning_pulses"] = [learning_pulses] * epochs * 4 data["training_voltage"] = [training_voltage] * epochs * 4 data["current_application_time"] = [current_application_time] * epochs * 4 if output_to_csv: utility.save_data(data, "./output", "solve-xor-complex-noisy-snn") # Plot the MSE as a function of epoch. utility.plot_mse(data)
def save(self): self.savable_environment.parent = None utility.save_data("lisp_state", self.savable_environment) self.savable_environment.parent = self.globals
def solve_xor_complex(learning_pulses, epochs, output_to_csv=False): hrs_set_voltage = -4 spike = 1 false_negative = 1 false_positive = -1 training_voltage = 1 input_spikes_1 = [0, 0, 1, 1] input_spikes_2 = [0, 1, 0, 1] targets = [0, 1, 1, 0] # Create a network with 2 input neurons, 3 hidden layer neurons # and 1 output neuron. The input neurons are simulated by their spikes. hidden_neuron_1 = neuron.Neuron() hidden_neuron_2 = neuron.Neuron() hidden_neuron_3 = neuron.Neuron() output_neuron = neuron.Neuron() # Create 7 synapses with pos_input_synapse_ij representing the excitatory # connection between the input neuron i and the hidden neuron j, and # pos_hidden_synapse_k representing the excitatory connection between # the hidden neuron k and the output neuron. pos_input_synapse_11 = memristor.Memristor() pos_input_synapse_13 = memristor.Memristor() pos_input_synapse_22 = memristor.Memristor() pos_input_synapse_23 = memristor.Memristor() pos_hidden_synapse_1 = memristor.Memristor() pos_hidden_synapse_2 = memristor.Memristor() pos_hidden_synapse_3 = memristor.Memristor() # Create 7 synapses with neg_input_synapse_ij representing the inhibitory # connection between the input neuron i and the hidden neuron j, and # neg_hidden_synapse_k representing the inhibitory connection between # the hidden neuron k and the output neuron. neg_input_synapse_11 = memristor.Memristor() neg_input_synapse_13 = memristor.Memristor() neg_input_synapse_22 = memristor.Memristor() neg_input_synapse_23 = memristor.Memristor() neg_hidden_synapse_1 = memristor.Memristor() neg_hidden_synapse_2 = memristor.Memristor() neg_hidden_synapse_3 = memristor.Memristor() # Set all the synapses to a high resistance state. pos_input_synapse_11.set_resistance(hrs_set_voltage) pos_input_synapse_13.set_resistance(hrs_set_voltage) pos_input_synapse_22.set_resistance(hrs_set_voltage) pos_input_synapse_23.set_resistance(hrs_set_voltage) pos_hidden_synapse_1.set_resistance(hrs_set_voltage) pos_hidden_synapse_2.set_resistance(hrs_set_voltage) pos_hidden_synapse_3.set_resistance(hrs_set_voltage) neg_input_synapse_11.set_resistance(hrs_set_voltage) neg_input_synapse_13.set_resistance(hrs_set_voltage) neg_input_synapse_22.set_resistance(hrs_set_voltage) neg_input_synapse_23.set_resistance(hrs_set_voltage) neg_hidden_synapse_1.set_resistance(hrs_set_voltage) neg_hidden_synapse_2.set_resistance(hrs_set_voltage) neg_hidden_synapse_3.set_resistance(hrs_set_voltage) c_11 = [] c_13 = [] c_22 = [] c_23 = [] hidden_spikes_1 = [] hidden_spikes_2 = [] hidden_spikes_3 = [] z_1 = [] z_2 = [] z_3 = [] output_spikes = [] errors = [] squared_errors = [] epoch_numbers = [] for epoch_number in range(1, epochs + 1): for spike_1, spike_2, target in zip(input_spikes_1, input_spikes_2, targets): epoch_numbers.append(epoch_number) # The current pos_c_ij passes through the pos_input_synapse_ij. # The input voltage is 1 V if the input neuron fired, else it is 0 V. pos_c_11 = spike_1 / pos_input_synapse_11.read_resistance() pos_c_13 = spike_1 / pos_input_synapse_13.read_resistance() pos_c_22 = spike_2 / pos_input_synapse_22.read_resistance() pos_c_23 = spike_2 / pos_input_synapse_23.read_resistance() # The current neg_c_ij passes through the neg_input_synapse_ij. # The current is inverted to represent the inhibitory connection. # The input voltage is 1 V if the input neuron fired, else it is 0 V. neg_c_11 = utility.invert(spike_1 / neg_input_synapse_11.read_resistance()) neg_c_13 = utility.invert(spike_1 / neg_input_synapse_13.read_resistance()) neg_c_22 = utility.invert(spike_2 / neg_input_synapse_22.read_resistance()) neg_c_23 = utility.invert(spike_2 / neg_input_synapse_23.read_resistance()) c_11.append(pos_c_11 + neg_c_11) c_13.append(pos_c_13 + neg_c_13) c_22.append(pos_c_22 + neg_c_22) c_23.append(pos_c_23 + neg_c_23) hidden_spikes_1.append(hidden_neuron_1.apply_current(c_11[-1])) hidden_spikes_2.append(hidden_neuron_2.apply_current(c_22[-1])) hidden_spikes_3.append( hidden_neuron_3.apply_current(c_13[-1] + c_23[-1])) # The curent pos_z_i passes through the pos_hidden_synapse_i. # The input voltage is 1 V if the hidden neuron fired, else it is 0 V. pos_z_1 = hidden_spikes_1[ -1] / pos_hidden_synapse_1.read_resistance() pos_z_2 = hidden_spikes_2[ -1] / pos_hidden_synapse_2.read_resistance() pos_z_3 = hidden_spikes_3[ -1] / pos_hidden_synapse_3.read_resistance() # The curent neg_z_i passes through the neg_hidden_synapse_i. # The current is inverted to represent the inhibitory connection. # The input voltage is 1 V if the hidden neuron fired, else it is 0 V. neg_z_1 = utility.invert(hidden_spikes_1[-1] / neg_hidden_synapse_1.read_resistance()) neg_z_2 = utility.invert(hidden_spikes_2[-1] / neg_hidden_synapse_2.read_resistance()) neg_z_3 = utility.invert(hidden_spikes_3[-1] / neg_hidden_synapse_3.read_resistance()) z_1.append(pos_z_1 + neg_z_1) z_2.append(pos_z_2 + neg_z_2) z_3.append(pos_z_3 + neg_z_3) output_spikes.append( output_neuron.apply_current(z_1[-1] + z_2[-1] + z_3[-1])) errors.append(target - output_spikes[-1]) squared_errors.append(errors[-1]**2) # Update the synapses based on the error. # If the output neuron should have fired and it did not, # then strengthen the excitatory hidden synapses of the # hidden neurons that fired. Otherwise, do the equivalent # update for the input synapses of the input neurons that fired. if errors[-1] == false_negative: if hidden_spikes_1[-1] == spike or hidden_spikes_2[ -1] == spike or hidden_spikes_3[-1] == spike: if hidden_spikes_1[-1] == spike: for _ in range(learning_pulses): pos_hidden_synapse_1.apply_voltage( training_voltage) if hidden_spikes_2[-1] == spike: for _ in range(learning_pulses): pos_hidden_synapse_2.apply_voltage( training_voltage) if hidden_spikes_3[-1] == spike: for _ in range(learning_pulses): pos_hidden_synapse_3.apply_voltage( training_voltage) else: if spike_1 == spike: for _ in range(learning_pulses): pos_input_synapse_11.apply_voltage( training_voltage) pos_input_synapse_13.apply_voltage( training_voltage) if spike_2 == spike: for _ in range(learning_pulses): pos_input_synapse_22.apply_voltage( training_voltage) pos_input_synapse_23.apply_voltage( training_voltage) # If the output neuron should not have fired and it did, then # strengthen the inhibitory hidden synapses of the hidden neurons # that fired. elif errors[-1] == false_positive: if hidden_spikes_1[-1] == spike: for _ in range(learning_pulses): neg_hidden_synapse_1.apply_voltage(training_voltage) if hidden_spikes_2[-1] == spike: for _ in range(learning_pulses): neg_hidden_synapse_2.apply_voltage(training_voltage) if hidden_spikes_3[-1] == spike: for _ in range(learning_pulses): neg_hidden_synapse_3.apply_voltage(training_voltage) # Store the data into a data frame. data = pd.DataFrame() data["input_spike_1"] = input_spikes_1 * epochs data["input_spike_2"] = input_spikes_2 * epochs data["c_11"] = c_11 data["c_13"] = c_13 data["c_22"] = c_22 data["c_23"] = c_23 data["hidden_spike_1"] = hidden_spikes_1 data["hidden_spike_2"] = hidden_spikes_2 data["hidden_spike_3"] = hidden_spikes_3 data["z_1"] = z_1 data["z_2"] = z_2 data["z_3"] = z_3 data["output_spike"] = output_spikes data["target"] = targets * epochs data["error"] = errors data["squared_error"] = squared_errors data["epoch"] = epoch_numbers data["learning_pulses"] = [learning_pulses] * epochs * 4 data["training_voltage"] = [training_voltage] * epochs * 4 if output_to_csv: utility.save_data(data, "./output", "solve-xor-complex") # Plot the MSE as a function of epoch. utility.plot_mse(data)
def save(self): utility.save_data("stockaliases", self.__aliases)
# S = Society(delta=delta, beta=beta, tau=0.1, **config["society"]) mc = MCMC(system=S, **config["mcmc"]) r = mc.sample() # print("accptance ratio at {} was {}".format( # (tau,beta,delta), # (1 - mc.rejected/(S.N*mc.max_sweeps)) # )) return idx, (tau, delta, beta), r # result = map(run,args[:3]) pool = mp.Pool() t0 = time.time() print(40*"=") print("Passed:", json.dumps(config, indent=4), sep="\n") print("Starting at: ", time.asctime()) result = pool.map(run, args) pool.close() pool.join() result.sort() t = time.time() print("pool.map(run, args) took %s"%(timedelta(seconds=(t-t0)))) t0_save = time.time() save_data(result, config) t_save = time.time() print("save_data took %s"%(timedelta(seconds=(t_save-t0_save)))) t_final = time.time() print("Total time spent: %s"%(timedelta(seconds=(t_save-t0)))) print("Finished at: ", time.asctime()) print(40*"=")