def params2stack(params, net_config):
    """
    Converts a flattened parameter vector into a nice "stack" structure
    for us to work with. This is useful when you're building multilayer
    networks.
    stack = params2stack(params, netconfig)

    :param params: flattened parameter vector
    :param net_config: aux. variable containing network config.
    :return: stack structure (see above)

    """
    # Map the params (a vector into a stack of weights)
    depth = len(net_config['layer_sizes'])
    stack = [dict() for i in range(depth)]

    prev_layer_size = net_config['input_size']
    current_pos = 0

    for i in range(depth):
        # Extract weights
        wlen = prev_layer_size * net_config['layer_sizes'][i]
        stack[i]['w'] = params[current_pos:current_pos + wlen].reshape(net_config['layer_sizes'][i], prev_layer_size)
        current_pos = current_pos + wlen

        # Extract bias
        blen = net_config['layer_sizes'][i]
        stack[i]['b'] = params[current_pos:current_pos + blen]
        current_pos = current_pos + blen

        # Set previous layer size
        prev_layer_size = net_config['layer_sizes'][i]

    return stack