Esempio n. 1
0
    def build(self, time_offs=0, topology_params={}, input_params={},
              meta_data={}, seed=None):
        """
        Builds a network with the given topology and input data that is ready
        to be handed of to PyNNLess.
        """

        old_state = utils.initialize_seed(seed, 1)
        try:
            topology = self.build_topology(topology_params=topology_params)
        finally:
            utils.finalize_seed(old_state)

        old_state = utils.initialize_seed(seed, 2)
        try:
            input_times, input_indices, input_split = self.build_input(
                time_offs=time_offs,
                topology_params=topology_params,
                input_params=input_params)
        finally:
            utils.finalize_seed(old_state)

        return NetworkInstance(
            self.inject_input(topology, input_times),
            input_times=input_times,
            input_indices=input_indices,
            input_split=input_split,
            input_params=input_params,
            data_params=self.data_params,
            topology_params=topology_params,
            meta_data=meta_data,
            mat_in=self.mat_in,
            mat_out=self.mat_out)
Esempio n. 2
0
    def build(self, time_offs=0, topology_params={}, input_params={}, meta_data={}, seed=None):
        """
        Builds a network with the given topology and input data that is ready
        to be handed of to PyNNLess.
        """

        old_state = utils.initialize_seed(seed, 1)
        try:
            topology = self.build_topology(topology_params=topology_params)
        finally:
            utils.finalize_seed(old_state)

        old_state = utils.initialize_seed(seed, 2)
        try:
            input_times, input_indices, input_split = self.build_input(
                time_offs=time_offs, topology_params=topology_params, input_params=input_params
            )
        finally:
            utils.finalize_seed(old_state)

        return NetworkInstance(
            self.inject_input(topology, input_times),
            input_times=input_times,
            input_indices=input_indices,
            input_split=input_split,
            input_params=input_params,
            data_params=self.data_params,
            topology_params=topology_params,
            meta_data=meta_data,
            mat_in=self.mat_in,
            mat_out=self.mat_out,
        )
Esempio n. 3
0
def _initialize_generate(n_bits, n_ones, n_samples, seed=None):
    if (n_bits < 0 or n_ones < 0 or n_samples < 0):
        raise Exception("Arguments must be non-negative!")
    if (n_ones > n_bits):
        raise Exception("n_ones must be smaller or equal to n_bits!")
    return utils.initialize_seed(seed)
