def trainModelParallel(inputData): settingsFileName = inputData[0] settings = inputData[1] np.random.seed(int(settings['random_seed'])) import os if ('THEANO_FLAGS' in os.environ): os.environ['THEANO_FLAGS'] = os.environ[ 'THEANO_FLAGS'] + "mode=FAST_RUN,device=" + settings[ 'training_processor_type'] + ",floatX=" + settings['float_type'] else: os.environ['THEANO_FLAGS'] = "mode=FAST_RUN,device=" + settings[ 'training_processor_type'] + ",floatX=" + settings['float_type'] import keras.backend keras.backend.set_floatx(settings['float_type']) print("K.floatx()", keras.backend.floatx()) from ModelEvaluation import SimWorker, evalModelParrallel, collectExperience, simEpoch, evalModel, simModelParrallel from model.ModelUtil import validBounds, fixBounds, anneal_value from model.LearningAgent import LearningAgent, LearningWorker from util.SimulationUtil import validateSettings from util.SimulationUtil import createEnvironment from util.SimulationUtil import createRLAgent from util.SimulationUtil import createActor, getAgentName from util.SimulationUtil import getDataDirectory, createForwardDynamicsModel, createSampler from util.ExperienceMemory import ExperienceMemory from RLVisualize import RLVisualize from NNVisualize import NNVisualize #from sim.PendulumEnvState import PendulumEnvState #from sim.PendulumEnv import PendulumEnv #from sim.BallGame2DEnv import BallGame2DEnv settings = validateSettings(settings) model_type = settings["model_type"] directory = getDataDirectory(settings) if not os.path.exists(directory): os.makedirs(directory) # copy settings file out_file_name = directory + os.path.basename(settingsFileName) print("Saving settings file with data: ", out_file_name) out_file = open(out_file_name, 'w') out_file.write(json.dumps(settings, indent=4)) out_file.close() ### Try and save algorithm and model files for reference if "." in settings['model_type']: ### convert . to / and copy file over file_name = settings['model_type'] k = file_name.rfind(".") file_name = file_name[:k] file_name_read = file_name.replace(".", "/") file_name_read = file_name_read + ".py" print("model file name:", file_name) print("os.path.basename(file_name): ", os.path.basename(file_name)) file = open(file_name_read, 'r') out_file = open(directory + file_name + ".py", 'w') out_file.write(file.read()) file.close() out_file.close() if "." in settings['agent_name']: ### convert . to / and copy file over file_name = settings['agent_name'] k = file_name.rfind(".") file_name = file_name[:k] file_name_read = file_name.replace(".", "/") file_name_read = file_name_read + ".py" print("model file name:", file_name) print("os.path.basename(file_name): ", os.path.basename(file_name)) file = open(file_name_read, 'r') out_file = open(directory + file_name + ".py", 'w') out_file.write(file.read()) file.close() out_file.close() if (settings['train_forward_dynamics']): if "." in settings['forward_dynamics_model_type']: ### convert . to / and copy file over file_name = settings['forward_dynamics_model_type'] k = file_name.rfind(".") file_name = file_name[:k] file_name_read = file_name.replace(".", "/") file_name_read = file_name_read + ".py" print("model file name:", file_name) print("os.path.basename(file_name): ", os.path.basename(file_name)) file = open(file_name_read, 'r') out_file = open(directory + file_name + ".py", 'w') out_file.write(file.read()) file.close() out_file.close() rounds = settings["rounds"] epochs = settings["epochs"] epsilon = settings["epsilon"] discount_factor = settings["discount_factor"] reward_bounds = np.array(settings["reward_bounds"]) batch_size = settings["batch_size"] train_on_validation_set = settings["train_on_validation_set"] state_bounds = np.array(settings['state_bounds']) discrete_actions = np.array(settings['discrete_actions']) #9*6 num_actions = discrete_actions.shape[0] # number of rows print("Sim config file name: " + str(settings["sim_config_file"])) action_space_continuous = settings['action_space_continuous'] if (settings['num_available_threads'] == 1): input_anchor_queue = multiprocessing.Queue( settings['queue_size_limit']) input_anchor_queue_eval = multiprocessing.Queue( settings['queue_size_limit']) output_experience_queue = multiprocessing.Queue( settings['queue_size_limit']) eval_episode_data_queue = multiprocessing.Queue( settings['queue_size_limit']) else: input_anchor_queue = multiprocessing.Queue(settings['epochs']) input_anchor_queue_eval = multiprocessing.Queue(settings['epochs']) output_experience_queue = multiprocessing.Queue( settings['queue_size_limit']) eval_episode_data_queue = multiprocessing.Queue( settings['eval_epochs']) if (settings['on_policy']): ## So that off policy agent does not learn output_experience_queue = None sim_work_queues = [] action_space_continuous = settings['action_space_continuous'] if action_space_continuous: action_bounds = np.array(settings["action_bounds"], dtype=float) ### Using a wrapper for the type of actor now actor = createActor(settings['environment_type'], settings, None) exp_val = None if (not validBounds(action_bounds)): # Check that the action bounds are spcified correctly print("Action bounds invalid: ", action_bounds) sys.exit() if (not validBounds(state_bounds)): # Probably did not collect enough bootstrapping samples to get good state bounds. print("State bounds invalid: ", state_bounds) state_bounds = fixBounds(np.array(state_bounds)) bound_fixed = validBounds(state_bounds) print("State bounds fixed: ", bound_fixed) sys.exit() if (not validBounds(reward_bounds)): print("Reward bounds invalid: ", reward_bounds) sys.exit() if settings['action_space_continuous']: experience = ExperienceMemory(len(state_bounds[0]), len(action_bounds[0]), settings['expereince_length'], continuous_actions=True, settings=settings) else: experience = ExperienceMemory(len(state_bounds[0]), 1, settings['expereince_length']) experience.setSettings(settings) if settings['visualize_learning']: title = settings['agent_name'] k = title.rfind(".") + 1 if (k > len(title)): ## name does not contain a . k = 0 title = title[k:] rlv = RLVisualize(title=title + " agent on " + str(settings['environment_type']), settings=settings) rlv.setInteractive() rlv.init() if (settings['train_forward_dynamics']): if settings['visualize_learning']: title = settings['forward_dynamics_model_type'] k = title.rfind(".") + 1 if (k > len(title)): ## name does not contain a . k = 0 title = title[k:] nlv = NNVisualize(title=str("Dynamics Model") + " with " + title, settings=settings) nlv.setInteractive() nlv.init() if (settings['train_reward_predictor']): if settings['visualize_learning']: title = settings['forward_dynamics_model_type'] k = title.rfind(".") + 1 if (k > len(title)): ## name does not contain a . k = 0 title = title[k:] rewardlv = NNVisualize(title=str("Reward Model") + " with " + title, settings=settings) rewardlv.setInteractive() rewardlv.init() if (settings['debug_critic']): #True criticLosses = [] criticRegularizationCosts = [] if (settings['visualize_learning']): title = settings['agent_name'] k = title.rfind(".") + 1 if (k > len(title)): ## name does not contain a . k = 0 title = title[k:] critic_loss_viz = NNVisualize(title=str("Critic Loss") + " with " + title) critic_loss_viz.setInteractive() critic_loss_viz.init() critic_regularization_viz = NNVisualize( title=str("Critic Reg Cost") + " with " + title) critic_regularization_viz.setInteractive() critic_regularization_viz.init() if (settings['debug_actor']): # True actorLosses = [] actorRegularizationCosts = [] if (settings['visualize_learning']): #False title = settings['agent_name'] k = title.rfind(".") + 1 if (k > len(title)): ## name does not contain a . k = 0 title = title[k:] actor_loss_viz = NNVisualize(title=str("Actor Loss") + " with " + title) actor_loss_viz.setInteractive() actor_loss_viz.init() actor_regularization_viz = NNVisualize( title=str("Actor Reg Cost") + " with " + title) actor_regularization_viz.setInteractive() actor_regularization_viz.init() model = createRLAgent(settings['agent_name'], state_bounds, discrete_actions, reward_bounds, settings) #return a model class forwardDynamicsModel = None if (settings['train_forward_dynamics']): #False if (settings['forward_dynamics_model_type'] == "SingleNet"): print( "Creating forward dynamics network: Using single network model" ) forwardDynamicsModel = createForwardDynamicsModel(settings, state_bounds, action_bounds, None, None, agentModel=model) else: print("Creating forward dynamics network") forwardDynamicsModel = createForwardDynamicsModel(settings, state_bounds, action_bounds, None, None, agentModel=None) forwardDynamicsModel.setActor(actor) forwardDynamicsModel.init(len(state_bounds[0]), len(action_bounds[0]), state_bounds, action_bounds, actor, None, settings) (agent, learning_workers) = createLearningAgent(settings, output_experience_queue, state_bounds, action_bounds, reward_bounds) masterAgent = agent ### These are the workers for training (sim_workers, sim_work_queues) = createSimWorkers( settings, input_anchor_queue, output_experience_queue, eval_episode_data_queue, model, forwardDynamicsModel, exp_val, state_bounds, action_bounds, reward_bounds) eval_sim_workers = sim_workers eval_sim_work_queues = sim_work_queues if ('override_sim_env_id' in settings and (settings['override_sim_env_id'] != False)): #True (eval_sim_workers, eval_sim_work_queues) = createSimWorkers( settings, input_anchor_queue_eval, output_experience_queue, eval_episode_data_queue, model, forwardDynamicsModel, exp_val, state_bounds, action_bounds, reward_bounds, default_sim_id=settings['override_sim_env_id']) # id=1 else: input_anchor_queue_eval = input_anchor_queue best_eval = -100000000.0 best_dynamicsLosses = best_eval * -1.0 values = [] discounted_values = [] bellman_error = [] reward_over_epoc = [] dynamicsLosses = [] dynamicsRewardLosses = [] for lw in learning_workers: print("Learning worker") print(lw) if (int(settings["num_available_threads"]) > 1): for sw in sim_workers: print("Sim worker") print(sw) sw.start() if ('override_sim_env_id' in settings and (settings['override_sim_env_id'] != False)): for sw in eval_sim_workers: print("Sim worker") print(sw) sw.start() ## This needs to be done after the simulation worker processes are created exp_val = createEnvironment(settings["forwardDynamics_config_file"], settings['environment_type'], settings, render=settings['shouldRender'], index=0) exp_val.setActor(actor) exp_val.getActor().init() exp_val.init() ### This is for a single-threaded Synchronous sim only. if (int(settings["num_available_threads"]) == 1 ): # This is okay if there is one thread only... sim_workers[0].setEnvironment(exp_val) sim_workers[0].start() if ('override_sim_env_id' in settings and (settings['override_sim_env_id'] != False)): eval_sim_workers[0].setEnvironment(exp_val) eval_sim_workers[0].start() masterAgent.setPolicy(model) if (settings['train_forward_dynamics']): masterAgent.setForwardDynamics(forwardDynamicsModel) tmp_p = 1.0 message = {} if (settings['load_saved_model']): tmp_p = settings['min_epsilon'] data = ('Update_Policy', tmp_p, model.getStateBounds(), model.getActionBounds(), model.getRewardBounds(), masterAgent.getPolicy().getNetworkParameters()) if (settings['train_forward_dynamics']): data = ('Update_Policy', tmp_p, model.getStateBounds(), model.getActionBounds(), model.getRewardBounds(), masterAgent.getPolicy().getNetworkParameters(), masterAgent.getForwardDynamics().getNetworkParameters()) message['type'] = 'Update_Policy' message['data'] = data for m_q in sim_work_queues: print("trainModel: Sending current network parameters: ", m_q) m_q.put(message) if (int(settings["num_available_threads"]) == 1): experience, state_bounds, reward_bounds, action_bounds = collectExperience( actor, exp_val, model, settings, sim_work_queues=None, eval_episode_data_queue=None ) #experience: state, action, nextstate, rewards, else: if (settings['on_policy']): experience, state_bounds, reward_bounds, action_bounds = collectExperience( actor, None, model, settings, sim_work_queues=sim_work_queues, eval_episode_data_queue=eval_episode_data_queue) else: experience, state_bounds, reward_bounds, action_bounds = collectExperience( actor, None, model, settings, sim_work_queues=input_anchor_queue, eval_episode_data_queue=eval_episode_data_queue) masterAgent.setExperience(experience) if ('keep_seperate_fd_exp_buffer' in settings and (settings['keep_seperate_fd_exp_buffer'])): masterAgent.setFDExperience(copy.deepcopy(experience)) if (not validBounds(action_bounds)): # Check that the action bounds are spcified correctly print("Action bounds invalid: ", action_bounds) sys.exit() if (not validBounds(state_bounds)): # Probably did not collect enough bootstrapping samples to get good state bounds. print("State bounds invalid: ", state_bounds) state_bounds = fixBounds(np.array(state_bounds)) bound_fixed = validBounds(state_bounds) print("State bounds fixed: ", bound_fixed) if (not validBounds(reward_bounds)): print("Reward bounds invalid: ", reward_bounds) sys.exit() print("Reward History: ", experience._reward_history) print("Action History: ", experience._action_history) print("Action Mean: ", np.mean(experience._action_history)) print("Experience Samples: ", (experience.samples())) if (settings["save_experience_memory"]): print("Saving initial experience memory") file_name = directory + getAgentName() + "_expBufferInit.hdf5" experience.saveToFile(file_name) if (settings['load_saved_model'] or (settings['load_saved_model'] == 'network_and_scales')): ## Transfer learning experience.setStateBounds(copy.deepcopy(model.getStateBounds())) experience.setRewardBounds(copy.deepcopy(model.getRewardBounds())) experience.setActionBounds(copy.deepcopy(model.getActionBounds())) model.setSettings(settings) else: ## Normal model.setStateBounds(state_bounds) model.setActionBounds(action_bounds) model.setRewardBounds(reward_bounds) experience.setStateBounds(copy.deepcopy(model.getStateBounds())) experience.setRewardBounds(copy.deepcopy(model.getRewardBounds())) experience.setActionBounds(copy.deepcopy(model.getActionBounds())) masterAgent_message_queue = multiprocessing.Queue(settings['epochs']) if (settings['train_forward_dynamics']): if (not settings['load_saved_model']): forwardDynamicsModel.setStateBounds(state_bounds) forwardDynamicsModel.setActionBounds(action_bounds) forwardDynamicsModel.setRewardBounds(reward_bounds) masterAgent.setForwardDynamics(forwardDynamicsModel) ## Now everything related to the exp memory needs to be updated bellman_errors = [] masterAgent.setPolicy(model) print("Master agent state bounds: ", repr(masterAgent.getPolicy().getStateBounds())) for sw in sim_workers: # Need to update parameter bounds for models print("exp: ", sw._exp) print("sw modle: ", sw._model.getPolicy()) ## If not on policy if (not settings['on_policy']): for lw in learning_workers: lw._agent.setPolicy(model) lw.setMasterAgentMessageQueue(masterAgent_message_queue) lw.updateExperience(experience) print("ls policy: ", lw._agent.getPolicy()) lw.start() tmp_p = 1.0 if (settings['load_saved_model']): tmp_p = settings['min_epsilon'] data = ('Update_Policy', tmp_p, model.getStateBounds(), model.getActionBounds(), model.getRewardBounds(), masterAgent.getPolicy().getNetworkParameters()) if (settings['train_forward_dynamics']): data = ('Update_Policy', tmp_p, model.getStateBounds(), model.getActionBounds(), model.getRewardBounds(), masterAgent.getPolicy().getNetworkParameters(), masterAgent.getForwardDynamics().getNetworkParameters()) message['type'] = 'Update_Policy' message['data'] = data for m_q in sim_work_queues: print("trainModel: Sending current network parameters: ", m_q) m_q.put(message) del model ## Give gloabl access to processes to they can be terminated when ctrl+c is pressed global sim_processes sim_processes = sim_workers global learning_processes learning_processes = learning_workers global _input_anchor_queue _input_anchor_queue = input_anchor_queue global _output_experience_queue _output_experience_queue = output_experience_queue global _eval_episode_data_queue _eval_episode_data_queue = eval_episode_data_queue global _sim_work_queues _sim_work_queues = sim_work_queues trainData = {} trainData["mean_reward"] = [] trainData["std_reward"] = [] trainData["mean_bellman_error"] = [] trainData["std_bellman_error"] = [] trainData["mean_discount_error"] = [] trainData["std_discount_error"] = [] trainData["mean_forward_dynamics_loss"] = [] trainData["std_forward_dynamics_loss"] = [] trainData["mean_forward_dynamics_reward_loss"] = [] trainData["std_forward_dynamics_reward_loss"] = [] trainData["mean_eval"] = [] trainData["std_eval"] = [] trainData["mean_critic_loss"] = [] trainData["std_critic_loss"] = [] trainData["mean_critic_regularization_cost"] = [] trainData["std_critic_regularization_cost"] = [] trainData["mean_actor_loss"] = [] trainData["std_actor_loss"] = [] trainData["mean_actor_regularization_cost"] = [] trainData["std_actor_regularization_cost"] = [] trainData["anneal_p"] = [] if (False): print("State Bounds:", masterAgent.getStateBounds()) print("Action Bounds:", masterAgent.getActionBounds()) print("Exp State Bounds: ", experience.getStateBounds()) print("Exp Action Bounds: ", experience.getActionBounds()) print("Starting first round") if (settings['on_policy']): sim_epochs_ = epochs for round_ in range( 0, rounds): #annel value # the parameter of greedy exploration if ('annealing_schedule' in settings and (settings['annealing_schedule'] != False)): p = anneal_value(float(round_ / rounds), settings_=settings) else: p = ((settings['initial_temperature'] / math.log(round_ + 2))) p = max(settings['min_epsilon'], min(settings['epsilon'], p)) # Keeps it between 1.0 and 0.2 if (settings['load_saved_model']): p = settings['min_epsilon'] for epoch in range(epochs): if (settings['on_policy']): out = simModelParrallel( sw_message_queues=sim_work_queues, model=masterAgent, settings=settings, eval_episode_data_queue=eval_episode_data_queue, anchors=settings['num_on_policy_rollouts']) ( tuples, discounted_sum, q_value, evalData ) = out # tuples = states, actions, result_states, rewards, falls, G_ts, advantage, exp_actions (__states, __actions, __result_states, __rewards, __falls, __G_ts, advantage__, exp_actions__) = tuples for i in range(1): masterAgent.train(_states=__states, _actions=__actions, _rewards=__rewards, _result_states=__result_states, _falls=__falls, _advantage=advantage__, _exp_actions=exp_actions__) if (('anneal_on_policy' in settings) and settings['anneal_on_policy']): p_tmp_ = p else: p_tmp_ = 1.0 data = ('Update_Policy', p_tmp_, masterAgent.getStateBounds(), masterAgent.getActionBounds(), masterAgent.getRewardBounds(), masterAgent.getPolicy().getNetworkParameters()) message = {} message['type'] = 'Update_Policy' message['data'] = data if (settings['train_forward_dynamics']): data = ('Update_Policy', p_tmp_, masterAgent.getStateBounds(), masterAgent.getActionBounds(), masterAgent.getRewardBounds(), masterAgent.getPolicy().getNetworkParameters(), masterAgent.getForwardDynamics(). getNetworkParameters()) message['data'] = data for m_q in sim_work_queues: ## block on full queue m_q.put(message) if ('override_sim_env_id' in settings and (settings['override_sim_env_id'] != False)): for m_q in eval_sim_work_queues: ## block on full queue m_q.put(message) else: episodeData = {} episodeData['data'] = epoch episodeData['type'] = 'sim' input_anchor_queue.put(episodeData) if masterAgent.getExperience().samples( ) >= batch_size: #更新policy网络 states, actions, result_states, rewards, falls, G_ts, exp_actions = masterAgent.getExperience( ).get_batch(batch_size) error = masterAgent.bellman_error(states, actions, rewards, result_states, falls) bellman_errors.append(error) if (settings['debug_critic']): loss__ = masterAgent.getPolicy()._get_critic_loss( ) # uses previous call batch data criticLosses.append(loss__) regularizationCost__ = masterAgent.getPolicy( )._get_critic_regularization() criticRegularizationCosts.append(regularizationCost__) if (settings['debug_actor']): #True loss__ = masterAgent.getPolicy()._get_actor_loss( ) # uses previous call batch data actorLosses.append(loss__) regularizationCost__ = masterAgent.getPolicy( )._get_actor_regularization() actorRegularizationCosts.append(regularizationCost__) if not all(np.isfinite(error)): print( "States: " + str(states) + " ResultsStates: " + str(result_states) + " Rewards: " + str(rewards) + " Actions: " + str(actions) + " Falls: ", str(falls)) print("Bellman Error is Nan: " + str(error) + str(np.isfinite(error))) sys.exit() error = np.mean(np.fabs(error)) if error > 10000: print("Error to big: ") print(states, actions, rewards, result_states) if (settings['train_forward_dynamics']): #False dynamicsLoss = masterAgent.getForwardDynamics( ).bellman_error(states, actions, result_states, rewards) dynamicsLoss = np.mean(np.fabs(dynamicsLoss)) #fabs:计算绝对值 dynamicsLosses.append(dynamicsLoss) if (settings['train_reward_predictor']): dynamicsRewardLoss = masterAgent.getForwardDynamics( ).reward_error(states, actions, result_states, rewards) dynamicsRewardLoss = np.mean( np.fabs(dynamicsRewardLoss)) dynamicsRewardLosses.append(dynamicsRewardLoss) if (settings['train_forward_dynamics']): print("Round: " + str(round_) + " Epoch: " + str(epoch) + " p: " + str(p) + " With mean reward: " + str(np.mean(rewards)) + " bellman error: " + str(error) + " ForwardPredictionLoss: " + str(dynamicsLoss)) else: print("Round: " + str(round_) + " Epoch: " + str(epoch) + " p: " + str(p) + " With mean reward: " + str(np.mean(rewards)) + " bellman error: " + str(error)) if (settings["print_levels"][settings["print_level"]] >= settings["print_levels"]['train']): print("Master agent experience size: " + str(masterAgent.getExperience().samples())) if (not settings['on_policy']): ## There could be stale policy parameters in here, use the last set put in the queue data = None while (not masterAgent_message_queue.empty()): ## Don't block try: data = masterAgent_message_queue.get(False) except Exception as inst: pass if (not (data == None)): masterAgent.setExperience(data[0]) masterAgent.getPolicy().setNetworkParameters(data[1]) masterAgent.setStateBounds( masterAgent.getExperience().getStateBounds()) masterAgent.setActionBounds( masterAgent.getExperience().getActionBounds()) masterAgent.setRewardBounds( masterAgent.getExperience().getRewardBounds()) if (settings['train_forward_dynamics']): masterAgent.getForwardDynamics().setNetworkParameters( data[2]) if ('keep_seperate_fd_exp_buffer' in settings and (settings['keep_seperate_fd_exp_buffer'])): masterAgent.setFDExperience(data[3]) # this->_actor->iterate(); ## This will let me know which part of learning is going slower training updates or simulation if (settings["print_levels"][settings["print_level"]] >= settings["print_levels"]['train']): print("sim queue size: ", input_anchor_queue.qsize()) #返回队列的大小 if (output_experience_queue != None): print("exp tuple queue size: ", output_experience_queue.qsize()) if (not settings['on_policy']): data = ('Update_Policy', p, masterAgent.getStateBounds(), masterAgent.getActionBounds(), masterAgent.getRewardBounds(), masterAgent.getPolicy().getNetworkParameters()) if (settings['train_forward_dynamics']): data = ( 'Update_Policy', p, masterAgent.getStateBounds(), masterAgent.getActionBounds(), masterAgent.getRewardBounds(), masterAgent.getPolicy().getNetworkParameters(), masterAgent.getForwardDynamics().getNetworkParameters()) message['type'] = 'Update_Policy' message['data'] = data for m_q in sim_work_queues: ## Don't block on full queue try: m_q.put(message, False) except: print("SimWorker model parameter message queue full: ", m_q.qsize()) if ('override_sim_env_id' in settings and (settings['override_sim_env_id'] != False)): for m_q in eval_sim_work_queues: ## Don't block on full queue try: m_q.put(message, False) except: print("SimWorker model parameter message queue full: ", m_q.qsize()) if (round_ % settings['plotting_update_freq_num_rounds']) == 0: # Running less often helps speed learning up. # Sync up sim actors if (settings['on_policy']): mean_reward, std_reward, mean_bellman_error, std_bellman_error, mean_discount_error, std_discount_error, mean_eval, std_eval = evalModelParrallel( input_anchor_queue=eval_sim_work_queues, model=masterAgent, settings=settings, eval_episode_data_queue=eval_episode_data_queue, anchors=settings['eval_epochs']) else: mean_reward, std_reward, mean_bellman_error, std_bellman_error, mean_discount_error, std_discount_error, mean_eval, std_eval = evalModelParrallel( input_anchor_queue=input_anchor_queue_eval, model=masterAgent, settings=settings, eval_episode_data_queue=eval_episode_data_queue, anchors=settings['eval_epochs']) print(mean_reward, std_reward, mean_bellman_error, std_bellman_error, mean_discount_error, std_discount_error) if mean_bellman_error > 10000: print("Error to big: ") else: if (settings['train_forward_dynamics']): #false mean_dynamicsLosses = np.mean(dynamicsLosses) std_dynamicsLosses = np.std(dynamicsLosses) dynamicsLosses = [] if (settings['train_reward_predictor']): #false mean_dynamicsRewardLosses = np.mean(dynamicsRewardLosses) std_dynamicsRewardLosses = np.std(dynamicsRewardLosses) dynamicsRewardLosses = [] trainData["mean_reward"].append(mean_reward) trainData["std_reward"].append(std_reward) trainData["anneal_p"].append(p) trainData["mean_bellman_error"].append( np.mean(np.fabs(bellman_errors))) trainData["std_bellman_error"].append(np.std(bellman_errors)) bellman_errors = [] trainData["mean_discount_error"].append(mean_discount_error) trainData["std_discount_error"].append(std_discount_error) trainData["mean_eval"].append(mean_eval) trainData["std_eval"].append(std_eval) if (settings['train_forward_dynamics']): trainData["mean_forward_dynamics_loss"].append( mean_dynamicsLosses) trainData["std_forward_dynamics_loss"].append( std_dynamicsLosses) if (settings['train_reward_predictor']): trainData["mean_forward_dynamics_reward_loss"].append( mean_dynamicsRewardLosses) trainData["std_forward_dynamics_reward_loss"].append( std_dynamicsRewardLosses) if (round_ % settings['saving_update_freq_num_rounds']) == 0: if (settings['train_forward_dynamics']): file_name_dynamics = directory + "forward_dynamics_" + ".pkl" f = open(file_name_dynamics, 'wb') dill.dump(masterAgent.getForwardDynamics(), f) f.close() if mean_dynamicsLosses < best_dynamicsLosses: best_dynamicsLosses = mean_dynamicsLosses print("Saving BEST current forward dynamics agent: " + str(best_dynamicsLosses)) file_name_dynamics = directory + "forward_dynamics_" + "_Best.pkl" f = open(file_name_dynamics, 'wb') dill.dump(masterAgent.getForwardDynamics(), f) #save model f.close() if (mean_eval > best_eval): best_eval = mean_eval print("Saving BEST current agent: " + str(best_eval)) file_name = directory + getAgentName() + "_Best.pkl" f = open(file_name, 'wb') dill.dump(masterAgent.getPolicy(), f) f.close() if settings['save_trainData']: fp = open( directory + "trainingData_" + str(settings['agent_name']) + ".json", 'w') ## because json does not serialize np.float32 for key in trainData: trainData[key] = [float(i) for i in trainData[key]] json.dump(trainData, fp) fp.close() print("Saving current masterAgent") file_name = directory + getAgentName() + ".pkl" f = open(file_name, 'wb') dill.dump(masterAgent.getPolicy(), f) f.close() gc.collect() print("Terminating Workers") if (settings['on_policy']): for m_q in sim_work_queues: ## block on full queue m_q.put(None) if ('override_sim_env_id' in settings and (settings['override_sim_env_id'] != False)): for m_q in eval_sim_work_queues: ## block on full queue m_q.put(None) for sw in sim_workers: # Should update these more often sw.join() if ('override_sim_env_id' in settings and (settings['override_sim_env_id'] != False)): for sw in eval_sim_workers: # Should update these more often sw.join() for i in range(len(sim_work_queues)): print("sim_work_queues size: ", sim_work_queues[i].qsize()) while (not sim_work_queues[i].empty()): ### Empty the queue ## Don't block try: data_ = sim_work_queues[i].get(False) except Exception as inst: pass print("sim_work_queues size: ", sim_work_queues[i].qsize()) for i in range(len(eval_sim_work_queues)): print("eval_sim_work_queues size: ", eval_sim_work_queues[i].qsize()) while (not eval_sim_work_queues[i].empty()): ### Empty the queue ## Don't block try: data_ = eval_sim_work_queues[i].get(False) except Exception as inst: pass print("eval_sim_work_queues size: ", eval_sim_work_queues[i].qsize()) print("Finish sim") exp_val.finish() print("Save last versions of files.") file_name = directory + getAgentName() + ".pkl" f = open(file_name, 'wb') dill.dump(masterAgent.getPolicy(), f) f.close() f = open( directory + "trainingData_" + str(settings['agent_name']) + ".json", "w") for key in trainData: trainData[key] = [float(i) for i in trainData[key]] json.dump(trainData, f, sort_keys=True, indent=4) f.close() if (settings['train_forward_dynamics']): file_name_dynamics = directory + "forward_dynamics_" + ".pkl" f = open(file_name_dynamics, 'wb') dill.dump(masterAgent.getForwardDynamics(), f) f.close() print("Delete any plots being used") gc.collect() #立即释放内存
actionsNoNoise = np.array(list(map(f, states))) # states2 = np.transpose(np.repeat([states], 2, axis=0)) # print states2 model = createForwardDynamicsModel(settings, state_bounds, action_bounds, None, None, None) experience = ExperienceMemory(len(state_bounds[0]), len(action_bounds[0]), experience_length, continuous_actions=True, settings=settings) experience.setStateBounds(state_bounds) experience.setRewardBounds(reward_bounds) experience.setActionBounds(action_bounds) experience.setSettings(settings) arr = list(range(experience_length)) random.shuffle(arr) num_samples_to_keep = 300 given_actions = [] given_states = [] for i in range(num_samples_to_keep): a = actions[arr[i]] action_ = np.array([a]) given_actions.append(action_) state_ = np.array([states[arr[i]]]) given_states.append(state_) # print "Action: " + str([actions[i]]) experience.insert(state_, state_, action_, np.array([0])) errors = []
def trainForwardDynamics(settingsFileName): """ State is the input state and Action is the desired output (y). """ # from model.ModelUtil import * np.random.seed(23) file = open(settingsFileName) settings = json.load(file) print("Settings: ", str(json.dumps(settings))) file.close() import os os.environ['THEANO_FLAGS'] = "mode=FAST_RUN,device=" + settings[ 'training_processor_type'] + ",floatX=" + settings['float_type'] # import theano # from theano import tensor as T # import lasagne from util.SimulationUtil import validateSettings from util.SimulationUtil import getDataDirectory from util.SimulationUtil import createForwardDynamicsModel, createRLAgent from model.NeuralNetwork import NeuralNetwork from util.ExperienceMemory import ExperienceMemory import matplotlib.pyplot as plt import math # from ModelEvaluation import * # from util.SimulationUtil import * import time settings = validateSettings(settings) # anchor_data_file = open(settings["anchor_file"]) # _anchors = getAnchors(anchor_data_file) # print ("Length of anchors epochs: ", str(len(_anchors))) # anchor_data_file.close() train_forward_dynamics = True model_type = settings["model_type"] directory = getDataDirectory(settings) if not os.path.exists(directory): os.makedirs(directory) if (settings['train_forward_dynamics']): if "." in settings['forward_dynamics_model_type']: ### convert . to / and copy file over file_name = settings['forward_dynamics_model_type'] k = file_name.rfind(".") file_name = file_name[:k] file_name_read = file_name.replace(".", "/") file_name_read = file_name_read + ".py" print("model file name:", file_name) print("os.path.basename(file_name): ", os.path.basename(file_name)) file = open(file_name_read, 'r') out_file = open(directory + file_name + ".py", 'w') out_file.write(file.read()) file.close() out_file.close() discrete_actions = np.array(settings['discrete_actions']) num_actions = discrete_actions.shape[0] # number of rows rounds = settings["rounds"] epochs = settings["epochs"] # num_states=settings["num_states"] epsilon = settings["epsilon"] discount_factor = settings["discount_factor"] # max_reward=settings["max_reward"] reward_bounds = np.array([[-10.1], [0.0]]) batch_size = settings["batch_size"] train_on_validation_set = settings["train_on_validation_set"] state_bounds = np.array(settings['state_bounds']) discrete_actions = np.array(settings['discrete_actions']) print("Sim config file name: ", str(settings["sim_config_file"])) # c = characterSim.Configuration(str(settings["sim_config_file"])) # c = characterSim.Configuration("../data/epsilon0Config.ini") action_space_continuous = settings['action_space_continuous'] # states2 = np.transpose(np.repeat([states], 2, axis=0)) # print states2 if action_space_continuous: action_bounds = np.array(settings["action_bounds"], dtype=float) if action_space_continuous: experience = ExperienceMemory(len(state_bounds[0]), len(action_bounds[0]), settings['expereince_length'], continuous_actions=True, settings=settings) else: experience = ExperienceMemory(len(state_bounds[0]), 1, settings['expereince_length']) experience.setSettings(settings) file_name = directory + getAgentName() + "expBufferInit.hdf5" # experience.saveToFile(file_name) experience.loadFromFile(file_name) state_bounds = experience._state_bounds print("Samples in experience: ", experience.samples()) if (settings['train_forward_dynamics']): if (settings['forward_dynamics_model_type'] == "SingleNet"): print( "Creating forward dynamics network: Using single network model" ) model = createRLAgent(settings['agent_name'], state_bounds, discrete_actions, reward_bounds, settings) forwardDynamicsModel = createForwardDynamicsModel(settings, state_bounds, action_bounds, None, None, agentModel=model) # forwardDynamicsModel = model else: print("Creating forward dynamics network") # forwardDynamicsModel = ForwardDynamicsNetwork(state_length=len(state_bounds[0]),action_length=len(action_bounds[0]), state_bounds=state_bounds, action_bounds=action_bounds, settings_=settings) forwardDynamicsModel = createForwardDynamicsModel(settings, state_bounds, action_bounds, None, None, agentModel=None) if settings['visualize_learning']: from NNVisualize import NNVisualize title = file_name = settings['forward_dynamics_model_type'] k = title.rfind(".") + 1 if (k > len(title)): ## name does not contain a . k = 0 file_name = file_name[k:] nlv = NNVisualize(title=str("Forward Dynamics Model") + " with " + str(file_name)) nlv.setInteractive() nlv.init() if (settings['train_reward_predictor']): if settings['visualize_learning']: rewardlv = NNVisualize(title=str("Reward Model") + " with " + str(settings["model_type"]), settings=settings) rewardlv.setInteractive() rewardlv.init() # experience = ExperienceMemory(len(state_bounds[0]), len(action_bounds[0]), experience_length, continuous_actions=True) """ for i in range(experience_length): action_ = np.array([actions[i]]) state_ = np.array([states[i]]) # print "Action: " + str([actions[i]]) experience.insert(norm_state(state_, state_bounds), norm_action(action_, action_bounds), norm_state(state_, state_bounds), norm_reward(np.array([0]), reward_bounds)) """ trainData = {} trainData["mean_reward"] = [] trainData["std_reward"] = [] trainData["mean_bellman_error"] = [] trainData["std_bellman_error"] = [] trainData["mean_discount_error"] = [] trainData["std_discount_error"] = [] trainData["mean_forward_dynamics_loss"] = [] trainData["std_forward_dynamics_loss"] = [] trainData["mean_forward_dynamics_reward_loss"] = [] trainData["std_forward_dynamics_reward_loss"] = [] trainData["mean_eval"] = [] trainData["std_eval"] = [] # dynamicsLosses=[] best_dynamicsLosses = 1000000 _states, _actions, _result_states, _rewards, _falls, _G_ts, exp_actions__ = experience.get_batch( batch_size) """ _states = theano.shared(np.array(_states, dtype=theano.config.floatX)) _actions = theano.shared(np.array(_actions, dtype=theano.config.floatX)) _result_states = theano.shared(np.array(_result_states, dtype=theano.config.floatX)) _rewards = theano.shared(np.array(_rewards, dtype=theano.config.floatX)) """ forwardDynamicsModel.setData(_states, _actions, _result_states) for round_ in range(rounds): t0 = time.time() for epoch in range(epochs): _states, _actions, _result_states, _rewards, _falls, _G_ts, exp_actions__ = experience.get_batch( batch_size) # print _actions # dynamicsLoss = forwardDynamicsModel.train(states=_states, actions=_actions, result_states=_result_states) # forwardDynamicsModel.setData(_states, _actions, _result_states) dynamicsLoss = forwardDynamicsModel.train(_states, _actions, _result_states, _rewards) # dynamicsLoss = forwardDynamicsModel._train() t1 = time.time() if (round_ % settings['plotting_update_freq_num_rounds']) == 0: dynamicsLoss_ = forwardDynamicsModel.bellman_error( _states, _actions, _result_states, _rewards) # dynamicsLoss_ = forwardDynamicsModel.bellman_error((_states), (_actions), (_result_states)) if (settings['use_stochastic_forward_dynamics']): dynamicsLoss = np.mean(dynamicsLoss_) else: dynamicsLoss = np.mean(np.fabs(dynamicsLoss_)) if (settings['train_reward_predictor']): dynamicsRewardLoss_ = forwardDynamicsModel.reward_error( _states, _actions, _result_states, _rewards) dynamicsRewardLoss = np.mean(np.fabs(dynamicsRewardLoss_)) # dynamicsRewardLosses.append(dynamicsRewardLoss) dynamicsRewardLosses = dynamicsRewardLoss if (settings['train_forward_dynamics'] and ((round_ % settings['plotting_update_freq_num_rounds']) == 0)): # dynamicsLosses.append(dynamicsLoss) mean_dynamicsLosses = dynamicsLoss std_dynamicsLosses = np.std((dynamicsLoss_)) if (settings['train_forward_dynamics']): trainData["mean_forward_dynamics_loss"].append( mean_dynamicsLosses) trainData["std_forward_dynamics_loss"].append( std_dynamicsLosses) print("Round: " + str(round_) + " Epoch: " + str(epoch) + " ForwardPredictionLoss: " + str(dynamicsLoss) + " in " + str(datetime.timedelta(seconds=(t1 - t0))) + " seconds") # print ("State Bounds: ", forwardDynamicsModel.getStateBounds(), " exp: ", experience.getStateBounds()) # print ("Action Bounds: ", forwardDynamicsModel.getActionBounds(), " exp: ", experience.getActionBounds()) # print (str(datetime.timedelta(seconds=(t1-t0)))) if (settings['visualize_learning']): nlv.updateLoss( np.array(trainData["mean_forward_dynamics_loss"]), np.array(trainData["std_forward_dynamics_loss"])) nlv.redraw() nlv.setInteractiveOff() nlv.saveVisual(directory + "trainingGraphNN") nlv.setInteractive() if (settings['train_reward_predictor']): mean_dynamicsRewardLosses = np.mean(dynamicsRewardLoss) std_dynamicsRewardLosses = np.std(dynamicsRewardLoss_) dynamicsRewardLosses = [] trainData["mean_forward_dynamics_reward_loss"].append( mean_dynamicsRewardLosses) trainData["std_forward_dynamics_reward_loss"].append( std_dynamicsRewardLosses) if (settings['train_reward_predictor'] and settings['visualize_learning']): rewardlv.updateLoss( np.array(trainData["mean_forward_dynamics_reward_loss"]), np.array(trainData["std_forward_dynamics_reward_loss"])) rewardlv.redraw() rewardlv.setInteractiveOff() rewardlv.saveVisual(directory + "rewardTrainingGraph") rewardlv.setInteractive() if (round_ % settings['saving_update_freq_num_rounds']) == 0: if mean_dynamicsLosses < best_dynamicsLosses: best_dynamicsLosses = mean_dynamicsLosses print("Saving BEST current forward dynamics model: " + str(best_dynamicsLosses)) file_name_dynamics = directory + "forward_dynamics_" + "_Best_pretrain.pkl" f = open(file_name_dynamics, 'wb') dill.dump(forwardDynamicsModel, f) f.close() if settings['save_trainData']: fp = open( directory + "FD_trainingData_" + str(settings['agent_name']) + ".json", 'w') # print ("Train data: ", trainData) ## because json does not serialize np.float32 for key in trainData: trainData[key] = [float(i) for i in trainData[key]] json.dump(trainData, fp) fp.close()