示例#1
0
        def fn(query=None, **kwargs):
            # Feed_dict dictionary
            feed_dict = dict()
            for key, arg in kwargs.items():
                if arg is None:
                    continue
                elif isinstance(arg, dict):
                    # Support single nesting (for states, internals, actions)
                    for key, arg in arg.items():
                        feed_dict[util.join_scopes(self.name, key) + '-input:0'] = arg
                else:
                    feed_dict[util.join_scopes(self.name, key) + '-input:0'] = arg
            if not all(isinstance(x, str) and x.endswith('-input:0') for x in feed_dict):
                raise TensorforceError.unexpected()

            # Fetches value/tuple
            fetches = util.fmap(function=(lambda x: x.name), xs=results)
            if query is not None:
                # If additional tensors are to be fetched
                query = util.fmap(
                    function=(lambda x: util.join_scopes(name, x) + '-output:0'), xs=query
                )
                if util.is_iterable(x=fetches):
                    fetches = tuple(fetches) + (query,)
                else:
                    fetches = (fetches, query)
            if not util.reduce_all(
                predicate=(lambda x: isinstance(x, str) and x.endswith('-output:0')), xs=fetches
            ):
                raise TensorforceError.unexpected()

            # TensorFlow session call
            fetched = self.monitored_session.run(fetches=fetches, feed_dict=feed_dict)

            return fetched
示例#2
0
 def assign_variable(self, *, variable, value):
     if variable.startswith(self.name + '/'):
         variable = variable[len(self.name) + 1:]
     module = self
     scope = variable.split('/')
     for _ in range(len(scope) - 1):
         module = module.modules[scope.pop(0)]
     fetches = util.join_scopes(self.name, variable) + '-assign'
     dtype = util.dtype(x=module.variables[scope[0]])
     feed_dict = {util.join_scopes(self.name, 'assignment-') + dtype + '-input:0': value}
     self.monitored_session.run(fetches=fetches, feed_dict=feed_dict)
示例#3
0
    def update_tensor(name, tensor):
        # 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_spec:
        #         break
        # else:
        #     raise TensorforceError("Global tensor is not registered: {}.".format(name))
        if name not in Module.global_tensors_spec:
            raise TensorforceError(
                "Global tensor is not registered: {}.".format(name))

        scoped_name = name
        spec = Module.global_tensors_spec[scoped_name]

        if not util.is_consistent_with_value_spec(value_spec=spec, x=tensor):
            raise TensorforceError(
                "Invalid overwriting tensor: {}.".format(tensor))

        scoped_name = util.join_scopes(*Module.global_scope, name)

        previous = Module.global_tensors.get(scoped_name)
        Module.global_tensors[scoped_name] = tensor

        return previous
示例#4
0
    def retrieve_tensor(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_spec:
        #         break
        # else:
        #     raise TensorforceError("Global tensor is not registered: {}.".format(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))

        # scoped_name = util.join_scopes(*Module.global_scope, name)

        # if scoped_name not in Module.global_tensors:
        #     raise TensorforceError("Global tensor is not set: {}.".format(scoped_name))

        return Module.global_tensors[scoped_name]
示例#5
0
    def retrieve_tensor(name):
        if name not in Module.global_tensors_spec:
            raise TensorforceError.value(
                name='Module.retrieve_tensor', argument='name', value=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.value(
                name='Module.retrieve_tensor', argument='name', value=name
            )

        return Module.global_tensors[scoped_name]
示例#6
0
    def update_tensor(name, tensor):
        if name not in Module.global_tensors_spec:
            raise TensorforceError("Global tensor is not registered: {}.".format(name))

        scoped_name = name
        spec = Module.global_tensors_spec[scoped_name]

        if not util.is_consistent_with_value_spec(value_spec=spec, x=tensor):
            raise TensorforceError("Invalid overwriting tensor: {}.".format(tensor))

        scoped_name = util.join_scopes(*Module.global_scope, name)

        previous = Module.global_tensors.get(scoped_name)
        Module.global_tensors[scoped_name] = tensor
        if Module.cond_counter == 0 and Module.while_counter == 0:
            Module.queryable_tensors[scoped_name] = tensor

        return previous
示例#7
0
    def update_tensor(name, tensor):
        if name not in Module.global_tensors_spec:
            raise TensorforceError.value(
                name='Module.update_tensor', argument='name', value=name
            )

        scoped_name = name
        spec = Module.global_tensors_spec[scoped_name]

        if not util.is_consistent_with_value_spec(value_spec=spec, x=tensor):
            raise TensorforceError.value(
                name='Module.update_tensor', argument='tensor', value=tensor
            )

        scoped_name = util.join_scopes(*Module.global_scope, name)

        previous = Module.global_tensors.get(scoped_name)
        Module.global_tensors[scoped_name] = tensor
        if Module.cond_counter == 0 and Module.while_counter == 0:
            Module.queryable_tensors[scoped_name] = tensor

        return previous
示例#8
0
 def summarize(self, *, summary, value, step=None):
     fetches = util.join_scopes(self.name, summary, 'write_summary', 'Const:0')
     feed_dict = {util.join_scopes(self.name, 'summarize-input:0'): value}
     if step is not None:
         feed_dict[util.join_scopes(self.name, 'summarize-step-input:0')] = step
     self.monitored_session.run(fetches=fetches, feed_dict=feed_dict)
示例#9
0
 def get_variable(self, *, variable):
     assert False, 'Not updated yet!'
     if not variable.startswith(self.name):
         variable = util.join_scopes(self.name, variable)
     fetches = variable + '-output:0'
     return self.monitored_session.run(fetches=fetches)