Search code examples
pythonneural-networkevolutionary-algorithm

In Python, what does object(**config) do?


Can someone please help explain what this means.

Background: 'network' is a class and represents a Neural Network object, it's constructor requires several inputs such as; nodes, inputs, outputs, num_functions etc. However, the python implementation I'm using as a reference uses a dictionary to load these parameters into the constructor (I believe that is whats going on). Can anyone help explain how this works network(**config)? Ps. I'm converting this to Java.

The constructor for the network class looks like this:

public network(int _graph_length, int _input_length, int _output_length, int _max_arity, int _function_length){

The dictionaries do this:

output is a dictionary used to store data.
config is a dictionary uses to load parameters for the NN.

And the code I don't understand is:

//Output data reset:
output.put("skipped", 0);
output.put("estimated", 0);

//if single mutation method:
if (config.get("speed") == "single"){       
    network.mutate = network.one_active_mutation;
    }

    parent = network(**config);
    yield parent;
while true: 
    //code to evolve networks here!

Solution

  • network(**config) will unpack the dictionary config and use the key value pairs in that dictionary as arguments to network.

    For example, these will all make the same call to func:

    def func(foo, bar):
        print foo, bar
    
    d = {'foo': 'value1', 'bar': 'value2'}
    
    func(**d)
    func(**{'bar': 'value2', 'foo': 'value1'})
    func(bar='value2', foo='value1')
    func('value1', 'value2')