def _evaluate_test(self): self.keep_prob = 1.0 self.is_training = False self.net = Network(self.is_training) self.ten_accuracy = [] self.epoch_accuracy = [] self.test_writer = tf.summary.FileWriter(self.test_logs_path, graph=self.net.graph) # TODO: reset graph and update model with tf.Session(graph=self.net.graph) as sess: self._restore_checkpoint_or_init(sess) step_num = 1 max_steps = FLAGS.epoch * 100 while step_num <= max_steps: if step_num % 10 == 0: gs, acc = self._test_step(sess) self._add_accuracy(step_num, gs, acc) if step_num % 100 == 0: self._evaluate_test() else: gs, acc = self._test_step(sess) self._add_accuracy(step_num, gs, acc) step_num += 1 self.keep_prob = 0.75 self.is_training = True self.net = Network(self.is_training) self.net.saver.restore(sess, self.chkpt_file)
def _evaluate_train(self): self.keep_prob = 0.75 self.is_training = True self.net = Network(self.is_training) self.ten_accuracy = [] self.epoch_accuracy = [] self.train_writer = tf.summary.FileWriter(self.train_logs_path, graph=self.net.graph) with tf.Session(graph=self.net.graph) as sess: self._restore_checkpoint_or_init(sess) step_num = 1 max_steps = FLAGS.epoch * 100 while step_num <= max_steps: if step_num % 10 == 0: gs, acc = self._train_step( sess, tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE), tf.RunMetadata()) self._add_accuracy(step_num, gs, acc) save_path = self.net.saver.save(sess, self.chkpt_file) print("Model saved in file: %s" % save_path) if step_num % 100 == 0: self._evaluate_test() else: gs, acc = self._train_step(sess) self._add_accuracy(step_num, gs, acc) step_num += 1
def _evaluate_train(self): self.keep_prob = 0.75 self.is_training = True self.net = Network(self.is_training) self.train_writer = tf.summary.FileWriter(self.train_logs_path, graph=self.net.graph) with tf.device("/gpu:0"): config = tf.ConfigProto() config.gpu_options.per_process_gpu_memory_fraction = 0.8 # config.gpu_options.allow_growth = True with tf.Session(graph=self.net.graph, config=config) as sess: self._restore_checkpoint_or_init(sess) step_num = 1 max_steps = FLAGS.max_steps while step_num <= max_steps: if step_num % 1000 == 0: gs = self._train_step( sess, tf.RunOptions( trace_level=tf.RunOptions.FULL_TRACE), tf.RunMetadata()) save_path = self.net.saver.save( sess, self.logs_dir + "/model.ckpt", global_step=self.net.global_step) print("Model saved in file: %s" % save_path) else: gs = self._train_step(sess) step_num += 1
def task_2(): network = Network(1, [10], [3]) population = network.populations[0] population.connect_randomly(0.3) cluster_number = 3 output_populations = [ population.neurons[i * 2:(i + 1) * 2] for i in range(0, cluster_number) ] print(output_populations) input_population = population.neurons[6:10] for neuron in population.neurons: neuron.set_current(0, TOTAL_TIME, 0) population.synapse.target_neurons = output_populations[0] population.learn_method = "rstdp" encode_task2(input_population) size_activity_neuron = [[] for i in range(0, cluster_number)] time = np.arange(0, TOTAL_TIME) for t in range(TOTAL_TIME): network.update_voltage(t) for index, output_neurons in enumerate(output_populations): activity = 0 for neuron in output_neurons: activity += neuron.voltage[t] size_activity_neuron[index].append(activity / len(output_neurons)) fig, ax = plt.subplots(1, 1, figsize=(50, 100)) for i in range(cluster_number): ax.scatter(time, size_activity_neuron[i]) plt.show()
def _evaluate_train(self): self.keep_prob = 0.75 self.is_training = True self.net = Network(self.is_training) self.ten_accuracy = [] self.epoch_accuracy = [] self.train_writer = tf.summary.FileWriter(self.train_logs_path, graph=self.net.graph) config = tf.ConfigProto() config.gpu_options.allow_growth = True config.gpu_options.per_process_gpu_memory_fraction = 0.4 with tf.Session(config=config,graph=self.net.graph) as sess: with tf.device("/device:GPU:0"): self._restore_checkpoint_or_init(sess) step_num = 1 max_steps = FLAGS.epoch * 100 while step_num <= max_steps: if step_num % 10 == 0: gs, acc = self._train_step(sess, tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE), tf.RunMetadata()) self._add_accuracy(step_num, gs, acc) save_path = self.net.saver.save(sess, self.chkpt_file) print("Model saved in file: %s" % save_path) if step_num % 100 == 0: self._evaluate_test() else: gs, acc = self._train_step(sess) print("acc is ",acc) self._add_accuracy(step_num, gs, acc) step_num += 1
def __init__(self): """ Instantiate the plugin and all its modules. """ self._core = Core(self) self._interface = Interface(self) self._network = Network(self)
def __init__(self, network = None, wxId = wx.ID_ANY, populateDisplay = True): if network is None: title = Network().name() else: title = network.name() wx.Frame.__init__(self, None, wxId, title, size = (800, 600), style = wx.DEFAULT_FRAME_STYLE | wx.FULL_REPAINT_ON_RESIZE) self.Bind(wx.EVT_ACTIVATE, self.onActivate) self.Bind(wx.EVT_UPDATE_UI, self.onUpdateUI) self.Bind(wx.EVT_CLOSE, self.onClose) self.splitter = wx.SplitterWindow(self, wx.ID_ANY, style = wx.SP_LIVE_UPDATE) self.splitter.SetMinimumPaneSize(20) self._modified = False self.display = display.display.Display(self.splitter) self.display.autoVisualize = populateDisplay self.display.setNetwork(network) dispatcher.connect(self.networkDidChange, ('set', 'network'), self.display) dispatcher.connect(self.networkDidChangeSavePath, ('set', 'savePath'), network) dispatcher.connect(self.displayDidChange, dispatcher.Any, self.display) self._scriptLocals = self.scriptLocals() self._console = wx.py.shell.Shell(self.splitter, wx.ID_ANY, locals = self._scriptLocals, introText = gettext('Welcome to Neuroptikon.')) self._console.autoCompleteIncludeSingle = False self._console.autoCompleteIncludeDouble = False self.splitter.SplitHorizontally(self.display, self._console) self.splitter.SetSashGravity(1.0) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add(self.splitter, 1, wx.EXPAND) self.SetAutoLayout(True) self.SetSizer(sizer) self.finder = None self._progressNestingLevel = 0 self._progressDialog = None self._progressDisplayTime = None self._progressMessage = None self._progressFractionComplete = None self._progressShouldContinue = True self._progressLastUpdate = datetime.datetime.now() self._progressUpdateDelta = datetime.timedelta(0, 0, 100000) # Don't update the GUI more than 10 times a second. self.layoutClasses = {} self.SetMenuBar(self.menuBar()) self.SetToolBar(self.toolBar()) dispatcher.connect(self.onDisplayChangedHighlightOnlyWithinSelection, ('set', 'highlightOnlyWithinSelection'), self.display) if platform.system() == 'Darwin': # Have new windows cascade so they don't sit right on top of each other. carbon.RepositionWindow(self.MacGetTopLevelWindowRef(), 0, 4) # 4 = kWindowCascadeOnMainScreen self.Show(1) self.splitter.SetSashPosition(-100)
def __init__(self, population_count, exc_neurons_count, inh_neurons_count): self.dt = DT self.current_time = 0 self.total_time = TOTAL_TIME self.population_count = population_count self.exc_neurons_count = exc_neurons_count self.inh_neurons_count = inh_neurons_count self.network = Network(self.population_count, self.exc_neurons_count, self.inh_neurons_count)
def __init__(self, data): self._data = data self.network = Network() self.solution = None self.requirements = None self.id = self._data["id"] self.hash = hash(self.get_id()) self.int_id = 0
def main(): n = Network() l1 = Layer(4) l2 = Layer(1) c = Connection(l1, l2) n.addHiddenLayer(l1) n.addHiddenLayer(l2) n.addConnection(c)
def start_evolution(self, nr_of_genotypes): self.genotypes = [] temp_network = Network(self.topology) for i in range(nr_of_genotypes): new_genotype = Genotype([0] * temp_network.weight_count) new_genotype.set_rand_params(-5, 5) self.genotypes.append(new_genotype) self.start_evaluation()
def __init__(self, stop_event, config: Config): threading.Thread.__init__(self) self.config = config self._stop_event = stop_event self._scheduler = sched.scheduler(time.time, time.sleep) self._network = Network(config) self._scheduler.enter(self.get_scheduler_time(), self.get_scheduler_priority(), self.update) self._scheduler.run()
def main(): net = Network() train( net=net, criterion=nn.CrossEntropyLoss(), optimizer=optim.RMSprop(net.parameters(), lr=0.001), num_epochs=NUM_EPOCHS ) validate(net)
def solve(train, test): net = Network(LAYERS, ACTIVATION, ACTIVATION_PRIME) net.gradient_descent(train, EPOCHS, BATCH_SIZE, L_RATE, EPOCH_FEEDBACK, test) print("FINAL COST: {0}".format(net.evaluate(test))) print_result("files/result.txt", str(net.evaluate(test)))
def generate(self): """Generates a neural network with the given structure specified. :return: New neural network with specfied structure and random weights """ self.networks_created += 1 return Network(self.num_layers, self.num_inputs, self.num_neurons, self.num_outputs, self.activation, self.networks_created - 1)
def build(configuration): parameters = ParametersFactory.build(configuration.layers) forward_propagator = ForwardPropagator(configuration.layers) trainer = TrainerFactory.build(forward_propagator, configuration.input, configuration.output, configuration.iterations, configuration.learning_rate) return Network(forward_propagator, parameters, trainer)
def ten_arr_test(file_name): weights, topology = get_genotype_data_from_file(file_name + ".csv") net = Network(topology) net.set_weights(weights) while True: ins, out = get_test_ten_arr() print("Answer: " + ", ".join("{:.2f}".format(x) for x in net.process_input(ins))) print(net)
def blockchain_factory(): """ Create a new blockchain with its network. """ network = Network() bc = Blockchain(network) network.set_blockchain(bc) return bc, network
def test_correct_shape(T): encoder = StateEncoder(T) prev_state = encoder.get_empty_state() curr_state = encoder.encode_state(chess.Board(), prev_state) curr_state = curr_state.unsqueeze(0) net = Network(M * T + L) out = net(curr_state) self.assertEqual(out[0].shape, torch.Size([1])) self.assertEqual(out[1].shape, torch.Size((1, 8, 8, 73)))
def load_state(T, chkpt_path, device, network_temp=2): net = Network(T, temp=network_temp).to(device) optimizer = Adam(net.parameters(), lr=1e-4, weight_decay=1e-4) if chkpt_path is not None and os.path.exists(chkpt_path): checkpoint = torch.load(chkpt_path, map_location=torch.device(device)) net.load_state_dict(checkpoint[MODEL_KEY]) optimizer.load_state_dict(checkpoint[OPTIMIZER_KEY]) games_trained = checkpoint[GAMES_TRAINED_KEY] replay_mem = checkpoint[REPLAY_MEM_KEY] else: games_trained = 0 replay_mem = ReplayMemory() return net, optimizer, games_trained, replay_mem
def task_2(): network = Network(1, [12], [0]) population = network.populations[0] population.add_layer(10) population.add_layer(2) population.connect_layer_fully() for neuron in population.neurons: neuron.set_current(0, TOTAL_TIME, 0) # encode(population, [1, 0, 1, 1, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1, 1, 0]) encode(population, [1, 0, 1, 1, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1, 1, 1, 0]) weights = dict() pre_synaptic_neurons = population.neurons[:10] post_synaptic_neurons = population.neurons[10:] for pre_synaptic_neuron in pre_synaptic_neurons: for post_synaptic_neuron in post_synaptic_neurons: if pre_synaptic_neuron not in weights.keys(): weights[pre_synaptic_neuron] = dict() weights[pre_synaptic_neuron][post_synaptic_neuron] = list() for t in range(TOTAL_TIME): network.update_voltage(t) for pre_synaptic_neuron in pre_synaptic_neurons: for post_synaptic_neuron in post_synaptic_neurons: weights[pre_synaptic_neuron][post_synaptic_neuron].append( population.synapse.adjacency[pre_synaptic_neuron] [post_synaptic_neuron]['weight']) for neuron in population.neurons: if neuron in population.synapse.adjacency.keys(): for post_synaptic_neuron in population.synapse.adjacency[ neuron].keys(): print(population.synapse.adjacency[neuron] [post_synaptic_neuron]['weight'], end=" ") print() # fig, ax = plt.subplots(1, 1, figsize=(50, 30)) # time = np.arange(0, TOTAL_TIME) # ax.plot(time, population.neurons[0].voltage) # ax.plot(time, population.neurons[10].voltage) # plt.show() fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(50, 100)) time = np.arange(0, TOTAL_TIME) for pre_synaptic_neuron in pre_synaptic_neurons: for post_synaptic_neuron in post_synaptic_neurons: ax1.plot(time, weights[pre_synaptic_neuron][post_synaptic_neuron]) # ax2.plot(time, population.neurons[0].voltage) ax2.plot(time, population.neurons[11].voltage) ax2.plot(time, population.neurons[10].voltage) plt.show()
def setUpClass(cls): # create a cMix mixnet of 3 mix-nodes and 10 users cls.b = 5 cls.nodes = 3 cls.usersNum = 10 cls.network = Network() cls.network.setNetworkHandler(NetworkHandler(cls.b)) for _ in range(0, cls.nodes): cls.network.addMixNode(MixNode(cls.b)) cls.network.init() cls.users = [User("user") for _ in range(0, cls.usersNum)] for user in cls.users: cls.network.addUser(user)
def build_network(*args, input_dim=3, p0_z=None, z_dim=128, beta=None, skip_connection=True, variational=False, use_kl=False, geo_initial=True): net = Network(*args, input_dim=input_dim, p0_z=p0_z, z_dim=z_dim, beta=beta, skip_connection=skip_connection, variational=variational, use_kl=use_kl) if geo_initial: print("Perform geometric initialization!\n") for k, v in net.named_parameters(): if 'encoder' in k: pass else: if 'weight' in k: std = np.sqrt(2) / np.sqrt(v.shape[0]) nn.init.normal_(v, 0.0, std) if 'bias' in k: nn.init.constant_(v, 0) if 'l_out.weight' in k: std = np.sqrt(np.pi) / np.sqrt(v.shape[1]) nn.init.constant_(v, std) if 'l_out.bias' in k: nn.init.constant_(v, -0.5) return net
def load_pretrained_weights(args): model = Network(args.classes) model_path = args.model_path.replace('RealWorld', args.domain) pre = torch.load(args.model_path) new_pre = OrderedDict() for p in pre: if ('classifier' in p): # print('----', p) continue else: new_pre[p] = pre[p] model.load_state_dict(new_pre, strict=False) for name, p in model.state_dict().items(): if ('classifier' in name): continue else: p.requires_grad = False torch.nn.init.xavier_uniform_(model.classifier.fc8.weight) del new_pre return model
def task_1(): network = Network(1, [12], [0]) population = network.populations[0] population.add_layer(10) population.add_layer(2) population.connect_layer_fully() population.synapse.target_neurons = [population.neurons[10]] population.learn_method = "rstdp" for neuron in population.neurons: neuron.set_current(0, TOTAL_TIME, 0) encode(population, [1, 0, 1, 1, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1, 1, 0]) pre_synaptic_neurons, post_synaptic_neurons, weights = initialize_weight( population) for t in range(TOTAL_TIME): network.update_voltage(t) update_weight(pre_synaptic_neurons, post_synaptic_neurons, weights, population) if t == TOTAL_TIME // 2: population.synapse.target_neurons = [population.neurons[11]] show_weight_plot(population, pre_synaptic_neurons, post_synaptic_neurons, weights)
def task_3(): network = Network(1, [12], [1]) population = network.populations[0] population.add_layer(10) population.add_layer(3) population.connect_layer_fully() neuron_10 = population.neurons[10] neuron_11 = population.neurons[11] neuron_12 = population.neurons[12] population.synapse.connect(neuron_12, neuron_10) population.synapse.connect(neuron_12, neuron_11) for neuron in population.neurons: neuron.set_current(0, TOTAL_TIME, 0) encode(population, [1, 0, 1, 1, 1, 0, 0, 0, 0, 0]) for t in range(TOTAL_TIME): network.update_voltage(t) for neuron in population.neurons: if neuron in population.synapse.adjacency.keys(): for post_synaptic_neuron in population.synapse.adjacency[ neuron].keys(): print( population.synapse.adjacency[neuron][post_synaptic_neuron], end=" ") print()
laser_images = dummy.load_laser_images( '/home/hao/others/data/CNN_SLAM/laser_images') size = len(images) if len(images) <= len(laser_images) else len( laser_images) images = images[0:size] laser_images = laser_images[0:size] processed_images = image_process(images, laser_images) num_images = len(processed_images) shape = processed_images[0].shape tf_images = tf.placeholder(shape=[None, shape[0], shape[1], shape[2] * 2], dtype=tf.float32, name='images') tf_batch_size = tf.placeholder(dtype=tf.float32, name='batch_size') network = Network() output = network.inference(tf_images, tf_batch_size, 'images') sess = tf.InteractiveSession() with sess.as_default(): saver = tf.train.Saver() sess.run(tf.global_variables_initializer()) saver.restore(sess, '/home/hao/others/CNN_SLAM/check_points/snap.ckpt-0') for offset1 in range(0, num_images, BATCH_SIZE): end1 = offset1 + BATCH_SIZE for offset2 in range(0, num_images, BATCH_SIZE): end2 = offset2 + BATCH_SIZE if num_images - offset1 < BATCH_SIZE: tf_batch_images1 = processed_images[offset1:end1] tf_batch_images2 = processed_images[offset2:offset2 +
import importlib agentModule = importlib.import_module(agentModuleName) cascadeModule = importlib.import_module(cascadeModuleName) # initialize the network and cascade (diffusion) process model cascade = cascadeModule.CascadeModel(Config) avetime = 0 aveutility = 0 numSamples = 10 for s in range(numSamples): random.seed(s) nw = Network(Config, cascade, random) # initialize agent agent = agentModule.MyAgent(0, Config) # run simulations budget = int(ConfigSectionMap(Config, "AgentParameters")["budget"]) start = time.time() selected = EnforceSelectionFeasibility( int(math.floor(budget)), agent.selectNodes(copy.deepcopy(nw))) adopters = nw.update(selected) # subtract selection from remaining budget budget -= len(selected)
samples=SERVICE_SAMPLES) for idx in range(n) ] # create the distributor that distributes to the smallest queue and random otherwise d0 = Distributor(servers, "smallq", "random") # define the network as nodes and edges # nodes nodes = {srvr.cID: srvr for srvr in servers} nodes['d0'] = d0 # all nodes connect to the distributor, but this won't need to be defined # as the distributor stores it's connections # this is a simple system, so each service connects to an endpoint edges = [(srvr.cID, 'end') for srvr in servers] system = Network(edges, nodes, injection_point='d0') # run the simulation and get the logs scenario_log = run_simulation(system=system, event_queue=eq) # label the scenario scenario_log['scenario'] = n results.append(scenario_log) bar.tick() print("Simulation finished") # concatenate the results and save to file results = pd.concat(results).reset_index(drop=True) results.to_csv("sim_results.csv", index=False)
def relu_prime(z): z[z < 0] = 0 z[z > 0] = 1 return z def loss(y_true, y_pred): return 0.5 * (y_pred - y_true)**2 def loss_prime(y_true, y_pred): return y_pred - y_true x_train = np.array([[[0, 0]], [[0, 1]], [[1, 0]], [[1, 1]]]) y_train = np.array([[[0]], [[1]], [[1]], [[0]]]) net = Network() net.add(FCLayer((1, 2), (1, 3))) net.add(ActivationLayer((1, 3), (1, 3), relu, relu_prime)) net.add(FCLayer((1, 3), (1, 1))) net.add(ActivationLayer((1, 3), (1, 3), relu, relu_prime)) net.setup_loss(loss, loss_prime) net.fit(x_train, y_train, epochs=1000, learning_rate=0.01) out = net.predict([[0, 1]]) print(out)