def __init__(self, name: str, access_modifier: str, parent: Optional[SymbolTable] = None): """The MethodScope object is responsible for keeping track of the information of a method. Arguments: - name [str]: The name of the method. - access_modifier [str]: Whether the method is public of private. - parent [SymbolTable]: The method's parent class attribute_directory, only Optional for the Global Scope. - parent_memory [Memory]: The memory of the parent class, only Optional for the Global Scope. """ self._name = name self._access_modifier = access_modifier self._parent = parent self._local_memory = Memory(Scopes.LOCAL, ScopeRanges.LOCAL) self._instruction_pointer = None if parent is not None: logger.debug( f"Created MethodScope {access_modifier} {name}, parent {parent.name}") # These are added later, as there could be multiple arguments, # so we parse them one by one, and finally add the return type # which goes after the arguments self._arguments = SymbolTable(f'{name} Arguments', parent) self._ordered_arguments = [] self._return_type = None self._return_memory_address = -1 self._variables_directory = SymbolTable(name, self._arguments)
def __init__(self, source="google-news", apiKey="7488ba8ff8dc43459d36f06e7141c9e5"): self.apiKey = apiKey self.source = source self.url = "https://newsapi.org/v1/articles?source=google-news&sortBy=top&apiKey=7488ba8ff8dc43459d36f06e7141c9e5" self.m = Memory()
def __init__(self): self.table = SymbolTable() self.oracle = SemanticCube() self.quads = QuadGenerator() self.memory = Memory() self.flow = FlowManager(self.quads, self.memory) # Creating the quad for the initial functions jump self.create_initial_jump()
def __init__(self, stop_criteria, memory_size, progress_writer=None): self.stop_criteria = stop_criteria self.color_permutator = FastColorPermutator() self.memory = Memory(memory_size) self.progress_writer = progress_writer self.root_node = None self.best_score = None self.best_score_graph = None self.color_set = []
def _save_experience(experience: Experience, memory: Memory, next_state: np.ndarray): """ Save a new experience by replaced next state of agent :param next_state: nest state of agent :param experience: Experience of made action :param memory: memory of robot with all saved experiences :return: next state which agent will make """ experience = experience._replace(next_state=next_state) memory.add(experience)
def __init__(self): self.functions_table = FunctionsTable() self.current_function = Function("void", "global", [], {}, function_memory=Memory( MemoryConstants.GLOBAL_INITIAL)) self.semantic_cube = SemanticCube() self.quadruples = QuadruplesTable() self.operators_stack = [] self.operands_stack = [] self.jumps_stack = [] self.types_stack = [] self.temporal_memory = Memory(MemoryConstants.TEMPORAL_INITIAL) self.constant_memory = Memory(MemoryConstants.CONSTANT_INITIAL) self.constant_exec_memory = ExecMemory( MemoryConstants.CONSTANT_INITIAL)
def __init__(self, quad_file): self.state = GlobalState(self) self.memory = Memory() self.jumps = [] self.returns = [] self.grabs = [] self.context = None self.quads = self.store_constants(quad_file.read().splitlines())
def __init__(self): super().__init__() self.memory = Memory() self.setupUi(self) self.scan_widget.setEnabled(False) self.found_table.setModel(FoundAddressModel(self)) self.found_table.horizontalHeader().setSectionResizeMode( 0, QHeaderView.Stretch) self.new_scan.clicked.connect(self.new_scan_clicked) self.next_scan.clicked.connect(self.next_scan_clicked) self.actionAttach.triggered.connect(self.attach_triggered) self.found_table.doubleClicked.connect(self.found_table_double_clicked) self.saved_results.doubleClicked.connect( self.saved_model_double_clicked)
def __init__(self, sess, args): with tf.variable_scope(args.model_name): self.args = args self.learning_rate = args.learning_rate self.session = sess self.x = tf.placeholder(tf.float32, shape=[None, 784], name="x") self.y = tf.placeholder(tf.float32, shape=[None, 10], name="y") # self.trainable = tf.placeholder(tf.int32, shape=(), name="trainable") self.memory_sample_batch = tf.placeholder( tf.int16, shape=(), name="memory_sample_batch") self.embed = self.embedding(self.x) self.M = Memory(args.memory_size, self.embed.get_shape()[-1], self.y.get_shape()[-1]) embs_and_values = tf.py_func(self.get_memory_sample, [self.memory_sample_batch], [tf.float64, tf.float64]) self.memory_batch_x = tf.to_float(embs_and_values[0]) self.memory_batch_y = tf.to_float(embs_and_values[1]) self.xa = tf.concat(values=[self.embed, self.memory_batch_x], axis=0) self.ya = tf.concat(values=[self.y, self.memory_batch_y], axis=0) self.y_ = self.output_network(self.xa) self.cross_entropy = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(labels=self.ya, logits=self.y_)) self.optim = tf.train.GradientDescentOptimizer( self.learning_rate).minimize(self.cross_entropy) self.correct_prediction = tf.equal(tf.argmax(self.ya, 1), tf.argmax(self.y_, 1)) self.accuracy = tf.reduce_mean( tf.cast(self.correct_prediction, tf.float32)) self.session.run(tf.global_variables_initializer())
def main(): path = os.getcwd()[:-10] env = CarRacingWrapper() agent = Agent(env, state_dim=288) memory = Memory(num_timesteps=1, batch_size=1, load_model=True, results_dir=path + 'memory/160model_v2') vision = VAE(load_model=True, results_dir=path + 'vision') episodes = 1 agent.train(vision, memory, episodes=episodes) agent.train(vision, memory, render=True)
def __init__(self, name: str, inherits: Optional["ClassScope"] = None, global_scope: Optional["SymbolTable"] = None): """ClassScope is responsible for keeping track of a classes methods, attributes, and local memory. Arguments: - name [str]: The name of the class. - inherits [ClassScope]: The class scope of the parent class if there is one. """ self._name = name if inherits is None: self._method_directory = SymbolTable(name) self._attribute_directory = SymbolTable(name, global_scope) self._instance_memory = Memory(Scopes.INSTANCE, ScopeRanges.INSTANCE) else: self._method_directory = SymbolTable(name, inherits.method_directory) self._attribute_directory = SymbolTable( name, inherits.attribute_directory) self._instance_memory = inherits.instance_memory
def main(robot_name: str, env_monitor: bool = True): """ Main function to learn robot walking using DQL algorithm """ print(robot_name) env = gym.make(robot_name) env.render(mode=config.MODEL) dqn = DeepQNetwork(env=env) memory = Memory(param.MEMORY_SIZE) if env_monitor: env = gym.wrappers.Monitor( env=env, directory=config.MONITOR_PATH, force=True, video_callable=lambda episode_id: episode_id % 1 == 0) print(f'Start training agent: {robot_name}') training(env, dqn, memory)
def update_model(dqn: DeepQNetwork, env: gym.Env, memory: Memory): """ Update Q-table and fit model :param dqn: Deep Q-network model :param env: robot Environment :param memory: Memory of agent """ inputs = np.zeros((param.BATCH_SIZE, ) + env.observation_space.shape) targets = np.zeros((param.BATCH_SIZE, env.action_space.shape[0])) states_, actions_, rewards_, done_, next_states_ = memory.sample_batch( param.BATCH_SIZE) for i, state_ in enumerate(states_): inputs[i:i + 1] = state_ targets[i] = dqn.model.predict(state_) q_state = dqn.model.predict(next_states_[i])[0] if (next_states_[i] == np.zeros(state_.shape)).all(): targets[i] = rewards_[i] else: targets[i] = rewards_[i] + param.GAMMA * np.amax(q_state) dqn.model.fit(inputs, targets, epochs=1, verbose=0)
class MainWindow(QMainWindow, Ui_MainWindow): def __init__(self): super().__init__() self.memory = Memory() self.setupUi(self) self.scan_widget.setEnabled(False) self.found_table.setModel(FoundAddressModel(self)) self.found_table.horizontalHeader().setSectionResizeMode( 0, QHeaderView.Stretch) self.new_scan.clicked.connect(self.new_scan_clicked) self.next_scan.clicked.connect(self.next_scan_clicked) self.actionAttach.triggered.connect(self.attach_triggered) self.found_table.doubleClicked.connect(self.found_table_double_clicked) self.saved_results.doubleClicked.connect( self.saved_model_double_clicked) @pyqtSlot() def new_scan_clicked(self): if self.next_scan.isEnabled(): self.next_scan.setEnabled(False) self.found_table.model().set_values([]) self.memory.reset_scan() else: # the isnumeric is a simple workaround, since I'll support strings eventually # until then it can stay if len(self.search_for.text()) > 0 and self.search_for.text( ).lstrip("-").isnumeric(): self.next_scan_clicked() self.next_scan.setEnabled(True) @pyqtSlot() def next_scan_clicked(self): # TODO: fix looking for negative values, probably have to fiddle with the type system try: scan_type = { 0: operator.eq, 1: operator.gt, 2: operator.lt, 3: operator.ne } match_index = self.scan_matching.currentIndex() values = self.memory.scan(self.search_for.text(), Type(self.scan_byte_size.currentIndex()), scan_type[match_index]) self.amount_found.setText("Found: {}".format(len(values))) self.found_table.model().set_values(values) except NoSuchProcess: QMessageBox(QMessageBox.NoIcon, "Error", "No readable memory", QMessageBox.Ok, self).exec_() @pyqtSlot() def attach_triggered(self): process_view = ProcessView() pid = process_view.exec_() if pid != 0: self.memory.attach(pid) self.scan_widget.setEnabled(True) @pyqtSlot(QModelIndex) def found_table_double_clicked(self, index: QModelIndex): if not index.isValid(): return clicked_value = self.found_table.model().get_value(index.row()) self.saved_results.model().append_row(clicked_value) @pyqtSlot(QModelIndex) def saved_model_double_clicked(self, index: QModelIndex): if not index.isValid(): return column = index.column() if column == SavedAddressHeaderEnum.VALUE: form = WriteForm(self) x = form.exec_() text = form.value.text() if x and len(text) != 0: item: TreeItem = self.saved_results.model().get_item(index) value = item.get_internal_pointer() try: value.write( value.type.parse_value(text, form.ishex.isChecked())) except struct.error: QMessageBox( QMessageBox.NoIcon, "Error", "You can't write a larger number to " "memory than you currently have set " "as a type.\nSee limits.h for more " "information", QMessageBox.Ok, self).exec_() except ValueError: # this is a kinda temporary fix, I'm planning on adding a validator or some # shit later, not entirely sure how to add them yet so this will do QMessageBox( QMessageBox.NoIcon, "Error", "Error parsing value, you probably " "entered text when trying to write " "an integer...", QMessageBox.Ok, self).exec_()
def setUp(self) -> None: self.memory = Memory() # just attach to own process, we're overriding the read values anyway self.memory.process = psutil.Process()
class MemoryTest(unittest.TestCase): def setUp(self) -> None: self.memory = Memory() # just attach to own process, we're overriding the read values anyway self.memory.process = psutil.Process() @patch.object(psutil.Process, "memory_maps", autospec=True) @patch.object(Memory, "read", autospec=True) def test_scan(self, read_mock: Mock, mem_map_mock: Mock): read_mock.side_effect = [123, 123] mem_map_mock.return_value = [InternalMmap("0-8", 8, "r--w")] self.assertEqual(self.memory.scan("123"), [Value(0, 0, 123), Value(0, 4, 123)]) @patch.object(psutil.Process, "memory_maps", autospec=True) @patch.object(Memory, "read", autospec=True) def test_scan_empty_search(self, read_mock: Mock, mem_map_mock: Mock): read_mock.side_effect = [123, 123] mem_map_mock.return_value = [InternalMmap("0-8", 8, "r--w")] def test_scan_cull_empty(self): self.assertEqual(self.memory._scan_cull(Value(0, 0, 123)), []) def test_scan_cull(self): maps = [InternalMmap("0-32", 32, "r--w")] # 15 because 32/4 = 8, since we need twice the scan area, we have a total of 16, then we # append a false value too to make sure the filtering works packed = [Value(0, 0, 123)] * 15 packed.append(Value(0, 0, 12)) with patch.object(Memory, "read", side_effect=packed), \ patch("builtins.open", mock_open()), \ patch("psutil.Process.__init__", return_value=None), \ patch("psutil.Process.memory_maps", return_value=maps): self.memory.attach(123) self.assertEqual(len(self.memory.scan(123)), 8) self.assertEqual(len(self.memory.scan(123)), 7) def test_attach(self): with patch("builtins.open", mock_open()), patch("psutil.Process.__init__", return_value=None): self.memory.attach(123) self.assertEqual(self.memory.pid, 123) def test_detach(self): with patch("builtins.open", mock_open()), patch("psutil.Process.__init__", return_value=None): self.memory.attach(123) self.memory.detach() self.assertEqual(self.memory.pid, 0)
class StatementManager: in_local_scope = False def __init__(self): self.table = SymbolTable() self.oracle = SemanticCube() self.quads = QuadGenerator() self.memory = Memory() self.flow = FlowManager(self.quads, self.memory) # Creating the quad for the initial functions jump self.create_initial_jump() def get_symbol_from_name(self, id): ''' Function that checks if an id exists in current scope or above ''' return self.table.lookup(id) def create_initial_jump(self): ''' Function that creates the initial jump where the main functions are being executed ''' # We create a new GOTO quad self.quads.store_jump() # Current index quad is at 2, we store the previous quad by adding -1 self.flow.generate_goto() def start_class_scope(self, class_name, parent_name, params): ''' Function that starts a new class scope in symbol table and enters it ''' # We first have be sure that the class isn't already defined class_exists = self.get_symbol_from_name(class_name) # Cannot redeclare class if class_exists: raise Exception('Class name already defined') # Fetching the current number index of the quad quad_number = self.quads.current_index # Checks if it has a parent, if it does then it copies every attribute and function from it if parent_name: # Get parent address from symbol parent_symbol = self.table.get_class(parent_name) p_addr = parent_symbol.address # Storing the class in the symbol table and creating scope self.table.store_with_parent_symbols( parent_name, class_name, ClassSymbol(quad_number, class_name, parent_name, params), 'class') # Generating the quad self.quads.generate(OpIds.inherit, 0, 0, p_addr) else: # Store the class symbol in the symbol table and creating scope self.table.store( class_name, ClassSymbol(quad_number, class_name, parent_name, params), 'class') # Add params to new scope self.store_params(params, 'instance', OpIds.attr) # Creating a quad that indicates that its the end of the class' attributes self.quads.generate(OpIds.endattr, 0, 0, 0) def store_params(self, params, address_type, operation): ''' Function that stores the params with a VariableSymbol that contains its address type -instace, local- ''' params.reverse() for param in params: # Assigns a new address for the current param new_address = self.memory.get_address(address_type, param[1]) # Stores the variable symbol in the symbol table self.table.store(param[0], VariableSymbol(new_address, param[1])) # Generates a new quad with the given operation and the new address self.quads.generate(operation, 0, 0, new_address) def check_class_exists(self, class_name): ''' Function that checks if a class, that is being inherited from, exists, if it doesn't it throws and exception ''' class_exists = self.table.get_class(class_name) if not class_exists: raise Exception('Class ' + class_name + ' doesn\'t exist') def start_function_scope(self, function_name, return_type, parameters): # Stores the function element self.memory.locals.expand() self.table.store( function_name, FunctionSymbol(self.quads.current_index, return_type, parameters), 'function') self.in_local_scope = True self.store_params(parameters, 'local', OpIds.grab) def close_function_scope(self): ''' Function that closes a function scope ''' self.memory.locals.end_function() self.table.close_scope() self.in_local_scope = False self.quads.generate(OpIds.func_return, 0, 0, 0) def get_attrs_dict_for_class(self, class_name, args): # Gets symbol of the class class_symbol = self.table.get_class(class_name) # Make sure args are same type as params argument_types = list(map(lambda x: x[1], args)) accepts = class_symbol.accepts_arguments(argument_types) if not accepts: raise Exception('Arguments do not match constructor for class \'' + class_name + '\'.') attrs = {} # Matches class param with passed args temp_args = list(args) temp_args.reverse() class_attributes = zip(class_symbol.params, temp_args) for (param, arg) in class_attributes: attrs[param[0]] = arg return attrs def instantiate(self, class_name, args): class_symbol = self.table.get_class(class_name) attrs = self.get_attrs_dict_for_class(class_name, args) obj = self.new_var_of_type(class_name, 'temp', attrs) for arg in args: self.quads.generate(OpIds.param, 0, 0, arg[0]) self.quads.generate(OpIds.instance, class_symbol.address, 0, obj[0]) self.foo = obj return obj def end_class_scope(self): ''' Function that closes a class scope, by default it returns a quad of type RETURN ''' self.table.close_scope() def assign(self, variable, value): # Make sure value can be assigned to variable self.oracle.can_assign(variable[1], value[1]) if len(value) == 3: variable = self.assign_variable(variable, value) else: variable = self.assign_temp(variable, value) # If trying to copy existing variable # if len(value) == 3 and isinstance(value[2], VariableSymbol): # new_symbol = copy.deepcopy(value[2]) # new_attrs = self.copy_attributes(new_symbol) # new_symbol.address = variable[0] # print(new_symbol) # for name,attr in new_symbol.attrs.items(): # new_address = self.memory.get_address('local' if self.in_local_scope else 'global', attr.type) # self.table.replace_symbol(variable[2], new_symbol) # self.quads.generate(OpIds.assign, value[0], 0, variable[0]) return variable def assign_variable(self, variable, value): if isinstance(value[2], StackSymbol): return self.assign_temp(variable, value) return self.replicate(variable, value) def assign_temp(self, variable, value): self.quads.generate(OpIds.assign, value[0], 0, variable[0]) self.free_temp_memory(value[0]) return variable def copy_attributes(self, symbol): # If it's an object instance for key, attr in symbol.attrs.items(): if isinstance(attr, VariableSymbol): prev = attr.address new = attr.address = self.memory.get_address( 'local' if self.in_local_scope else 'global', attr.type) self.quads.generate(OpIds.assign, prev, 0, new) if attr.type not in ['int', 'float', 'bool', 'str']: self.copy_attributes_aux(attr) def copy_attributes_aux(self, symbol): symbol.address = self.memory.get_address( 'local' if self.in_local_scope else 'global', symbol.type) for key, attr in symbol.attrs.items(): if isinstance(attr, VariableSymbol): prev = attr.address new = attr.address = self.memory.get_address( 'local' if self.in_local_scope else 'global', attr.type) self.quads.generate(OpIds.assign, prev, 0, new) if attr.type not in ['int', 'float', 'bool', 'str']: self.copy_attributes_aux(attr) def this_property(self, prop_id): ''' Function that handles the this.property functionallity ''' # returns tuple with the address of the attribute and str of type # First we check if user is in a class scope in_class_scope = self.table.check_class_scope() # We are in a class scope if in_class_scope: class_has_property = self.table.check_class_property(prop_id) if class_has_property: if isinstance(class_has_property, StackSymbol): raise Exception('Cannot use stack ' + prop_id + ' outside a stack call.') # Returns the symbol return class_has_property.to_tuple() else: raise Exception('Property ' + prop_id + ' does not exist in class ' + in_class_scope + '.') # If we are not inside a class scope, then the keyword this is not available else: raise Exception( 'Keyword this is not available outside a class scope') def var_property(self, var_id, property_id): ''' Function that handles the id.id functionallity ''' variable_exists = self.id_property(var_id)[2] if isinstance(variable_exists, FunctionSymbol): raise Exception('Cannot access property of function ' + var_id + '.') if variable_exists: self.quads.generate(OpIds.context, 0, 0, variable_exists.address) symbol = variable_exists.get_attribute(property_id) if not symbol: raise Exception('Variable ' + var_id + ' does not have attribute ' + property_id + '.') return symbol def id_property(self, property_id): class_property = self.table.check_property(property_id) if isinstance(class_property, StackSymbol): raise Exception('Cannot use stack ' + property_id + ' outside a stack call.') if class_property: return class_property.to_tuple() else: raise Exception('Variable ' + property_id + ' is not defined.') def print_output(self, expression): self.quads.generate(OpIds.io_print, 0, 0, expression[0]) self.free_temp_memory(expression[0]) return expression def return_value(self, return_value): # Validates that the return object is the same type as the function return type has_correct_return = self.table.check_return(return_value[1]) if has_correct_return: self.quads.generate(OpIds.func_return, 0, 0, return_value[0]) else: raise Exception('Cannot return ' + return_value[1] + ' in function of type ' + self.table.scope().symbol.type) def return_void(self): has_correct_return = self.table.check_return('void') if has_correct_return: self.quads.generate(OpIds.func_return, 0, 0, 0) else: raise Exception('Cannot return void in function of type ' + self.table.scope().symbol.type) def float_constant(self, value): address = self.memory.get_constant_address('float', value) return (address, 'float') def int_constant(self, value): address = self.memory.get_constant_address('int', value) return (address, 'int') def string_constant(self, value): address = self.memory.get_constant_address('str', value) return (address, 'str') def free_temp_memory(self, memory_address): self.memory.free_if_temp(memory_address) def read(self): #Return temporal temp_addr = self.memory.get_address('temp', 'str') self.quads.generate(OpIds.io_read, 0, 0, temp_addr) return (temp_addr, 'str') def operate(self, operator, left_op, right_op): # left_op & right_op = tuple(address, type) res_type = self.oracle.is_valid(operator, left_op, right_op) new_address = self.memory.get_address('temp', res_type) self.quads.generate(OpIds.get(operator), left_op[0], right_op[0], new_address) # liberar memoria temporal de left_op y right_op self.memory.free_if_temp(left_op[0]) self.memory.free_if_temp(right_op[0]) return (new_address, res_type) def check_call_validity(self, prop, arguments, context=0): if isinstance(prop, tuple): prop = prop[2] symbol = prop argument_types = list(map(lambda x: x[1], arguments)) if not symbol.is_callable: raise Exception('Cannot call a non callable object') if not symbol.accepts_arguments(argument_types): raise Exception('Arguments do not match function parameters.') new_address = self.memory.get_address('temp', symbol.type) # arguments.reverse() for param in arguments: self.quads.generate(OpIds.param, 0, 0, param[0]) self.quads.generate(OpIds.call, symbol.address, 0, new_address) return (new_address, symbol.type) def call_stack(self, stack_symbol, call): if call[0] == 'push': return self.push_stack(stack_symbol, call[1]) elif call == 'peek': return self.peek_stack(stack_symbol) elif call == 'size': return self.size_stack(stack_symbol) else: return self.pop_stack(stack_symbol) def push_stack(self, symbol, tuple_expr): if symbol.type != tuple_expr[1]: raise Exception('Cannot push element of type ' + tuple_expr[1] + ' into a stack of type ' + symbol.type) self.quads.generate(OpIds.push, tuple_expr[0], 0, symbol.address) return tuple_expr def size_stack(self, symbol): new_address = self.memory.get_address('temp', 'int') self.quads.generate(OpIds.size, symbol.address, 0, new_address) return (new_address, 'int') def pop_stack(self, symbol): new_address = self.memory.get_address('temp', symbol.type) self.quads.generate(OpIds.pop, symbol.address, 0, new_address) return (new_address, symbol.type) def peek_stack(self, symbol): new_address = self.memory.get_address('temp', symbol.type) self.quads.generate(OpIds.peek, symbol.address, 0, new_address) return (new_address, symbol.type) def is_stack_type(self, variable): symbol = self.table.lookup(variable) if not isinstance(symbol, StackSymbol): raise Exception( 'Cannot perform stack methods on non-stack property ' + variable + '.') return symbol def clear_instance_memory(self): self.memory.clear_instance_memory() def id_does_not_exist(self, identifier): id_exist = self.table.lookup(identifier) if id_exist: raise Exception('Identifier ' + identifier + ' already exists') def fill_goto(self): self.quads.fill_jump() # TODO : move to quad_generator def create_quads_txt(self, filename): file = open(filename, "w+") for addr, value in self.memory.consts.ints.items(): if addr > 0: file.write(str(addr) + ',' + str(value) + '\n') for addr, value in self.memory.consts.floats.items(): if addr > 0: file.write(str(addr) + ',' + str(value) + '\n') for addr, value in self.memory.consts.bools.items(): if addr > 0: file.write(str(addr) + ',' + str(value) + '\n') for addr, value in self.memory.consts.strs.items(): if addr > 0: file.write(str(addr) + ',' + str(value) + '\n') file.write(str(OpIds.endconst) + '\n') for index, quad in self.quads.quads.items(): file.write(str(quad) + '\n') file.close() # # Var instantiation # def var_type_is_obj(self, type_name): return self.mem_type_from_var_type(type_name) == 'obj' def mem_type_from_var_type(self, type_name): return type_name if type_name in ['int', 'float', 'bool', 'str' ] else 'obj' def new_var_of_type(self, type_name, scope, args={}): is_stack = isinstance(type_name, tuple) and type_name[0] == 'stack' if is_stack: type_name = type_name[1] mem_type = self.mem_type_from_var_type(type_name) new_address = self.memory.get_address(scope, mem_type) self.quads.generate(OpIds.declare, 0, 0, new_address) if is_stack: return StackSymbol(new_address, type_name).to_tuple() new_attributes = {} if self.var_type_is_obj(type_name): new_attributes = self.get_attributes_for_class( type_name, scope, args) new_symbol = VariableSymbol(new_address, type_name, new_attributes) return new_symbol.to_tuple() def get_attributes_for_class(self, type_name, scope, args={}): class_scope = self.table.get_class_scope(type_name) attrs = {} for name, symbol in class_scope.symbols.items(): if isinstance(symbol, VariableSymbol): arg = args.get(name, None) symbol = self.new_var_of_type(symbol.type, scope) self.quads.generate(OpIds.relate, symbol[0], 0, class_scope.symbols[name].address) attrs[name] = symbol return attrs def replicate(self, target, origin): if isinstance(target, FunctionSymbol): return target self.quads.generate(OpIds.assign, origin[0], 0, target[0]) self.free_temp_memory(origin[0]) if len(origin) == 3: for name, target_attr in target[2].attrs.items(): self.replicate(target_attr, origin[2].attrs[name]) return target def declare(self, var_tuple): scope = 'local' if self.in_local_scope else 'global' var_name = var_tuple[0] # Checks it doesnt exist self.table.local_neg_lookup(var_name) var_type = var_tuple[1] var = self.new_var_of_type(var_type, scope) self.table.store(var_name, var[2]) return var
class MethodScope: def __init__(self, name: str, access_modifier: str, parent: Optional[SymbolTable] = None): """The MethodScope object is responsible for keeping track of the information of a method. Arguments: - name [str]: The name of the method. - access_modifier [str]: Whether the method is public of private. - parent [SymbolTable]: The method's parent class attribute_directory, only Optional for the Global Scope. - parent_memory [Memory]: The memory of the parent class, only Optional for the Global Scope. """ self._name = name self._access_modifier = access_modifier self._parent = parent self._local_memory = Memory(Scopes.LOCAL, ScopeRanges.LOCAL) self._instruction_pointer = None if parent is not None: logger.debug( f"Created MethodScope {access_modifier} {name}, parent {parent.name}") # These are added later, as there could be multiple arguments, # so we parse them one by one, and finally add the return type # which goes after the arguments self._arguments = SymbolTable(f'{name} Arguments', parent) self._ordered_arguments = [] self._return_type = None self._return_memory_address = -1 self._variables_directory = SymbolTable(name, self._arguments) @property def access_modifier(self): return self._access_modifier @property def name(self) -> str: """The name of the class. Returns: - The name [str] of the method. """ return self._name @property def local_memory(self) -> Memory: return self._local_memory @property def variables_directory(self) -> SymbolTable: """The SymbolTable which keeps track of the variables in the method. Returns: - The SymbolTable instance. """ return self._variables_directory @property def return_type(self) -> str: """The return type of the method. Returns: - The return type [str] of the method. """ return self._return_type @property def return_memory_address(self) -> int: """Where the return value of the method will point to in memory. Returns: - The address [int] where the value of the return will be stored. """ return self._return_memory_address @property def ordered_arguments(self): return self._ordered_arguments def add_return_type(self, return_type: str) -> None: """Adds the return type to the method after parsing it. Arguments: - return_type [str]: The return type of the method. """ self._return_type = return_type if return_type != "void": address = CompilationMemory.next_global_memory_space(return_type) self._return_memory_address = Variable(address, return_type, address) def add_argument(self, name: str, arg_type: str) -> None: """Adds an argument to the method. Arguments: - name [str]: The name of the argument. - arg_type [str]: The type of the argument. """ memory_space = self._local_memory.next_memory_space(arg_type) variable = Variable(name, arg_type, memory_space) self._arguments.add_symbol(variable) self._ordered_arguments.append(variable) def add_variable(self, name: str, var_type: str) -> None: """Adds a variable to the method after parsing it. Arguments: - name [str]: The name of the variable. - var_type [str]: The type of the variable. """ # It belongs to global memory if it has no parent. if self._parent is not None: memory_space = self._local_memory.next_memory_space(var_type) else: memory_space = CompilationMemory.next_global_memory_space(var_type) self._variables_directory.add_symbol( Variable(name, var_type, memory_space)) def allocate_memory_chunk(self, variable) -> None: if variable.is_global(): CompilationMemory.next_global_memory_chunk(variable.var_type, variable.size - 1) elif variable.is_local(): self._local_memory.next_memory_chunk(variable.var_type, variable.size - 1) @property def instruction_pointer(self): return self._instruction_pointer @instruction_pointer.setter def instruction_pointer(self, ip): self._instruction_pointer = ip
class Compiler: def __init__(self): self.functions_table = FunctionsTable() self.current_function = Function("void", "global", [], {}, function_memory=Memory( MemoryConstants.GLOBAL_INITIAL)) self.semantic_cube = SemanticCube() self.quadruples = QuadruplesTable() self.operators_stack = [] self.operands_stack = [] self.jumps_stack = [] self.types_stack = [] self.temporal_memory = Memory(MemoryConstants.TEMPORAL_INITIAL) self.constant_memory = Memory(MemoryConstants.CONSTANT_INITIAL) self.constant_exec_memory = ExecMemory( MemoryConstants.CONSTANT_INITIAL) def add_function(self, function: Function): self.current_function.start_quadruple = self.quadruples.length() self.functions_table.add_function(function) if function.function_type != "void": self.functions_table.functions["global"].update_variables( function.function_type, function.function_name) def add_variable(self, variable_name): if variable_name in self.current_function.function_variables: variable = self.current_function.function_variables[variable_name] self.operands_stack.append(variable.variable_address) self.add_type(variable.variable_type) elif variable_name in self.functions_table.functions[ "global"].function_variables: variable = self.functions_table.functions[ "global"].function_variables[variable_name] self.operands_stack.append(variable.variable_address) self.add_type(variable.variable_type) elif variable_name in self.functions_table.functions: if self.functions_table.functions[ variable_name].function_type == 'void': print('ERROR: No se puede asignar una función void ' + variable_name + ' a una variable') else: self.operands_stack.append(variable_name) self.add_type(self.functions_table.functions[variable_name]. function_type) else: print('ERROR: La variable ' + str(variable_name) + ' no está declarada') def add_operator(self, operator): self.operators_stack.append(operator) # print('Operator: ', operator, self.operators_stack) def add_type(self, type): self.types_stack.append(type) # print('Type: ', type, self.types_stack) def add_constant_operand(self, operand, type): address = self.constant_memory.get_address(type) constant = Variable(type, operand, operand, address) self.constant_exec_memory.save_value(address, operand) self.operands_stack.append(constant.variable_address) self.types_stack.append(type) # print('Operand: ', operand, self.operators_stack) def left_parenthesis(self): self.operators_stack.append('(') # print('Parenthesis: (', self.operators_stack) def right_parenthesis(self): self.operators_stack.pop() # print('Parenthesis: )') def operation_quadruple(self): # print("Operation Quad Gen: ", self.operands_stack, self.operators_stack, self.types_stack) right_operand = self.operands_stack.pop() left_operand = self.operands_stack.pop() operator = self.operators_stack.pop() right_operand_type = self.types_stack.pop() left_operand_type = self.types_stack.pop() result_type = SemanticCube().check_operation(left_operand_type, right_operand_type, operator) if result_type == Types.ERROR: print( 'ERROR: Los tipos de datos de la operación no son compatibles.' ) else: # Create quadruple and add its type and and result to type and operand stacks result_address = self.temporal_memory.get_address(result_type) self.quadruples.append(operator, left_operand, right_operand, result_address) self.types_stack.append(result_type) self.operands_stack.append(result_address) def read_quadruple(self, operand): result_type = self.types_stack.pop() self.operands_stack.pop() if (operand in self.current_function.function_variables) or ( operand in self.functions_table.functions["global"].function_variables): result_address = self.temporal_memory.get_address(result_type) self.quadruples.append("read", operand, None, result_address) else: print('ERROR: La variable ' + str(operand) + ' no está declarada') def write_quadruple(self): # print("Write Quad Gen: ", self.operands_stack[len(self.operands_stack)-1]) if len(self.operands_stack) == 0: print( 'ERROR: Se quiere hacer un print pero no hay operandos en el stack' ) else: printed_operand = self.operands_stack.pop() self.types_stack.pop() self.quadruples.append("print", None, None, printed_operand) def assign_quadruple(self): # print("Assign Quad Gen: ", self.operators_stack[len(self.operators_stack)-1], self.operands_stack) if self.operators_stack[len(self.operators_stack) - 1] == '=': right_operand = self.operands_stack.pop() left_operand = self.operands_stack.pop() operator = self.operators_stack.pop() right_operand_type = self.types_stack.pop() left_operand_type = self.types_stack.pop() result_type = SemanticCube().check_operation( left_operand_type, right_operand_type, operator) if result_type == Types.ERROR: print( 'ERROR: Los tipos de datos de la asignación no son compatibles.' ) self.quadruples.append(operator, right_operand, None, left_operand) else: print( 'ERROR: Se entró a crear un quad de asignación pero no está el operando = en el stack.', self.operators_stack) def check_for_mult_or_div(self): # print("Mult/Div Check", len(self.operators_stack), self.operators_stack[-1:]) if len(self.operators_stack) is not 0 and self.operators_stack[ len(self.operators_stack) - 1] in [Operations.MULTIPLICATION, Operations.DIVISION]: self.operation_quadruple() def check_for_add_or_subs(self): # print("Add/Sub Check", len(self.operators_stack), self.operators_stack[-1:]) if len(self.operators_stack) > 0 and self.operators_stack[ len(self.operators_stack) - 1] in [Operations.ADDITION, Operations.SUBTRACTION]: self.operation_quadruple() def check_for_relational_operators(self): # print("Relational Check", len(self.operators_stack), self.operators_stack[-1:]) if len(self.operators_stack) > 0 and self.operators_stack[ len(self.operators_stack) - 1] in [ Operations.EQUAL, Operations.NOT_EQUAL, Operations.GREATER_OR_EQUAL, Operations.LESS_OR_EQUAL, Operations.GREATER, Operations.LESS ]: self.operation_quadruple() def check_for_logic_operators(self): # print("Logic Check", len(self.operators_stack), self.operators_stack[-1:]) if len(self.operators_stack) > 0 and self.operators_stack[ len(self.operators_stack) - 1] in [Operations.AND, Operations.OR]: self.operation_quadruple() def check_for_assignment(self): # print("Asignment Check", len(self.operators_stack), self.operators_stack[-1:]) if len(self.operators_stack) > 0 and self.operators_stack[ len(self.operators_stack) - 1] == Operations.ASSIGN: self.operation_quadruple() def if_statement(self): if len(self.operands_stack) != 0: # Check if while statement is valid condition_var = self.operands_stack.pop() type_condition_var = self.types_stack.pop() # print(self.operands_stack, self.operators_stack, self.types_stack, self.jumps_stack) if type_condition_var == Types.BOOL: # Create GOTOF and save quad in jumps stack to fill when we know false jump location self.jumps_stack.append(self.quadruples.length()) self.quadruples.append("GOTOF", condition_var, None, None) else: print('ERROR: ' + str(condition_var) + ' no es un boolean, es ' + str(type_condition_var)) def else_statement(self): self.quadruples.quads[ self.jumps_stack.pop()].result = self.quadruples.length() + 1 # Create GOTO and save quad in jumps stack to fill when we know jump location self.jumps_stack.append(self.quadruples.length()) self.quadruples.append("GOTO", None, None, None) def end_if_else_function(self): # Fill if's GOTOF quad with current index self.quadruples.quads[ self.jumps_stack.pop()].result = self.quadruples.length() def while_statement(self): # Save while statement quad in jumps stack to fill when we know jump location self.jumps_stack.append(self.quadruples.length()) def while_statutes(self): if len(self.operands_stack) != 0: condition_var = self.operands_stack.pop() type_condition_var = self.types_stack.pop() if type_condition_var == Types.BOOL: # Save begin statutes quad in jumps stack to fill when we know jump location self.jumps_stack.append(self.quadruples.length()) self.quadruples.append("GOTOF", condition_var, None, None) else: print('ERROR: ' + condition_var + ' es ' + type_condition_var + 'en vez de boolean.') def while_end(self): # Fill while statutes quad with ending of while while_statutes_index = self.jumps_stack.pop() self.quadruples.quads[ while_statutes_index].result = self.quadruples.length() + 1 # Get while statement index to generate GOTO quad to loop in while while_statement_index = self.jumps_stack.pop() self.quadruples.append("GOTO", None, None, while_statement_index) def from_initialize(self, operand): var_operand = None if operand in self.current_function.function_variables: var_operand = self.current_function.function_variables[operand] elif operand in self.functions_table.functions[ "global"].function_variables: var_operand = self.functions_table.functions[ "global"].function_variables[operand] # From variable was already declared locally or globally if var_operand: # Check if variable is INT to procede if (var_operand.variable_type == Types.INT): # Pop variable value and save from_variable_value = self.operands_stack.pop() self.types_stack.pop() self.quadruples.append("=", from_variable_value, None, var_operand.variable_address) self.current_function.function_variables[ 'from_variable'] = Variable(Types.INT, 'from_variable', from_variable_value, var_operand.variable_address) else: print('ERROR: La variable ' + operand + 'no es un entero') else: print('ERROR: La variable ' + operand + ' no está declarada') def from_statutes(self): if len(self.operands_stack) != 0: # Get the initial and limit values of the from loop try: from_variable = self.current_function.function_variables[ 'from_variable'] except: print('ERROR: No se encontró la variable del loop desde-hasta') from_variable_limit_value = self.operands_stack.pop() from_variable_limit_type = self.types_stack.pop() if from_variable_limit_type == Types.INT: res_address = self.temporal_memory.get_address(Types.BOOL) self.jumps_stack.append(self.quadruples.length()) self.quadruples.append("<", from_variable.variable_address, from_variable_limit_value, res_address) # Add point to jump stack and create GOTOF self.jumps_stack.append(self.quadruples.length()) self.quadruples.append("GOTOF", res_address, None, None) self.add_constant_operand("1", Types.INT) self.types_stack.pop() self.quadruples.append("+", from_variable.variable_address, self.operands_stack.pop(), from_variable.variable_address) else: print( 'ERROR: La expresión asignada a la variable del from debe de ser un entero' ) def end_from(self): # Fill from statutes quad with ending of while from_statutes_index = self.jumps_stack.pop() self.quadruples.quads[ from_statutes_index].result = self.quadruples.length() + 1 # Get from statutes index to generate GOTO quad to loop back to from from_statement_index = self.jumps_stack.pop() self.quadruples.append("GOTO", None, None, from_statement_index) def add_function_operand_type(self, operand): if self.functions_table.functions[operand].function_type == 'void': print('ERROR: No se le puede asignar a la función void ' + operand + ' un valor.') else: self.operands_stack.append(operand) self.add_type( self.functions_table.functions[operand].function_type) def goto_main(self): self.jumps_stack.append(0) self.quadruples.append("GOTO", None, None, "_") def start_main(self): mainQuad = self.jumps_stack.pop() self.quadruples.quads[mainQuad].result = self.quadruples.length() def goto_function(self, id): self.quadruples.append( 'GOSUB', None, None, self.functions_table.functions[id].start_quadruple) if self.functions_table.functions[id].function_type != "void": ret_type = self.functions_table.functions[id].function_type ret_address = self.temporal_memory.get_address(ret_type) self.quadruples.append( '=', self.functions_table.functions["global"]. function_variables[id].variable_address, None, ret_address) self.operands_stack.append(ret_address) self.types_stack.append(ret_type) def create_era(self, function_name): self.quadruples.append("ERA", None, None, function_name) def add_parameters(self, function_name): for parameter in reversed( self.functions_table.functions[function_name]. function_parameters): parameter_operand = self.operands_stack.pop() self.types_stack.pop() self.quadruples.append( "PARAM", parameter_operand, None, self.functions_table.functions[function_name]. function_variables[parameter.variable_name].variable_address) def end_function(self): self.current_function.end_quadruple = self.quadruples.length() self.quadruples.append('ENDPROC', None, None, None) def return_end_function(self): if self.current_function.function_type != "void": self.current_function.end_quadruple = self.quadruples.length() return_address = self.functions_table.functions[ "global"].function_variables[ self.current_function.function_name].variable_address self.types_stack.pop() self.quadruples.append('RETURN', self.operands_stack.pop(), None, return_address) else: print('ERROR: La función void ' + self.current_function.function_name + ' no puede tener un estatuto regresa.') def void_end_function(self): if self.current_function.function_type == "void": self.current_function.end_quadruple = self.quadruples.length() self.quadruples.append('GOTO', None, None, '_') else: print('ERROR: No se encontró valor de retorno en la función' + self.current_function.name + 'de tipo ' + self.current_function.function_type) def finish_program(self): self.quadruples.append("END", None, None, None) print("Quadruplos del Programa: ") self.quadruples.print() print('Stack de operandos: ', self.operands_stack) print('Stack de operadores: ', self.operators_stack) print('Stack de tipos', self.types_stack) print('Stack de saltos', self.jumps_stack) vm = VirtualMachine(self.quadruples, self.constant_exec_memory) vm.execute()
def __init__(self, env, monitor_path: str, **usercfg) -> None: super(DDPG, self).__init__(**usercfg) self.env = env self.monitor_path: str = monitor_path self.config.update( n_episodes=100000, n_timesteps=env.spec.tags.get( "wrapper_config.TimeLimit.max_episode_steps"), actor_learning_rate=1e-4, critic_learning_rate=1e-3, ou_theta=0.15, ou_sigma=0.2, gamma=0.99, batch_size=64, tau=0.001, l2_loss_coef=1e-2, n_actor_layers=2, n_hidden_units=64, actor_layer_norm=True, critic_layer_norm= False, # Batch norm for critic does not seem to work replay_buffer_size=1e6, replay_start_size= 10000 # Required number of replay buffer entries to start training ) self.config.update(usercfg) self.state_shape: list = list(env.observation_space.shape) self.n_actions: int = env.action_space.shape[0] self.states = tf.placeholder(tf.float32, [None] + self.state_shape, name="states") self.actions_taken = tf.placeholder(tf.float32, [None, self.n_actions], name="actions_taken") self.critic_target = tf.placeholder(tf.float32, [None, 1], name="critic_target") self.is_training = tf.placeholder(tf.bool, name="is_training") with tf.variable_scope("actor"): self.action_output, self.actor_vars = self.build_actor_network() self.target_action_output, actor_target_update = self.build_target_actor_network( self.actor_vars) self.q_gradient_input = tf.placeholder("float", [None, self.n_actions], name="q_grad_input") self.actor_policy_gradients = tf.gradients(self.action_output, self.actor_vars, -self.q_gradient_input, name="actor_gradients") self.actor_train_op = tf.train.AdamOptimizer( self.config["actor_learning_rate"], name="actor_optimizer").apply_gradients( list(zip(self.actor_policy_gradients, self.actor_vars))) with tf.variable_scope("critic"): self.q_value_output, self.critic_vars = self.build_critic_network() self.target_q_value_output, critic_target_update = self.build_target_critic_network( self.critic_vars) l2_loss = tf.add_n([ self.config["l2_loss_coef"] * tf.nn.l2_loss(var) for var in self.critic_vars ]) self.critic_loss = tf.reduce_mean( tf.square(self.critic_target - self.q_value_output)) + l2_loss self.critic_train_op = tf.train.AdamOptimizer( self.config["critic_learning_rate"], name="critic_optimizer").minimize(self.critic_loss) self.action_gradients = tf.gradients(self.q_value_output, self.actions_taken, name="action_gradients") summaries = [] for v in self.actor_vars + self.critic_vars: summaries.append(tf.summary.histogram(v.name, v)) self.model_summary_op = tf.summary.merge(summaries) self.update_targets_op = tf.group(actor_target_update, critic_target_update, name="update_targets") self.init_op = tf.global_variables_initializer() self.action_noise = OrnsteinUhlenbeckActionNoise( self.n_actions, self.config["ou_sigma"], self.config["ou_theta"]) self.replay_buffer = Memory(int(self.config["replay_buffer_size"])) self.n_updates = 0 self.summary_writer = tf.summary.FileWriter( os.path.join(self.monitor_path, "summaries"), tf.get_default_graph())
def __init__(self, sess, args): self.args = args self.session = sess self.w = {} self.eval_w = {} with tf.variable_scope(self.args.model_name): self.x = tf.placeholder(tf.float32, shape=[None, 784], name="x") self.y = tf.placeholder(tf.float32, shape=[None, 10], name="y") self.memory_sample_batch = tf.placeholder( tf.int16, shape=(), name="memory_sample_batch") with tf.variable_scope("training"): with tf.variable_scope("embedding"): self.out = tf.reshape(self.x, [-1, 28, 28, 1]) with tf.variable_scope("conv"): # # self.out, self.w["l1_w"], self.w["l1_b"] = conv2d( # # x=self.out, # # output_dim=16, # # kernel_size=[8, 8], # # stride=[4, 4], # # activation_fn=tf.nn.relu, # # name="conv1" # # ) # # self.out, self.w["l2_w"], self.w["l2_b"] = conv2d( # # x=self.out, # # output_dim=32, # # kernel_size=[4, 4], # # stride=[2, 2], # # activation_fn=tf.nn.relu, # # name="conv2" # # ) self.embed = layers.flatten(self.out) # self.embed_dim = self.embed.get_shape()[-1] self.M = Memory(self.args.memory_size, self.x.get_shape()[-1], self.y.get_shape()[-1]) embs_and_values = tf.py_func(self.get_memory_sample, [self.memory_sample_batch], [tf.float64, tf.float64]) self.memory_batch_x = tf.to_float(embs_and_values[0]) self.memory_batch_y = tf.to_float(embs_and_values[1]) self.xa = tf.concat(values=[self.x, self.memory_batch_x], axis=0) self.ya = tf.concat(values=[self.y, self.memory_batch_y], axis=0) with tf.variable_scope("fc"): self.out = self.xa # self.out, self.w["l3_w"], self.w["l3_b"] = linear( # input_=self.out, # output_size=1024, # activation_fn=tf.nn.relu, # name="fc_1" # ) self.out, self.w["l4_w"], self.w["l4_b"] = linear( input_=self.out, output_size=10, name="fc_2") self.ya_ = self.out self.cross_entropy = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(labels=self.ya, logits=self.ya_)) self.optim = tf.train.GradientDescentOptimizer( self.args.learning_rate).minimize(self.cross_entropy) self.correct_prediction = tf.equal(tf.argmax(self.ya, 1), tf.argmax(self.ya_, 1)) self.accuracy = tf.reduce_mean( tf.cast(self.correct_prediction, tf.float32)) self.session.run(tf.global_variables_initializer())
def __init__(self): self.memory = Memory() self.display_adapter = DisplayAdapter(self.memory)
class DDPG(Agent): def __init__(self, env, monitor_path: str, **usercfg) -> None: super(DDPG, self).__init__(**usercfg) self.env = env self.monitor_path: str = monitor_path self.config.update( n_episodes=100000, n_timesteps=env.spec.tags.get( "wrapper_config.TimeLimit.max_episode_steps"), actor_learning_rate=1e-4, critic_learning_rate=1e-3, ou_theta=0.15, ou_sigma=0.2, gamma=0.99, batch_size=64, tau=0.001, l2_loss_coef=1e-2, n_actor_layers=2, n_hidden_units=64, actor_layer_norm=True, critic_layer_norm= False, # Batch norm for critic does not seem to work replay_buffer_size=1e6, replay_start_size= 10000 # Required number of replay buffer entries to start training ) self.config.update(usercfg) self.state_shape: list = list(env.observation_space.shape) self.n_actions: int = env.action_space.shape[0] self.states = tf.placeholder(tf.float32, [None] + self.state_shape, name="states") self.actions_taken = tf.placeholder(tf.float32, [None, self.n_actions], name="actions_taken") self.critic_target = tf.placeholder(tf.float32, [None, 1], name="critic_target") self.is_training = tf.placeholder(tf.bool, name="is_training") with tf.variable_scope("actor"): self.action_output, self.actor_vars = self.build_actor_network() self.target_action_output, actor_target_update = self.build_target_actor_network( self.actor_vars) self.q_gradient_input = tf.placeholder("float", [None, self.n_actions], name="q_grad_input") self.actor_policy_gradients = tf.gradients(self.action_output, self.actor_vars, -self.q_gradient_input, name="actor_gradients") self.actor_train_op = tf.train.AdamOptimizer( self.config["actor_learning_rate"], name="actor_optimizer").apply_gradients( list(zip(self.actor_policy_gradients, self.actor_vars))) with tf.variable_scope("critic"): self.q_value_output, self.critic_vars = self.build_critic_network() self.target_q_value_output, critic_target_update = self.build_target_critic_network( self.critic_vars) l2_loss = tf.add_n([ self.config["l2_loss_coef"] * tf.nn.l2_loss(var) for var in self.critic_vars ]) self.critic_loss = tf.reduce_mean( tf.square(self.critic_target - self.q_value_output)) + l2_loss self.critic_train_op = tf.train.AdamOptimizer( self.config["critic_learning_rate"], name="critic_optimizer").minimize(self.critic_loss) self.action_gradients = tf.gradients(self.q_value_output, self.actions_taken, name="action_gradients") summaries = [] for v in self.actor_vars + self.critic_vars: summaries.append(tf.summary.histogram(v.name, v)) self.model_summary_op = tf.summary.merge(summaries) self.update_targets_op = tf.group(actor_target_update, critic_target_update, name="update_targets") self.init_op = tf.global_variables_initializer() self.action_noise = OrnsteinUhlenbeckActionNoise( self.n_actions, self.config["ou_sigma"], self.config["ou_theta"]) self.replay_buffer = Memory(int(self.config["replay_buffer_size"])) self.n_updates = 0 self.summary_writer = tf.summary.FileWriter( os.path.join(self.monitor_path, "summaries"), tf.get_default_graph()) def build_actor_network(self): layer1_size = 400 layer2_size = 300 x = self.states if self.config["actor_layer_norm"]: x = batch_norm_layer(x, training_phase=self.is_training, scope_bn="batch_norm_0", activation=tf.identity) with tf.variable_scope("L1"): x, l1_vars = linear_fan_in(x, layer1_size) if self.config["actor_layer_norm"]: x = batch_norm_layer(x, training_phase=self.is_training, scope_bn="batch_norm_1", activation=tf.nn.relu) with tf.variable_scope("L2"): x, l2_vars = linear_fan_in(x, layer2_size) if self.config["actor_layer_norm"]: x = batch_norm_layer(x, training_phase=self.is_training, scope_bn="batch_norm_2", activation=tf.nn.relu) with tf.variable_scope("L3"): W3 = tf.Variable(tf.random_uniform([layer2_size, self.n_actions], -3e-3, 3e-3), name="w") b3 = tf.Variable(tf.random_uniform([self.n_actions], -3e-3, 3e-3), name="b") action_output = tf.tanh(tf.nn.xw_plus_b(x, W3, b3)) l3_vars = [W3, b3] return action_output, l1_vars + l2_vars + l3_vars def build_target_actor_network(self, actor_vars: list): ema = tf.train.ExponentialMovingAverage(decay=1 - self.config["tau"]) target_update = ema.apply(actor_vars) target_net = [ema.average(v) for v in actor_vars] x = self.states if self.config["actor_layer_norm"]: x = batch_norm_layer(x, training_phase=self.is_training, scope_bn="target_batch_norm_0", activation=tf.identity) x = tf.nn.xw_plus_b(x, target_net[0], target_net[1]) if self.config["actor_layer_norm"]: x = batch_norm_layer(x, training_phase=self.is_training, scope_bn="target_batch_norm_1", activation=tf.nn.relu) x = tf.nn.xw_plus_b(x, target_net[2], target_net[3]) if self.config["actor_layer_norm"]: x = batch_norm_layer(x, training_phase=self.is_training, scope_bn="target_batch_norm_2", activation=tf.nn.relu) action_output = tf.tanh( tf.nn.xw_plus_b(x, target_net[4], target_net[5])) return action_output, target_update def build_critic_network(self): layer1_size = 400 layer2_size = 300 x = self.states with tf.variable_scope("L1"): if self.config[ "critic_layer_norm"]: # Defaults to False (= don't use it) x = batch_norm_layer(x, training_phase=self.is_training, scope_bn="batch_norm_0", activation=tf.identity) x, l1_vars = linear_fan_in(x, layer1_size) x = tf.nn.relu(x) with tf.variable_scope("L2"): W2 = tf.get_variable( "w", [layer1_size, layer2_size], initializer=fan_in_initializer(layer1_size + self.n_actions)) W2_action = tf.get_variable( "w_action", [self.n_actions, layer2_size], initializer=fan_in_initializer(layer1_size + self.n_actions)) b2 = tf.get_variable( "b", [layer2_size], initializer=fan_in_initializer(layer1_size + self.n_actions)) x = tf.nn.relu( tf.matmul(x, W2) + tf.matmul(self.actions_taken, W2_action) + b2) with tf.variable_scope("L3"): W3 = tf.Variable(tf.random_uniform([layer2_size, 1], -3e-3, 3e-3), name="w") b3 = tf.Variable(tf.random_uniform([1], -3e-3, 3e-3), name="b") q_value_output = tf.nn.xw_plus_b(x, W3, b3, name="q_value") return q_value_output, l1_vars + [W2, W2_action, b2, W3, b3] def build_target_critic_network(self, critic_vars: list): ema = tf.train.ExponentialMovingAverage(decay=1 - self.config["tau"]) target_update = ema.apply(critic_vars) target_net = [ema.average(v) for v in critic_vars] x = self.states if self.config["critic_layer_norm"]: x = batch_norm_layer(x, training_phase=self.is_training, scope_bn="batch_norm_0", activation=tf.identity) x = tf.nn.relu(tf.nn.xw_plus_b(x, target_net[0], target_net[1])) x = tf.nn.relu( tf.matmul(x, target_net[2]) + tf.matmul(self.actions_taken, target_net[3]) + target_net[4]) q_value_output = tf.nn.xw_plus_b(x, target_net[5], target_net[6]) return q_value_output, target_update def actor_gradients(self, state_batch: np.ndarray, action_batch: np.ndarray): q, grads = tf.get_default_session().run( [self.q_value_output, self.action_gradients], feed_dict={ self.states: state_batch, self.actions_taken: action_batch, self.is_training: False }) summary = tf.Summary() summary.value.add(tag="model/actor_loss", simple_value=float(-np.mean(q))) self.summary_writer.add_summary(summary, self.n_updates) return grads[0] def target_q(self, states: np.ndarray, actions: np.ndarray): return tf.get_default_session().run(self.target_q_value_output, feed_dict={ self.states: states, self.actions_taken: actions, self.is_training: False }) def q_value(self, states: np.ndarray, actions: np.ndarray): return tf.get_default_session().run(self.q_value_output, feed_dict={ self.states: states, self.actions_taken: actions, self.is_training: False }) def actions(self, states: np.ndarray) -> np.ndarray: """Get the actions for a batch of states.""" return tf.get_default_session().run(self.action_output, feed_dict={ self.states: states, self.is_training: True }) def action(self, state: np.ndarray) -> np.ndarray: """Get the action for a single state.""" return tf.get_default_session().run(self.action_output, feed_dict={ self.states: [state], self.is_training: False })[0] def target_actions(self, states: np.ndarray) -> np.ndarray: """Get the actions for a batch of states using the target actor network.""" return tf.get_default_session().run(self.target_action_output, feed_dict={ self.states: states, self.is_training: True }) def train(self): sample = self.replay_buffer.get_batch(self.config["batch_size"]) # for n_actions = 1 action_batch = np.resize(sample["actions"], [self.config["batch_size"], self.n_actions]) # Calculate critic targets next_action_batch = self.target_actions(sample["states1"]) q_value_batch = self.target_q(sample["states1"], next_action_batch) critic_targets = sample["rewards"] + (1 - sample["terminals1"]) * \ self.config["gamma"] * q_value_batch.squeeze() critic_targets = np.resize( critic_targets, [self.config["batch_size"], 1]).astype(np.float32) # Update actor weights fetches = [self.q_value_output, self.critic_loss, self.critic_train_op] predicted_q, critic_loss, _ = tf.get_default_session().run( fetches, feed_dict={ self.critic_target: critic_targets, self.states: sample["states0"], self.actions_taken: action_batch, self.is_training: True }) summary = tf.Summary() summary.value.add(tag="model/critic_loss", simple_value=float(critic_loss)) summary.value.add(tag="model/predicted_q_mean", simple_value=np.mean(predicted_q)) summary.value.add(tag="model/predicted_q_std", simple_value=np.std(predicted_q)) self.summary_writer.add_summary(summary, self.n_updates) # Update the actor using the sampled gradient: action_batch_for_gradients = self.actions(sample["states0"]) q_gradient_batch = self.actor_gradients(sample["states0"], action_batch_for_gradients) tf.get_default_session().run(self.actor_train_op, feed_dict={ self.q_gradient_input: q_gradient_batch, self.states: sample["states0"], self.is_training: True }) # Update the target networks tf.get_default_session().run( [self.update_targets_op, self.model_summary_op]) self.n_updates += 1 def noise_action(self, state: np.ndarray): """Choose an action based on the actor and exploration noise.""" action = self.action(state) return action + self.action_noise() def learn(self): max_action = self.env.action_space.high with tf.Session() as sess, sess.as_default(): sess.run(self.init_op) for episode in range(self.config["n_episodes"]): state = self.env.reset() episode_reward = 0 episode_length = 0 for _ in range(self.config["n_timesteps"]): action = self.noise_action(state) new_state, reward, done, _ = self.env.step(action * max_action) episode_length += 1 episode_reward += reward self.replay_buffer.add(state, action, reward, new_state, done) if self.replay_buffer.n_entries > self.config[ "replay_start_size"]: self.train() state = new_state if done: self.action_noise.reset() summary = tf.Summary() summary.value.add(tag="global/Episode_length", simple_value=float(episode_length)) summary.value.add(tag="global/Reward", simple_value=float(episode_reward)) self.summary_writer.add_summary(summary, episode) self.summary_writer.flush() break
class News: def __init__(self, source="google-news", api_key="7488ba8ff8dc43459d36f06e7141c9e5"): self.apiKey = api_key self.source = source self.url = "https://newsapi.org/v1/articles?source=google-news&sortBy=top" \ "&apiKey=7488ba8ff8dc43459d36f06e7141c9e5" self.m = Memory() ''' This is the defualt news function. It checks to see if the user has a preference and and goes through all choices. ''' def news(self): self.news_options() self.get_news() ''' This is the quickest way to get news it also has the least amount of options for the user. ''' def quick_news(self): self.request_news() ''' Gets and returns JSON data of news ''' def get_news_json(self): response = urllib.urlopen(self.url) return json.loads(response.read()) ''' This sets the users options and loads them from Memory if they exist ''' def news_options(self): # check to see if user already has default news source if self.m.get_data('news-source'): print("your default news source is " + self.m.get_data('news-source')) print("Would you like news from this source? (yes/no): ") try: x = raw_input() except: x = input() if x == 'y' or x == 'yes': self.source = self.m.get_data('news-source') # if not set get users preference else: self.get_opt() else: self.get_opt() def get_opt(self): # Other sources available here: https://newsapi.org/sources print("Select Source (1-5):") print("1: BBC") print("2: BUZZFEED") print("3: Google") print("4: Reddit") print("5: TechCrunch") try: i = int(raw_input()) except: i = int(input()) if i == 1: self.source = "bbc-news" elif i == 2: self.source = "buzzfeed" elif i == 3: self.source = "google-news" elif i == 4: self.source = "reddit-r-all" elif i == 5: self.source = "techcrunch" print("would you like to set this as your default? (yes/no): ") try: x = raw_input() except: x = input() if x == 'y' or x == 'yes': self.m.update_data('news-source', self.source) # save to memory self.m.save() ''' This sets the url and sends it to request_news() ''' def get_news(self): u = "https://newsapi.org/v1/articles?source=" + \ self.source + "&sortby=top&apiKey=" + self.apiKey self.request_news(u) ''' This has all the logic to request and parse the json. This function DOES NOT check user preferences. It also includes user interactions for getting more info on an articles ''' def request_news(self, url=None): # check to see if a url was passed if url is None: url = self.url response = urllib.urlopen(url) # Load json data = json.loads(response.read()) article_list = {} index = 1 # print articles with their index print("Top News Articles from " + self.source) for article in data['articles']: # print (Fore.GREEN + str(index) + ": " + article['title'] + Fore.RESET) print(str(index) + ": " + article['title']) article_list[index] = article index += 1 # Attribution link for News API to comply with TOU print("Powered by News API. Type NewsAPI to learn more") print("Type index to expand news\n") # Check to see if index or NewsAPI was enterd try: idx = raw_input() except: idx = input() if idx.lower() == "newsapi": webbrowser.open('https://newsapi.org/') return # check if we have a valid index try: int(idx) if int(idx) > index: print("Not a valid index") return except: print("Not a valid index") return # if index valid print article description print(article_list[int(idx)]['description']) print("Do you want to read more? (yes/no): ") try: i = raw_input() except: i = input() # if user wants to read more open browser to article url if i.lower() == "yes" or i.lower() == 'y': webbrowser.open(article_list[int(idx)]['url']) return else: return
from semantic_cube.semantic_cube import Cube from semantic_cube.semantic_cube_helper import ( code_to_type, token_to_code, type_to_code, type_to_init_value, scope_to_code, code_to_scope, ) from error.error_helper import ErrorHelper parser_helper = ParserHelper() quad_helper = QuadrupleHelper() error_helper = ErrorHelper() semantic_cube = Cube() memory = Memory() def p_program(p): """program : PROGRAM ID program1""" # print("DIR TABLE: ", parser_helper.procedure_directory) def p_program1(p): """program1 : script program2 | program2""" def p_program2(p): """program2 : htmltag program2 | empty"""
class GraphColoringSearchPerformer: def __init__(self, stop_criteria, memory_size, progress_writer=None): self.stop_criteria = stop_criteria self.color_permutator = FastColorPermutator() self.memory = Memory(memory_size) self.progress_writer = progress_writer self.root_node = None self.best_score = None self.best_score_graph = None self.color_set = [] def search(self, root_node, color_set): self.memory.clear_memory() self.root_node = root_node self.best_score = CostEvaluator.evaluate(root_node, color_set) self.best_score_graph = self.root_node self.color_set = color_set return self.recursive_search() def recursive_search(self): iteration_task = self.tabu_search_task_name() if self.progress_writer: self.progress_writer.start_task(iteration_task, True) self.progress_writer.start_subtask('finding permutations', True) permutations, score = self.find_permutations(self.root_node, self.color_set) if len(permutations) == 0: return self.return_score() if self.progress_writer: self.progress_writer.stop_subtask('finding permutations', '', True) node, color = self.get_best_permutation_for_iteration(permutations) node.set_color(color) self.memory.add_to_memory(node, node.previous_color) if self.best_score > score: self.best_score = score self.best_score_graph = GraphCloner.clone(self.root_node) self.stop_criteria.next_iteration(self.best_score) if self.progress_writer: self.progress_writer.stop_task(iteration_task, self.get_iteration_summary(score), True) if self.stop_criteria.should_stop(): return self.return_score() return self.recursive_search() def tabu_search_task_name(self): return 'tabu search {0} iteration'.format(self.stop_criteria.current_iterations + 1) def find_permutations(self, node, color_set): aspiration_criteria = AspirationCriteria(self.memory.get_short_term_memory(), self.best_score) permutations, score = self.color_permutator.permutate(node, color_set, aspiration_criteria) for index in range(1, len(self.memory.get_short_term_memory())): if len(permutations) > 0: break aspiration_criteria = AspirationCriteria(self.memory.get_short_term_memory()[index:], self.best_score) permutations, score = self.color_permutator.permutate(node, color_set, aspiration_criteria) return permutations, score def return_score(self): return self.best_score_graph def get_best_permutation_for_iteration(self, permutations): if len(permutations) == 1: return permutations[0] permutation_to_return, permutation_to_return_usages = permutations[0], None for permutation in permutations: node_usages = sum(1 for _ in filter(lambda t: t[0] == permutation[0].node_id, self.memory.get_long_term_memory())) if permutation_to_return_usages is None or node_usages < permutation_to_return_usages: permutation_to_return, permutation_to_return_usages = permutation, node_usages return permutation_to_return def get_iteration_summary(self, iteration_best_score): summary = 'iterations without score change: {0}\n'.format( self.stop_criteria.current_iterations_without_score_change) summary += 'iteration\'s best score: {0}\n'.format(iteration_best_score) summary += 'overall best score: {0}\n'.format(self.best_score) summary += 'memory: {0}'.format(self.memory) summary += 'permutation: {0}'.format(self.root_node) return summary
class MbPA_KNN_Test: def __init__(self, sess, args): self.args = args self.session = sess self.w = {} self.eval_w = {} with tf.variable_scope(self.args.model_name): self.x = tf.placeholder(tf.float32, shape=[None, 784], name="x") self.y = tf.placeholder(tf.float32, shape=[None, 10], name="y") self.memory_sample_batch = tf.placeholder( tf.int16, shape=(), name="memory_sample_batch") with tf.variable_scope("training"): with tf.variable_scope("embedding"): self.out = tf.reshape(self.x, [-1, 28, 28, 1]) with tf.variable_scope("conv"): # # self.out, self.w["l1_w"], self.w["l1_b"] = conv2d( # # x=self.out, # # output_dim=16, # # kernel_size=[8, 8], # # stride=[4, 4], # # activation_fn=tf.nn.relu, # # name="conv1" # # ) # # self.out, self.w["l2_w"], self.w["l2_b"] = conv2d( # # x=self.out, # # output_dim=32, # # kernel_size=[4, 4], # # stride=[2, 2], # # activation_fn=tf.nn.relu, # # name="conv2" # # ) self.embed = layers.flatten(self.out) # self.embed_dim = self.embed.get_shape()[-1] self.M = Memory(self.args.memory_size, self.x.get_shape()[-1], self.y.get_shape()[-1]) embs_and_values = tf.py_func(self.get_memory_sample, [self.memory_sample_batch], [tf.float64, tf.float64]) self.memory_batch_x = tf.to_float(embs_and_values[0]) self.memory_batch_y = tf.to_float(embs_and_values[1]) self.xa = tf.concat(values=[self.x, self.memory_batch_x], axis=0) self.ya = tf.concat(values=[self.y, self.memory_batch_y], axis=0) with tf.variable_scope("fc"): self.out = self.xa # self.out, self.w["l3_w"], self.w["l3_b"] = linear( # input_=self.out, # output_size=1024, # activation_fn=tf.nn.relu, # name="fc_1" # ) self.out, self.w["l4_w"], self.w["l4_b"] = linear( input_=self.out, output_size=10, name="fc_2") self.ya_ = self.out self.cross_entropy = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(labels=self.ya, logits=self.ya_)) self.optim = tf.train.GradientDescentOptimizer( self.args.learning_rate).minimize(self.cross_entropy) self.correct_prediction = tf.equal(tf.argmax(self.ya, 1), tf.argmax(self.ya_, 1)) self.accuracy = tf.reduce_mean( tf.cast(self.correct_prediction, tf.float32)) self.session.run(tf.global_variables_initializer()) def update_training_to_prediction(self): for name in self.eval_w.keys(): self.t_w_assign_op[name].eval( {self.t_w_input[name]: self.w[name].eval()}) def train(self, xs, ys, memory_sample_batch): embeds, _ = self.session.run([self.embed, self.optim], feed_dict={ self.x: xs, self.y: ys, self.memory_sample_batch: memory_sample_batch }) return embeds def get_memory_sample(self, batch_size): xs, ys = self.M.sample(batch_size) return xs, ys def add_to_memory(self, xs, ys): if self.args.sample_add == "normal": self.M.add(xs, ys) elif self.args.sample_add == "lru": self.M.add_lru(xs, ys) elif self.args.sample_add == "rand": self.M.add_rand(xs, ys) elif self.args.sample_add == "knn": self.M.add_knn(xs, ys) elif self.args.sample_add == "knn_lru": self.M.add_knn_lru(xs, ys) else: raise Exception( "error sample adding type, pleace choose in ['normal', 'lru', 'rand']" ) def test(self, xs_test, ys_test): # self.update_training_to_prediction() acc = self.session.run(self.accuracy, feed_dict={ self.x: xs_test, self.y: ys_test, self.memory_sample_batch: 0 }) return acc @property def memory_length(self): return self.M.length
class ClassScope: # Recursive typing, see: https://stackoverflow.com/a/38341145 def __init__(self, name: str, inherits: Optional["ClassScope"] = None, global_scope: Optional["SymbolTable"] = None): """ClassScope is responsible for keeping track of a classes methods, attributes, and local memory. Arguments: - name [str]: The name of the class. - inherits [ClassScope]: The class scope of the parent class if there is one. """ self._name = name if inherits is None: self._method_directory = SymbolTable(name) self._attribute_directory = SymbolTable(name, global_scope) self._instance_memory = Memory(Scopes.INSTANCE, ScopeRanges.INSTANCE) else: self._method_directory = SymbolTable(name, inherits.method_directory) self._attribute_directory = SymbolTable( name, inherits.attribute_directory) self._instance_memory = inherits.instance_memory @property def name(self) -> str: """The name of the class. Returns: - The name [str] of the class. """ return self._name @property def method_directory(self) -> SymbolTable: """The SymbolTable which keeps track of the methods in the class. Returns: - The SymbolTable instance. """ return self._method_directory @property def attribute_directory(self) -> SymbolTable: """The SymbolTable which keeps track of the attributes in the class. Returns: - The SymbolTable instance. """ return self._attribute_directory @property def instance_memory(self) -> Memory: return self._instance_memory def add_method(self, name: str, access_modifier: str) -> MethodScope: """Adds a method to the class. Arguments: - name [str]: The name of the method. - access_modifier [str]: Whether the method is public or private. Returns: - The MethodScope object created for this method. """ method_scope = MethodScope(name, access_modifier, self._attribute_directory) self._method_directory.add_symbol(method_scope) return method_scope def add_attribute(self, name: str, var_type: str, access_modifier: str) -> None: """Adds an attribute to the class. Arguments: - name [str]: The name of the attribute. - var_type [str]: The type of the attribute. - access_modifier [str]: Whether the attribute is public or private. """ memory_address = self._instance_memory.next_memory_space(var_type) self._attribute_directory.add_symbol( Variable(f"@{name}", var_type, memory_address, access_modifier=access_modifier))
class MemoryTests(unittest.TestCase): memory = Memory(3) def test_is_in_short_term_memory_method(self): self.memory.clear_memory() n1 = Node(1, 18) n2 = Node(2, 33) n3 = Node(3, 52) n4 = Node(4, 89) self.memory.add_to_memory(n1, 2) n1_2_in_memory_first = self.memory.is_in_short_term_memory(n1, 2) self.memory.add_to_memory(n2, 3) n1_2_in_memory_second = self.memory.is_in_short_term_memory(n1, 2) n2_3_in_memory_first = self.memory.is_in_short_term_memory(n2, 3) self.memory.add_to_memory(n3, 4) n1_2_in_memory_third = self.memory.is_in_short_term_memory(n1, 2) n2_3_in_memory_second = self.memory.is_in_short_term_memory(n2, 3) n3_4_in_memory_first = self.memory.is_in_short_term_memory(n3, 4) self.memory.add_to_memory(n4, 1) n1_2_in_memory_fourth = self.memory.is_in_short_term_memory(n1, 2) n2_3_in_memory_third = self.memory.is_in_short_term_memory(n2, 3) n3_4_in_memory_second = self.memory.is_in_short_term_memory(n3, 4) n4_1_in_memory_first = self.memory.is_in_short_term_memory(n4, 1) self.assertTrue(n1_2_in_memory_first) self.assertTrue(n1_2_in_memory_second) self.assertTrue(n2_3_in_memory_first) self.assertTrue(n1_2_in_memory_third) self.assertTrue(n2_3_in_memory_second) self.assertTrue(n3_4_in_memory_first) self.assertFalse(n1_2_in_memory_fourth) self.assertTrue(n2_3_in_memory_third) self.assertTrue(n3_4_in_memory_second) self.assertTrue(n4_1_in_memory_first) def test_is_in_long_term_memory_method(self): self.memory.clear_memory() n1 = Node(1) n2 = Node(2) n3 = Node(3) n4 = Node(4) self.memory.add_to_memory(n1, 2) n1_2_in_memory_first = self.memory.is_in_long_term_memory(n1, 2) self.memory.add_to_memory(n2, 3) n1_2_in_memory_second = self.memory.is_in_long_term_memory(n1, 2) n2_3_in_memory_first = self.memory.is_in_long_term_memory(n2, 3) self.memory.add_to_memory(n3, 4) n1_2_in_memory_third = self.memory.is_in_long_term_memory(n1, 2) n2_3_in_memory_second = self.memory.is_in_long_term_memory(n2, 3) n3_4_in_memory_first = self.memory.is_in_long_term_memory(n3, 4) self.memory.add_to_memory(n4, 1) n1_2_in_memory_fourth = self.memory.is_in_long_term_memory(n1, 2) n2_3_in_memory_third = self.memory.is_in_long_term_memory(n2, 3) n3_4_in_memory_second = self.memory.is_in_long_term_memory(n3, 4) n4_1_in_memory_first = self.memory.is_in_long_term_memory(n4, 1) self.assertTrue(n1_2_in_memory_first) self.assertTrue(n1_2_in_memory_second) self.assertTrue(n2_3_in_memory_first) self.assertTrue(n1_2_in_memory_third) self.assertTrue(n2_3_in_memory_second) self.assertTrue(n3_4_in_memory_first) self.assertTrue(n1_2_in_memory_fourth) self.assertTrue(n2_3_in_memory_third) self.assertTrue(n3_4_in_memory_second) self.assertTrue(n4_1_in_memory_first)
class MbPA: def __init__(self, sess, args): with tf.variable_scope(args.model_name): self.args = args self.learning_rate = args.learning_rate self.session = sess self.x = tf.placeholder(tf.float32, shape=[None, 784], name="x") self.y = tf.placeholder(tf.float32, shape=[None, 10], name="y") # self.trainable = tf.placeholder(tf.int32, shape=(), name="trainable") self.memory_sample_batch = tf.placeholder( tf.int16, shape=(), name="memory_sample_batch") self.embed = self.embedding(self.x) self.M = Memory(args.memory_size, self.embed.get_shape()[-1], self.y.get_shape()[-1]) embs_and_values = tf.py_func(self.get_memory_sample, [self.memory_sample_batch], [tf.float64, tf.float64]) self.memory_batch_x = tf.to_float(embs_and_values[0]) self.memory_batch_y = tf.to_float(embs_and_values[1]) self.xa = tf.concat(values=[self.embed, self.memory_batch_x], axis=0) self.ya = tf.concat(values=[self.y, self.memory_batch_y], axis=0) self.y_ = self.output_network(self.xa) self.cross_entropy = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(labels=self.ya, logits=self.y_)) self.optim = tf.train.GradientDescentOptimizer( self.learning_rate).minimize(self.cross_entropy) self.correct_prediction = tf.equal(tf.argmax(self.ya, 1), tf.argmax(self.y_, 1)) self.accuracy = tf.reduce_mean( tf.cast(self.correct_prediction, tf.float32)) self.session.run(tf.global_variables_initializer()) def train(self, xs, ys, memory_sample_batch): # print(memory_sample_batch) embeds, _ = self.session.run([self.embed, self.optim], feed_dict={ self.x: xs, self.y: ys, self.memory_sample_batch: memory_sample_batch }) return embeds def test(self, xs_test, ys_test): acc = self.session.run(self.accuracy, feed_dict={ self.x: xs_test, self.y: ys_test, self.memory_sample_batch: 0 }) return acc def get_memory_sample(self, batch_size): x, y = self.M.sample(batch_size) return x, y def add_to_memory(self, xs, ys): if self.args.sample_add == "normal": self.M.add(xs, ys) elif self.args.sample_add == "lru": self.M.add_lru(xs, ys) elif self.args.sample_add == "rand": self.M.add_rand(xs, ys) elif self.args.sample_add == "knn": self.M.add_knn(xs, ys) elif self.args.sample_add == "knn_lru": self.M.add_knn_lru(xs, ys) else: raise Exception( "error sample adding type, pleace choose in ['normal', 'lru', 'rand']" ) @staticmethod def embedding(x): out = tf.reshape(x, [-1, 28, 28, 1]) # convs = [(16, 8, 4), (32, 4, 2)] # with tf.variable_scope("conv1"): # out = layers.convolution2d(inputs=out, # num_outputs=16, # kernel_size=8, # stride=4, # trainable=trainable) # out = tf.nn.relu(out) # out = tf.nn.max_pool(out, ksize=[1, 2, 3, 1], strides=[1, 2, 2, 1], padding="SAME") with tf.variable_scope("conv2"): # out = layers.convolution2d(inputs=out, # num_outputs=32, # kernel_size=4, # stride=2, # trainable=trainable) # out = tf.nn.relu(out) # out = tf.nn.max_pool(out, ksize=[1, 2, 3, 1], strides=[1, 2, 2, 1], padding="SAME") embed = layers.flatten(out) return embed @staticmethod def output_network(embed): out = embed with tf.variable_scope("fc_1"): out = layers.fully_connected(inputs=out, num_outputs=1024) out = tf.nn.relu(out) with tf.variable_scope("fc_2"): out = layers.fully_connected(inputs=out, num_outputs=10) return out