How to use the tensorforce.TensorforceError function in Tensorforce

To help you get started, we’ve selected a few Tensorforce examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github tensorforce / tensorforce / tensorforce / core / layers / layer.py View on Github external
def validated_tf_function(x, previous):
                if not util.is_consistent_with_value_spec(value_spec=self.input_spec, x=x):
                    raise TensorforceError("Invalid input arguments for tf_apply.")

                # previous spec!

                x, previous = tf_function(x=x, previous=previous)

                if not util.is_consistent_with_value_spec(value_spec=self.output_spec, x=x):
                    raise TensorforceError("Invalid output arguments for tf_apply.")

                return x, previous
github tensorforce / tensorforce / tensorforce / core / layers / misc.py View on Github external
def __init__(
        self, name, tensors, aggregation='concat', axis=0, input_spec=None, summary_labels=None
    ):
        if not isinstance(tensors, str) and not util.is_iterable(x=tensors):
            raise TensorforceError.type(name='retrieve', argument='tensors', value=tensors)
        elif util.is_iterable(x=tensors) and len(tensors) == 0:
            raise TensorforceError.value(name='retrieve', argument='tensors', value=tensors)
        if aggregation not in ('concat', 'product', 'stack', 'sum'):
            raise TensorforceError.value(
                name='retrieve', argument='aggregation', value=aggregation
            )

        self.tensors = (tensors,) if isinstance(tensors, str) else tuple(tensors)
        self.aggregation = aggregation
        self.axis = axis

        super().__init__(
            name=name, input_spec=input_spec, summary_labels=summary_labels, l2_regularization=0.0
        )

        self.input_spec = None
github tensorforce / tensorforce / tensorforce / core / distributions / gaussian.py View on Github external
input_spec=input_spec
            )
            self.log_stddev = self.add_module(
                name='log-stddev', module='linear', modules=layer_modules, size=action_size,
                input_spec=input_spec
            )

        else:
            if len(self.embedding_shape) < 1 or len(self.embedding_shape) > 3:
                raise TensorforceError.unexpected()
            if self.embedding_shape[:-1] == self.action_spec['shape'][:-1]:
                size = self.action_spec['shape'][-1]
            elif self.embedding_shape[:-1] == self.action_spec['shape']:
                size = 0
            else:
                raise TensorforceError.unexpected()
            self.mean = self.add_module(
                name='mean', module='linear', modules=layer_modules, size=size,
                input_spec=input_spec
            )
            self.log_stddev = self.add_module(
                name='log-stddev', module='linear', modules=layer_modules, size=size,
                input_spec=input_spec
            )

        Module.register_tensor(
            name=(self.name + '-mean'), spec=dict(type='float', shape=self.action_spec['shape']),
            batched=True
        )
        Module.register_tensor(
            name=(self.name + '-stddev'), spec=dict(type='float', shape=self.action_spec['shape']),
            batched=True
github tensorforce / tensorforce / tensorforce / util.py View on Github external
min_value = value_spec.pop('min_value')
            max_value = value_spec.pop('max_value')
            if isinstance(min_value, np_dtype(dtype='float')):
                min_value = min_value.item()
            if isinstance(max_value, np_dtype(dtype='float')):
                max_value = max_value.item()
            if not isinstance(min_value, float):
                raise TensorforceError.type(
                    name=(value_type + ' spec'), argument='min_value', value=min_value
                )
            if not isinstance(max_value, float):
                raise TensorforceError.type(
                    name=(value_type + ' spec'), argument='max_value', value=max_value
                )
            if min_value >= max_value:
                raise TensorforceError.value(
                    name=(value_type + ' spec'), argument='min/max_value',
                    value=(min_value, max_value)
                )
            if return_normalized:
                normalized_spec['min_value'] = min_value
                normalized_spec['max_value'] = max_value

    if len(value_spec) > 0:
        raise TensorforceError.value(name=(value_type + ' spec'), value=tuple(value_spec))

    if return_normalized:
        return normalized_spec
    else:
        return True
github tensorforce / tensorforce / tensorforce / core / networks / layered.py View on Github external
def internals_spec(cls, layers=None, network=None, name=None, **kwargs):
        internals_spec = super().internals_spec(network=network)

        if network is None:
            assert layers is not None and name is not None

            for internal_name, spec in cls.internals_from_layers_spec(
                layers_spec=layers, layer_counter=Counter()
            ):
                internal_name = name + '-' + internal_name
                if internal_name in internals_spec:
                    raise TensorforceError.unexpected()
                internals_spec[internal_name] = spec

        else:
            assert layers is None and name is None

        return internals_spec
github tensorforce / tensorforce / tensorforce / core / module.py View on Github external
def retrieve_tensor(name):
        if name not in Module.global_tensors_spec:
            raise TensorforceError("Global tensor is not registered: {}.".format(name))

        for n in range(len(Module.global_scope) + 1):
            partial_scope = Module.global_scope[:len(Module.global_scope) - n]
            scoped_name = util.join_scopes(*partial_scope, name)
            if scoped_name in Module.global_tensors:
                break
        else:
            raise TensorforceError("Global tensor is not set: {}.".format(name))

        return Module.global_tensors[scoped_name]
github tensorforce / tensorforce / tensorforce / environments / openai_gym.py View on Github external
def flatten_state(state, states_spec):
        if isinstance(state, tuple):
            states = dict()
            for n, state in enumerate(state):
                if 'gymtpl{}-{}'.format(n, name) in states_spec:
                    spec = states_spec['gymtpl{}-{}'.format(n, name)]
                elif 'gymtpl{}'.format(n) in states_spec:
                    spec = states_spec['gymtpl{}'.format(n)]
                else:
                    raise TensorforceError.unexpected()
                state = OpenAIGym.flatten_state(state=state, states_spec=spec)
                if isinstance(state, dict):
                    for name, state in state.items():
                        states['gymtpl{}-{}'.format(n, name)] = state
                else:
                    states['gymtpl{}'.format(n)] = state
            return states

        elif isinstance(state, dict):
            states = dict()
            for state_name, state in state.items():
                if '{}-{}'.format(state_name, name) in states_spec:
                    spec = states_spec['{}-{}'.format(state_name, name)]
                elif state_name in states_spec:
                    spec = states_spec[state_name]
                else:
github tensorforce / tensorforce / tensorforce / agents / tensorforce.py View on Github external
baseline_policy=baseline_policy, baseline_optimizer=baseline_optimizer,
                baseline_objective=baseline_objective,
                preprocessing=preprocessing,
                exploration=exploration, variable_noise=variable_noise,
                l2_regularization=l2_regularization, entropy_regularization=entropy_regularization,
                name=name, device=device, parallel_interactions=parallel_interactions,
                buffer_observe=buffer_observe, seed=seed, execution=execution, saver=saver,
                summarizer=summarizer, recorder=recorder, config=config
            )

        if buffer_observe is True and parallel_interactions == 1 and summarizer is not None:
            buffer_observe = False

        if isinstance(update, int) or update['unit'] == 'timesteps':
            if buffer_observe is not True or parallel_interactions > 1:
                TensorforceError.unexpected()
            buffer_observe = False

        super().__init__(
            states=states, actions=actions, max_episode_timesteps=max_episode_timesteps,
            parallel_interactions=parallel_interactions, buffer_observe=buffer_observe, seed=seed,
            recorder=recorder
        )

        if isinstance(update, int):
            update = dict(unit='timesteps', batch_size=update)

        reward_estimation = dict(reward_estimation)
        if reward_estimation['horizon'] == 'episode':
            if max_episode_timesteps is None:
                raise TensorforceError.unexpected()
            reward_estimation['horizon'] = max_episode_timesteps
