def from_spec(spec, kwargs=None): """ Creates a network from a specification dict. """ if isinstance(spec, list) and len(spec) > 0: # Default case is a list of dict() with each dict describing a layer. if type(spec[0]) is dict: network = util.get_object( obj=spec, default_object=LayeredNetwork, kwargs=kwargs ) # ComplexLayeredNetwork forced for testing if type(spec[0]) is list: # Spec contains List of List of Dict(), Complex network specification # Load "ComplexLayeredNetwork" here to avoid a recurring loop which fails from tensorforce.core.networks.complex_network import ComplexLayeredNetwork network = util.get_object( obj=spec, default_object=ComplexLayeredNetwork, kwargs=kwargs ) else: network = util.get_object( obj=spec, default_object=LayeredNetwork, kwargs=kwargs ) # If neither format, invalid spec and will fail on assert assert isinstance(network, Network) return network
def network_builder(inputs, summary_level=0): input_length = len(inputs) if input_length != 1: raise TensorForceError('Layered network must have only one input,' ' input length {} given.'.format(input_length)) x = next(iter(inputs.values())) internal_inputs = [] internal_outputs = [] internal_inits = [] layer_counter = Counter() for layer_config in layers_config: if callable(layer_config['type']): scope = layer_config['type'].__name__ + str(layer_counter[layer_config['type']]) else: scope = layer_config['type'] + str(layer_counter[layer_config['type']]) x = util.get_object( obj=layer_config, predefined=layers, kwargs=dict(x=x, scope=scope, summary_level=summary_level) ) layer_counter[layer_config['type']] += 1 if isinstance(x, list) or isinstance(x, tuple): assert len(x) == 4 internal_inputs.extend(x[1]) internal_outputs.extend(x[2]) internal_inits.extend(x[3]) x = x[0] if internal_inputs: return x, internal_inputs, internal_outputs, internal_inits else: return x
def from_spec(spec, kwargs=None): lr_schedule = util.get_object(obj=spec, predefined_objects=lr_schedulers, kwargs=kwargs) assert isinstance(lr_schedule, DecaySchedule) return lr_schedule
def network_builder(inputs): input_length = len(inputs) if input_length != 1: raise TensorForceError( 'Layered network must have only one input,' ' input length {} given.'.format(input_length)) x = next(iter(inputs.values())) internal_inputs = [] internal_outputs = [] internal_inits = [] for layer_config in layers_config: x = util.get_object(obj=layer_config, predefined=layers, kwargs=dict(x=x)) if isinstance(x, list) or isinstance(x, tuple): assert len(x) == 4 internal_inputs.extend(x[1]) internal_outputs.extend(x[2]) internal_inits.extend(x[3]) x = x[0] if internal_inputs: return x, internal_inputs, internal_outputs, internal_inits else: return x
def from_config(config, kwargs=None): """ Creates a solver from a specification dict. """ return util.get_object( obj=config, predefined=tensorforce.core.optimizers.solvers.solvers, kwargs=kwargs)
def from_spec(spec): """ Creates an exploration object from a specification dict. """ exploration = util.get_object( obj=spec, predefined_objects=tensorforce.core.explorations.explorations) assert isinstance(exploration, Exploration) return exploration
def from_spec(spec, kwargs): """ Creates an agent from a specification dict. """ agent = util.get_object(obj=spec, predefined_objects=tensorforce.agents.agents, kwargs=kwargs) assert isinstance(agent, Agent) return agent
def from_spec(spec, kwargs=None): """ Creates an optimizer from a specification dict. """ optimizer = util.get_object(obj=spec, predefined_objects=optimizers, kwargs=kwargs) assert isinstance(optimizer, Optimizer) return optimizer
def from_spec(spec, kwargs=None): """ Creates a network from a specification dict. """ network = util.get_object(obj=spec, default_object=LayeredNetwork, kwargs=kwargs) assert isinstance(network, Network) return network
def from_spec(spec, kwargs=None): """ Creates a layer from a specification dict. """ layer = util.get_object( obj=spec, predefined_objects=tensorforce.core.networks.layers, kwargs=kwargs) assert isinstance(layer, Layer) return layer
def from_spec(spec, kwargs=None): """ Creates a memory from a specification dict. """ memory = util.get_object( obj=spec, predefined_objects=tensorforce.core.memories.memories, kwargs=kwargs) assert isinstance(memory, Memory) return memory
def from_spec(spec, kwargs=None): """ Creates a baseline from a specification dict. """ baseline = util.get_object( obj=spec, predefined_objects=tensorforce.core.baselines.baselines, kwargs=kwargs) assert isinstance(baseline, Baseline) return baseline
def from_spec(spec, kwargs=None): """ Creates a distribution from a specification dict. """ distribution = util.get_object( obj=spec, predefined_objects=tensorforce.core.distributions.distributions, kwargs=kwargs) assert isinstance(distribution, Distribution) return distribution
def from_config(config): if not isinstance(config, list): config = [config] preprocessing = Preprocessing() for config in config: preprocessor = util.get_object( obj=config, predefined=tensorforce.core.preprocessing.preprocessors) preprocessing.add(preprocessor=preprocessor) return preprocessing
def from_config(config): """ Creates a baseline from a configuration dict. Args: config: Returns: """ return util.get_object(obj=config, predefined=tensorforce.core.baselines.baselines)
def __init__(self, layer, scope='tf-layer', summary_labels=(), **kwargs): """ Creates a new layer instance of a TensorFlow layer. Args: name: The name of the layer, one of 'dense'. **kwargs: Additional arguments passed on to the TensorFlow layer constructor. """ self.layer_spec = layer self.layer = util.get_object(obj=layer, predefined_objects=TFLayer.tf_layers, kwargs=kwargs) self.first_scope = None super(TFLayer, self).__init__(scope=scope, summary_labels=summary_labels)
def from_config(config, kwargs=None): """ Creates an optimizer from a configuration object. Args: config: Name of optimizer kwargs: Dict of optimizer hyperparameters Returns: """ return util.get_object( obj=config, predefined=tensorforce.core.optimizers.optimizers, kwargs=kwargs)
def from_spec(spec): """ Creates a preprocessing stack from a specification dict. """ if not isinstance(spec, list): spec = [spec] preprocessing = Preprocessing() for spec in spec: preprocessor = util.get_object(obj=spec, predefined_objects=tensorforce.core. preprocessing.preprocessors) assert isinstance(preprocessor, Preprocessor) preprocessing.add(preprocessor=preprocessor) return preprocessing
def from_spec(spec): """ Creates a preprocessing stack from a specification dict. """ if isinstance(spec, dict): spec = [spec] stack = PreprocessorStack() for spec in spec: preprocessor = util.get_object(obj=spec, predefined_objects=tensorforce.core. preprocessing.preprocessors) assert isinstance(preprocessor, Preprocessor) stack.preprocessors.append(preprocessor) return stack
def from_spec(spec, kwargs=None): """ Creates a preprocessing stack from a specification dict. """ if isinstance(spec, dict): spec = [spec] stack = PreprocessorStack() for preprocessor_spec in spec: # need to deep copy, otherwise will add first processors spec_ to kwargs to second processor preprocessor_kwargs = copy.deepcopy(kwargs) preprocessor = util.get_object(obj=preprocessor_spec, predefined_objects=tensorforce.core. preprocessors.preprocessors, kwargs=preprocessor_kwargs) assert isinstance(preprocessor, Preprocessor) stack.preprocessors.append(preprocessor) return stack
def from_config(config): return util.get_object( obj=config, predefined=tensorforce.core.explorations.explorations )
def from_config(config, kwargs=None): return util.get_object(obj=config, predefined=tensorforce.core.memories.memories, kwargs=kwargs)
def from_config(config, kwargs=None): return util.get_object( obj=config, predefined=tensorforce.core.distributions.distributions, kwargs=kwargs)
def from_config(config, kwargs=None): return util.get_object( obj=config, predefined=tensorforce.core.optimizers.optimizers, kwargs=kwargs )