コード例 #1
0
def test_stat(_type=Stat.MAType.Simple):

    stat = Stat.SMAStat(
        "Test stats") if _type == Stat.MAType.Simple else Stat.EMAStat()
    for i in range(1, 100):
        stat.update(i)

    stat.print()
コード例 #2
0
def linscale(_list):

    if len(_list) == 0:
        return _list

    import cortex.statistics as Stat

    stat = Stat.SMAStat()

    for elem in _list:
        stat.update(elem)

    # Scale between 0 and the absolute maximum
    scaled = [stat.abs_max * (x - stat.min) / (stat.max - stat.min) for x in _list]

    #print(scaled)

    return scaled
コード例 #3
0
ファイル: species.py プロジェクト: cantordust/pycortex
    def calibrate(self):

        if len(self.nets) == 0:
            return

        import cortex.network as cn
        import cortex.functions as Func

        # Reset the champion
        self.champion = None

        net_stats = Stat.SMAStat()
        top_fitness = -math.inf

        # Compute the absolute fitness of the species
        for net_id in self.nets:

            absolute_fitness = cn.Net.Ecosystem[net_id].fitness.absolute

            if (self.champion is None or absolute_fitness > top_fitness):
                self.champion = net_id
                top_fitness = absolute_fitness

            net_stats.update(absolute_fitness)

        print(
            f'>>> Champion for species {self.ID}: {self.champion} (fitness: {cn.Net.Ecosystem[self.champion].fitness.absolute})'
        )

        # Compute the relative fitness of the networks
        # belonging to this species
        for net_id in self.nets:
            # Compute the relative fitness
            net = cn.Net.Ecosystem[net_id]
            net.fitness.relative = net_stats.get_offset(net.fitness.absolute)

#            print("Network", net_id, "fitness:",
#                  "\t\tAbsolute:", cn.Net.Ecosystem[net_id].fitness.absolute,
#                  "\t\tRelative:", cn.Net.Ecosystem[net_id].fitness.relative)

        self.fitness.set(net_stats.mean)
コード例 #4
0
ファイル: cortex.py プロジェクト: cantordust/pycortex
def calibrate(_run,
              _epoch):

    if get_rank() != 0:
        return

    print("\n======[ Calibrating ecosystem ]======\n")

    # Remove extinct species.
    extinct = [species.ID for species in cs.Species.Populations.values() if len(species.nets) == 0]

    while len(extinct) > 0:
        del cs.Species.Populations[extinct.pop()]

    # Reset the global champion.
    cn.Net.Champion = None

    # Running statistics about species fitness
    species_stat = Stat.SMAStat()

    # Highest fitness seen so far.
    top_fitness = -math.inf

    params = {}
    complexity_stat = Stat.SMAStat()
    for net in cn.Net.Ecosystem.values():
        params[net.ID] = net.get_parameter_count()
        complexity_stat.update(params[net.ID])

    for net_id in params.keys():
        # This means that for identical absolute fitness, an individual with
        # lower complexity would have a higher relative fitness.
        cn.Net.Ecosystem[net_id].complexity = complexity_stat.get_inv_offset(params[net_id])

    for species in cs.Species.Populations.values():

        # Compute the relative network fitness
        # and the absolute species fitness.
        species.calibrate()
        species_stat.update(species.fitness.absolute)

        if (cn.Net.Champion is None or
            cn.Net.Ecosystem[species.champion].fitness.absolute > top_fitness):

            cn.Net.Champion = species.champion
            top_fitness = cn.Net.Ecosystem[species.champion].fitness.absolute

    for species in cs.Species.Populations.values():
        # Compute the relative species fitness
        species.fitness.relative = species_stat.get_offset(species.fitness.absolute)

    print(f'>>> Global champion: {cn.Net.Champion} (fitness: {cn.Net.Ecosystem[cn.Net.Champion].fitness.absolute})')

    # Save the champion
    if cn.Net.Champion is not None:
        save_net(cn.Net.Ecosystem[cn.Net.Champion], f'/run_{_run}/epoch_{_epoch}', 'Champion')

    # Store statistics
    Conf.Stats['Species_count'].update(len(cs.Species.Populations))
    Conf.Stats['Network_count'].update(len(cn.Net.Ecosystem))
    Conf.Stats['Champion_parameter_count'].update(cn.Net.Ecosystem[cn.Net.Champion].get_parameter_count())
    Conf.Stats['Highest_fitness'].update(cn.Net.Ecosystem[cn.Net.Champion].fitness.absolute)

    epoch_stats = {
                  'Average_fitness': Stat.SMAStat(),
                  'Average_initial_fitness': Stat.SMAStat(),
                  'Champion_parameter_count': Stat.SMAStat()
                  }

    if not Conf.UnitTestMode:

        for net in cn.Net.Ecosystem.values():

            if net.ID == cn.Net.Champion:
                epoch_stats['Champion_parameter_count'].update(net.get_parameter_count())

            epoch_stats['Average_fitness'].update(net.fitness.absolute)

            if net.age == 0:
                epoch_stats['Average_initial_fitness'].update(net.fitness.absolute)

        # Global statistics
        for key, stat in Conf.Stats.items():
            Conf.Logger.add_scalar(key, stat.current_value, _epoch)

        # Epoch statistics
        for key, stat in epoch_stats.items():
            Conf.Logger.add_scalar(key, stat.mean, _epoch)
            stat.title = key
            save_stat(stat, f'/run_{_run}/epoch_{_epoch}')