github tensorforce / tensorforce / tensorforce / core / models / tensorforce.py View on Github external
values_spec=self.values_spec
        )

        # Update mode
        if not all(key in ('batch_size', 'frequency', 'start', 'unit') for key in update):
            raise TensorforceError.value(name='update', value=list(update))
        # update: unit
        elif 'unit' not in update:
            raise TensorforceError.required(name='update', value='unit')
        elif update['unit'] not in ('timesteps', 'episodes'):
            raise TensorforceError.value(
                name='update', argument='unit', value=update['unit']
            )
        # update: batch_size
        elif 'batch_size' not in update:
            raise TensorforceError.required(name='update', value='batch_size')

        self.update_unit = update['unit']
        self.update_batch_size = self.add_module(
            name='update-batch-size', module=update['batch_size'], modules=parameter_modules,
            is_trainable=False, dtype='long'
        )
        if 'frequency' in update and update['frequency'] == 'never':
            self.update_frequency = 'never'
        else:
            self.update_frequency = self.add_module(
                name='update-frequency', module=update.get('frequency', update['batch_size']),
                modules=parameter_modules, is_trainable=False, dtype='long'
            )
            self.update_start = self.add_module(
                name='update-start', module=update.get('start', 0), modules=parameter_modules,
                is_trainable=False, dtype='long'
github tensorforce / tensorforce / tensorforce / core / layers / misc.py View on Github external
def default_input_spec(self):
        # from tensorforce.core.networks import Network

        # if not isinstance(self.parent, Network):
        #     raise TensorforceError.unexpected()

        # if self.layer not in self.parent.modules:
        #     raise TensorforceError.unexpected()

        # self.layer = self.parent.modules[self.layer]

        if self.layer not in Layer.layers:
            raise TensorforceError.unexpected()

        self.layer = Layer.layers[self.layer]

        return dict(self.layer.input_spec)