partition
stringclasses
3 values
func_name
stringlengths
1
134
docstring
stringlengths
1
46.9k
path
stringlengths
4
223
original_string
stringlengths
75
104k
code
stringlengths
75
104k
docstring_tokens
listlengths
1
1.97k
repo
stringlengths
7
55
language
stringclasses
1 value
url
stringlengths
87
315
code_tokens
listlengths
19
28.4k
sha
stringlengths
40
40
valid
SRE_Match.group
Returns one or more subgroups of the match. Each argument is either a group index or a group name.
third_party/pypy/_sre.py
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 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)
[ "Returns", "one", "or", "more", "subgroups", "of", "the", "match", ".", "Each", "argument", "is", "either", "a", "group", "index", "or", "a", "group", "name", "." ]
google/grumpy
python
https://github.com/google/grumpy/blob/3ec87959189cfcdeae82eb68a47648ac25ceb10b/third_party/pypy/_sre.py#L317-L328
[ "def", "group", "(", "self", ",", "*", "args", ")", ":", "if", "len", "(", "args", ")", "==", "0", ":", "args", "=", "(", "0", ",", ")", "grouplist", "=", "[", "]", "for", "group", "in", "args", ":", "grouplist", ".", "append", "(", "self", "...
3ec87959189cfcdeae82eb68a47648ac25ceb10b
valid
_State.fast_search
Skips forward in a string as fast as possible using information from an optimization info block.
third_party/pypy/_sre.py
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 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
[ "Skips", "forward", "in", "a", "string", "as", "fast", "as", "possible", "using", "information", "from", "an", "optimization", "info", "block", "." ]
google/grumpy
python
https://github.com/google/grumpy/blob/3ec87959189cfcdeae82eb68a47648ac25ceb10b/third_party/pypy/_sre.py#L419-L453
[ "def", "fast_search", "(", "self", ",", "pattern_codes", ")", ":", "# 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...
3ec87959189cfcdeae82eb68a47648ac25ceb10b
valid
_MatchContext.push_new_context
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.
third_party/pypy/_sre.py
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 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
[ "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", "." ]
google/grumpy
python
https://github.com/google/grumpy/blob/3ec87959189cfcdeae82eb68a47648ac25ceb10b/third_party/pypy/_sre.py#L501-L508
[ "def", "push_new_context", "(", "self", ",", "pattern_offset", ")", ":", "child_context", "=", "_MatchContext", "(", "self", ".", "state", ",", "self", ".", "pattern_codes", "[", "self", ".", "code_position", "+", "pattern_offset", ":", "]", ")", "self", "."...
3ec87959189cfcdeae82eb68a47648ac25ceb10b
valid
_OpcodeDispatcher.match
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.
third_party/pypy/_sre.py
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 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
[ "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", "." ]
google/grumpy
python
https://github.com/google/grumpy/blob/3ec87959189cfcdeae82eb68a47648ac25ceb10b/third_party/pypy/_sre.py#L586-L596
[ "def", "match", "(", "self", ",", "context", ")", ":", "while", "context", ".", "remaining_codes", "(", ")", ">", "0", "and", "context", ".", "has_matched", "is", "None", ":", "opcode", "=", "context", ".", "peek_code", "(", ")", "if", "not", "self", ...
3ec87959189cfcdeae82eb68a47648ac25ceb10b
valid
_OpcodeDispatcher.dispatch
Dispatches a context on a given opcode. Returns True if the context is done matching, False if it must be resumed when next encountered.
third_party/pypy/_sre.py
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 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
[ "Dispatches", "a", "context", "on", "a", "given", "opcode", ".", "Returns", "True", "if", "the", "context", "is", "done", "matching", "False", "if", "it", "must", "be", "resumed", "when", "next", "encountered", "." ]
google/grumpy
python
https://github.com/google/grumpy/blob/3ec87959189cfcdeae82eb68a47648ac25ceb10b/third_party/pypy/_sre.py#L598-L613
[ "def", "dispatch", "(", "self", ",", "opcode", ",", "context", ")", ":", "if", "id", "(", "context", ")", "in", "self", ".", "executing_contexts", ":", "generator", "=", "self", ".", "executing_contexts", "[", "id", "(", "context", ")", "]", "del", "se...
3ec87959189cfcdeae82eb68a47648ac25ceb10b
valid
_OpcodeDispatcher.check_charset
Checks whether a character matches set of arbitrary length. Assumes the code pointer is at the first member of the set.
third_party/pypy/_sre.py
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 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
[ "Checks", "whether", "a", "character", "matches", "set", "of", "arbitrary", "length", ".", "Assumes", "the", "code", "pointer", "is", "at", "the", "first", "member", "of", "the", "set", "." ]
google/grumpy
python
https://github.com/google/grumpy/blob/3ec87959189cfcdeae82eb68a47648ac25ceb10b/third_party/pypy/_sre.py#L1068-L1077
[ "def", "check_charset", "(", "self", ",", "ctx", ",", "char", ")", ":", "self", ".", "set_dispatcher", ".", "reset", "(", "char", ")", "save_position", "=", "ctx", ".", "code_position", "result", "=", "None", "while", "result", "is", "None", ":", "result...
3ec87959189cfcdeae82eb68a47648ac25ceb10b
valid
_OpcodeDispatcher.count_repetitions
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).
third_party/pypy/_sre.py
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 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
[ "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"...
google/grumpy
python
https://github.com/google/grumpy/blob/3ec87959189cfcdeae82eb68a47648ac25ceb10b/third_party/pypy/_sre.py#L1079-L1105
[ "def", "count_repetitions", "(", "self", ",", "ctx", ",", "maxcount", ")", ":", "count", "=", "0", "real_maxcount", "=", "ctx", ".", "state", ".", "end", "-", "ctx", ".", "string_position", "if", "maxcount", "<", "real_maxcount", "and", "maxcount", "!=", ...
3ec87959189cfcdeae82eb68a47648ac25ceb10b
valid
extract
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
third_party/stdlib/fpformat.py
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 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
[ "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", "dig...
google/grumpy
python
https://github.com/google/grumpy/blob/3ec87959189cfcdeae82eb68a47648ac25ceb10b/third_party/stdlib/fpformat.py#L35-L48
[ "def", "extract", "(", "s", ")", ":", "res", "=", "decoder", ".", "match", "(", "s", ")", "if", "res", "is", "None", ":", "raise", "NotANumber", ",", "s", "sign", ",", "intpart", ",", "fraction", ",", "exppart", "=", "res", ".", "group", "(", "1"...
3ec87959189cfcdeae82eb68a47648ac25ceb10b
valid
unexpo
Remove the exponent by changing intpart and fraction.
third_party/stdlib/fpformat.py
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 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
[ "Remove", "the", "exponent", "by", "changing", "intpart", "and", "fraction", "." ]
google/grumpy
python
https://github.com/google/grumpy/blob/3ec87959189cfcdeae82eb68a47648ac25ceb10b/third_party/stdlib/fpformat.py#L50-L62
[ "def", "unexpo", "(", "intpart", ",", "fraction", ",", "expo", ")", ":", "if", "expo", ">", "0", ":", "# Move the point left", "f", "=", "len", "(", "fraction", ")", "intpart", ",", "fraction", "=", "intpart", "+", "fraction", "[", ":", "expo", "]", ...
3ec87959189cfcdeae82eb68a47648ac25ceb10b
valid
roundfrac
Round or extend the fraction to size digs.
third_party/stdlib/fpformat.py
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 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, ''
[ "Round", "or", "extend", "the", "fraction", "to", "size", "digs", "." ]
google/grumpy
python
https://github.com/google/grumpy/blob/3ec87959189cfcdeae82eb68a47648ac25ceb10b/third_party/stdlib/fpformat.py#L64-L88
[ "def", "roundfrac", "(", "intpart", ",", "fraction", ",", "digs", ")", ":", "f", "=", "len", "(", "fraction", ")", "if", "f", "<=", "digs", ":", "return", "intpart", ",", "fraction", "+", "'0'", "*", "(", "digs", "-", "f", ")", "i", "=", "len", ...
3ec87959189cfcdeae82eb68a47648ac25ceb10b
valid
fix
Format x as [-]ddd.ddd with 'digs' digits after the point and at least one digit before. If digs <= 0, the point is suppressed.
third_party/stdlib/fpformat.py
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 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
[ "Format", "x", "as", "[", "-", "]", "ddd", ".", "ddd", "with", "digs", "digits", "after", "the", "point", "and", "at", "least", "one", "digit", "before", ".", "If", "digs", "<", "=", "0", "the", "point", "is", "suppressed", "." ]
google/grumpy
python
https://github.com/google/grumpy/blob/3ec87959189cfcdeae82eb68a47648ac25ceb10b/third_party/stdlib/fpformat.py#L90-L104
[ "def", "fix", "(", "x", ",", "digs", ")", ":", "if", "type", "(", "x", ")", "!=", "type", "(", "''", ")", ":", "x", "=", "repr", "(", "x", ")", "try", ":", "sign", ",", "intpart", ",", "fraction", ",", "expo", "=", "extract", "(", "x", ")",...
3ec87959189cfcdeae82eb68a47648ac25ceb10b
valid
sci
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.
third_party/stdlib/fpformat.py
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 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
[ "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", ...
google/grumpy
python
https://github.com/google/grumpy/blob/3ec87959189cfcdeae82eb68a47648ac25ceb10b/third_party/stdlib/fpformat.py#L106-L136
[ "def", "sci", "(", "x", ",", "digs", ")", ":", "if", "type", "(", "x", ")", "!=", "type", "(", "''", ")", ":", "x", "=", "repr", "(", "x", ")", "sign", ",", "intpart", ",", "fraction", ",", "expo", "=", "extract", "(", "x", ")", "if", "not"...
3ec87959189cfcdeae82eb68a47648ac25ceb10b
valid
GrumpyRandom.getrandbits
getrandbits(k) -> x. Generates an int with k random bits.
lib/_random.py
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 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)
[ "getrandbits", "(", "k", ")", "-", ">", "x", ".", "Generates", "an", "int", "with", "k", "random", "bits", "." ]
google/grumpy
python
https://github.com/google/grumpy/blob/3ec87959189cfcdeae82eb68a47648ac25ceb10b/lib/_random.py#L74-L82
[ "def", "getrandbits", "(", "self", ",", "k", ")", ":", "if", "k", "<=", "0", ":", "raise", "ValueError", "(", "'number of bits must be greater than zero'", ")", "if", "k", "!=", "int", "(", "k", ")", ":", "raise", "TypeError", "(", "'number of bits should be...
3ec87959189cfcdeae82eb68a47648ac25ceb10b
valid
GrumpyRandom._randbelow
Return a random int in the range [0,n).
lib/_random.py
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 _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
[ "Return", "a", "random", "int", "in", "the", "range", "[", "0", "n", ")", "." ]
google/grumpy
python
https://github.com/google/grumpy/blob/3ec87959189cfcdeae82eb68a47648ac25ceb10b/lib/_random.py#L90-L99
[ "def", "_randbelow", "(", "self", ",", "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", ...
3ec87959189cfcdeae82eb68a47648ac25ceb10b
valid
getopt
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.
third_party/stdlib/getopt.py
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 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
[ "getopt", "(", "args", "options", "[", "long_options", "]", ")", "-", ">", "opts", "args" ]
google/grumpy
python
https://github.com/google/grumpy/blob/3ec87959189cfcdeae82eb68a47648ac25ceb10b/third_party/stdlib/getopt.py#L51-L92
[ "def", "getopt", "(", "args", ",", "shortopts", ",", "longopts", "=", "[", "]", ")", ":", "opts", "=", "[", "]", "if", "type", "(", "longopts", ")", "==", "type", "(", "\"\"", ")", ":", "longopts", "=", "[", "longopts", "]", "else", ":", "longopt...
3ec87959189cfcdeae82eb68a47648ac25ceb10b
valid
gnu_getopt
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.
third_party/stdlib/getopt.py
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 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
[ "getopt", "(", "args", "options", "[", "long_options", "]", ")", "-", ">", "opts", "args" ]
google/grumpy
python
https://github.com/google/grumpy/blob/3ec87959189cfcdeae82eb68a47648ac25ceb10b/third_party/stdlib/getopt.py#L94-L142
[ "def", "gnu_getopt", "(", "args", ",", "shortopts", ",", "longopts", "=", "[", "]", ")", ":", "opts", "=", "[", "]", "prog_args", "=", "[", "]", "if", "isinstance", "(", "longopts", ",", "str", ")", ":", "longopts", "=", "[", "longopts", "]", "else...
3ec87959189cfcdeae82eb68a47648ac25ceb10b
valid
filter
Return the subset of the list NAMES that match PAT
third_party/stdlib/fnmatch.py
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 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
[ "Return", "the", "subset", "of", "the", "list", "NAMES", "that", "match", "PAT" ]
google/grumpy
python
https://github.com/google/grumpy/blob/3ec87959189cfcdeae82eb68a47648ac25ceb10b/third_party/stdlib/fnmatch.py#L46-L71
[ "def", "filter", "(", "names", ",", "pat", ")", ":", "import", "os", "# import posixpath", "result", "=", "[", "]", "# pat=os.path.normcase(pat)", "try", ":", "re_pat", "=", "_cache", "[", "pat", "]", "except", "KeyError", ":", "res", "=", "translate", "("...
3ec87959189cfcdeae82eb68a47648ac25ceb10b
valid
fnmatchcase
Test whether FILENAME matches PATTERN, including case. This is a version of fnmatch() which doesn't case-normalize its arguments.
third_party/stdlib/fnmatch.py
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 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
[ "Test", "whether", "FILENAME", "matches", "PATTERN", "including", "case", "." ]
google/grumpy
python
https://github.com/google/grumpy/blob/3ec87959189cfcdeae82eb68a47648ac25ceb10b/third_party/stdlib/fnmatch.py#L73-L88
[ "def", "fnmatchcase", "(", "name", ",", "pat", ")", ":", "try", ":", "re_pat", "=", "_cache", "[", "pat", "]", "except", "KeyError", ":", "res", "=", "translate", "(", "pat", ")", "if", "len", "(", "_cache", ")", ">=", "_MAXCACHE", ":", "# _cache.cle...
3ec87959189cfcdeae82eb68a47648ac25ceb10b
valid
translate
Translate a shell PATTERN to a regular expression. There is no way to quote meta-characters.
third_party/stdlib/fnmatch.py
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 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)'
[ "Translate", "a", "shell", "PATTERN", "to", "a", "regular", "expression", "." ]
google/grumpy
python
https://github.com/google/grumpy/blob/3ec87959189cfcdeae82eb68a47648ac25ceb10b/third_party/stdlib/fnmatch.py#L90-L125
[ "def", "translate", "(", "pat", ")", ":", "i", ",", "n", "=", "0", ",", "len", "(", "pat", ")", "res", "=", "''", "while", "i", "<", "n", ":", "c", "=", "pat", "[", "i", "]", "i", "=", "i", "+", "1", "if", "c", "==", "'*'", ":", "res", ...
3ec87959189cfcdeae82eb68a47648ac25ceb10b
valid
Queue.task_done
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.
third_party/stdlib/Queue.py
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 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()
[ "Indicate", "that", "a", "formerly", "enqueued", "task", "is", "complete", "." ]
google/grumpy
python
https://github.com/google/grumpy/blob/3ec87959189cfcdeae82eb68a47648ac25ceb10b/third_party/stdlib/Queue.py#L45-L68
[ "def", "task_done", "(", "self", ")", ":", "self", ".", "all_tasks_done", ".", "acquire", "(", ")", "try", ":", "unfinished", "=", "self", ".", "unfinished_tasks", "-", "1", "if", "unfinished", "<=", "0", ":", "if", "unfinished", "<", "0", ":", "raise"...
3ec87959189cfcdeae82eb68a47648ac25ceb10b
valid
Queue.qsize
Return the approximate size of the queue (not reliable!).
third_party/stdlib/Queue.py
def qsize(self): """Return the approximate size of the queue (not reliable!).""" self.mutex.acquire() n = self._qsize() self.mutex.release() return n
def qsize(self): """Return the approximate size of the queue (not reliable!).""" self.mutex.acquire() n = self._qsize() self.mutex.release() return n
[ "Return", "the", "approximate", "size", "of", "the", "queue", "(", "not", "reliable!", ")", "." ]
google/grumpy
python
https://github.com/google/grumpy/blob/3ec87959189cfcdeae82eb68a47648ac25ceb10b/third_party/stdlib/Queue.py#L86-L91
[ "def", "qsize", "(", "self", ")", ":", "self", ".", "mutex", ".", "acquire", "(", ")", "n", "=", "self", ".", "_qsize", "(", ")", "self", ".", "mutex", ".", "release", "(", ")", "return", "n" ]
3ec87959189cfcdeae82eb68a47648ac25ceb10b
valid
Queue.empty
Return True if the queue is empty, False otherwise (not reliable!).
third_party/stdlib/Queue.py
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 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
[ "Return", "True", "if", "the", "queue", "is", "empty", "False", "otherwise", "(", "not", "reliable!", ")", "." ]
google/grumpy
python
https://github.com/google/grumpy/blob/3ec87959189cfcdeae82eb68a47648ac25ceb10b/third_party/stdlib/Queue.py#L93-L98
[ "def", "empty", "(", "self", ")", ":", "self", ".", "mutex", ".", "acquire", "(", ")", "n", "=", "not", "self", ".", "_qsize", "(", ")", "self", ".", "mutex", ".", "release", "(", ")", "return", "n" ]
3ec87959189cfcdeae82eb68a47648ac25ceb10b
valid
Queue.full
Return True if the queue is full, False otherwise (not reliable!).
third_party/stdlib/Queue.py
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 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
[ "Return", "True", "if", "the", "queue", "is", "full", "False", "otherwise", "(", "not", "reliable!", ")", "." ]
google/grumpy
python
https://github.com/google/grumpy/blob/3ec87959189cfcdeae82eb68a47648ac25ceb10b/third_party/stdlib/Queue.py#L100-L105
[ "def", "full", "(", "self", ")", ":", "self", ".", "mutex", ".", "acquire", "(", ")", "n", "=", "0", "<", "self", ".", "maxsize", "==", "self", ".", "_qsize", "(", ")", "self", ".", "mutex", ".", "release", "(", ")", "return", "n" ]
3ec87959189cfcdeae82eb68a47648ac25ceb10b
valid
Queue.put
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).
third_party/stdlib/Queue.py
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 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()
[ "Put", "an", "item", "into", "the", "queue", "." ]
google/grumpy
python
https://github.com/google/grumpy/blob/3ec87959189cfcdeae82eb68a47648ac25ceb10b/third_party/stdlib/Queue.py#L107-L140
[ "def", "put", "(", "self", ",", "item", ",", "block", "=", "True", ",", "timeout", "=", "None", ")", ":", "self", ".", "not_full", ".", "acquire", "(", ")", "try", ":", "if", "self", ".", "maxsize", ">", "0", ":", "if", "not", "block", ":", "if...
3ec87959189cfcdeae82eb68a47648ac25ceb10b
valid
calculate_transitive_deps
Determines all modules that script transitively depends upon.
compiler/imputil.py
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 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
[ "Determines", "all", "modules", "that", "script", "transitively", "depends", "upon", "." ]
google/grumpy
python
https://github.com/google/grumpy/blob/3ec87959189cfcdeae82eb68a47648ac25ceb10b/compiler/imputil.py#L207-L233
[ "def", "calculate_transitive_deps", "(", "modname", ",", "script", ",", "gopath", ")", ":", "deps", "=", "set", "(", ")", "def", "calc", "(", "modname", ",", "script", ")", ":", "if", "modname", "in", "deps", ":", "return", "deps", ".", "add", "(", "...
3ec87959189cfcdeae82eb68a47648ac25ceb10b
valid
_make_future_features
Processes a future import statement, returning set of flags it defines.
compiler/imputil.py
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 _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
[ "Processes", "a", "future", "import", "statement", "returning", "set", "of", "flags", "it", "defines", "." ]
google/grumpy
python
https://github.com/google/grumpy/blob/3ec87959189cfcdeae82eb68a47648ac25ceb10b/compiler/imputil.py#L276-L293
[ "def", "_make_future_features", "(", "node", ")", ":", "assert", "isinstance", "(", "node", ",", "ast", ".", "ImportFrom", ")", "assert", "node", ".", "module", "==", "'__future__'", "features", "=", "FutureFeatures", "(", ")", "for", "alias", "in", "node", ...
3ec87959189cfcdeae82eb68a47648ac25ceb10b
valid
parse_future_features
Accumulates a set of flags for the compiler __future__ imports.
compiler/imputil.py
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 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()
[ "Accumulates", "a", "set", "of", "flags", "for", "the", "compiler", "__future__", "imports", "." ]
google/grumpy
python
https://github.com/google/grumpy/blob/3ec87959189cfcdeae82eb68a47648ac25ceb10b/compiler/imputil.py#L296-L311
[ "def", "parse_future_features", "(", "mod", ")", ":", "assert", "isinstance", "(", "mod", ",", "ast", ".", "Module", ")", "found_docstring", "=", "False", "for", "node", "in", "mod", ".", "body", ":", "if", "isinstance", "(", "node", ",", "ast", ".", "...
3ec87959189cfcdeae82eb68a47648ac25ceb10b
valid
contextmanager
@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>
third_party/stdlib/contextlib.py
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 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
[ "@contextmanager", "decorator", "." ]
google/grumpy
python
https://github.com/google/grumpy/blob/3ec87959189cfcdeae82eb68a47648ac25ceb10b/third_party/stdlib/contextlib.py#L60-L91
[ "def", "contextmanager", "(", "func", ")", ":", "@", "wraps", "(", "func", ")", "def", "helper", "(", "*", "args", ",", "*", "*", "kwds", ")", ":", "return", "GeneratorContextManager", "(", "func", "(", "*", "args", ",", "*", "*", "kwds", ")", ")",...
3ec87959189cfcdeae82eb68a47648ac25ceb10b
valid
nested
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()
third_party/stdlib/contextlib.py
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 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]
[ "Combine", "multiple", "context", "managers", "into", "a", "single", "nested", "context", "manager", "." ]
google/grumpy
python
https://github.com/google/grumpy/blob/3ec87959189cfcdeae82eb68a47648ac25ceb10b/third_party/stdlib/contextlib.py#L95-L135
[ "def", "nested", "(", "*", "managers", ")", ":", "warn", "(", "\"With-statements now directly support multiple context managers\"", ",", "DeprecationWarning", ",", "3", ")", "exits", "=", "[", "]", "vars", "=", "[", "]", "exc", "=", "(", "None", ",", "None", ...
3ec87959189cfcdeae82eb68a47648ac25ceb10b
valid
JSONDecoder.decode
Return the Python representation of ``s`` (a ``str`` or ``unicode`` instance containing a JSON document)
third_party/stdlib/json/decoder.py
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 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
[ "Return", "the", "Python", "representation", "of", "s", "(", "a", "str", "or", "unicode", "instance", "containing", "a", "JSON", "document", ")" ]
google/grumpy
python
https://github.com/google/grumpy/blob/3ec87959189cfcdeae82eb68a47648ac25ceb10b/third_party/stdlib/json/decoder.py#L362-L371
[ "def", "decode", "(", "self", ",", "s", ",", "_w", "=", "WHITESPACE", ".", "match", ")", ":", "obj", ",", "end", "=", "self", ".", "raw_decode", "(", "s", ",", "idx", "=", "_w", "(", "s", ",", "0", ")", ".", "end", "(", ")", ")", "end", "="...
3ec87959189cfcdeae82eb68a47648ac25ceb10b
valid
Baseline.tf_loss
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
tensorforce/core/baselines/baseline.py
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 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))
[ "Creates", "the", "TensorFlow", "operations", "for", "calculating", "the", "L2", "loss", "between", "predicted", "state", "values", "and", "actual", "rewards", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/core/baselines/baseline.py#L107-L123
[ "def", "tf_loss", "(", "self", ",", "states", ",", "internals", ",", "reward", ",", "update", ",", "reference", "=", "None", ")", ":", "prediction", "=", "self", ".", "predict", "(", "states", "=", "states", ",", "internals", "=", "internals", ",", "up...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
Baseline.get_variables
Returns the TensorFlow variables used by the baseline. Returns: List of variables
tensorforce/core/baselines/baseline.py
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 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)]
[ "Returns", "the", "TensorFlow", "variables", "used", "by", "the", "baseline", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/core/baselines/baseline.py#L134-L144
[ "def", "get_variables", "(", "self", ",", "include_nontrainable", "=", "False", ")", ":", "if", "include_nontrainable", ":", "return", "[", "self", ".", "all_variables", "[", "key", "]", "for", "key", "in", "sorted", "(", "self", ".", "all_variables", ")", ...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
Baseline.from_spec
Creates a baseline from a specification dict.
tensorforce/core/baselines/baseline.py
def from_spec(spec, kwargs=None): """ Creates a baseline from a specification dict. """ baseline = util.get_object( obj=spec, predefined_objects=tensorforce.core.baselines.baselines, kwargs=kwargs ) assert isinstance(baseline, Baseline) return baseline
def from_spec(spec, kwargs=None): """ Creates a 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
[ "Creates", "a", "baseline", "from", "a", "specification", "dict", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/core/baselines/baseline.py#L147-L157
[ "def", "from_spec", "(", "spec", ",", "kwargs", "=", "None", ")", ":", "baseline", "=", "util", ".", "get_object", "(", "obj", "=", "spec", ",", "predefined_objects", "=", "tensorforce", ".", "core", ".", "baselines", ".", "baselines", ",", "kwargs", "="...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
UE4Environment.reset
same as step (no kwargs to pass), but needs to block and return observation_dict - stores the received observation in self.last_observation
tensorforce/contrib/unreal_engine.py
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 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)
[ "same", "as", "step", "(", "no", "kwargs", "to", "pass", ")", "but", "needs", "to", "block", "and", "return", "observation_dict", "-", "stores", "the", "received", "observation", "in", "self", ".", "last_observation" ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/contrib/unreal_engine.py#L118-L128
[ "def", "reset", "(", "self", ")", ":", "# Send command.", "self", ".", "protocol", ".", "send", "(", "{", "\"cmd\"", ":", "\"reset\"", "}", ",", "self", ".", "socket", ")", "# Wait for response.", "response", "=", "self", ".", "protocol", ".", "recv", "(...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
UE4Environment.execute
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.
tensorforce/contrib/unreal_engine.py
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 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
[ "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", "-", ...
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/contrib/unreal_engine.py#L155-L209
[ "def", "execute", "(", "self", ",", "action", ")", ":", "action_mappings", ",", "axis_mappings", "=", "[", "]", ",", "[", "]", "# TODO: what if more than one actions are passed?", "# Discretized -> each action is an int", "if", "self", ".", "discretize_actions", ":", ...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
UE4Environment.translate_abstract_actions_to_keys
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)
tensorforce/contrib/unreal_engine.py
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 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
[ "Translates", "a", "list", "of", "tuples", "(", "[", "pretty", "mapping", "]", "[", "value", "]", ")", "to", "a", "list", "of", "tuples", "(", "[", "some", "key", "]", "[", "translated", "value", "]", ")", "each", "single", "item", "in", "abstract", ...
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/contrib/unreal_engine.py#L276-L311
[ "def", "translate_abstract_actions_to_keys", "(", "self", ",", "abstract", ")", ":", "# Solve single tuple with name and value -> should become a list (len=1) of this tuple.", "if", "len", "(", "abstract", ")", ">=", "2", "and", "not", "isinstance", "(", "abstract", "[", ...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
UE4Environment.discretize_action_space_desc
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)], ] ```
tensorforce/contrib/unreal_engine.py
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 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
[ "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", "co...
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/contrib/unreal_engine.py#L313-L376
[ "def", "discretize_action_space_desc", "(", "self", ")", ":", "# Put all unique_keys lists in one list and itertools.product that list.", "unique_list", "=", "[", "]", "for", "nice", ",", "record", "in", "self", ".", "action_space_desc", ".", "items", "(", ")", ":", "...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
DeepMindLab.reset
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
tensorforce/contrib/deepmind_lab.py
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 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]
[ "Resets", "the", "environment", "to", "its", "initialization", "state", ".", "This", "method", "needs", "to", "be", "called", "to", "start", "a", "new", "episode", "after", "the", "last", "episode", "ended", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/contrib/deepmind_lab.py#L102-L110
[ "def", "reset", "(", "self", ")", ":", "self", ".", "level", ".", "reset", "(", ")", "# optional: episode=-1, seed=None", "return", "self", ".", "level", ".", "observations", "(", ")", "[", "self", ".", "state_attribute", "]" ]
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
DeepMindLab.execute
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
tensorforce/contrib/deepmind_lab.py
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 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
[ "Pass", "action", "to", "universe", "environment", "return", "reward", "next", "step", "terminal", "state", "and", "additional", "info", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/contrib/deepmind_lab.py#L112-L133
[ "def", "execute", "(", "self", ",", "action", ")", ":", "adjusted_action", "=", "list", "(", ")", "for", "action_spec", "in", "self", ".", "level", ".", "action_spec", "(", ")", ":", "if", "action_spec", "[", "'min'", "]", "==", "-", "1", "and", "act...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
ConjugateGradient.tf_solve
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.
tensorforce/core/optimizers/solvers/conjugate_gradient.py
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_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)
[ "Iteratively", "solves", "the", "system", "of", "linear", "equations", "$A", "x", "=", "b$", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/core/optimizers/solvers/conjugate_gradient.py#L68-L80
[ "def", "tf_solve", "(", "self", ",", "fn_x", ",", "x_init", ",", "b", ")", ":", "return", "super", "(", "ConjugateGradient", ",", "self", ")", ".", "tf_solve", "(", "fn_x", ",", "x_init", ",", "b", ")" ]
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
ConjugateGradient.tf_initialize
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.
tensorforce/core/optimizers/solvers/conjugate_gradient.py
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_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)
[ "Initialization", "step", "preparing", "the", "arguments", "for", "the", "first", "iteration", "of", "the", "loop", "body", ":", "$x_0", "0", "p_0", "r_0", "r_0^2$", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/core/optimizers/solvers/conjugate_gradient.py#L82-L107
[ "def", "tf_initialize", "(", "self", ",", "x_init", ",", "b", ")", ":", "if", "x_init", "is", "None", ":", "# Initial guess is zero vector if not given.", "x_init", "=", "[", "tf", ".", "zeros", "(", "shape", "=", "util", ".", "shape", "(", "t", ")", ")"...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
ConjugateGradient.tf_step
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.
tensorforce/core/optimizers/solvers/conjugate_gradient.py
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_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
[ "Iteration", "loop", "body", "of", "the", "conjugate", "gradient", "algorithm", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/core/optimizers/solvers/conjugate_gradient.py#L109-L157
[ "def", "tf_step", "(", "self", ",", "x", ",", "iteration", ",", "conjugate", ",", "residual", ",", "squared_residual", ")", ":", "x", ",", "next_iteration", ",", "conjugate", ",", "residual", ",", "squared_residual", "=", "super", "(", "ConjugateGradient", "...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
ConjugateGradient.tf_next_step
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.
tensorforce/core/optimizers/solvers/conjugate_gradient.py
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_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))
[ "Termination", "condition", ":", "max", "number", "of", "iterations", "or", "residual", "sufficiently", "small", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/core/optimizers/solvers/conjugate_gradient.py#L159-L174
[ "def", "tf_next_step", "(", "self", ",", "x", ",", "iteration", ",", "conjugate", ",", "residual", ",", "squared_residual", ")", ":", "next_step", "=", "super", "(", "ConjugateGradient", ",", "self", ")", ".", "tf_next_step", "(", "x", ",", "iteration", ",...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
ClippedStep.tf_step
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.
tensorforce/core/optimizers/clipped_step.py
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 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]
[ "Creates", "the", "TensorFlow", "operations", "for", "performing", "an", "optimization", "step", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/core/optimizers/clipped_step.py#L44-L73
[ "def", "tf_step", "(", "self", ",", "time", ",", "variables", ",", "*", "*", "kwargs", ")", ":", "deltas", "=", "self", ".", "optimizer", ".", "step", "(", "time", "=", "time", ",", "variables", "=", "variables", ",", "*", "*", "kwargs", ")", "with...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
Layer.from_spec
Creates a layer from a specification dict.
tensorforce/core/networks/layer.py
def from_spec(spec, kwargs=None): """ Creates a layer from a specification dict. """ layer = util.get_object( obj=spec, predefined_objects=tensorforce.core.networks.layers, kwargs=kwargs ) assert isinstance(layer, Layer) return layer
def from_spec(spec, kwargs=None): """ Creates a 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
[ "Creates", "a", "layer", "from", "a", "specification", "dict", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/core/networks/layer.py#L121-L131
[ "def", "from_spec", "(", "spec", ",", "kwargs", "=", "None", ")", ":", "layer", "=", "util", ".", "get_object", "(", "obj", "=", "spec", ",", "predefined_objects", "=", "tensorforce", ".", "core", ".", "networks", ".", "layers", ",", "kwargs", "=", "kw...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
QModel.tf_q_delta
Creates the deltas (or advantage) of the Q values. :return: A list of deltas per action
tensorforce/models/q_model.py
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 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
[ "Creates", "the", "deltas", "(", "or", "advantage", ")", "of", "the", "Q", "values", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/models/q_model.py#L137-L154
[ "def", "tf_q_delta", "(", "self", ",", "q_value", ",", "next_q_value", ",", "terminal", ",", "reward", ")", ":", "for", "_", "in", "range", "(", "util", ".", "rank", "(", "q_value", ")", "-", "1", ")", ":", "terminal", "=", "tf", ".", "expand_dims", ...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
QModel.target_optimizer_arguments
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.
tensorforce/models/q_model.py
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 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
[ "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", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/models/q_model.py#L215-L242
[ "def", "target_optimizer_arguments", "(", "self", ")", ":", "variables", "=", "self", ".", "target_network", ".", "get_variables", "(", ")", "+", "[", "variable", "for", "name", "in", "sorted", "(", "self", ".", "target_distributions", ")", "for", "variable", ...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
Environment.from_spec
Creates an environment from a specification dict.
tensorforce/environments/environment.py
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 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
[ "Creates", "an", "environment", "from", "a", "specification", "dict", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/environments/environment.py#L102-L112
[ "def", "from_spec", "(", "spec", ",", "kwargs", ")", ":", "env", "=", "tensorforce", ".", "util", ".", "get_object", "(", "obj", "=", "spec", ",", "predefined_objects", "=", "tensorforce", ".", "environments", ".", "environments", ",", "kwargs", "=", "kwar...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
setup
When used for spinx extension.
docs/m2r.py
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 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)
[ "When", "used", "for", "spinx", "extension", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/docs/m2r.py#L539-L545
[ "def", "setup", "(", "app", ")", ":", "global", "_is_sphinx", "_is_sphinx", "=", "True", "app", ".", "add_config_value", "(", "'no_underscore_emphasis'", ",", "False", ",", "'env'", ")", "app", ".", "add_source_parser", "(", "'.md'", ",", "M2RParser", ")", "...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
RestInlineLexer.output_image_link
Pass through rest role.
docs/m2r.py
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_image_link(self, m): """Pass through rest role.""" return self.renderer.image_link( m.group('url'), m.group('target'), m.group('alt'))
[ "Pass", "through", "rest", "role", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/docs/m2r.py#L146-L149
[ "def", "output_image_link", "(", "self", ",", "m", ")", ":", "return", "self", ".", "renderer", ".", "image_link", "(", "m", ".", "group", "(", "'url'", ")", ",", "m", ".", "group", "(", "'target'", ")", ",", "m", ".", "group", "(", "'alt'", ")", ...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
RestInlineLexer.output_eol_literal_marker
Pass through rest link.
docs/m2r.py
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 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)
[ "Pass", "through", "rest", "link", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/docs/m2r.py#L163-L166
[ "def", "output_eol_literal_marker", "(", "self", ",", "m", ")", ":", "marker", "=", "':'", "if", "m", ".", "group", "(", "1", ")", "is", "None", "else", "''", "return", "self", ".", "renderer", ".", "eol_literal_marker", "(", "marker", ")" ]
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
RestRenderer.header
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.
docs/m2r.py
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 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))
[ "Rendering", "header", "/", "heading", "tags", "like", "<h1", ">", "<h2", ">", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/docs/m2r.py#L213-L220
[ "def", "header", "(", "self", ",", "text", ",", "level", ",", "raw", "=", "None", ")", ":", "return", "'\\n{0}\\n{1}\\n'", ".", "format", "(", "text", ",", "self", ".", "hmarks", "[", "level", "]", "*", "len", "(", "text", ")", ")" ]
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
RestRenderer.list
Rendering list tags like ``<ul>`` and ``<ol>``. :param body: body contents of the list. :param ordered: whether this list is ordered or not.
docs/m2r.py
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 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)
[ "Rendering", "list", "tags", "like", "<ul", ">", "and", "<ol", ">", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/docs/m2r.py#L226-L238
[ "def", "list", "(", "self", ",", "body", ",", "ordered", "=", "True", ")", ":", "mark", "=", "'#. '", "if", "ordered", "else", "'* '", "lines", "=", "body", ".", "splitlines", "(", ")", "for", "i", ",", "line", "in", "enumerate", "(", "lines", ")",...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
RestRenderer.table
Rendering table element. Wrap header and body in it. :param header: header part of the table. :param body: body part of the table.
docs/m2r.py
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(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
[ "Rendering", "table", "element", ".", "Wrap", "header", "and", "body", "in", "it", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/docs/m2r.py#L248-L261
[ "def", "table", "(", "self", ",", "header", ",", "body", ")", ":", "table", "=", "'\\n.. list-table::\\n'", "if", "header", "and", "not", "header", ".", "isspace", "(", ")", ":", "table", "=", "(", "table", "+", "self", ".", "indent", "+", "':header-ro...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
RestRenderer.table_row
Rendering a table row. Like ``<tr>``. :param content: content of current table row.
docs/m2r.py
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 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'
[ "Rendering", "a", "table", "row", ".", "Like", "<tr", ">", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/docs/m2r.py#L263-L275
[ "def", "table_row", "(", "self", ",", "content", ")", ":", "contents", "=", "content", ".", "splitlines", "(", ")", "if", "not", "contents", ":", "return", "''", "clist", "=", "[", "'* '", "+", "contents", "[", "0", "]", "]", "if", "len", "(", "con...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
RestRenderer.codespan
Rendering inline `code` text. :param text: text content for inline code.
docs/m2r.py
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 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;')))
[ "Rendering", "inline", "code", "text", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/docs/m2r.py#L300-L312
[ "def", "codespan", "(", "self", ",", "text", ")", ":", "if", "'``'", "not", "in", "text", ":", "return", "'\\ ``{}``\\ '", ".", "format", "(", "text", ")", "else", ":", "# actually, docutils split spaces in literal", "return", "self", ".", "_raw_html", "(", ...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
RestRenderer.link
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.
docs/m2r.py
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 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)
[ "Rendering", "a", "given", "link", "with", "content", "and", "title", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/docs/m2r.py#L342-L351
[ "def", "link", "(", "self", ",", "link", ",", "title", ",", "text", ")", ":", "if", "title", ":", "raise", "NotImplementedError", "(", "'sorry'", ")", "return", "'\\ `{text} <{target}>`_\\ '", ".", "format", "(", "target", "=", "link", ",", "text", "=", ...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
RestRenderer.image
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.
docs/m2r.py
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 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), '', ])
[ "Rendering", "a", "image", "with", "title", "and", "text", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/docs/m2r.py#L353-L368
[ "def", "image", "(", "self", ",", "src", ",", "title", ",", "text", ")", ":", "# rst does not support title option", "# and I couldn't find title attribute in HTML standard", "return", "'\\n'", ".", "join", "(", "[", "''", ",", "'.. image:: {}'", ".", "format", "(",...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
MdInclude.run
Most of this method is from ``docutils.parser.rst.Directive``. docutils version: 0.12
docs/m2r.py
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 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 []
[ "Most", "of", "this", "method", "is", "from", "docutils", ".", "parser", ".", "rst", ".", "Directive", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/docs/m2r.py#L486-L536
[ "def", "run", "(", "self", ")", ":", "if", "not", "self", ".", "state", ".", "document", ".", "settings", ".", "file_insertion_enabled", ":", "raise", "self", ".", "warning", "(", "'\"%s\" directive disabled.'", "%", "self", ".", "name", ")", "source", "="...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
WorkerAgentGenerator
Worker Agent generator, receives an Agent class and creates a Worker Agent class that inherits from that Agent.
tensorforce/execution/threaded_runner.py
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 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
[ "Worker", "Agent", "generator", "receives", "an", "Agent", "class", "and", "creates", "a", "Worker", "Agent", "class", "that", "inherits", "from", "that", "Agent", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/execution/threaded_runner.py#L297-L329
[ "def", "WorkerAgentGenerator", "(", "agent_class", ")", ":", "# Support special case where class is given as type-string (AgentsDictionary) or class-name-string.", "if", "isinstance", "(", "agent_class", ",", "str", ")", ":", "agent_class", "=", "AgentsDictionary", ".", "get", ...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
clone_worker_agent
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).
tensorforce/execution/threaded_runner.py
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 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
[ "Clones", "a", "given", "Agent", "(", "factor", "times", ")", "and", "returns", "a", "list", "of", "the", "cloned", "Agents", "with", "the", "original", "Agent", "in", "the", "first", "slot", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/execution/threaded_runner.py#L332-L357
[ "def", "clone_worker_agent", "(", "agent", ",", "factor", ",", "environment", ",", "network", ",", "agent_config", ")", ":", "ret", "=", "[", "agent", "]", "for", "i", "in", "xrange", "(", "factor", "-", "1", ")", ":", "worker", "=", "WorkerAgentGenerato...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
ThreadedRunner.run
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.
tensorforce/execution/threaded_runner.py
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( 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')
[ "Executes", "this", "runner", "by", "starting", "all", "Agents", "in", "parallel", "(", "each", "one", "in", "one", "thread", ")", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/execution/threaded_runner.py#L88-L186
[ "def", "run", "(", "self", ",", "num_episodes", "=", "-", "1", ",", "max_episode_timesteps", "=", "-", "1", ",", "episode_finished", "=", "None", ",", "summary_report", "=", "None", ",", "summary_interval", "=", "0", ",", "num_timesteps", "=", "None", ",",...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
ThreadedRunner._run_single
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.
tensorforce/execution/threaded_runner.py
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 _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
[ "The", "target", "function", "for", "a", "thread", "runs", "an", "agent", "and", "environment", "until", "signaled", "to", "stop", ".", "Adds", "rewards", "to", "shared", "episode", "rewards", "list", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/execution/threaded_runner.py#L188-L277
[ "def", "_run_single", "(", "self", ",", "thread_id", ",", "agent", ",", "environment", ",", "deterministic", "=", "False", ",", "max_episode_timesteps", "=", "-", "1", ",", "episode_finished", "=", "None", ",", "testing", "=", "False", ",", "sleep", "=", "...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
OpenAIUniverse._int_to_pos
Returns x, y from flat_position integer. Args: flat_position: flattened position integer Returns: x, y
tensorforce/contrib/openai_universe.py
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 _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]
[ "Returns", "x", "y", "from", "flat_position", "integer", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/contrib/openai_universe.py#L86-L96
[ "def", "_int_to_pos", "(", "self", ",", "flat_position", ")", ":", "return", "flat_position", "%", "self", ".", "env", ".", "action_space", ".", "screen_shape", "[", "0", "]", ",", "flat_position", "%", "self", ".", "env", ".", "action_space", ".", "screen...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
OpenAIUniverse._wait_state
Wait until there is a state.
tensorforce/contrib/openai_universe.py
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 _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
[ "Wait", "until", "there", "is", "a", "state", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/contrib/openai_universe.py#L110-L117
[ "def", "_wait_state", "(", "self", ",", "state", ",", "reward", ",", "terminal", ")", ":", "while", "state", "==", "[", "None", "]", "or", "not", "state", ":", "state", ",", "terminal", ",", "reward", "=", "self", ".", "_execute", "(", "dict", "(", ...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
Optimizer.apply_step
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.
tensorforce/core/optimizers/optimizer.py
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 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)) )
[ "Applies", "the", "given", "(", "and", "already", "calculated", ")", "step", "deltas", "to", "the", "variable", "values", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/core/optimizers/optimizer.py#L75-L90
[ "def", "apply_step", "(", "self", ",", "variables", ",", "deltas", ")", ":", "if", "len", "(", "variables", ")", "!=", "len", "(", "deltas", ")", ":", "raise", "TensorForceError", "(", "\"Invalid variables and deltas lists.\"", ")", "return", "tf", ".", "gro...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
Optimizer.minimize
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.
tensorforce/core/optimizers/optimizer.py
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 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()
[ "Performs", "an", "optimization", "step", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/core/optimizers/optimizer.py#L92-L146
[ "def", "minimize", "(", "self", ",", "time", ",", "variables", ",", "*", "*", "kwargs", ")", ":", "# # 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', 'gradie...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
Optimizer.from_spec
Creates an optimizer from a specification dict.
tensorforce/core/optimizers/optimizer.py
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 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
[ "Creates", "an", "optimizer", "from", "a", "specification", "dict", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/core/optimizers/optimizer.py#L158-L168
[ "def", "from_spec", "(", "spec", ",", "kwargs", "=", "None", ")", ":", "optimizer", "=", "util", ".", "get_object", "(", "obj", "=", "spec", ",", "predefined_objects", "=", "tensorforce", ".", "core", ".", "optimizers", ".", "optimizers", ",", "kwargs", ...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
np_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
tensorforce/util.py
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 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)))
[ "Translates", "dtype", "specifications", "in", "configurations", "to", "numpy", "data", "types", ".", "Args", ":", "dtype", ":", "String", "describing", "a", "numerical", "type", "(", "e", ".", "g", ".", "float", ")", "or", "numerical", "type", "primitive", ...
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/util.py#L61-L84
[ "def", "np_dtype", "(", "dtype", ")", ":", "if", "dtype", "==", "'float'", "or", "dtype", "==", "float", "or", "dtype", "==", "np", ".", "float32", "or", "dtype", "==", "tf", ".", "float32", ":", "return", "np", ".", "float32", "elif", "dtype", "==",...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
get_tensor_dependencies
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.
tensorforce/util.py
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_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
[ "Utility", "method", "to", "get", "all", "dependencies", "(", "including", "placeholders", ")", "of", "a", "tensor", "(", "backwards", "through", "the", "graph", ")", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/util.py#L133-L146
[ "def", "get_tensor_dependencies", "(", "tensor", ")", ":", "dependencies", "=", "set", "(", ")", "dependencies", ".", "update", "(", "tensor", ".", "op", ".", "inputs", ")", "for", "sub_op", "in", "tensor", ".", "op", ".", "inputs", ":", "dependencies", ...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
get_object
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
tensorforce/util.py
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 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)
[ "Utility", "method", "to", "map", "some", "kind", "of", "object", "specification", "to", "its", "content", "e", ".", "g", ".", "optimizer", "or", "baseline", "specifications", "to", "the", "respective", "classes", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/util.py#L149-L198
[ "def", "get_object", "(", "obj", ",", "predefined_objects", "=", "None", ",", "default_object", "=", "None", ",", "kwargs", "=", "None", ")", ":", "args", "=", "(", ")", "kwargs", "=", "dict", "(", ")", "if", "kwargs", "is", "None", "else", "kwargs", ...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
prepare_kwargs
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
tensorforce/util.py
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 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
[ "Utility", "method", "to", "convert", "raw", "string", "/", "diction", "input", "into", "a", "dictionary", "to", "pass", "into", "a", "function", ".", "Always", "returns", "a", "dictionary", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/util.py#L201-L220
[ "def", "prepare_kwargs", "(", "raw", ",", "string_parameter", "=", "'name'", ")", ":", "kwargs", "=", "dict", "(", ")", "if", "isinstance", "(", "raw", ",", "dict", ")", ":", "kwargs", ".", "update", "(", "raw", ")", "elif", "isinstance", "(", "raw", ...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
SavableComponent.register_saver_ops
Registers the saver operations to the graph in context.
tensorforce/util.py
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 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 )
[ "Registers", "the", "saver", "operations", "to", "the", "graph", "in", "context", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/util.py#L235-L263
[ "def", "register_saver_ops", "(", "self", ")", ":", "variables", "=", "self", ".", "get_savable_variables", "(", ")", "if", "variables", "is", "None", "or", "len", "(", "variables", ")", "==", "0", ":", "self", ".", "_saver", "=", "None", "return", "base...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
SavableComponent.save
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.
tensorforce/util.py
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 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? )
[ "Saves", "this", "component", "s", "managed", "variables", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/util.py#L275-L296
[ "def", "save", "(", "self", ",", "sess", ",", "save_path", ",", "timestep", "=", "None", ")", ":", "if", "self", ".", "_saver", "is", "None", ":", "raise", "TensorForceError", "(", "\"register_saver_ops should be called before save\"", ")", "return", "self", "...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
SavableComponent.restore
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.
tensorforce/util.py
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 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)
[ "Restores", "the", "values", "of", "the", "managed", "variables", "from", "disk", "location", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/util.py#L298-L309
[ "def", "restore", "(", "self", ",", "sess", ",", "save_path", ")", ":", "if", "self", ".", "_saver", "is", "None", ":", "raise", "TensorForceError", "(", "\"register_saver_ops should be called before restore\"", ")", "self", ".", "_saver", ".", "restore", "(", ...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
PreprocessorStack.reset
Calls `reset` on all our Preprocessor objects. Returns: A list of tensors to be fetched.
tensorforce/core/preprocessors/preprocessor.py
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 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
[ "Calls", "reset", "on", "all", "our", "Preprocessor", "objects", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/core/preprocessors/preprocessor.py#L113-L123
[ "def", "reset", "(", "self", ")", ":", "fetches", "=", "[", "]", "for", "processor", "in", "self", ".", "preprocessors", ":", "fetches", ".", "extend", "(", "processor", ".", "reset", "(", ")", "or", "[", "]", ")", "return", "fetches" ]
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
PreprocessorStack.process
Process state. Args: tensor: tensor to process Returns: processed state
tensorforce/core/preprocessors/preprocessor.py
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 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
[ "Process", "state", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/core/preprocessors/preprocessor.py#L125-L137
[ "def", "process", "(", "self", ",", "tensor", ")", ":", "for", "processor", "in", "self", ".", "preprocessors", ":", "tensor", "=", "processor", ".", "process", "(", "tensor", "=", "tensor", ")", "return", "tensor" ]
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
PreprocessorStack.processed_shape
Shape of preprocessed state given original shape. Args: shape: original state shape Returns: processed state shape
tensorforce/core/preprocessors/preprocessor.py
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 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
[ "Shape", "of", "preprocessed", "state", "given", "original", "shape", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/core/preprocessors/preprocessor.py#L139-L150
[ "def", "processed_shape", "(", "self", ",", "shape", ")", ":", "for", "processor", "in", "self", ".", "preprocessors", ":", "shape", "=", "processor", ".", "processed_shape", "(", "shape", "=", "shape", ")", "return", "shape" ]
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
PreprocessorStack.from_spec
Creates a preprocessing stack from a specification dict.
tensorforce/core/preprocessors/preprocessor.py
def from_spec(spec, kwargs=None): """ Creates a preprocessing stack from a specification dict. """ if isinstance(spec, dict): spec = [spec] stack = PreprocessorStack() for preprocessor_spec in spec: # need to deep copy, otherwise will add first processors spec_ to kwargs to second processor preprocessor_kwargs = copy.deepcopy(kwargs) preprocessor = util.get_object( obj=preprocessor_spec, predefined_objects=tensorforce.core.preprocessors.preprocessors, kwargs=preprocessor_kwargs ) assert isinstance(preprocessor, Preprocessor) stack.preprocessors.append(preprocessor) return stack
def from_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
[ "Creates", "a", "preprocessing", "stack", "from", "a", "specification", "dict", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/core/preprocessors/preprocessor.py#L156-L175
[ "def", "from_spec", "(", "spec", ",", "kwargs", "=", "None", ")", ":", "if", "isinstance", "(", "spec", ",", "dict", ")", ":", "spec", "=", "[", "spec", "]", "stack", "=", "PreprocessorStack", "(", ")", "for", "preprocessor_spec", "in", "spec", ":", ...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
Iterative.tf_solve
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.
tensorforce/core/optimizers/solvers/iterative.py
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 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]
[ "Iteratively", "solves", "an", "equation", "/", "optimization", "for", "$x$", "involving", "an", "expression", "$f", "(", "x", ")", "$", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/core/optimizers/solvers/iterative.py#L49-L81
[ "def", "tf_solve", "(", "self", ",", "fn_x", ",", "x_init", ",", "*", "args", ")", ":", "self", ".", "fn_x", "=", "fn_x", "# Initialization step", "args", "=", "self", ".", "initialize", "(", "x_init", ",", "*", "args", ")", "# args = util.map_tensors(fn=t...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
OpenAIRetro.execute
Executes action, observes next state and reward. Args: actions: Actions to execute. Returns: Tuple of (next state, bool indicating terminal, reward)
tensorforce/contrib/openai_retro.py
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 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
[ "Executes", "action", "observes", "next", "state", "and", "reward", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/contrib/openai_retro.py#L69-L80
[ "def", "execute", "(", "self", ",", "action", ")", ":", "next_state", ",", "rew", ",", "done", ",", "_", "=", "self", ".", "env", ".", "step", "(", "action", ")", "return", "next_state", ",", "rew", ",", "done" ]
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
MemoryModel.as_local_model
Makes sure our optimizer is wrapped into the global_optimizer meta. This is only relevant for distributed RL.
tensorforce/models/memory_model.py
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 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 )
[ "Makes", "sure", "our", "optimizer", "is", "wrapped", "into", "the", "global_optimizer", "meta", ".", "This", "is", "only", "relevant", "for", "distributed", "RL", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/models/memory_model.py#L117-L125
[ "def", "as_local_model", "(", "self", ")", ":", "super", "(", "MemoryModel", ",", "self", ")", ".", "as_local_model", "(", ")", "self", ".", "optimizer_spec", "=", "dict", "(", "type", "=", "'global_optimizer'", ",", "optimizer", "=", "self", ".", "optimiz...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
MemoryModel.setup_components_and_tf_funcs
Constructs the memory and the optimizer objects. Generates and stores all template functions.
tensorforce/models/memory_model.py
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 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
[ "Constructs", "the", "memory", "and", "the", "optimizer", "objects", ".", "Generates", "and", "stores", "all", "template", "functions", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/models/memory_model.py#L127-L188
[ "def", "setup_components_and_tf_funcs", "(", "self", ",", "custom_getter", "=", "None", ")", ":", "custom_getter", "=", "super", "(", "MemoryModel", ",", "self", ")", ".", "setup_components_and_tf_funcs", "(", "custom_getter", ")", "# Memory", "self", ".", "memory...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
MemoryModel.tf_discounted_cumulative_reward
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`.
tensorforce/models/memory_model.py
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_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,))
[ "Creates", "and", "returns", "the", "TensorFlow", "operations", "for", "calculating", "the", "sequence", "of", "discounted", "cumulative", "rewards", "for", "a", "given", "sequence", "of", "single", "rewards", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/models/memory_model.py#L227-L317
[ "def", "tf_discounted_cumulative_reward", "(", "self", ",", "terminal", ",", "reward", ",", "discount", "=", "None", ",", "final_reward", "=", "0.0", ",", "horizon", "=", "0", ")", ":", "# By default -> take Model's gamma value", "if", "discount", "is", "None", ...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
MemoryModel.tf_reference
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).
tensorforce/models/memory_model.py
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_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
[ "Creates", "the", "TensorFlow", "operations", "for", "obtaining", "the", "reference", "tensor", "(", "s", ")", "in", "case", "of", "a", "comparative", "loss", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/models/memory_model.py#L319-L337
[ "def", "tf_reference", "(", "self", ",", "states", ",", "internals", ",", "actions", ",", "terminal", ",", "reward", ",", "next_states", ",", "next_internals", ",", "update", ")", ":", "return", "None" ]
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
MemoryModel.tf_loss_per_instance
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.
tensorforce/models/memory_model.py
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_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
[ "Creates", "the", "TensorFlow", "operations", "for", "calculating", "the", "loss", "per", "batch", "instance", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/models/memory_model.py#L339-L358
[ "def", "tf_loss_per_instance", "(", "self", ",", "states", ",", "internals", ",", "actions", ",", "terminal", ",", "reward", ",", "next_states", ",", "next_internals", ",", "update", ",", "reference", "=", "None", ")", ":", "raise", "NotImplementedError" ]
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
MemoryModel.tf_loss
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.
tensorforce/models/memory_model.py
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 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
[ "Creates", "the", "TensorFlow", "operations", "for", "calculating", "the", "full", "loss", "of", "a", "batch", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/models/memory_model.py#L374-L426
[ "def", "tf_loss", "(", "self", ",", "states", ",", "internals", ",", "actions", ",", "terminal", ",", "reward", ",", "next_states", ",", "next_internals", ",", "update", ",", "reference", "=", "None", ")", ":", "# Mean loss per instance", "loss_per_instance", ...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
MemoryModel.optimizer_arguments
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.
tensorforce/models/memory_model.py
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 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
[ "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", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/models/memory_model.py#L428-L463
[ "def", "optimizer_arguments", "(", "self", ",", "states", ",", "internals", ",", "actions", ",", "terminal", ",", "reward", ",", "next_states", ",", "next_internals", ")", ":", "arguments", "=", "dict", "(", "time", "=", "self", ".", "global_timestep", ",", ...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
MemoryModel.tf_optimization
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.
tensorforce/models/memory_model.py
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_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)
[ "Creates", "the", "TensorFlow", "operations", "for", "performing", "an", "optimization", "update", "step", "based", "on", "the", "given", "input", "states", "and", "actions", "batch", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/models/memory_model.py#L465-L491
[ "def", "tf_optimization", "(", "self", ",", "states", ",", "internals", ",", "actions", ",", "terminal", ",", "reward", ",", "next_states", "=", "None", ",", "next_internals", "=", "None", ")", ":", "arguments", "=", "self", ".", "optimizer_arguments", "(", ...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
MemoryModel.tf_observe_timestep
Creates and returns the op that - if frequency condition is hit - pulls a batch from the memory and does one optimization step.
tensorforce/models/memory_model.py
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_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)
[ "Creates", "and", "returns", "the", "op", "that", "-", "if", "frequency", "condition", "is", "hit", "-", "pulls", "a", "batch", "from", "the", "memory", "and", "does", "one", "optimization", "step", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/models/memory_model.py#L493-L573
[ "def", "tf_observe_timestep", "(", "self", ",", "states", ",", "internals", ",", "actions", ",", "terminal", ",", "reward", ")", ":", "# Store timestep in memory", "stored", "=", "self", ".", "memory", ".", "store", "(", "states", "=", "states", ",", "intern...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
MemoryModel.tf_import_experience
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)
tensorforce/models/memory_model.py
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 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 )
[ "Imports", "experiences", "into", "the", "TensorFlow", "memory", "structure", ".", "Can", "be", "used", "to", "import", "off", "-", "policy", "data", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/models/memory_model.py#L575-L593
[ "def", "tf_import_experience", "(", "self", ",", "states", ",", "internals", ",", "actions", ",", "terminal", ",", "reward", ")", ":", "return", "self", ".", "memory", ".", "store", "(", "states", "=", "states", ",", "internals", "=", "internals", ",", "...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
MemoryModel.import_experience
Stores experiences.
tensorforce/models/memory_model.py
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 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)
[ "Stores", "experiences", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/models/memory_model.py#L635-L649
[ "def", "import_experience", "(", "self", ",", "states", ",", "internals", ",", "actions", ",", "terminal", ",", "reward", ")", ":", "fetches", "=", "self", ".", "import_experience_output", "feed_dict", "=", "self", ".", "get_feed_dict", "(", "states", "=", "...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
OptimizedStep.tf_step
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.
tensorforce/core/optimizers/optimized_step.py
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 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 )
[ "Creates", "the", "TensorFlow", "operations", "for", "performing", "an", "optimization", "step", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/core/optimizers/optimized_step.py#L65-L134
[ "def", "tf_step", "(", "self", ",", "time", ",", "variables", ",", "arguments", ",", "fn_loss", ",", "fn_reference", ",", "*", "*", "kwargs", ")", ":", "# Set reference to compare with at each optimization step, in case of a comparative loss.", "arguments", "[", "'refer...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
Distribution.from_spec
Creates a distribution from a specification dict.
tensorforce/core/distributions/distribution.py
def from_spec(spec, kwargs=None): """ Creates a distribution from a specification dict. """ distribution = util.get_object( obj=spec, predefined_objects=tensorforce.core.distributions.distributions, kwargs=kwargs ) assert isinstance(distribution, Distribution) return distribution
def from_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
[ "Creates", "a", "distribution", "from", "a", "specification", "dict", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/core/distributions/distribution.py#L184-L194
[ "def", "from_spec", "(", "spec", ",", "kwargs", "=", "None", ")", ":", "distribution", "=", "util", ".", "get_object", "(", "obj", "=", "spec", ",", "predefined_objects", "=", "tensorforce", ".", "core", ".", "distributions", ".", "distributions", ",", "kw...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
Agent.reset
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.
tensorforce/agents/agent.py
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 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
[ "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", "pr...
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/agents/agent.py#L96-L102
[ "def", "reset", "(", "self", ")", ":", "self", ".", "episode", ",", "self", ".", "timestep", ",", "self", ".", "next_internals", "=", "self", ".", "model", ".", "reset", "(", ")", "self", ".", "current_internals", "=", "self", ".", "next_internals" ]
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
Agent.act
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
tensorforce/agents/agent.py
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 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
[ "Return", "action", "(", "s", ")", "for", "given", "state", "(", "s", ")", ".", "States", "preprocessing", "and", "exploration", "are", "applied", "if", "configured", "accordingly", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/agents/agent.py#L104-L164
[ "def", "act", "(", "self", ",", "states", ",", "deterministic", "=", "False", ",", "independent", "=", "False", ",", "fetch_tensors", "=", "None", ",", "buffered", "=", "True", ",", "index", "=", "0", ")", ":", "self", ".", "current_internals", "=", "s...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
Agent.observe
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.
tensorforce/agents/agent.py
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 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 )
[ "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", "=",...
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/agents/agent.py#L166-L197
[ "def", "observe", "(", "self", ",", "terminal", ",", "reward", ",", "index", "=", "0", ")", ":", "self", ".", "current_terminal", "=", "terminal", "self", ".", "current_reward", "=", "reward", "if", "self", ".", "batched_observe", ":", "# Batched observe for...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
Agent.atomic_observe
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.
tensorforce/agents/agent.py
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 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 )
[ "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", "." ]
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/agents/agent.py#L199-L230
[ "def", "atomic_observe", "(", "self", ",", "states", ",", "actions", ",", "internals", ",", "reward", ",", "terminal", ")", ":", "# TODO probably unnecessary here.", "self", ".", "current_terminal", "=", "terminal", "self", ".", "current_reward", "=", "reward", ...
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
Agent.save_model
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.
tensorforce/agents/agent.py
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 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)
[ "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", "checkp...
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/agents/agent.py#L244-L264
[ "def", "save_model", "(", "self", ",", "directory", "=", "None", ",", "append_timestep", "=", "True", ")", ":", "return", "self", ".", "model", ".", "save", "(", "directory", "=", "directory", ",", "append_timestep", "=", "append_timestep", ")" ]
520a8d992230e382f08e315ede5fc477f5e26bfb
valid
Agent.restore_model
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.
tensorforce/agents/agent.py
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)
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)
[ "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", "u...
tensorforce/tensorforce
python
https://github.com/tensorforce/tensorforce/blob/520a8d992230e382f08e315ede5fc477f5e26bfb/tensorforce/agents/agent.py#L266-L276
[ "def", "restore_model", "(", "self", ",", "directory", "=", "None", ",", "file", "=", "None", ")", ":", "self", ".", "model", ".", "restore", "(", "directory", "=", "directory", ",", "file", "=", "file", ")" ]
520a8d992230e382f08e315ede5fc477f5e26bfb