def main(_): action_reward_dict = {} policy_sess = tf.Session() #K.set_session(policy_sess) manager = NetworkManager(FLAGS, clip_rewards=CLIP_REWARDS) size = [len(LAYER_SIZES)] * NUM_LAYERS reward_space = np.zeros((size)) #print(reward_space.shape) for i in range(NUM_ENUM): for idx, val in np.ndenumerate(reward_space): action = [LAYER_SIZES[i] for i in idx] #print(action) with policy_sess.as_default(): _, acc = manager.get_rewards(model_fn, action) print(action, acc) acc = round(acc * JSON_SCALE, 2) action = str(tuple(action)) if action not in action_reward_dict: action_reward_dict[action] = [acc] else: action_reward_dict[action].append(acc) action_average_reward_dict = {} for k in action_reward_dict.keys(): action_average_reward_dict[k] = round(np.mean(action_reward_dict[k]), 2) with open('action_reward_dict.json', 'w') as f: json.dump(action_reward_dict, f) f.close() with open('action_average_reward_dict.json', 'w') as f: json.dump(action_average_reward_dict, f) f.close()
def main(_): CLIP_REWARDS = False value = [30,60,100,144] state_space = [v for v in itertools.product(value, repeat=3)] data = defaultdict(list) for itr in xrange(500): for state in state_space: states = list(state) manager = NetworkManager(FLAGS, clip_rewards=CLIP_REWARDS) reward, previous_acc = manager.get_rewards(model_fn, states) previous_acc = round(previous_acc*100000,2) data[str(state)].append(previous_acc) with open('data.json', 'w') as outfile: json.dump(data, outfile)
def main(_): CLIP_REWARDS = False #filter_space = [v for v in itertools.product(filter_val, repeat=3)] #stride_space = [v for v in itertools.product(stride_val, repeat=3)] #kernal_space = [v for v in itertools.product(kernel_val, repeat=3)] data = defaultdict(list) with open('result.txt', 'w') as out: for state in states: print(state) manager = NetworkManager(FLAGS, clip_rewards=CLIP_REWARDS) reward, previous_acc = manager.get_rewards(model_fn_cnn, state) previous_acc = round(previous_acc * 100000, 2) print(previous_acc) data[str(state)].append(previous_acc) out.write("{} {}\n".format(state, previous_acc)) with open('data.json', 'w') as outfile: json.dump(data, outfile)
def main(_): CLIP_REWARDS = False data = defaultdict(list) with open('main_result.txt', 'w') as out: for ite in xrange(3): ite += 1 print('outter iteration:', ite) iteration = 0 for state in states: iteration += 1 print(iteration, state) manager = NetworkManager(FLAGS, clip_rewards=CLIP_REWARDS) reward, previous_acc = manager.get_rewards(model_fn_cnn, state) previous_acc = round(previous_acc * 100000, 2) print(previous_acc) data[str(state)].append(previous_acc) out.write("{} {}\n".format(state, previous_acc)) with open('main_sample.json', 'w') as outfile: json.dump(data, outfile)
def main(_): # create a shared session between Keras and Tensorflow policy_sess = tf.Session() K.set_session(policy_sess) NUM_LAYERS = 3 # number of layers of the state space MAX_TRIALS = 250 # maximum number of models generated MAX_EPOCHS = 60 # maximum number of epochs to train BATCHSIZE = 100 # batchsize EXPLORATION = 0.5 # high exploration for the first 1000 steps REGULARIZATION = 1e-3 # regularization strength CONTROLLER_CELLS = 32 # number of cells in RNN controller CLIP_REWARDS = False # clip rewards in the [-0.05, 0.05] range RESTORE_CONTROLLER = True # restore controller to continue training # construct a state space state_space = StateSpace() # add states #state_space.add_state(name='kernel', values=[3]) state_space.add_state(name='filters', values=[30, 60, 100, 144]) #state_space.add_state(name='stride', values=[1]) # print the state space being searched state_space.print_state_space() previous_acc = 0.0 total_reward = 0.0 with policy_sess.as_default(): # create the Controller and build the internal policy network controller = Controller(policy_sess, NUM_LAYERS, state_space, reg_param=REGULARIZATION, exploration=EXPLORATION, controller_cells=CONTROLLER_CELLS, restore_controller=RESTORE_CONTROLLER) print('done') # create the Network Manager manager = NetworkManager(FLAGS, clip_rewards=CLIP_REWARDS) # get an initial random state space if controller needs to predict an # action from the initial state state = state_space.get_random_state_space(NUM_LAYERS) print("Initial Random State : ", state_space.parse_state_space_list(state)) #print() # train for number of trails for trial in range(MAX_TRIALS): with policy_sess.as_default(): actions = controller.get_action( state) # get an action for the previous state # print the action probabilities state_space.print_actions(actions) print("Predicted actions : ", state_space.parse_state_space_list(actions)) # build a model, train and get reward and accuracy from the network manager reward, previous_acc = manager.get_rewards( model_fn_cnn, state_space.parse_state_space_list(actions)) print("Rewards : ", reward, "Accuracy : ", previous_acc) with policy_sess.as_default(): total_reward += reward print("Total reward : ", total_reward) # actions and states are equivalent, save the state and reward state = actions controller.store_rollout(state, reward) # train the controller on the saved state and the discounted rewards loss = controller.train_step() print("Trial %d: Controller loss : %0.6f" % (trial + 1, loss)) # write the results of this trial into a file with open('train_history.csv', mode='a+') as f: data = [previous_acc, reward] data.extend(state_space.parse_state_space_list(state)) writer = csv.writer(f) writer.writerow(data) print() print("Total Reward : ", total_reward)
with policy_sess.as_default(): # create the Controller and build the internal policy network controller = Controller(policy_sess, NUM_LAYERS, state_space, reg_param=REGULARIZATION, exploration=EXPLORATION, controller_cells=CONTROLLER_CELLS, embedding_dim=EMBEDDING_DIM, restore_controller=RESTORE_CONTROLLER) # create the Network Manager manager = NetworkManager(dataset, epochs=MAX_EPOCHS, child_batchsize=CHILD_BATCHSIZE, clip_rewards=CLIP_REWARDS, acc_beta=ACCURACY_BETA) # get an initial random state space if controller needs to predict an # action from the initial state state = state_space.get_random_state_space(NUM_LAYERS) print("Initial Random State : ", state_space.parse_state_space_list(state)) print() # clear the previous files controller.remove_files() # train for number of trails for trial in range(MAX_TRIALS): with policy_sess.as_default():
dataset = [x_train, y_train, x_test, y_test] # pack the dataset for the NetworkManager with policy_sess.as_default(): # create the Encoder and build the internal policy network controller = Encoder(policy_sess, state_space, B=B, K=K_, train_iterations=RNN_TRAINING_EPOCHS, reg_param=REGULARIZATION, controller_cells=CONTROLLER_CELLS, restore_controller=RESTORE_CONTROLLER) # create the Network Manager manager = NetworkManager(dataset, epochs=MAX_EPOCHS, batchsize=BATCHSIZE) print() # train for number of trails for trial in range(B): with policy_sess.as_default(): K.set_session(policy_sess) if trial == 0: k = None else: k = K_ actions = controller.get_actions( top_k=k) # get all actions for the previous state
dataset = [x_train, y_train, x_test, y_test] # pack the dataset for the NetworkManager previous_acc = 0.0 total_reward = 0.0 with policy_sess.as_default(): # create the Controller and build the internal policy network controller = Controller(policy_sess, NUM_LAYERS, state_space, reg_param=REGULARIZATION, exploration=EXPLORATION, controller_cells=CONTROLLER_CELLS, restore_controller=RESTORE_CONTROLLER) # create the Network Manager manager = NetworkManager(dataset, epochs=MAX_EPOCHS, batchsize=BATCHSIZE, clip_rewards=CLIP_REWARDS) # get an initial random state space if controller needs to predict an # action from the initial state state = state_space.get_random_state_space(NUM_LAYERS) print("Initial Random State : ", state_space.parse_state_space_list(state)) print() # train for number of trails for trial in range(MAX_TRIALS): with policy_sess.as_default(): K.set_session(policy_sess) actions = controller.get_action(state) # get an action for the previous state # print the action probabilities state_space.print_actions(actions)
sip.setapi(name, API_VERSION) from PyQt4 import QtCore from info import ServerInfo from manager import NetworkManager from transfer_thread import TransferThread global ess ess = ServerInfo() ess.fromAddr('misura.expertsystemsolutions.it:80') ess.name = 'Simulation Server' ess.host = 'misura.expertsystemsolutions.it' ess.serial = 'ESimulServ' manager = NetworkManager() def simpleConnection(addr, user='', password='', mac='', save=True): addr = str(addr) logging.debug('simpleConnection', addr, user, password, mac, save) if '@' in addr: usrpass, addr = addr.replace('https://', '').split('@') addr = addr.lower().replace('/rpc', '') if ':' in usrpass: user, password = usrpass.split(':') else: user = usrpass if not addr.startswith('https'): addr = 'https://' + addr if not addr.endswith('/RPC'):
def main_training(): # Number of GPUs available. Use 0 for CPU mode. ngpu = torch.cuda.device_count() # Decide which device we want to run on device = torch.device("cuda:0" if ( torch.cuda.is_available() and ngpu > 0) else "cpu") train_loader, val_loader, test_loader = get_dataset( CHILD_BATCHSIZE, '/tmp/datasets/cifar-10') dataloaders = [train_loader, val_loader, test_loader] # construct a state space state_space = StateSpace() # add states state_space.add_state(name='kernel', values=[1, 3, 5, 7]) state_space.add_state(name='filters', values=[24, 36, 48, 64]) previous_acc = 0.0 total_reward = 0.0 controller = Controller(NUM_LAYERS, state_space) manager = NetworkManager(dataloaders, device, epochs=2) # get an initial random state space if controller needs to predict an action from the initial state state = state_space.get_random_state_space(NUM_LAYERS) print("Initial Random State : ", state_space.parse_state_space_list(state)) for trial in range(MAX_TRIALS): actions, prob_actions = controller.get_action( state) # get an action for the previous state # print the action probabilities state_space.print_actions(actions) print("Predicted actions : ", state_space.parse_state_space_list(actions)) # build a model, train and get reward and accuracy from the network manager model = ChildNetwork(state_space.parse_state_space_list(actions)) reward, previous_acc = manager.get_rewards(model) print("Rewards : ", reward, "Accuracy : ", previous_acc) total_reward += reward print("Total reward : ", total_reward) # actions and states are equivalent, save the state and reward state = actions prob_state = prob_actions controller.store_rollout(state, reward, prob_state) # train the controller on the saved state and the discounted rewards loss = controller.update_policy() print("Trial %d: Controller loss : %0.6f" % (trial + 1, loss)) # write the results of this trial into a file with open('train_history.csv', mode='a+') as f: data = [previous_acc, reward] data.extend(state_space.parse_state_space_list(state)) writer = csv.writer(f) writer.writerow(data) print() print("Total Reward : ", total_reward)
with policy_sess.as_default(): # create the Encoder and build the internal policy network controller = Encoder(policy_sess, state_space, B=B, K=K_, train_iterations=RNN_TRAINING_EPOCHS, reg_param=REGULARIZATION, controller_cells=CONTROLLER_CELLS, restore_controller=RESTORE_CONTROLLER) # create the Network Manager manager = NetworkManager(dataset, epochs=MAX_EPOCHS, batchsize=BATCHSIZE, cell_number=NORMAL_CELL_NUMBER, filters=FIRST_LAYER_FILTERS) print() #reset all shared weights if os.path.isdir('shared_weights') and CLEAR_SHARE_WEIGHTS: shutil.rmtree('shared_weights') # train for number of trails for trial in range(B): with policy_sess.as_default(): K.set_session(policy_sess) if trial == 0: k = None
def __init__(self, *args, **kwargs): self.state = ToolState.EMPTY # dimensions of the area where nodes can be placed self.workAreaXDim = 720 self.workAreaYDim = 520 self.workAreaXPos = 40 self.workAreaYPos = 40 # multiple choice bubble menu for node addition self.defaultBubble = Bubble(size_hint=(None, None), size=(250, 40)) self.newComputerButton = Button(text="Nowy komputer") self.newRouterButton = Button(text="Nowy router") self.defaultBubble.add_widget(self.newComputerButton) self.defaultBubble.add_widget(self.newRouterButton) # coordinates of node-button to be placed self.pendingNodePosX = 0 self.pendingNodePosY = 0 # multiple choice bubble menu for node editing/removal self.nodeBubble = Bubble(size_hint=(None, None), size=(250, 40)) self.newConnButton = Button(text="Dodaj połączenie") self.deleteNodeButton = Button(text="Usuń węzeł") self.nodeBubble.add_widget(self.newConnButton) self.nodeBubble.add_widget(self.deleteNodeButton) # reference to button being edited/removed self.pendingNodeRef = Button() # global state - adding connection or not self.isInConnectionMode = False self.netManager = NetworkManager(self.__deleteLine, self.appendLog, self.animatePacket) # define widgets of side panel and add them to window self.sidePanelTabbedPanel = TabbedPanel(do_default_tab=False, size=(200, 600), pos=(800, 0), background_color=(0, 0, 0, 0), tab_width=100) # define widgets of log display and add them to window self.sidePanelLogTab = TabbedPanelItem(text="Symulacja") self.sidePanelLogLayout = GridLayout(cols=1, spacing=10, size_hint=(None, None), size=(200, 550)) self.logField = TextInput(padding=10, readonly=True, size_hint_max_y=None, size_hint=(None, None), size=(190, 550)) self.logField.bind(minimum_height=self.logField.setter('height')) self.sidePanelLogScrollView = ScrollView( size_hint=(None, None), size=(200, 450), scroll_type=['bars'], bar_width=10, bar_inactive_color=[1, 1, 1, .8], bar_color=[0.3, 1, 1, 1]) self.sidePanelLogScrollView.add_widget(self.logField) self.sidePanelLogLayout.add_widget(self.sidePanelLogScrollView) self.sidePanelLogTab.add_widget(self.sidePanelLogLayout) self.sidePanelTabbedPanel.add_widget(self.sidePanelLogTab) # A welcome log self.appendLog('A new workspace has been created.') # define widgets of node edit panel to be added to window later # when a node is selected, add sidePanelNodeTab with the proper tab name to sidePanelTabbedPanel # and fill sidePanelNodeTab with content - interface configuration form self.sidePanelNodeTab = TabbedPanelItem() self.sidePanelNodeLayout = GridLayout(cols=1, spacing=5, size_hint=(None, None), size=(200, 550)) self.sidePanelNodeLayout.bind( minimum_height=self.sidePanelNodeLayout.setter('height')) self.sidePanelNodeScrollView = ScrollView( size_hint=(None, None), size=(200, 550), pos=(800, 0), scroll_type=['bars'], bar_width=10, bar_inactive_color=[1, 1, 1, .8], bar_color=[1, 1, 1, 1]) self.sidePanelNodeTab.add_widget(self.sidePanelNodeScrollView) self.sidePanelNodeScrollView.add_widget(self.sidePanelNodeLayout) # define simulation-related buttons and add them to the window self.toggleSimButton = Button(text="Symuluj") self.addPacketButton = Button(text="Nowy pakiet", on_press=self.on_new_packet) self.stepSimButton = Button(text=">|", on_press=self.on_step) self.playSimButton = Button(text=">") self.simButtonLayout = BoxLayout() self.sidePanelLogLayout.add_widget(self.addPacketButton) self.sidePanelLogLayout.add_widget(self.toggleSimButton) self.sidePanelLogLayout.add_widget(self.simButtonLayout) self.simButtonLayout.add_widget(self.playSimButton) self.simButtonLayout.add_widget(self.stepSimButton) self.packetNodes = [] # set window color Window.clearcolor = (0.7, 0.7, 0.7, 1) super(Demoer, self).__init__(*args, **kwargs) self.add_widget(self.sidePanelTabbedPanel) # create delimiter line around the work area self.canvas.before.add( Line(rectangle=(self.workAreaXPos, self.workAreaYPos, self.workAreaXDim, self.workAreaYDim), width=0.5)) # create background square behind side panel with self.canvas.before: Color(0.3, 0.3, 0.3, 1) Rectangle(pos=(2 * self.workAreaXPos + self.workAreaXDim, 0), size=(200, 600)) Color(1, 1, 1, 1)
with policy_sess.as_default(): # create the Controller and build the internal policy network controller = Controller(policy_sess, NUM_LAYERS, state_space, reg_param=REGULARIZATION, exploration=EXPLORATION, controller_cells=CONTROLLER_CELLS, embedding_dim=EMBEDDING_DIM, restore_controller=RESTORE_CONTROLLER) # create the Network Manager manager = NetworkManager(dataset, epochs=MAX_EPOCHS, child_batchsize=CHILD_BATCHSIZE, clip_rewards=CLIP_REWARDS, acc_beta=ACCURACY_BETA, model_dir="%s/%s" % (pb_dir, MODEL_NAME)) # get an initial random state space if controller needs to predict an # action from the initial state state = state_space.get_random_state_space(NUM_LAYERS) print("Initial Random State : ", state_space.parse_state_space_list(state)) print() # clear the previous files controller.remove_files() # train for number of trails for trial in range(MAX_TRIALS): with policy_sess.as_default():