def sample_open(sample_idxs, col_features, col_labels, p_dic): tool_fn.progress_bar(0, 1, prefix='>>> Sample extracting...') feature_samples = os.listdir(p_dic.get('sample_data_dir') + '/feature') label_samples = os.listdir(p_dic.get('sample_data_dir') + '/label') feature_samples.sort() label_samples.sort() feature = np.empty([0, len(col_features)], dtype=np.float32) label = np.empty([0, len(col_labels)], dtype=np.float32) loads = 0 for sample_idx in sample_idxs: feature_csv_path = p_dic.get( 'sample_data_dir') + '/feature/' + feature_samples[sample_idx] label_csv_path = p_dic.get( 'sample_data_dir') + '/label/' + label_samples[sample_idx] feature_part = pd.read_csv(feature_csv_path, dtype=np.float32, delimiter=',', error_bad_lines=False, index_col=0).values label_part = pd.read_csv(label_csv_path, dtype=np.float32, delimiter=',', error_bad_lines=False, index_col=0).values feature = np.concatenate((feature, feature_part)) label = np.concatenate((label, label_part)) loads += 1 tool_fn.progress_bar(loads, len(sample_idxs), prefix='>>> Sample extracting...') print("Sample extracting done") return feature, label
def main(): p_dic = getattr(conf.dic.path_dic, FLAGS.env_name) c_dic = getattr(conf.dic.col_dic, FLAGS.env_name) s_dic = getattr(conf.dic.space_dic, FLAGS.env_name) cols = { 'ORIGIN_F': getattr(conf.list.preproc.origin, FLAGS.env_name), 'PREPROC_F': getattr(conf.list.preproc.preproc, FLAGS.env_name) } origin_f = [c_dic[feature] for feature in cols.get('ORIGIN_F')] origins = os.listdir(p_dic.get('origin_data_dir')) origins.sort() print('Extract original data from dir: ' + p_dic.get('origin_data_dir')) tool_fn.progress_bar(0, 1, prefix='>>> Extracting...') for origin in range(len(origins)): csv_path = p_dic.get('origin_data_dir') + '/' + origins[origin] origin_df_part = pd.read_csv(csv_path, delimiter=',', error_bad_lines=False, usecols=origin_f)[origin_f] origin_df_part = origin_df_part.loc[origin_df_part.index % 4 == 3] if origin == 0: origin_df = origin_df_part else: origin_df = pd.concat([origin_df, origin_df_part]) tool_fn.progress_bar(origin + 1, len(origins), prefix='>>> Extracting...') striped_date = pd.Series(origin_df['Date/Time'].str.strip()) splited_date = pd.DataFrame(striped_date.str.split(r'[/ :]').tolist(), columns = ['Month', 'Day', '', 'Hour', '', '']) splited_date = splited_date.drop([''], axis=1) splited_date.reset_index(drop=True, inplace=True) origin_df = origin_df.drop(['Date/Time'], axis=1) origin_df.reset_index(drop=True, inplace=True) origin_df = pd.concat([splited_date, origin_df], axis=1) if not os.path.exists(p_dic.get('preproc_data_dir')): os.makedirs(p_dic.get('preproc_data_dir')) print('Preprocess to dir: ' + p_dic.get('preproc_data_dir')) tool_fn.progress_bar(0, 1, prefix='>>> Preprocessing...') f_space = np.array([s_dic[feature] for feature in cols.get('PREPROC_F')], dtype=np.float32).T origin_df_size = origin_df.shape[0] for split in range(FLAGS.frac_num): preprocessed_df = origin_df.head(np.ceil(origin_df_size / FLAGS.frac_num).astype(int)).apply(pd.to_numeric) preprocessed_df = tool_fn.normalize(preprocessed_df, f_space[0], f_space[1]) origin_df.drop(preprocessed_df.index) preprocessed_df.to_csv(p_dic.get('preproc_data_dir') + '/' + FLAGS.preproc_name + '_' + str(split) + '.csv') tool_fn.progress_bar(split + 1, FLAGS.frac_num, prefix='>>> Preprocessing...')
def learn(FLAGS, env, agent): render = False sd = 1 print('\n\n>> Agent training start') for e in range(FLAGS.num_episodes): obs = env.reset() ep_reward = 0 # rss : 추후 env 쪽으로 넘김 # env에 epsode_render 메서드 만들어서 거기에 포함 rss = [] start = time.time() for j in range(FLAGS.num_steps): if render: env.render() action = agent.choose_action(obs) action = np.random.normal(loc=action, scale=sd) action = np.clip(action, env.action_space.low, env.action_space.high) next_obs, reward, done, info = env.step(action[0]) next_obs = np.expand_dims(next_obs, axis=0) reward = np.expand_dims(reward, axis=0) # rss : 추후 env 쪽으로 넘김 if hasattr(env, 'envfn'): rss.append(env.rs.reshape(-1)) ep_reward += np.sum(reward) end = time.time() tool_fn.progress_bar( j + 1, FLAGS.num_steps, prefix='>> EP:%i' % e, suffix='(STEP:%i, R:%.2f, FPS:%.0f, SD:%.2f)' % (j, ep_reward / (j + 1), (j + 1) / (end - start), sd)) if j == FLAGS.num_steps - 1: agent.perceive(obs, action, reward, next_obs, episode=e, ep_reward=ep_reward) if ep_reward > 10000: render = True break else: agent.perceive(obs, action, reward, next_obs) obs = next_obs if sd >= 0.1: # sd *= .995 sd *= .9 # 플로팅 코드, 추후 rss 변수와 함께 env episode_render 메서드로 옮길 것 # if hasattr(env, 'envfn'): if not os.path.exists( env.p_dic.get('agent_graph_dir') + '/' + str(e)): os.makedirs(env.p_dic.get('agent_graph_dir') + '/' + str(e)) rss = np.array(rss) # indexing = np.arange(FLAGS.num_steps) # interval = indexing % (FLAGS.num_steps // 100) == 0 # for rs in range(len(env.cols.get('agent_rs'))): # plt.title(env.cols.get('agent_rs')[rs] + ' (instant)') # plt.plot(indexing[interval], rss[interval][:, rs]) # plt.figure(num=1, figsize=(6, 6), dpi=2000, facecolor='white') # plt.savefig(env.p_dic.get('agent_graph_dir') + '/' + str(e) + '/' + env.cols.get('agent_rs')[rs] + '_inst.png', bbox_inches='tight') # plt.close() # 데이터센터만, 일단위 플로팅 # rss_ = [] for i in range(rss.shape[0]): if ((i % 24) == 0 and (i + 24) <= rss.shape[0]): sum = 0 for j in range(24): sum += rss[i + j] rss_.append(sum / 24) rss = np.array(rss_) indexing = np.arange(rss.shape[0]) for rs in range(len(env.cols.get('agent_rs'))): plt.title(env.cols.get('agent_rs')[rs] + ' (instant)_day') plt.plot(indexing, rss[:, rs]) plt.figure(num=1, figsize=(6, 6), dpi=2000, facecolor='white') plt.savefig(env.p_dic.get('agent_graph_dir') + '/' + str(e) + '/' + env.cols.get('agent_rs')[rs] + '_inst_day.png', bbox_inches='tight') plt.close() # 일단위 플로팅 끝 # if hasattr(env, 'envfn'): env.sess.close() agent.sess.close()
def main(): p_dic = getattr(conf.dic.path_dic, FLAGS.env_name) c_dic = getattr(conf.dic.col_dic, FLAGS.env_name) cols = { 'ENV_F': getattr(conf.list.envfn.feature, FLAGS.env_name), 'ENV_L': getattr(conf.list.envfn.label, FLAGS.env_name), 'AGENT_IVS': getattr(conf.list.agent.iv_state, FLAGS.env_name) } features = [c_dic[feature] for feature in cols.get('ENV_F')] labels = [c_dic[label] for label in cols.get('ENV_L')] independents = [c_dic[ivs] for ivs in cols.get('AGENT_IVS')] origins = os.listdir(p_dic.get('origin_data_dir')) origins.sort() print('Extract original data from dir: ' + p_dic.get('origin_data_dir')) tool_fn.progress_bar(0, 1, prefix='>>> Extracting...') for origin in range(len(origins)): csv_path = p_dic.get('origin_data_dir') + '/' + origins[origin] origin_feature_df_part = pd.read_csv(csv_path, dtype=np.float32, delimiter=',', error_bad_lines=False, usecols=features)[features] origin_label_df_part = pd.read_csv(csv_path, dtype=np.float32, delimiter=',', error_bad_lines=False, usecols=labels)[labels] origin_ivs_df_part = pd.read_csv(csv_path, dtype=np.float32, delimiter=',', error_bad_lines=False, usecols=independents)[independents] origin_feature_df_part = origin_feature_df_part.loc[ origin_feature_df_part.index % 4 == 3].iloc[:-1] origin_label_df_part = origin_label_df_part.loc[ origin_label_df_part.index % 4 == 3].iloc[1:] origin_ivs_df_part = origin_ivs_df_part.loc[origin_ivs_df_part.index % 4 == 3].iloc[:-1] if origin == 0: origin_feature_df = origin_feature_df_part origin_label_df = origin_label_df_part origin_ivs_df = origin_ivs_df_part else: origin_feature_df = pd.concat( [origin_feature_df, origin_feature_df_part]) origin_label_df = pd.concat( [origin_label_df, origin_label_df_part]) origin_ivs_df = pd.concat([origin_ivs_df, origin_ivs_df_part]) tool_fn.progress_bar(origin + 1, len(origins), prefix='>>> Extracting...') origin_feature_df = origin_feature_df.reset_index(drop=True) origin_label_df = origin_label_df.reset_index(drop=True) if not os.path.exists(p_dic.get('sample_data_dir') + '/feature'): os.makedirs(p_dic.get('sample_data_dir') + '/feature') if not os.path.exists(p_dic.get('sample_data_dir') + '/label'): os.makedirs(p_dic.get('sample_data_dir') + '/label') if not os.path.exists(p_dic.get('sample_data_dir') + '/ivs'): os.makedirs(p_dic.get('sample_data_dir') + '/ivs') print('Sample to dir: ' + p_dic.get('sample_data_dir')) tool_fn.progress_bar(0, 1, prefix='>>> Sampling...') for split in range(FLAGS.frac_num): sampled_feature_df = origin_feature_df.sample(frac=1. / FLAGS.frac_num, random_state=sample_seed) origin_feature_df.drop(sampled_feature_df.index) sampled_feature_df.to_csv( p_dic.get('sample_data_dir') + '/feature/' + FLAGS.sample_name + '_feature_' + str(split) + '.csv') sampled_label_df = origin_label_df.iloc[sampled_feature_df.index] sampled_label_df.to_csv( p_dic.get('sample_data_dir') + '/label/' + FLAGS.sample_name + '_label_' + str(split) + '.csv') tool_fn.progress_bar(split + 1, FLAGS.frac_num, prefix='>>> Sampling...') origin_ivs_df.to_csv( p_dic.get('sample_data_dir') + '/ivs/' + 'extracted_ivs' + '.csv')
def shuffle_n_batch(data_tuple, data_length): tool_fn.progress_bar(0, 1, prefix='>>> Shuffling and batch making...') dataset = tf.data.Dataset.from_tensor_slices(data_tuple) batch = dataset.shuffle(data_length).repeat().batch(FLAGS.batch_size) tool_fn.progress_bar(1, 1, prefix='>>> Shuffling and batch making...') return batch