Esempio n. 4
0
    def build(self, simulator_info, simulator="", seed=None):
        """
        Builds all NetworkPool instances required to conduct the specified
        experiments.

        :param simulator_info: Information about the used simulator as returned
        from PyNNLess.get_simulator_info() -- contains the maximum number of
        neurons and the supported software concurrency.
        :param seed: seed to be used to spawn the seeds for the data generation.
        """

        # Spawn more random seeds
        old_state = utils.initialize_seed(seed)
        try:
            data_seed = np.random.randint(1 << 30)
            build_seed = np.random.randint(1 << 30)
        finally:
            utils.finalize_seed(old_state)

        # Add a dummy experiment if there are no experiments specified
        if len(self["experiments"]) == 0:
            self["experiments"] = [ExperimentDescriptor(name="eval")]

        # "Count sources" flag
        cs = simulator_info["sources_are_neurons"]

        # Create all NetworkPool instances
        pools = []
        for i, experiment in enumerate(self["experiments"]):
            # Gather the input and topology parameters for this experiment
            input_params_list, topology_params_list = \
                    self.build_parameters(experiment)

            # Generate an experiment name
            # TODO: Make sure the name is unique
            if experiment["name"] == "":
                experiment["name"] = "experiment_" + str(i)

            # Generate new pools for this experiment
            min_pool = len(pools)
            pidx = simulator_info["concurrency"]

            # Assemble a name for this repetition
            pools = pools + [NetworkPool(name=experiment["name"] + "." + str(c))
                    for c in xrange(simulator_info["concurrency"])]

            # Metadata to store along with the networks
            meta_data = {
                "experiment_idx": i,
                "experiment_name": experiment["name"],
                "experiment_size": (experiment["repeat"] *
                        (len(input_params_list) * len(topology_params_list))),
                "keys": experiment.get_keys(),
                "output_params": self["output"],
                "simulator": simulator
            }

            # Repeat the experiment as many times as specified in the "repeat"
            # parameter
            local_build_seed = build_seed
            net_idx = 0
            for j in xrange(experiment["repeat"]):
                # Create a random permutation of the topology parameters list
                perm = range(0, len(topology_params_list))
                random.shuffle(perm)
                for k in xrange(len(topology_params_list)):
                    # Print the current network number
                    net_idx = net_idx + 1
                    if (net_idx % 100 == 0):
                        n_nets = len(topology_params_list) * experiment["repeat"]
                        print("Generating network " + str(net_idx) + "/" +
                            str(n_nets))

                    # Create a build instance coupled with the topology
                    # parameters
                    topology_params = topology_params_list[perm[k]]
                    builder = NetworkBuilder(
                            data_params=topology_params["data"],
                            seed=data_seed)

                    # Build a network instance and add it to the network pool
                    net = builder.build(
                            topology_params=topology_params["topology"],
                            input_params=input_params_list,
                            meta_data=meta_data,
                            seed=local_build_seed)

                    # Search for a pool to which the network should be added.
                    # Use the pool with the fewest neurons which still has
                    # space for this experiment.
                    target_pool_idx = -1
                    for l in xrange(min_pool, len(pools)):
                        if ((target_pool_idx == -1 or pools[l].neuron_count(cs)
                                < pools[target_pool_idx].neuron_count(cs)) and
                                 pools[l].neuron_count(cs)
                                    + net.neuron_count(cs)
                                    <= simulator_info["max_neuron_count"]):
                            # If uniform parameter are required (Spikey), check
                            # whether the target network parameters are the same
                            # as the current network parameters
                            if pools[l].neuron_count(cs) > 0:
                                if self._check_shared_parameters_equal(
                                        simulator_info["shared_parameters"],
                                        pools[l]["topology_params"][0]["params"],
                                        topology_params["topology"]["params"]):
                                    target_pool_idx = l
                            else:
                                target_pool_idx = l

                    # No free pool has been found, add a new one
                    if target_pool_idx == -1:
                        pool_name = experiment["name"] + "." + str(pidx)
                        pools.append(NetworkPool(name=pool_name))
                        pidx = pidx + 1
                        target_pool_idx = len(pools) - 1

                    # Add the network to the pool
                    pools[target_pool_idx].add_network(net)

                    # Advance the build_seed -- the input and topology
                    # parameters should still vary between trials,
                    # but reproducibly
                    local_build_seed = local_build_seed * 2

        # Return non-empty pool instances
        return filter(lambda x: x.neuron_count(cs) > 0, pools)
Esempio n. 5
0
                    "-n",
                    type=int,
                    default=10000,
                    help="Number of nodes")
parser.add_argument("--evaluate_method",
                    type=str,
                    default="CGSS",
                    help="Mathod of evaluating: 'NN'/'CGSS'/'REGAL'")
parser.add_argument("--pair_method",
                    type=str,
                    default="NN",
                    help="Mathod of get pair: 'NN'/'CGSS'")
params = parser.parse_args()  # 进行解析

## 初始化随机数种子
initialize_seed(params)

## 读取节点嵌入,嵌入已经转换为tensor格式
src_emb = load_embeddings(params, True)
tgt_emb = load_embeddings(params, False)

## 构造GAN模型
mapping, discriminator = build_model(params)

## 构造训练器
trainer = Trainer(src_emb, tgt_emb, mapping, discriminator, params)

## 构造评估器
evaluator = Evaluator(trainer)

## 对抗训练过程