Search is not available for this dataset
text
stringlengths
75
104k
def group(self, *args): """Returns one or more subgroups of the match. Each argument is either a group index or a group name.""" if len(args) == 0: args = (0,) grouplist = [] for group in args: grouplist.append(self._get_slice(self._get_index(group), None)) if len(grouplist) == 1: return grouplist[0] else: return tuple(grouplist)
def fast_search(self, pattern_codes): """Skips forward in a string as fast as possible using information from an optimization info block.""" # pattern starts with a known prefix # <5=length> <6=skip> <7=prefix data> <overlap data> flags = pattern_codes[2] prefix_len = pattern_codes[5] prefix_skip = pattern_codes[6] # don't really know what this is good for prefix = pattern_codes[7:7 + prefix_len] overlap = pattern_codes[7 + prefix_len - 1:pattern_codes[1] + 1] pattern_codes = pattern_codes[pattern_codes[1] + 1:] i = 0 string_position = self.string_position while string_position < self.end: while True: if ord(self.string[string_position]) != prefix[i]: if i == 0: break else: i = overlap[i] else: i += 1 if i == prefix_len: # found a potential match self.start = string_position + 1 - prefix_len self.string_position = string_position + 1 \ - prefix_len + prefix_skip if flags & SRE_INFO_LITERAL: return True # matched all of pure literal pattern if self.match(pattern_codes[2 * prefix_skip:]): return True i = overlap[i] break string_position += 1 return False
def push_new_context(self, pattern_offset): """Creates a new child context of this context and pushes it on the stack. pattern_offset is the offset off the current code position to start interpreting from.""" child_context = _MatchContext(self.state, self.pattern_codes[self.code_position + pattern_offset:]) self.state.context_stack.append(child_context) return child_context
def match(self, context): """Returns True if the current context matches, False if it doesn't and None if matching is not finished, ie must be resumed after child contexts have been matched.""" while context.remaining_codes() > 0 and context.has_matched is None: opcode = context.peek_code() if not self.dispatch(opcode, context): return None if context.has_matched is None: context.has_matched = False return context.has_matched
def dispatch(self, opcode, context): """Dispatches a context on a given opcode. Returns True if the context is done matching, False if it must be resumed when next encountered.""" if id(context) in self.executing_contexts: generator = self.executing_contexts[id(context)] del self.executing_contexts[id(context)] has_finished = generator.next() else: method = self.DISPATCH_TABLE.get(opcode, _OpcodeDispatcher.unknown) has_finished = method(self, context) if hasattr(has_finished, "next"): # avoid using the types module generator = has_finished has_finished = generator.next() if not has_finished: self.executing_contexts[id(context)] = generator return has_finished
def check_charset(self, ctx, char): """Checks whether a character matches set of arbitrary length. Assumes the code pointer is at the first member of the set.""" self.set_dispatcher.reset(char) save_position = ctx.code_position result = None while result is None: result = self.set_dispatcher.dispatch(ctx.peek_code(), ctx) ctx.code_position = save_position return result
def count_repetitions(self, ctx, maxcount): """Returns the number of repetitions of a single item, starting from the current string position. The code pointer is expected to point to a REPEAT_ONE operation (with the repeated 4 ahead).""" count = 0 real_maxcount = ctx.state.end - ctx.string_position if maxcount < real_maxcount and maxcount != MAXREPEAT: real_maxcount = maxcount # XXX could special case every single character pattern here, as in C. # This is a general solution, a bit hackisch, but works and should be # efficient. code_position = ctx.code_position string_position = ctx.string_position ctx.skip_code(4) reset_position = ctx.code_position while count < real_maxcount: # this works because the single character pattern is followed by # a success opcode ctx.code_position = reset_position self.dispatch(ctx.peek_code(), ctx) if ctx.has_matched is False: # could be None as well break count += 1 ctx.has_matched = None ctx.code_position = code_position ctx.string_position = string_position return count
def extract(s): """Return (sign, intpart, fraction, expo) or raise an exception: sign is '+' or '-' intpart is 0 or more digits beginning with a nonzero fraction is 0 or more digits expo is an integer""" res = decoder.match(s) if res is None: raise NotANumber, s sign, intpart, fraction, exppart = res.group(1,2,3,4) if sign == '+': sign = '' if fraction: fraction = fraction[1:] if exppart: expo = int(exppart[1:]) else: expo = 0 return sign, intpart, fraction, expo
def unexpo(intpart, fraction, expo): """Remove the exponent by changing intpart and fraction.""" if expo > 0: # Move the point left f = len(fraction) intpart, fraction = intpart + fraction[:expo], fraction[expo:] if expo > f: intpart = intpart + '0'*(expo-f) elif expo < 0: # Move the point right i = len(intpart) intpart, fraction = intpart[:expo], intpart[expo:] + fraction if expo < -i: fraction = '0'*(-expo-i) + fraction return intpart, fraction
def roundfrac(intpart, fraction, digs): """Round or extend the fraction to size digs.""" f = len(fraction) if f <= digs: return intpart, fraction + '0'*(digs-f) i = len(intpart) if i+digs < 0: return '0'*-digs, '' total = intpart + fraction nextdigit = total[i+digs] if nextdigit >= '5': # Hard case: increment last digit, may have carry! n = i + digs - 1 while n >= 0: if total[n] != '9': break n = n-1 else: total = '0' + total i = i+1 n = 0 total = total[:n] + chr(ord(total[n]) + 1) + '0'*(len(total)-n-1) intpart, fraction = total[:i], total[i:] if digs >= 0: return intpart, fraction[:digs] else: return intpart[:digs] + '0'*-digs, ''
def fix(x, digs): """Format x as [-]ddd.ddd with 'digs' digits after the point and at least one digit before. If digs <= 0, the point is suppressed.""" if type(x) != type(''): x = repr(x) try: sign, intpart, fraction, expo = extract(x) except NotANumber: return x intpart, fraction = unexpo(intpart, fraction, expo) intpart, fraction = roundfrac(intpart, fraction, digs) while intpart and intpart[0] == '0': intpart = intpart[1:] if intpart == '': intpart = '0' if digs > 0: return sign + intpart + '.' + fraction else: return sign + intpart
def sci(x, digs): """Format x as [-]d.dddE[+-]ddd with 'digs' digits after the point and exactly one digit before. If digs is <= 0, one digit is kept and the point is suppressed.""" if type(x) != type(''): x = repr(x) sign, intpart, fraction, expo = extract(x) if not intpart: while fraction and fraction[0] == '0': fraction = fraction[1:] expo = expo - 1 if fraction: intpart, fraction = fraction[0], fraction[1:] expo = expo - 1 else: intpart = '0' else: expo = expo + len(intpart) - 1 intpart, fraction = intpart[0], intpart[1:] + fraction digs = max(0, digs) intpart, fraction = roundfrac(intpart, fraction, digs) if len(intpart) > 1: intpart, fraction, expo = \ intpart[0], intpart[1:] + fraction[:-1], \ expo + len(intpart) - 1 s = sign + intpart if digs > 0: s = s + '.' + fraction e = repr(abs(expo)) e = '0'*(3-len(e)) + e if expo < 0: e = '-' + e else: e = '+' + e return s + 'e' + e
def getrandbits(self, k): """getrandbits(k) -> x. Generates an int with k random bits.""" if k <= 0: raise ValueError('number of bits must be greater than zero') if k != int(k): raise TypeError('number of bits should be an integer') numbytes = (k + 7) // 8 # bits / 8 and rounded up x = _int_from_bytes(_gorandom(numbytes)) return x >> (numbytes * 8 - k)
def _randbelow(self, n): """Return a random int in the range [0,n).""" # TODO # change once int.bit_length is implemented. # k = n.bit_length() k = _int_bit_length(n) r = self.getrandbits(k) while r >= n: r = self.getrandbits(k) return r
def getopt(args, shortopts, longopts = []): """getopt(args, options[, long_options]) -> opts, args Parses command line options and parameter list. args is the argument list to be parsed, without the leading reference to the running program. Typically, this means "sys.argv[1:]". shortopts is the string of option letters that the script wants to recognize, with options that require an argument followed by a colon (i.e., the same format that Unix getopt() uses). If specified, longopts is a list of strings with the names of the long options which should be supported. The leading '--' characters should not be included in the option name. Options which require an argument should be followed by an equal sign ('='). The return value consists of two elements: the first is a list of (option, value) pairs; the second is the list of program arguments left after the option list was stripped (this is a trailing slice of the first argument). Each option-and-value pair returned has the option as its first element, prefixed with a hyphen (e.g., '-x'), and the option argument as its second element, or an empty string if the option has no argument. The options occur in the list in the same order in which they were found, thus allowing multiple occurrences. Long and short options may be mixed. """ opts = [] if type(longopts) == type(""): longopts = [longopts] else: longopts = list(longopts) while args and args[0].startswith('-') and args[0] != '-': if args[0] == '--': args = args[1:] break if args[0].startswith('--'): opts, args = do_longs(opts, args[0][2:], longopts, args[1:]) else: opts, args = do_shorts(opts, args[0][1:], shortopts, args[1:]) return opts, args
def gnu_getopt(args, shortopts, longopts = []): """getopt(args, options[, long_options]) -> opts, args This function works like getopt(), except that GNU style scanning mode is used by default. This means that option and non-option arguments may be intermixed. The getopt() function stops processing options as soon as a non-option argument is encountered. If the first character of the option string is `+', or if the environment variable POSIXLY_CORRECT is set, then option processing stops as soon as a non-option argument is encountered. """ opts = [] prog_args = [] if isinstance(longopts, str): longopts = [longopts] else: longopts = list(longopts) # Allow options after non-option arguments? if shortopts.startswith('+'): shortopts = shortopts[1:] all_options_first = True elif os.environ.get("POSIXLY_CORRECT"): all_options_first = True else: all_options_first = False while args: if args[0] == '--': prog_args += args[1:] break if args[0][:2] == '--': opts, args = do_longs(opts, args[0][2:], longopts, args[1:]) elif args[0][:1] == '-' and args[0] != '-': opts, args = do_shorts(opts, args[0][1:], shortopts, args[1:]) else: if all_options_first: prog_args += args break else: prog_args.append(args[0]) args = args[1:] return opts, prog_args
def filter(names, pat): """Return the subset of the list NAMES that match PAT""" import os # import posixpath result=[] # pat=os.path.normcase(pat) try: re_pat = _cache[pat] except KeyError: res = translate(pat) if len(_cache) >= _MAXCACHE: # _cache.clear() globals()['_cache'] = {} _cache[pat] = re_pat = re.compile(res) match = re_pat.match # if os.path is posixpath: if 1: # normcase on posix is NOP. Optimize it away from the loop. for name in names: if match(name): result.append(name) else: for name in names: if match(os.path.normcase(name)): result.append(name) return result
def fnmatchcase(name, pat): """Test whether FILENAME matches PATTERN, including case. This is a version of fnmatch() which doesn't case-normalize its arguments. """ try: re_pat = _cache[pat] except KeyError: res = translate(pat) if len(_cache) >= _MAXCACHE: # _cache.clear() globals()['_cache'] = {} _cache[pat] = re_pat = re.compile(res) return re_pat.match(name) is not None
def translate(pat): """Translate a shell PATTERN to a regular expression. There is no way to quote meta-characters. """ i, n = 0, len(pat) res = '' while i < n: c = pat[i] i = i+1 if c == '*': res = res + '.*' elif c == '?': res = res + '.' elif c == '[': j = i if j < n and pat[j] == '!': j = j+1 if j < n and pat[j] == ']': j = j+1 while j < n and pat[j] != ']': j = j+1 if j >= n: res = res + '\\[' else: stuff = pat[i:j].replace('\\','\\\\') i = j+1 if stuff[0] == '!': stuff = '^' + stuff[1:] elif stuff[0] == '^': stuff = '\\' + stuff res = '%s[%s]' % (res, stuff) else: res = res + re.escape(c) return res + '\Z(?ms)'
def task_done(self): """Indicate that a formerly enqueued task is complete. Used by Queue consumer threads. For each get() used to fetch a task, a subsequent call to task_done() tells the queue that the processing on the task is complete. If a join() is currently blocking, it will resume when all items have been processed (meaning that a task_done() call was received for every item that had been put() into the queue). Raises a ValueError if called more times than there were items placed in the queue. """ self.all_tasks_done.acquire() try: unfinished = self.unfinished_tasks - 1 if unfinished <= 0: if unfinished < 0: raise ValueError('task_done() called too many times') self.all_tasks_done.notify_all() self.unfinished_tasks = unfinished finally: self.all_tasks_done.release()
def qsize(self): """Return the approximate size of the queue (not reliable!).""" self.mutex.acquire() n = self._qsize() self.mutex.release() return n
def empty(self): """Return True if the queue is empty, False otherwise (not reliable!).""" self.mutex.acquire() n = not self._qsize() self.mutex.release() return n
def full(self): """Return True if the queue is full, False otherwise (not reliable!).""" self.mutex.acquire() n = 0 < self.maxsize == self._qsize() self.mutex.release() return n
def put(self, item, block=True, timeout=None): """Put an item into the queue. If optional args 'block' is true and 'timeout' is None (the default), block if necessary until a free slot is available. If 'timeout' is a non-negative number, it blocks at most 'timeout' seconds and raises the Full exception if no free slot was available within that time. Otherwise ('block' is false), put an item on the queue if a free slot is immediately available, else raise the Full exception ('timeout' is ignored in that case). """ self.not_full.acquire() try: if self.maxsize > 0: if not block: if self._qsize() == self.maxsize: raise Full elif timeout is None: while self._qsize() == self.maxsize: self.not_full.wait() elif timeout < 0: raise ValueError("'timeout' must be a non-negative number") else: endtime = _time() + timeout while self._qsize() == self.maxsize: remaining = endtime - _time() if remaining <= 0.0: raise Full self.not_full.wait(remaining) self._put(item) self.unfinished_tasks += 1 self.not_empty.notify() finally: self.not_full.release()
def calculate_transitive_deps(modname, script, gopath): """Determines all modules that script transitively depends upon.""" deps = set() def calc(modname, script): if modname in deps: return deps.add(modname) for imp in collect_imports(modname, script, gopath): if imp.is_native: deps.add(imp.name) continue parts = imp.name.split('.') calc(imp.name, imp.script) if len(parts) == 1: continue # For submodules, the parent packages are also deps. package_dir, filename = os.path.split(imp.script) if filename == '__init__.py': package_dir = os.path.dirname(package_dir) for i in xrange(len(parts) - 1, 0, -1): modname = '.'.join(parts[:i]) script = os.path.join(package_dir, '__init__.py') calc(modname, script) package_dir = os.path.dirname(package_dir) calc(modname, script) deps.remove(modname) return deps
def _make_future_features(node): """Processes a future import statement, returning set of flags it defines.""" assert isinstance(node, ast.ImportFrom) assert node.module == '__future__' features = FutureFeatures() for alias in node.names: name = alias.name if name in _FUTURE_FEATURES: if name not in _IMPLEMENTED_FUTURE_FEATURES: msg = 'future feature {} not yet implemented by grumpy'.format(name) raise util.ParseError(node, msg) setattr(features, name, True) elif name == 'braces': raise util.ParseError(node, 'not a chance') elif name not in _REDUNDANT_FUTURE_FEATURES: msg = 'future feature {} is not defined'.format(name) raise util.ParseError(node, msg) return features
def parse_future_features(mod): """Accumulates a set of flags for the compiler __future__ imports.""" assert isinstance(mod, ast.Module) found_docstring = False for node in mod.body: if isinstance(node, ast.ImportFrom): if node.module == '__future__': return node, _make_future_features(node) break elif isinstance(node, ast.Expr) and not found_docstring: if not isinstance(node.value, ast.Str): break found_docstring = True else: break return None, FutureFeatures()
def contextmanager(func): """@contextmanager decorator. Typical usage: @contextmanager def some_generator(<arguments>): <setup> try: yield <value> finally: <cleanup> This makes this: with some_generator(<arguments>) as <variable>: <body> equivalent to this: <setup> try: <variable> = <value> <body> finally: <cleanup> """ @wraps(func) def helper(*args, **kwds): return GeneratorContextManager(func(*args, **kwds)) return helper
def nested(*managers): """Combine multiple context managers into a single nested context manager. This function has been deprecated in favour of the multiple manager form of the with statement. The one advantage of this function over the multiple manager form of the with statement is that argument unpacking allows it to be used with a variable number of context managers as follows: with nested(*managers): do_something() """ warn("With-statements now directly support multiple context managers", DeprecationWarning, 3) exits = [] vars = [] exc = (None, None, None) try: for mgr in managers: exit = mgr.__exit__ enter = mgr.__enter__ vars.append(enter()) exits.append(exit) yield vars except: exc = sys.exc_info() finally: while exits: exit = exits.pop() try: if exit(*exc): exc = (None, None, None) except: exc = sys.exc_info() if exc != (None, None, None): # Don't rely on sys.exc_info() still containing # the right information. Another exception may # have been raised and caught by an exit method raise exc[0], exc[1], exc[2]
def decode(self, s, _w=WHITESPACE.match): """Return the Python representation of ``s`` (a ``str`` or ``unicode`` instance containing a JSON document) """ obj, end = self.raw_decode(s, idx=_w(s, 0).end()) end = _w(s, end).end() if end != len(s): raise ValueError(errmsg("Extra data", s, end, len(s))) return obj
def tf_loss(self, states, internals, reward, update, reference=None): """ Creates the TensorFlow operations for calculating the L2 loss between predicted state values and actual rewards. Args: states: Dict of state tensors. internals: List of prior internal state tensors. reward: Reward tensor. update: Boolean tensor indicating whether this call happens during an update. reference: Optional reference tensor(s), in case of a comparative loss. Returns: Loss tensor """ prediction = self.predict(states=states, internals=internals, update=update) return tf.nn.l2_loss(t=(prediction - reward))
def get_variables(self, include_nontrainable=False): """ Returns the TensorFlow variables used by the baseline. Returns: List of variables """ if include_nontrainable: return [self.all_variables[key] for key in sorted(self.all_variables)] else: return [self.variables[key] for key in sorted(self.variables)]
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 reset(self): """ same as step (no kwargs to pass), but needs to block and return observation_dict - stores the received observation in self.last_observation """ # Send command. self.protocol.send({"cmd": "reset"}, self.socket) # Wait for response. response = self.protocol.recv(self.socket) # Extract observations. return self.extract_observation(response)
def execute(self, action): """ Executes a single step in the UE4 game. This step may be comprised of one or more actual game ticks for all of which the same given action- and axis-inputs (or action number in case of discretized actions) are repeated. UE4 distinguishes between action-mappings, which are boolean actions (e.g. jump or dont-jump) and axis-mappings, which are continuous actions like MoveForward with values between -1.0 (run backwards) and 1.0 (run forwards), 0.0 would mean: stop. """ action_mappings, axis_mappings = [], [] # TODO: what if more than one actions are passed? # Discretized -> each action is an int if self.discretize_actions: # Pull record from discretized_actions, which will look like: [A, Right, SpaceBar]. combination = self.discretized_actions[action] # Translate to {"axis_mappings": [('A', 1.0), (Right, 1.0)], "action_mappings": [(SpaceBar, True)]} for key, value in combination: # Action mapping (True or False). if isinstance(value, bool): action_mappings.append((key, value)) # Axis mapping: always use 1.0 as value as UE4 already multiplies with the correct scaling factor. else: axis_mappings.append((key, value)) # Non-discretized: Each action is a dict of action- and axis-mappings defined in UE4 game's input settings. # Re-translate Incoming action names into keyboard keys for the server. elif action: try: action_mappings, axis_mappings = self.translate_abstract_actions_to_keys(action) except KeyError as e: raise TensorForceError("Action- or axis-mapping with name '{}' not defined in connected UE4 game!". format(e)) # message = {"cmd": "step", 'delta_time': 0.33, # 'actions': [('X', True), ('Y', False)], # 'axes': [('Left': 1.0), ('Up': -1.0)] # } message = dict( cmd="step", delta_time=self.delta_time, num_ticks=self.num_ticks, actions=action_mappings, axes=axis_mappings ) self.protocol.send(message, self.socket) # Wait for response (blocks). response = self.protocol.recv(self.socket) r = response.pop(b"_reward", 0.0) is_terminal = response.pop(b"_is_terminal", False) obs = self.extract_observation(response) # Cache last observation self.last_observation = obs return obs, is_terminal, r
def translate_abstract_actions_to_keys(self, abstract): """ Translates a list of tuples ([pretty mapping], [value]) to a list of tuples ([some key], [translated value]) each single item in abstract will undergo the following translation: Example1: we want: "MoveRight": 5.0 possible keys for the action are: ("Right", 1.0), ("Left", -1.0) result: "Right": 5.0 * 1.0 = 5.0 Example2: we want: "MoveRight": -0.5 possible keys for the action are: ("Left", -1.0), ("Right", 1.0) result: "Left": -0.5 * -1.0 = 0.5 (same as "Right": -0.5) """ # Solve single tuple with name and value -> should become a list (len=1) of this tuple. if len(abstract) >= 2 and not isinstance(abstract[1], (list, tuple)): abstract = list((abstract,)) # Now go through the list and translate each axis into an actual keyboard key (or mouse event/etc..). actions, axes = [], [] for a in abstract: # first_key = key-name (action mapping or discretized axis mapping) OR tuple (key-name, scale) (continuous # axis mapping) first_key = self.action_space_desc[a[0]]["keys"][0] # action mapping if isinstance(first_key, (bytes, str)): actions.append((first_key, a[1])) # axis mapping elif isinstance(first_key, tuple): axes.append((first_key[0], a[1] * first_key[1])) else: raise TensorForceError("action_space_desc contains unsupported type for key {}!".format(a[0])) return actions, axes
def discretize_action_space_desc(self): """ Creates a list of discrete action(-combinations) in case we want to learn with a discrete set of actions, but only have action-combinations (maybe even continuous) available from the env. E.g. the UE4 game has the following action/axis-mappings: ```javascript { 'Fire': {'type': 'action', 'keys': ('SpaceBar',)}, 'MoveRight': {'type': 'axis', 'keys': (('Right', 1.0), ('Left', -1.0), ('A', -1.0), ('D', 1.0))}, } ``` -> this method will discretize them into the following 6 discrete actions: ```javascript [ [(Right, 0.0),(SpaceBar, False)], [(Right, 0.0),(SpaceBar, True)] [(Right, -1.0),(SpaceBar, False)], [(Right, -1.0),(SpaceBar, True)], [(Right, 1.0),(SpaceBar, False)], [(Right, 1.0),(SpaceBar, True)], ] ``` """ # Put all unique_keys lists in one list and itertools.product that list. unique_list = [] for nice, record in self.action_space_desc.items(): list_for_record = [] if record["type"] == "axis": # The main key for this record (always the first one) head_key = record["keys"][0][0] # The reference value (divide by this one to get the others) head_value = record["keys"][0][1] # The zero key (idle action; axis scale=0.0) list_for_record.append((head_key, 0.0)) set_ = set() for key_and_scale in self.action_space_desc[nice]["keys"]: # Build unique lists of mappings (each axis value should only be represented once). if key_and_scale[1] not in set_: list_for_record.append((head_key, key_and_scale[1] / head_value)) set_.add(key_and_scale[1]) else: # Action-mapping list_for_record = [(record["keys"][0], False), (record["keys"][0], True)] unique_list.append(list_for_record) def so(in_): # in_ is List[Tuple[str,any]] -> sort by concat'd sequence of str(any's) st = "" for i in in_: st += str(i[1]) return st # Then sort and get the entire list of all possible sorted meaningful key-combinations. combinations = list(itertools.product(*unique_list)) combinations = list(map(lambda x: sorted(list(x), key=lambda y: y[0]), combinations)) combinations = sorted(combinations, key=so) # Store that list as discretized_actions. self.discretized_actions = combinations
def reset(self): """ Resets the environment to its initialization state. This method needs to be called to start a new episode after the last episode ended. :return: initial state """ self.level.reset() # optional: episode=-1, seed=None return self.level.observations()[self.state_attribute]
def execute(self, action): """ Pass action to universe environment, return reward, next step, terminal state and additional info. :param action: action to execute as numpy array, should have dtype np.intc and should adhere to the specification given in DeepMindLabEnvironment.action_spec(level_id) :return: dict containing the next state, the reward, and a boolean indicating if the next state is a terminal state """ adjusted_action = list() for action_spec in self.level.action_spec(): if action_spec['min'] == -1 and action_spec['max'] == 1: adjusted_action.append(action[action_spec['name']] - 1) else: adjusted_action.append(action[action_spec['name']]) # clip? action = np.array(adjusted_action, dtype=np.intc) reward = self.level.step(action=action, num_steps=self.repeat_action) state = self.level.observations()['RGB_INTERLACED'] terminal = not self.level.is_running() return state, terminal, reward
def tf_solve(self, fn_x, x_init, b): """ Iteratively solves the system of linear equations $A x = b$. Args: fn_x: A callable returning the left-hand side $A x$ of the system of linear equations. x_init: Initial solution guess $x_0$, zero vector if None. b: The right-hand side $b$ of the system of linear equations. Returns: A solution $x$ to the problem as given by the solver. """ return super(ConjugateGradient, self).tf_solve(fn_x, x_init, b)
def tf_initialize(self, x_init, b): """ Initialization step preparing the arguments for the first iteration of the loop body: $x_0, 0, p_0, r_0, r_0^2$. Args: x_init: Initial solution guess $x_0$, zero vector if None. b: The right-hand side $b$ of the system of linear equations. Returns: Initial arguments for tf_step. """ if x_init is None: # Initial guess is zero vector if not given. x_init = [tf.zeros(shape=util.shape(t)) for t in b] initial_args = super(ConjugateGradient, self).tf_initialize(x_init) # r_0 := b - A * x_0 # c_0 := r_0 conjugate = residual = [t - fx for t, fx in zip(b, self.fn_x(x_init))] # r_0^2 := r^T * r squared_residual = tf.add_n(inputs=[tf.reduce_sum(input_tensor=(res * res)) for res in residual]) return initial_args + (conjugate, residual, squared_residual)
def tf_step(self, x, iteration, conjugate, residual, squared_residual): """ Iteration loop body of the conjugate gradient algorithm. Args: x: Current solution estimate $x_t$. iteration: Current iteration counter $t$. conjugate: Current conjugate $c_t$. residual: Current residual $r_t$. squared_residual: Current squared residual $r_t^2$. Returns: Updated arguments for next iteration. """ x, next_iteration, conjugate, residual, squared_residual = super(ConjugateGradient, self).tf_step( x, iteration, conjugate, residual, squared_residual ) # Ac := A * c_t A_conjugate = self.fn_x(conjugate) # TODO: reference? if self.damping > 0.0: A_conjugate = [A_conj + self.damping * conj for A_conj, conj in zip(A_conjugate, conjugate)] # cAc := c_t^T * Ac conjugate_A_conjugate = tf.add_n( inputs=[tf.reduce_sum(input_tensor=(conj * A_conj)) for conj, A_conj in zip(conjugate, A_conjugate)] ) # \alpha := r_t^2 / cAc alpha = squared_residual / tf.maximum(x=conjugate_A_conjugate, y=util.epsilon) # x_{t+1} := x_t + \alpha * c_t next_x = [t + alpha * conj for t, conj in zip(x, conjugate)] # r_{t+1} := r_t - \alpha * Ac next_residual = [res - alpha * A_conj for res, A_conj in zip(residual, A_conjugate)] # r_{t+1}^2 := r_{t+1}^T * r_{t+1} next_squared_residual = tf.add_n(inputs=[tf.reduce_sum(input_tensor=(res * res)) for res in next_residual]) # \beta = r_{t+1}^2 / r_t^2 beta = next_squared_residual / tf.maximum(x=squared_residual, y=util.epsilon) # c_{t+1} := r_{t+1} + \beta * c_t next_conjugate = [res + beta * conj for res, conj in zip(next_residual, conjugate)] return next_x, next_iteration, next_conjugate, next_residual, next_squared_residual
def tf_next_step(self, x, iteration, conjugate, residual, squared_residual): """ Termination condition: max number of iterations, or residual sufficiently small. Args: x: Current solution estimate $x_t$. iteration: Current iteration counter $t$. conjugate: Current conjugate $c_t$. residual: Current residual $r_t$. squared_residual: Current squared residual $r_t^2$. Returns: True if another iteration should be performed. """ next_step = super(ConjugateGradient, self).tf_next_step(x, iteration, conjugate, residual, squared_residual) return tf.logical_and(x=next_step, y=(squared_residual >= util.epsilon))
def tf_step(self, time, variables, **kwargs): """ Creates the TensorFlow operations for performing an optimization step. Args: time: Time tensor. variables: List of variables to optimize. **kwargs: Additional arguments passed on to the internal optimizer. Returns: List of delta tensors corresponding to the updates for each optimized variable. """ deltas = self.optimizer.step(time=time, variables=variables, **kwargs) with tf.control_dependencies(control_inputs=deltas): clipped_deltas = list() exceeding_deltas = list() for delta in deltas: clipped_delta = tf.clip_by_value( t=delta, clip_value_min=-self.clipping_value, clip_value_max=self.clipping_value ) clipped_deltas.append(clipped_delta) exceeding_deltas.append(clipped_delta - delta) applied = self.apply_step(variables=variables, deltas=exceeding_deltas) with tf.control_dependencies(control_inputs=(applied,)): return [delta + 0.0 for delta in clipped_deltas]
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 tf_q_delta(self, q_value, next_q_value, terminal, reward): """ Creates the deltas (or advantage) of the Q values. :return: A list of deltas per action """ for _ in range(util.rank(q_value) - 1): terminal = tf.expand_dims(input=terminal, axis=1) reward = tf.expand_dims(input=reward, axis=1) multiples = (1,) + util.shape(q_value)[1:] terminal = tf.tile(input=terminal, multiples=multiples) reward = tf.tile(input=reward, multiples=multiples) zeros = tf.zeros_like(tensor=next_q_value) next_q_value = tf.where(condition=terminal, x=zeros, y=(self.discount * next_q_value)) return reward + next_q_value - q_value
def target_optimizer_arguments(self): """ Returns the target optimizer arguments including the time, the list of variables to optimize, and various functions which the optimizer might require to perform an update step. Returns: Target optimizer arguments as dict. """ variables = self.target_network.get_variables() + [ variable for name in sorted(self.target_distributions) for variable in self.target_distributions[name].get_variables() ] source_variables = self.network.get_variables() + [ variable for name in sorted(self.distributions) for variable in self.distributions[name].get_variables() ] arguments = dict( time=self.global_timestep, variables=variables, source_variables=source_variables ) if self.global_model is not None: arguments['global_variables'] = self.global_model.target_network.get_variables() + [ variable for name in sorted(self.global_model.target_distributions) for variable in self.global_model.target_distributions[name].get_variables() ] return arguments
def from_spec(spec, kwargs): """ Creates an environment from a specification dict. """ env = tensorforce.util.get_object( obj=spec, predefined_objects=tensorforce.environments.environments, kwargs=kwargs ) assert isinstance(env, Environment) return env
def setup(app): """When used for spinx extension.""" global _is_sphinx _is_sphinx = True app.add_config_value('no_underscore_emphasis', False, 'env') app.add_source_parser('.md', M2RParser) app.add_directive('mdinclude', MdInclude)
def output_image_link(self, m): """Pass through rest role.""" return self.renderer.image_link( m.group('url'), m.group('target'), m.group('alt'))
def output_eol_literal_marker(self, m): """Pass through rest link.""" marker = ':' if m.group(1) is None else '' return self.renderer.eol_literal_marker(marker)
def header(self, text, level, raw=None): """Rendering header/heading tags like ``<h1>`` ``<h2>``. :param text: rendered text content for the header. :param level: a number for the header level, for example: 1. :param raw: raw text content of the header. """ return '\n{0}\n{1}\n'.format(text, self.hmarks[level] * len(text))
def list(self, body, ordered=True): """Rendering list tags like ``<ul>`` and ``<ol>``. :param body: body contents of the list. :param ordered: whether this list is ordered or not. """ mark = '#. ' if ordered else '* ' lines = body.splitlines() for i, line in enumerate(lines): if line and not line.startswith(self.list_marker): lines[i] = ' ' * len(mark) + line return '\n{}\n'.format( '\n'.join(lines)).replace(self.list_marker, mark)
def table(self, header, body): """Rendering table element. Wrap header and body in it. :param header: header part of the table. :param body: body part of the table. """ table = '\n.. list-table::\n' if header and not header.isspace(): table = (table + self.indent + ':header-rows: 1\n\n' + self._indent_block(header) + '\n') else: table = table + '\n' table = table + self._indent_block(body) + '\n\n' return table
def table_row(self, content): """Rendering a table row. Like ``<tr>``. :param content: content of current table row. """ contents = content.splitlines() if not contents: return '' clist = ['* ' + contents[0]] if len(contents) > 1: for c in contents[1:]: clist.append(' ' + c) return '\n'.join(clist) + '\n'
def codespan(self, text): """Rendering inline `code` text. :param text: text content for inline code. """ if '``' not in text: return '\ ``{}``\ '.format(text) else: # actually, docutils split spaces in literal return self._raw_html( '<code class="docutils literal">' '<span class="pre">{}</span>' '</code>'.format(text.replace('`', '&#96;')))
def link(self, link, title, text): """Rendering a given link with content and title. :param link: href link for ``<a>`` tag. :param title: title content for `title` attribute. :param text: text content for description. """ if title: raise NotImplementedError('sorry') return '\ `{text} <{target}>`_\ '.format(target=link, text=text)
def image(self, src, title, text): """Rendering a image with title and text. :param src: source link of the image. :param title: title text of the image. :param text: alt text of the image. """ # rst does not support title option # and I couldn't find title attribute in HTML standard return '\n'.join([ '', '.. image:: {}'.format(src), ' :target: {}'.format(src), ' :alt: {}'.format(text), '', ])
def run(self): """Most of this method is from ``docutils.parser.rst.Directive``. docutils version: 0.12 """ if not self.state.document.settings.file_insertion_enabled: raise self.warning('"%s" directive disabled.' % self.name) source = self.state_machine.input_lines.source( self.lineno - self.state_machine.input_offset - 1) source_dir = os.path.dirname(os.path.abspath(source)) path = rst.directives.path(self.arguments[0]) path = os.path.normpath(os.path.join(source_dir, path)) path = utils.relative_path(None, path) path = nodes.reprunicode(path) # get options (currently not use directive-specific options) encoding = self.options.get( 'encoding', self.state.document.settings.input_encoding) e_handler = self.state.document.settings.input_encoding_error_handler tab_width = self.options.get( 'tab-width', self.state.document.settings.tab_width) # open the inclding file try: self.state.document.settings.record_dependencies.add(path) include_file = io.FileInput(source_path=path, encoding=encoding, error_handler=e_handler) except UnicodeEncodeError as error: raise self.severe('Problems with "%s" directive path:\n' 'Cannot encode input file path "%s" ' '(wrong locale?).' % (self.name, SafeString(path))) except IOError as error: raise self.severe('Problems with "%s" directive path:\n%s.' % (self.name, ErrorString(error))) # read from the file try: rawtext = include_file.read() except UnicodeError as error: raise self.severe('Problem with "%s" directive:\n%s' % (self.name, ErrorString(error))) config = self.state.document.settings.env.config converter = M2R(no_underscore_emphasis=config.no_underscore_emphasis) include_lines = statemachine.string2lines(converter(rawtext), tab_width, convert_whitespace=True) self.state_machine.insert_input(include_lines, path) return []
def WorkerAgentGenerator(agent_class): """ Worker Agent generator, receives an Agent class and creates a Worker Agent class that inherits from that Agent. """ # Support special case where class is given as type-string (AgentsDictionary) or class-name-string. if isinstance(agent_class, str): agent_class = AgentsDictionary.get(agent_class) # Last resort: Class name given as string? if not agent_class and agent_class.find('.') != -1: module_name, function_name = agent_class.rsplit('.', 1) module = importlib.import_module(module_name) agent_class = getattr(module, function_name) class WorkerAgent(agent_class): """ Worker agent receiving a shared model to avoid creating multiple models. """ def __init__(self, model=None, **kwargs): # Set our model externally. self.model = model # Be robust against `network` coming in from kwargs even though this agent doesn't have one if not issubclass(agent_class, LearningAgent): kwargs.pop("network") # Call super c'tor (which will call initialize_model and assign self.model to the return value). super(WorkerAgent, self).__init__(**kwargs) def initialize_model(self): # Return our model (already given and initialized). return self.model return WorkerAgent
def clone_worker_agent(agent, factor, environment, network, agent_config): """ Clones a given Agent (`factor` times) and returns a list of the cloned Agents with the original Agent in the first slot. Args: agent (Agent): The Agent object to clone. factor (int): The length of the final list. environment (Environment): The Environment to use for all cloned agents. network (LayeredNetwork): The Network to use (or None) for an Agent's Model. agent_config (dict): A dict of Agent specifications passed into the Agent's c'tor as kwargs. Returns: The list with `factor` cloned agents (including the original one). """ ret = [agent] for i in xrange(factor - 1): worker = WorkerAgentGenerator(type(agent))( states=environment.states, actions=environment.actions, network=network, model=agent.model, **agent_config ) ret.append(worker) return ret
def run( self, num_episodes=-1, max_episode_timesteps=-1, episode_finished=None, summary_report=None, summary_interval=0, num_timesteps=None, deterministic=False, episodes=None, max_timesteps=None, testing=False, sleep=None ): """ Executes this runner by starting all Agents in parallel (each one in one thread). Args: episodes (int): Deprecated; see num_episodes. max_timesteps (int): Deprecated; see max_episode_timesteps. """ # Renamed episodes into num_episodes to match BaseRunner's signature (fully backw. compatible). if episodes is not None: num_episodes = episodes warnings.warn("WARNING: `episodes` parameter is deprecated, use `num_episodes` instead.", category=DeprecationWarning) assert isinstance(num_episodes, int) # Renamed max_timesteps into max_episode_timesteps to match single Runner's signature (fully backw. compatible). if max_timesteps is not None: max_episode_timesteps = max_timesteps warnings.warn("WARNING: `max_timesteps` parameter is deprecated, use `max_episode_timesteps` instead.", category=DeprecationWarning) assert isinstance(max_episode_timesteps, int) if summary_report is not None: warnings.warn("WARNING: `summary_report` parameter is deprecated, use `episode_finished` callback " "instead to generate summaries every n episodes.", category=DeprecationWarning) self.reset() # Reset counts/stop-condition for this run. self.global_episode = 0 self.global_timestep = 0 self.should_stop = False # Create threads. threads = [threading.Thread(target=self._run_single, args=(t, self.agent[t], self.environment[t],), kwargs={"deterministic": deterministic, "max_episode_timesteps": max_episode_timesteps, "episode_finished": episode_finished, "testing": testing, "sleep": sleep}) for t in range(len(self.agent))] # Start threads. self.start_time = time.time() [t.start() for t in threads] # Stay idle until killed by SIGINT or a global stop condition is met. try: next_summary = 0 next_save = 0 if self.save_frequency_unit != "s" else time.time() while any([t.is_alive() for t in threads]) and self.global_episode < num_episodes or num_episodes == -1: self.time = time.time() # This is deprecated (but still supported) and should be covered by the `episode_finished` callable. if summary_report is not None and self.global_episode > next_summary: summary_report(self) next_summary += summary_interval if self.save_path and self.save_frequency is not None: do_save = True current = None if self.save_frequency_unit == "e" and self.global_episode > next_save: current = self.global_episode elif self.save_frequency_unit == "s" and self.time > next_save: current = self.time elif self.save_frequency_unit == "t" and self.global_timestep > next_save: current = self.global_timestep else: do_save = False if do_save: self.agent[0].save_model(self.save_path) # Make sure next save is later than right now. while next_save < current: next_save += self.save_frequency time.sleep(1) except KeyboardInterrupt: print('Keyboard interrupt, sending stop command to threads') self.should_stop = True # Join threads. [t.join() for t in threads] print('All threads stopped')
def _run_single(self, thread_id, agent, environment, deterministic=False, max_episode_timesteps=-1, episode_finished=None, testing=False, sleep=None): """ The target function for a thread, runs an agent and environment until signaled to stop. Adds rewards to shared episode rewards list. Args: thread_id (int): The ID of the thread that's running this target function. agent (Agent): The Agent object that this particular thread uses. environment (Environment): The Environment object that this particular thread uses. max_episode_timesteps (int): Max. number of timesteps per episode. Use -1 or 0 for non-limited episodes. episode_finished (callable): Function called after each episode that takes an episode summary spec and returns False, if this single run should terminate after this episode. Can be used e.g. to set a particular mean reward threshold. """ # figure out whether we are using the deprecated way of "episode_finished" reporting old_episode_finished = False if episode_finished is not None and len(getargspec(episode_finished).args) == 1: old_episode_finished = True episode = 0 # Run this single worker (episode loop) as long as global count thresholds have not been reached. while not self.should_stop: state = environment.reset() agent.reset() self.global_timestep, self.global_episode = agent.timestep, agent.episode episode_reward = 0 # Time step (within episode) loop time_step = 0 time_start = time.time() while True: action, internals, states = agent.act(states=state, deterministic=deterministic, buffered=False) reward = 0 for repeat in xrange(self.repeat_actions): state, terminal, step_reward = environment.execute(action=action) reward += step_reward if terminal: break if not testing: # agent.observe(reward=reward, terminal=terminal) # Insert everything at once. agent.atomic_observe( states=state, actions=action, internals=internals, reward=reward, terminal=terminal ) if sleep is not None: time.sleep(sleep) time_step += 1 episode_reward += reward if terminal or time_step == max_episode_timesteps: break # Abort the episode (discard its results) when global says so. if self.should_stop: return self.global_timestep += time_step # Avoid race condition where order in episode_rewards won't match order in episode_timesteps. self.episode_list_lock.acquire() self.episode_rewards.append(episode_reward) self.episode_timesteps.append(time_step) self.episode_times.append(time.time() - time_start) self.episode_list_lock.release() if episode_finished is not None: # old way of calling episode_finished if old_episode_finished: summary_data = { "thread_id": thread_id, "episode": episode, "timestep": time_step, "episode_reward": episode_reward } if not episode_finished(summary_data): return # New way with BasicRunner (self) and thread-id. elif not episode_finished(self, thread_id): return episode += 1
def _int_to_pos(self, flat_position): """Returns x, y from flat_position integer. Args: flat_position: flattened position integer Returns: x, y """ return flat_position % self.env.action_space.screen_shape[0],\ flat_position % self.env.action_space.screen_shape[1]
def _wait_state(self, state, reward, terminal): """ Wait until there is a state. """ while state == [None] or not state: state, terminal, reward = self._execute(dict(key=0)) return state, terminal, reward
def apply_step(self, variables, deltas): """ Applies the given (and already calculated) step deltas to the variable values. Args: variables: List of variables. deltas: List of deltas of same length. Returns: The step-applied operation. A tf.group of tf.assign_add ops. """ if len(variables) != len(deltas): raise TensorForceError("Invalid variables and deltas lists.") return tf.group( *(tf.assign_add(ref=variable, value=delta) for variable, delta in zip(variables, deltas)) )
def minimize(self, time, variables, **kwargs): """ Performs an optimization step. Args: time: Time tensor. variables: List of variables to optimize. **kwargs: Additional optimizer-specific arguments. The following arguments are used by some optimizers: - arguments: Dict of arguments for callables, like fn_loss. - fn_loss: A callable returning the loss of the current model. - fn_reference: A callable returning the reference values, in case of a comparative loss. - fn_kl_divergence: A callable returning the KL-divergence relative to the current model. - sampled_loss: A sampled loss (integer). - return_estimated_improvement: Returns the estimated improvement resulting from the natural gradient calculation if true. - source_variables: List of source variables to synchronize with. - global_variables: List of global variables to apply the proposed optimization step to. Returns: The optimization operation. """ # # Add training variable gradient histograms/scalars to summary output # # if 'gradients' in self.summary_labels: # if any(k in self.summary_labels for k in ['gradients', 'gradients_histogram', 'gradients_scalar']): # valid = True # if isinstance(self, tensorforce.core.optimizers.TFOptimizer): # gradients = self.optimizer.compute_gradients(kwargs['fn_loss']()) # elif isinstance(self.optimizer, tensorforce.core.optimizers.TFOptimizer): # # This section handles "Multi_step" and may handle others # # if failure is found, add another elif to handle that case # gradients = self.optimizer.optimizer.compute_gradients(kwargs['fn_loss']()) # else: # # Didn't find proper gradient information # valid = False # # Valid gradient data found, create summary data items # if valid: # for grad, var in gradients: # if grad is not None: # if any(k in self.summary_labels for k in ('gradients', 'gradients_scalar')): # axes = list(range(len(grad.shape))) # mean, var = tf.nn.moments(grad, axes) # tf.contrib.summary.scalar(name='gradients/' + var.name + "/mean", tensor=mean) # tf.contrib.summary.scalar(name='gradients/' + var.name + "/variance", tensor=var) # if any(k in self.summary_labels for k in ('gradients', 'gradients_histogram')): # tf.contrib.summary.histogram(name='gradients/' + var.name, tensor=grad) deltas = self.step(time=time, variables=variables, **kwargs) with tf.control_dependencies(control_inputs=deltas): return tf.no_op()
def from_spec(spec, kwargs=None): """ Creates an optimizer from a specification dict. """ optimizer = util.get_object( obj=spec, predefined_objects=tensorforce.core.optimizers.optimizers, kwargs=kwargs ) assert isinstance(optimizer, Optimizer) return optimizer
def np_dtype(dtype): """Translates dtype specifications in configurations to numpy data types. Args: dtype: String describing a numerical type (e.g. 'float') or numerical type primitive. Returns: Numpy data type """ if dtype == 'float' or dtype == float or dtype == np.float32 or dtype == tf.float32: return np.float32 elif dtype == np.float64 or dtype == tf.float64: return np.float64 elif dtype == np.float16 or dtype == tf.float16: return np.float16 elif dtype == 'int' or dtype == int or dtype == np.int32 or dtype == tf.int32: return np.int32 elif dtype == np.int64 or dtype == tf.int64: return np.int64 elif dtype == np.int16 or dtype == tf.int16: return np.int16 elif dtype == 'bool' or dtype == bool or dtype == np.bool_ or dtype == tf.bool: return np.bool_ else: raise TensorForceError("Error: Type conversion from type {} not supported.".format(str(dtype)))
def get_tensor_dependencies(tensor): """ Utility method to get all dependencies (including placeholders) of a tensor (backwards through the graph). Args: tensor (tf.Tensor): The input tensor. Returns: Set of all dependencies (including needed placeholders) for the input tensor. """ dependencies = set() dependencies.update(tensor.op.inputs) for sub_op in tensor.op.inputs: dependencies.update(get_tensor_dependencies(sub_op)) return dependencies
def get_object(obj, predefined_objects=None, default_object=None, kwargs=None): """ Utility method to map some kind of object specification to its content, e.g. optimizer or baseline specifications to the respective classes. Args: obj: A specification dict (value for key 'type' optionally specifies the object, options as follows), a module path (e.g., my_module.MyClass), a key in predefined_objects, or a callable (e.g., the class type object). predefined_objects: Dict containing predefined set of objects, accessible via their key default_object: Default object is no other is specified kwargs: Arguments for object creation Returns: The retrieved object """ args = () kwargs = dict() if kwargs is None else kwargs if isinstance(obj, str) and os.path.isfile(obj): with open(obj, 'r') as fp: obj = json.load(fp=fp) if isinstance(obj, dict): kwargs.update(obj) obj = kwargs.pop('type', None) if predefined_objects is not None and obj in predefined_objects: obj = predefined_objects[obj] elif isinstance(obj, str): if obj.find('.') != -1: module_name, function_name = obj.rsplit('.', 1) module = importlib.import_module(module_name) obj = getattr(module, function_name) else: raise TensorForceError("Error: object {} not found in predefined objects: {}".format( obj, list(predefined_objects or ()) )) elif callable(obj): pass elif default_object is not None: args = (obj,) obj = default_object else: # assumes the object is already instantiated return obj return obj(*args, **kwargs)
def prepare_kwargs(raw, string_parameter='name'): """ Utility method to convert raw string/diction input into a dictionary to pass into a function. Always returns a dictionary. Args: raw: string or dictionary, string is assumed to be the name of the activation activation function. Dictionary will be passed through unchanged. Returns: kwargs dictionary for **kwargs """ kwargs = dict() if isinstance(raw, dict): kwargs.update(raw) elif isinstance(raw, str): kwargs[string_parameter] = raw return kwargs
def register_saver_ops(self): """ Registers the saver operations to the graph in context. """ variables = self.get_savable_variables() if variables is None or len(variables) == 0: self._saver = None return base_scope = self._get_base_variable_scope() variables_map = {strip_name_scope(v.name, base_scope): v for v in variables} self._saver = tf.train.Saver( var_list=variables_map, reshape=False, sharded=False, max_to_keep=5, keep_checkpoint_every_n_hours=10000.0, name=None, restore_sequentially=False, saver_def=None, builder=None, defer_build=False, allow_empty=True, write_version=tf.train.SaverDef.V2, pad_step_number=False, save_relative_paths=True )
def save(self, sess, save_path, timestep=None): """ Saves this component's managed variables. Args: sess: The session for which to save the managed variables. save_path: The path to save data to. timestep: Optional, the timestep to append to the file name. Returns: Checkpoint path where the model was saved. """ if self._saver is None: raise TensorForceError("register_saver_ops should be called before save") return self._saver.save( sess=sess, save_path=save_path, global_step=timestep, write_meta_graph=False, write_state=True, # Do we need this? )
def restore(self, sess, save_path): """ Restores the values of the managed variables from disk location. Args: sess: The session for which to save the managed variables. save_path: The path used to save the data to. """ if self._saver is None: raise TensorForceError("register_saver_ops should be called before restore") self._saver.restore(sess=sess, save_path=save_path)
def reset(self): """ Calls `reset` on all our Preprocessor objects. Returns: A list of tensors to be fetched. """ fetches = [] for processor in self.preprocessors: fetches.extend(processor.reset() or []) return fetches
def process(self, tensor): """ Process state. Args: tensor: tensor to process Returns: processed state """ for processor in self.preprocessors: tensor = processor.process(tensor=tensor) return tensor
def processed_shape(self, shape): """ Shape of preprocessed state given original shape. Args: shape: original state shape Returns: processed state shape """ for processor in self.preprocessors: shape = processor.processed_shape(shape=shape) return shape
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 tf_solve(self, fn_x, x_init, *args): """ Iteratively solves an equation/optimization for $x$ involving an expression $f(x)$. Args: fn_x: A callable returning an expression $f(x)$ given $x$. x_init: Initial solution guess $x_0$. *args: Additional solver-specific arguments. Returns: A solution $x$ to the problem as given by the solver. """ self.fn_x = fn_x # Initialization step args = self.initialize(x_init, *args) # args = util.map_tensors(fn=tf.stop_gradient, tensors=args) # Iteration loop with termination condition if self.unroll_loop: # Unrolled for loop for _ in range(self.max_iterations): next_step = self.next_step(*args) step = (lambda: self.step(*args)) do_nothing = (lambda: args) args = tf.cond(pred=next_step, true_fn=step, false_fn=do_nothing) else: # TensorFlow while loop args = tf.while_loop(cond=self.next_step, body=self.step, loop_vars=args) # First argument contains solution return args[0]
def execute(self, action): """ Executes action, observes next state and reward. Args: actions: Actions to execute. Returns: Tuple of (next state, bool indicating terminal, reward) """ next_state, rew, done, _ = self.env.step(action) return next_state, rew, done
def as_local_model(self): """ Makes sure our optimizer is wrapped into the global_optimizer meta. This is only relevant for distributed RL. """ super(MemoryModel, self).as_local_model() self.optimizer_spec = dict( type='global_optimizer', optimizer=self.optimizer_spec )
def setup_components_and_tf_funcs(self, custom_getter=None): """ Constructs the memory and the optimizer objects. Generates and stores all template functions. """ custom_getter = super(MemoryModel, self).setup_components_and_tf_funcs(custom_getter) # Memory self.memory = Memory.from_spec( spec=self.memory_spec, kwargs=dict( states=self.states_spec, internals=self.internals_spec, actions=self.actions_spec, summary_labels=self.summary_labels ) ) # Optimizer self.optimizer = Optimizer.from_spec( spec=self.optimizer_spec, kwargs=dict(summary_labels=self.summary_labels) ) # TensorFlow functions self.fn_discounted_cumulative_reward = tf.make_template( name_='discounted-cumulative-reward', func_=self.tf_discounted_cumulative_reward, custom_getter_=custom_getter ) self.fn_reference = tf.make_template( name_='reference', func_=self.tf_reference, custom_getter_=custom_getter ) self.fn_loss_per_instance = tf.make_template( name_='loss-per-instance', func_=self.tf_loss_per_instance, custom_getter_=custom_getter ) self.fn_regularization_losses = tf.make_template( name_='regularization-losses', func_=self.tf_regularization_losses, custom_getter_=custom_getter ) self.fn_loss = tf.make_template( name_='loss', func_=self.tf_loss, custom_getter_=custom_getter ) self.fn_optimization = tf.make_template( name_='optimization', func_=self.tf_optimization, custom_getter_=custom_getter ) self.fn_import_experience = tf.make_template( name_='import-experience', func_=self.tf_import_experience, custom_getter_=custom_getter ) return custom_getter
def tf_discounted_cumulative_reward(self, terminal, reward, discount=None, final_reward=0.0, horizon=0): """ Creates and returns the TensorFlow operations for calculating the sequence of discounted cumulative rewards for a given sequence of single rewards. Example: single rewards = 2.0 1.0 0.0 0.5 1.0 -1.0 terminal = False, False, False, False True False gamma = 0.95 final_reward = 100.0 (only matters for last episode (r=-1.0) as this episode has no terminal signal) horizon=3 output = 2.95 1.45 1.38 1.45 1.0 94.0 Args: terminal: Tensor (bool) holding the is-terminal sequence. This sequence may contain more than one True value. If its very last element is False (not terminating), the given `final_reward` value is assumed to follow the last value in the single rewards sequence (see below). reward: Tensor (float) holding the sequence of single rewards. If the last element of `terminal` is False, an assumed last reward of the value of `final_reward` will be used. discount (float): The discount factor (gamma). By default, take the Model's discount factor. final_reward (float): Reward value to use if last episode in sequence does not terminate (terminal sequence ends with False). This value will be ignored if horizon == 1 or discount == 0.0. horizon (int): The length of the horizon (e.g. for n-step cumulative rewards in continuous tasks without terminal signals). Use 0 (default) for an infinite horizon. Note that horizon=1 leads to the exact same results as a discount factor of 0.0. Returns: Discounted cumulative reward tensor with the same shape as `reward`. """ # By default -> take Model's gamma value if discount is None: discount = self.discount # Accumulates discounted (n-step) reward (start new if terminal) def cumulate(cumulative, reward_terminal_horizon_subtract): rew, is_terminal, is_over_horizon, sub = reward_terminal_horizon_subtract return tf.where( # If terminal, start new cumulation. condition=is_terminal, x=rew, y=tf.where( # If we are above the horizon length (H) -> subtract discounted value from H steps back. condition=is_over_horizon, x=(rew + cumulative * discount - sub), y=(rew + cumulative * discount) ) ) # Accumulates length of episodes (starts new if terminal) def len_(cumulative, term): return tf.where( condition=term, # Start counting from 1 after is-terminal signal x=tf.ones(shape=(), dtype=tf.int32), # Otherwise, increase length by 1 y=cumulative + 1 ) # Reverse, since reward cumulation is calculated right-to-left, but tf.scan only works left-to-right. reward = tf.reverse(tensor=reward, axis=(0,)) # e.g. -1.0 1.0 0.5 0.0 1.0 2.0 terminal = tf.reverse(tensor=terminal, axis=(0,)) # e.g. F T F F F F # Store the steps until end of the episode(s) determined by the input terminal signals (True starts new count). lengths = tf.scan(fn=len_, elems=terminal, initializer=0) # e.g. 1 1 2 3 4 5 off_horizon = tf.greater(lengths, tf.fill(dims=tf.shape(lengths), value=horizon)) # e.g. F F F F T T # Calculate the horizon-subtraction value for each step. if horizon > 0: horizon_subtractions = tf.map_fn(lambda x: (discount ** horizon) * x, reward, dtype=tf.float32) # Shift right by size of horizon (fill rest with 0.0). horizon_subtractions = tf.concat([np.zeros(shape=(horizon,)), horizon_subtractions], axis=0) horizon_subtractions = tf.slice(horizon_subtractions, begin=(0,), size=tf.shape(reward)) # e.g. 0.0, 0.0, 0.0, -1.0*g^3, 1.0*g^3, 0.5*g^3 # all 0.0 if infinite horizon (special case: horizon=0) else: horizon_subtractions = tf.zeros(shape=tf.shape(reward)) # Now do the scan, each time summing up the previous step (discounted by gamma) and # subtracting the respective `horizon_subtraction`. reward = tf.scan( fn=cumulate, elems=(reward, terminal, off_horizon, horizon_subtractions), initializer=final_reward if horizon != 1 else 0.0 ) # Re-reverse again to match input sequences. return tf.reverse(tensor=reward, axis=(0,))
def tf_reference(self, states, internals, actions, terminal, reward, next_states, next_internals, update): """ Creates the TensorFlow operations for obtaining the reference tensor(s), in case of a comparative loss. Args: states: Dict of state tensors. internals: List of prior internal state tensors. actions: Dict of action tensors. terminal: Terminal boolean tensor. reward: Reward tensor. next_states: Dict of successor state tensors. next_internals: List of posterior internal state tensors. update: Boolean tensor indicating whether this call happens during an update. Returns: Reference tensor(s). """ return None
def tf_loss_per_instance(self, states, internals, actions, terminal, reward, next_states, next_internals, update, reference=None): """ Creates the TensorFlow operations for calculating the loss per batch instance. Args: states: Dict of state tensors. internals: Dict of prior internal state tensors. actions: Dict of action tensors. terminal: Terminal boolean tensor. reward: Reward tensor. next_states: Dict of successor state tensors. next_internals: List of posterior internal state tensors. update: Boolean tensor indicating whether this call happens during an update. reference: Optional reference tensor(s), in case of a comparative loss. Returns: Loss per instance tensor. """ raise NotImplementedError
def tf_loss(self, states, internals, actions, terminal, reward, next_states, next_internals, update, reference=None): """ Creates the TensorFlow operations for calculating the full loss of a batch. Args: states: Dict of state tensors. internals: List of prior internal state tensors. actions: Dict of action tensors. terminal: Terminal boolean tensor. reward: Reward tensor. next_states: Dict of successor state tensors. next_internals: List of posterior internal state tensors. update: Boolean tensor indicating whether this call happens during an update. reference: Optional reference tensor(s), in case of a comparative loss. Returns: Loss tensor. """ # Mean loss per instance loss_per_instance = self.fn_loss_per_instance( states=states, internals=internals, actions=actions, terminal=terminal, reward=reward, next_states=next_states, next_internals=next_internals, update=update, reference=reference ) # Returns no-op. updated = self.memory.update_batch(loss_per_instance=loss_per_instance) with tf.control_dependencies(control_inputs=(updated,)): loss = tf.reduce_mean(input_tensor=loss_per_instance, axis=0) # Loss without regularization summary. if 'losses' in self.summary_labels: tf.contrib.summary.scalar(name='loss-without-regularization', tensor=loss) # Regularization losses. losses = self.fn_regularization_losses(states=states, internals=internals, update=update) if len(losses) > 0: loss += tf.add_n(inputs=[losses[name] for name in sorted(losses)]) if 'regularization' in self.summary_labels: for name in sorted(losses): tf.contrib.summary.scalar(name=('regularization/' + name), tensor=losses[name]) # Total loss summary. if 'losses' in self.summary_labels or 'total-loss' in self.summary_labels: tf.contrib.summary.scalar(name='total-loss', tensor=loss) return loss
def optimizer_arguments(self, states, internals, actions, terminal, reward, next_states, next_internals): """ Returns the optimizer arguments including the time, the list of variables to optimize, and various functions which the optimizer might require to perform an update step. Args: states (dict): Dict of state tensors. internals (dict): Dict of prior internal state tensors. actions (dict): Dict of action tensors. terminal: 1D boolean is-terminal tensor. reward: 1D (float) rewards tensor. next_states (dict): Dict of successor state tensors. next_internals (dict): Dict of posterior internal state tensors. Returns: Optimizer arguments as dict to be used as **kwargs to the optimizer. """ arguments = dict( time=self.global_timestep, variables=self.get_variables(), arguments=dict( states=states, internals=internals, actions=actions, terminal=terminal, reward=reward, next_states=next_states, next_internals=next_internals, update=tf.constant(value=True) ), fn_reference=self.fn_reference, fn_loss=self.fn_loss ) if self.global_model is not None: arguments['global_variables'] = self.global_model.get_variables() return arguments
def tf_optimization(self, states, internals, actions, terminal, reward, next_states=None, next_internals=None): """ Creates the TensorFlow operations for performing an optimization update step based on the given input states and actions batch. Args: states: Dict of state tensors. internals: List of prior internal state tensors. actions: Dict of action tensors. terminal: Terminal boolean tensor. reward: Reward tensor. next_states: Dict of successor state tensors. next_internals: List of posterior internal state tensors. Returns: The optimization operation. """ arguments = self.optimizer_arguments( states=states, internals=internals, actions=actions, terminal=terminal, reward=reward, next_states=next_states, next_internals=next_internals ) return self.optimizer.minimize(**arguments)
def tf_observe_timestep(self, states, internals, actions, terminal, reward): """ Creates and returns the op that - if frequency condition is hit - pulls a batch from the memory and does one optimization step. """ # Store timestep in memory stored = self.memory.store( states=states, internals=internals, actions=actions, terminal=terminal, reward=reward ) # Periodic optimization with tf.control_dependencies(control_inputs=(stored,)): unit = self.update_mode['unit'] batch_size = self.update_mode['batch_size'] frequency = self.update_mode.get('frequency', batch_size) first_update = self.update_mode.get('first_update', 0) if unit == 'timesteps': # Timestep-based batch optimize = tf.logical_and( x=tf.equal(x=(self.timestep % frequency), y=0), y=tf.logical_and( x=tf.greater_equal(x=self.timestep, y=batch_size), y=tf.greater_equal(x=self.timestep, y=first_update) ) ) elif unit == 'episodes': # Episode-based batch optimize = tf.logical_and( x=tf.equal(x=(self.episode % frequency), y=0), y=tf.logical_and( # Only update once per episode increment. x=tf.greater(x=tf.count_nonzero(input_tensor=terminal), y=0), y=tf.logical_and( x=tf.greater_equal(x=self.episode, y=batch_size), y=tf.greater_equal(x=self.episode, y=first_update) ) ) ) elif unit == 'sequences': # Timestep-sequence-based batch sequence_length = self.update_mode.get('length', 8) optimize = tf.logical_and( x=tf.equal(x=(self.timestep % frequency), y=0), y=tf.logical_and( x=tf.greater_equal(x=self.timestep, y=(batch_size + sequence_length - 1)), y=tf.greater_equal(x=self.timestep, y=first_update) ) ) else: raise TensorForceError("Invalid update unit: {}.".format(unit)) def true_fn(): if unit == 'timesteps': # Timestep-based batch batch = self.memory.retrieve_timesteps(n=batch_size) elif unit == 'episodes': # Episode-based batch batch = self.memory.retrieve_episodes(n=batch_size) elif unit == 'sequences': # Timestep-sequence-based batch batch = self.memory.retrieve_sequences(n=batch_size, sequence_length=sequence_length) # Do not calculate gradients for memory-internal operations. batch = util.map_tensors( fn=(lambda tensor: tf.stop_gradient(input=tensor)), tensors=batch ) optimize = self.fn_optimization(**batch) with tf.control_dependencies(control_inputs=(optimize,)): return tf.logical_and(x=True, y=True) return tf.cond(pred=optimize, true_fn=true_fn, false_fn=tf.no_op)
def tf_import_experience(self, states, internals, actions, terminal, reward): """ Imports experiences into the TensorFlow memory structure. Can be used to import off-policy data. :param states: Dict of state values to import with keys as state names and values as values to set. :param internals: Internal values to set, can be fetched from agent via agent.current_internals if no values available. :param actions: Dict of action values to import with keys as action names and values as values to set. :param terminal: Terminal value(s) :param reward: Reward value(s) """ return self.memory.store( states=states, internals=internals, actions=actions, terminal=terminal, reward=reward )
def import_experience(self, states, internals, actions, terminal, reward): """ Stores experiences. """ fetches = self.import_experience_output feed_dict = self.get_feed_dict( states=states, internals=internals, actions=actions, terminal=terminal, reward=reward ) self.monitored_session.run(fetches=fetches, feed_dict=feed_dict)
def tf_step( self, time, variables, arguments, fn_loss, fn_reference, **kwargs ): """ Creates the TensorFlow operations for performing an optimization step. Args: time: Time tensor. variables: List of variables to optimize. arguments: Dict of arguments for callables, like fn_loss. fn_loss: A callable returning the loss of the current model. fn_reference: A callable returning the reference values, in case of a comparative loss. **kwargs: Additional arguments passed on to the internal optimizer. Returns: List of delta tensors corresponding to the updates for each optimized variable. """ # Set reference to compare with at each optimization step, in case of a comparative loss. arguments['reference'] = fn_reference(**arguments) # Negative value since line search maximizes. loss_before = -fn_loss(**arguments) with tf.control_dependencies(control_inputs=(loss_before,)): deltas = self.optimizer.step( time=time, variables=variables, arguments=arguments, fn_loss=fn_loss, return_estimated_improvement=True, **kwargs ) if isinstance(deltas, tuple): # If 'return_estimated_improvement' argument exists. if len(deltas) != 2: raise TensorForceError("Unexpected output of internal optimizer.") deltas, estimated_improvement = deltas # Negative value since line search maximizes. estimated_improvement = -estimated_improvement else: estimated_improvement = None with tf.control_dependencies(control_inputs=deltas): # Negative value since line search maximizes. loss_step = -fn_loss(**arguments) with tf.control_dependencies(control_inputs=(loss_step,)): def evaluate_step(deltas): with tf.control_dependencies(control_inputs=deltas): applied = self.apply_step(variables=variables, deltas=deltas) with tf.control_dependencies(control_inputs=(applied,)): # Negative value since line search maximizes. return -fn_loss(**arguments) return self.solver.solve( fn_x=evaluate_step, x_init=deltas, base_value=loss_before, target_value=loss_step, estimated_improvement=estimated_improvement )
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 reset(self): """ Resets the agent to its initial state (e.g. on experiment start). Updates the Model's internal episode and time step counter, internal states, and resets preprocessors. """ self.episode, self.timestep, self.next_internals = self.model.reset() self.current_internals = self.next_internals
def act(self, states, deterministic=False, independent=False, fetch_tensors=None, buffered=True, index=0): """ Return action(s) for given state(s). States preprocessing and exploration are applied if configured accordingly. Args: states (any): One state (usually a value tuple) or dict of states if multiple states are expected. deterministic (bool): If true, no exploration and sampling is applied. independent (bool): If true, action is not followed by observe (and hence not included in updates). fetch_tensors (list): Optional String of named tensors to fetch buffered (bool): If true (default), states and internals are not returned but buffered with observes. Must be false for multi-threaded mode as we need atomic inserts. Returns: Scalar value of the action or dict of multiple actions the agent wants to execute. (fetched_tensors) Optional dict() with named tensors fetched """ self.current_internals = self.next_internals if self.unique_state: self.current_states = dict(state=np.asarray(states)) else: self.current_states = {name: np.asarray(states[name]) for name in sorted(states)} if fetch_tensors is not None: # Retrieve action self.current_actions, self.next_internals, self.timestep, self.fetched_tensors = self.model.act( states=self.current_states, internals=self.current_internals, deterministic=deterministic, independent=independent, fetch_tensors=fetch_tensors, index=index ) if self.unique_action: return self.current_actions['action'], self.fetched_tensors else: return self.current_actions, self.fetched_tensors # Retrieve action. self.current_actions, self.next_internals, self.timestep = self.model.act( states=self.current_states, internals=self.current_internals, deterministic=deterministic, independent=independent, index=index ) # Buffered mode only works single-threaded because buffer inserts # by multiple threads are non-atomic and can cause race conditions. if buffered: if self.unique_action: return self.current_actions['action'] else: return self.current_actions else: if self.unique_action: return self.current_actions['action'], self.current_states, self.current_internals else: return self.current_actions, self.current_states, self.current_internals
def observe(self, terminal, reward, index=0): """ Observe experience from the environment to learn from. Optionally pre-processes rewards Child classes should call super to get the processed reward EX: terminal, reward = super()... Args: terminal (bool): boolean indicating if the episode terminated after the observation. reward (float): scalar reward that resulted from executing the action. """ self.current_terminal = terminal self.current_reward = reward if self.batched_observe: # Batched observe for better performance with Python. self.observe_terminal[index].append(self.current_terminal) self.observe_reward[index].append(self.current_reward) if self.current_terminal or len(self.observe_terminal[index]) >= self.batching_capacity: self.episode = self.model.observe( terminal=self.observe_terminal[index], reward=self.observe_reward[index], index=index ) self.observe_terminal[index] = list() self.observe_reward[index] = list() else: self.episode = self.model.observe( terminal=self.current_terminal, reward=self.current_reward )
def atomic_observe(self, states, actions, internals, reward, terminal): """ Utility method for unbuffered observing where each tuple is inserted into TensorFlow via a single session call, thus avoiding race conditions in multi-threaded mode. Observe full experience tuplefrom the environment to learn from. Optionally pre-processes rewards Child classes should call super to get the processed reward EX: terminal, reward = super()... Args: states (any): One state (usually a value tuple) or dict of states if multiple states are expected. actions (any): One action (usually a value tuple) or dict of states if multiple actions are expected. internals (any): Internal list. terminal (bool): boolean indicating if the episode terminated after the observation. reward (float): scalar reward that resulted from executing the action. """ # TODO probably unnecessary here. self.current_terminal = terminal self.current_reward = reward # print('action = {}'.format(actions)) if self.unique_state: states = dict(state=states) if self.unique_action: actions = dict(action=actions) self.episode = self.model.atomic_observe( states=states, actions=actions, internals=internals, terminal=self.current_terminal, reward=self.current_reward )
def save_model(self, directory=None, append_timestep=True): """ Save TensorFlow model. If no checkpoint directory is given, the model's default saver directory is used. Optionally appends current timestep to prevent overwriting previous checkpoint files. Turn off to be able to load model from the same given path argument as given here. Args: directory (str): Optional checkpoint directory. append_timestep (bool): Appends the current timestep to the checkpoint file if true. If this is set to True, the load path must include the checkpoint timestep suffix. For example, if stored to models/ and set to true, the exported file will be of the form models/model.ckpt-X where X is the last timestep saved. The load path must precisely match this file name. If this option is turned off, the checkpoint will always overwrite the file specified in path and the model can always be loaded under this path. Returns: Checkpoint path were the model was saved. """ return self.model.save(directory=directory, append_timestep=append_timestep)
def restore_model(self, directory=None, file=None): """ Restore TensorFlow model. If no checkpoint file is given, the latest checkpoint is restored. If no checkpoint directory is given, the model's default saver directory is used (unless file specifies the entire path). Args: directory: Optional checkpoint directory. file: Optional checkpoint file, or path if directory not given. """ self.model.restore(directory=directory, file=file)