コード例 #5
0
ファイル: cortex.py プロジェクト: cantordust/pycortex
def init():

    if get_rank() != 0:
        return

    """
    Initialise the ecosystem and generate initial species if speciation is enabled.
    """

    print("\n======[ Initialising ecosystem ]======\n")

    # Reset the static network attributes
    cn.Net.reset()

    # Reset the static species attributes
    cs.Species.reset()

    # Reset all statistics
    for key, val in Conf.Stats.items():
        Conf.Stats[key] = Stat.SMAStat()

    # Sanity check on the species count
    if cs.Species.Enabled:
        assert cs.Species.Init.Count > 0, "Invalid initial species count %r" % cs.Species.Init.Count
        assert cs.Species.Init.Count <= cn.Net.Init.Count, "Initial species count (%r) is greater than the initial network count (%r)" % (cs.Species.Init.Count, cn.Net.Init.Count)

    else:
        cs.Species.Init.Count = 1
        cs.Species.Max.Count = 1

    # Population size (the number of networks per species).
    net_quota = cn.Net.Init.Count // cs.Species.Init.Count

    # Initial proto-net.
    # This is the first self-replicating prion to spring
    # into existence in the digital primordial bouillon.
    proto_net = cn.Net(_isolated = True)

    probabilities = {
                    'layer': 1,
                    'node': 1,
                    'stride': 1,
                    'kernel': 1
                    }

    if cs.Species.Enabled:
        # Generate proto-species and proto-nets
        while True:

            # Generate proto-species.
            proto_species = cs.Species(_genome = proto_net.get_genome())

            # Generate proto-nets for this proto-species.
            for n in range(net_quota):
                proto_net = cn.Net(_species = proto_species)

            if len(cs.Species.Populations) == cs.Species.Init.Count:
                break

            proto_net = cn.Net(_species = proto_species, _isolated = True)

            while cs.Species.find(proto_net.get_genome()) != 0:
                proto_net.mutate(_probabilities = probabilities, _structure = True, _parameters = True)

    else:

        # Generate proto-species.
        proto_species = cs.Species(_genome = proto_net.get_genome())

        # Generate proto-nets.
        for n in range(net_quota):
            proto_net = cn.Net(_species = proto_species)
            proto_net.mutate(_probabilities = probabilities, _structure = True, _parameters = True)

    print(f'Network count: {len(cn.Net.Ecosystem)}')
    for net in cn.Net.Ecosystem.values():
        print(net.as_str())

    print(f'Species count: {len(cs.Species.Populations)}')
    for species in cs.Species.Populations.values():
        print(species.as_str())
コード例 #6
0
ファイル: cortex.py プロジェクト: cantordust/pycortex
class Conf:
    ExperimentName = 'Experiment'
    Runs = 1
    Epochs = 50
    Episodes = 5

    DiscountFactor = 0.99
    Epsilon = 1.0e-8

    Device = torch.device('cpu')
    UseCuda = False
    GPUCount = 0
    MaxWorkers = 1

    DataDir = ''
    DownloadData = False
    DataLoadArgs = {}
    DataLoader = None

    TrainBatchSize = 128
    TestBatchSize = 1000
    TrainPortion = None

    LogDir = './logs'
    LogInterval = 500
    Logger = None

    OutputFunction = tnf.log_softmax
    OutputFunctionArgs = {'dim': 1}

    LossFunction = tnf.nll_loss

    Optimiser = torch.optim.Adadelta
    OptimiserArgs = {
                'lr': 1.0
                }

    UnitTestMode = False

    Evaluator = None

    GPUs = []
    Workers = []
    Tag = Tags.Start

    Stats = {
            'Species_count': Stat.SMAStat(),
            'Network_count': Stat.SMAStat(),
            'Champion_parameter_count': Stat.SMAStat(),
            'Highest_fitness': Stat.SMAStat()
            }

    def __init__(self,
                 _run,
                 _epoch,
                 _gpu_slot = None):
        self.run = _run
        self.epoch = _epoch
        self.episodes = Conf.Episodes
        self.gpu_slot = None

        self.train_batch_size = Conf.TrainBatchSize
        self.test_batch_size = Conf.TestBatchSize
        self.train_portion = Conf.TrainPortion

        self.data_dir = Conf.DataDir
        self.data_load_args = Conf.DataLoadArgs
        self.download_data = Conf.DownloadData

        self.device = Conf.Device

        self.log_interval = Conf.LogInterval

        self.loss_function = Conf.LossFunction

        self.optimiser = Conf.Optimiser
        self.optimiser_args = Conf.OptimiserArgs

        self.output_function = Conf.OutputFunction
        self.output_function_args = Conf.OutputFunctionArgs

        self.evaluator = Conf.Evaluator
        self.data_loader = Conf.DataLoader

        self.discount_factor = Conf.DiscountFactor
        self.epsilon = Conf.Epsilon