def evaluate(self, policy, n_episodes=1, initial_state=None, transform_to_internal_state=None, render=False, results_dir='/tmp/'): """ Evaluate a policy on the mdp. :param policy: a policy outputing an action :type policy: Policy :param n_episodes: number of episodes to evaluate :type n_episodes: int :param initial_state: the initial state to evaluate from :type initial_state: int :param transform_to_internal_state: transform the states or initial state to an internal state of the mdp :type transform_to_internal_state: function :param render: reder the environment :type render: bool :return: a Dataset with the trajectories """ dataset = Dataset(results_dir=results_dir) for episode in range(n_episodes): trajectory = [] state = self._env.reset() if initial_state is not None: state = initial_state if self._quanser_robots: self._env.env._sim_state = np.copy( transform_to_internal_state(state)) else: self._env.env.state = np.copy( transform_to_internal_state(state)) for j in range(self._env._max_episode_steps): with torch.no_grad(): state = torch.tensor(state, device=policy.device, dtype=TORCH_DTYPE) action = policy(state).to('cpu').numpy().reshape((-1, )) state_next, rew, done, _ = self._env.step(action) trajectory.append((state.to('cpu').numpy(), action, rew, state_next, done)) state = state_next if render: self._env.render() if done: break dataset.add_trajectory(trajectory) self._env.close() return dataset
def build_dataset(self, examples_path, labels_path): """ returns an instance of ParaphraseDatasat containing a list of examples""" examples = self.get_examples(examples_path) labels = self.get_labels(labels_path) for example, label in zip(examples, labels): example.label = label return Dataset(examples)
def get_train_and_test(): # Read the data and split into train and test. sc = SentenceAssigner("../data/datasetSplit.txt") dataset = Dataset("../data/datasetSentences.txt", sc) train_corpus = dataset.train_corpus test_corpus = dataset.test_corpus return train_corpus, test_corpus
def get_evaluation(self, sess, data_list, data_type, global_step=None): _logger.add() _logger.add('getting evaluation result') logits_list, loss_list, accu_list = [], [], [] for sample_batch, _, _, _ in Dataset.generate_batch_sample_iter( data_list): feed_dict = self.model.get_feed_dict(sample_batch, 'dev') logits, loss, accu = sess.run( [self.model.logits, self.model.loss, self.model.accuracy], feed_dict) logits_list.append(np.argmax(logits, -1)) loss_list.append(loss) accu_list.append(accu) logits_array = np.concatenate(logits_list, 0) loss_value = np.mean(loss_list) accu_array = np.concatenate(accu_list, 0) accu_value = np.mean(accu_array) if global_step is not None: if data_type == 'train': summary_feed_dict = { self.train_loss: loss_value, self.train_accuracy: accu_value, } summary = sess.run(self.train_summaries, summary_feed_dict) self.writer.add_summary(summary, global_step) elif data_type == 'dev': summary_feed_dict = { self.dev_loss: loss_value, self.dev_accuracy: accu_value, } summary = sess.run(self.dev_summaries, summary_feed_dict) self.writer.add_summary(summary, global_step) else: summary_feed_dict = { self.test_loss: loss_value, self.test_accuracy: accu_value, } summary = sess.run(self.test_summaries, summary_feed_dict) self.writer.add_summary(summary, global_step) return loss_value, accu_value
kwargs={ 'fs': 200.0, 'fs_ctrl': 200.0 }) env = gym.make('Qube-100-v1') mdp = MDP(env) ########################################################################################## # Gather an Off-Policy Dataset results_dir = '/home/carvalho/Documents/projects/nopg/results/qube/nopgs/' os.makedirs(results_dir, exist_ok=True) # Load trajectories from file filename = '/home/carvalho/Documents/projects/nopg/datasets/qube/15_trajectories.npy' dataset = Dataset(results_dir=results_dir) dataset.load_trajectories_from_file(filename, n_trajectories=8) dataset.update_dataset_internal() s_band_factor = [15., 15., 15, 15., 1., 1.] s_n_band_factor = s_band_factor a_band_factor = [7.] dataset.kde_bandwidths_internal(s_band_factor=s_band_factor, a_band_factor=a_band_factor, s_n_band_factor=s_n_band_factor) dataset.plot_data_kde(state_labels=mdp._env.observation_space.labels, action_labels=mdp._env.action_space.labels) ########################################################################################## # Define the Policy Network policy_params = {
from src.dataset.dataset import Dataset # Get daily data for a state in India kl_time_series = Dataset("India:KL") kl_confirmed = kl_time_series.get_confirmed() print(kl_confirmed) # Get daily data for a country india_time_series = Dataset("India") india_confirmed = india_time_series.get_confirmed() print(india_confirmed) # Get all states dataframe all_states = Dataset("India:AllStates") states_df = all_states.get_confirmed() print(states_df)
def train(): output_model_params() loadFile = True ifLoad, data = False, None if loadFile: ifLoad, data = load_file(cfg.processed_path, 'data', 'pickle') if not ifLoad or not loadFile: data_object = Dataset(cfg.train_data_path, cfg.dev_data_path) data_object.save_dict(cfg.dict_path) save_file({'data_obj': data_object}, cfg.processed_path) else: data_object = data['data_obj'] emb_mat_token, emb_mat_glove = data_object.emb_mat_token, data_object.emb_mat_glove with tf.variable_scope(network_type) as scope: if network_type in model_set: model = Model(emb_mat_token, emb_mat_glove, len(data_object.dicts['token']), len(data_object.dicts['char']), data_object.max_lens['token'], scope.name) graphHandler = GraphHandler(model) evaluator = Evaluator(model) performRecoder = PerformRecoder(5) if cfg.gpu_mem < 1.: gpu_options = tf.GPUOptions( per_process_gpu_memory_fraction=cfg.gpu_mem, allow_growth=True) else: gpu_options = tf.GPUOptions() graph_config = tf.ConfigProto(gpu_options=gpu_options, allow_soft_placement=True) sess = tf.Session(config=graph_config) graphHandler.initialize(sess) # begin training steps_per_epoch = int( math.ceil(1.0 * len(data_object.digitized_train_data_list) / cfg.train_batch_size)) num_steps = steps_per_epoch * cfg.max_epoch or cfg.num_steps global_step = 0 # debug or not if cfg.debug: sess = tf_debug.LocalCLIDebugWrapperSession(sess) for sample_batch, batch_num, data_round, idx_b in Dataset.generate_batch_sample_iter( data_object.digitized_train_data_list, num_steps): global_step = sess.run(model.global_step) + 1 if_get_summary = global_step % (cfg.log_period or steps_per_epoch) == 0 loss, summary, train_op = model.step(sess, sample_batch, get_summary=if_get_summary) if global_step % 10 == 0: _logger.add('data round: %d: %d/%d, global step:%d -- loss: %.4f' % (data_round, idx_b, batch_num, global_step, loss)) if if_get_summary: graphHandler.add_summary(summary, global_step) # Occasional evaluation if global_step % (cfg.eval_period or steps_per_epoch) == 0: # ---- dev ---- dev_loss, dev_accu = evaluator.get_evaluation( sess, data_object.digitized_dev_data_list, 'dev', global_step) _logger.add('==> for dev, loss: %.4f, accuracy: %.4f' % (dev_loss, dev_accu)) # ---- test ---- if cfg.test_data_name != None: test_loss, test_accu = evaluator.get_evaluation( sess, data_object.digitized_test_data_list, 'test', global_step) _logger.add('~~> for test, loss: %.4f, accuracy: %.4f' % (test_loss, test_accu)) is_in_top, deleted_step = performRecoder.update_top_list( global_step, dev_accu, sess) this_epoch_time, mean_epoch_time = cfg.time_counter.update_data_round( data_round) if this_epoch_time is not None and mean_epoch_time is not None: _logger.add('##> this epoch time: %f, mean epoch time: %f' % (this_epoch_time, mean_epoch_time))
def test(): output_model_params() loadFile = True ifLoad, data = False, None if loadFile: ifLoad, data = load_file(cfg.processed_path, 'processed data', 'pickle') if not ifLoad or not loadFile: train_data_obj = Dataset(cfg.train_data_path, 'train') dev_data_obj = Dataset(cfg.dev_data_path, 'dev', train_data_obj.dicts) test_data_obj = Dataset(cfg.test_data_path, 'test', train_data_obj.dicts) save_file( { 'train_data_obj': train_data_obj, 'dev_data_obj': dev_data_obj, 'test_data_obj': test_data_obj }, cfg.processed_path) train_data_obj.save_dict(cfg.dict_path) else: train_data_obj = data['train_data_obj'] dev_data_obj = data['dev_data_obj'] test_data_obj = data['test_data_obj'] emb_mat_token, emb_mat_glove = train_data_obj.emb_mat_token, train_data_obj.emb_mat_glove with tf.variable_scope(network_type) as scope: if network_type in model_set: model = Model(emb_mat_token, emb_mat_glove, len(train_data_obj.dicts['token']), len(train_data_obj.dicts['char']), train_data_obj.max_lens['token'], scope.name) graphHandler = GraphHandler(model) evaluator = Evaluator(model) if cfg.gpu_mem < 1.: gpu_options = tf.GPUOptions( per_process_gpu_memory_fraction=cfg.gpu_mem, allow_growth=True) else: gpu_options = tf.GPUOptions() graph_config = tf.ConfigProto(gpu_options=gpu_options, allow_soft_placement=True) # graph_config.gpu_options.allow_growth = True sess = tf.Session(config=graph_config) graphHandler.initialize(sess) # ---- dev ---- dev_loss, dev_accu = evaluator.get_evaluation(sess, dev_data_obj, 1) _logger.add('==> for dev, loss: %.4f, accuracy: %.4f' % (dev_loss, dev_accu)) # ---- test ---- test_loss, test_accu = evaluator.get_evaluation(sess, test_data_obj, 1) _logger.add('~~> for test, loss: %.4f, accuracy: %.4f' % (test_loss, test_accu)) # ---- train ---- train_loss, train_accu, train_sent_accu = evaluator.get_evaluation( sess, train_data_obj, 1) _logger.add('--> for test, loss: %.4f, accuracy: %.4f' % (train_loss, train_accu))
def get_samples(self, sampling_type=None, states=None, actions=None, transform_to_internal_state=lambda x: x, policy=None, n_samples=None, n_trajectories=None, initial_state=None, max_ep_transitions=100, render=False, press_enter_to_start=False, dataset=None, results_dir='/tmp/'): """ Collect samples from the mdp. If sampling_type is 'uniform', provide the grid of states and actions to samples uniformly from, and additionally transform_to_internal_state function. If sampling_type is 'behavioral', provide at least the behavioral policy, and no states and actions. NOTE: opt between either n_samples or n_trajectories. In the case of n_trajectories specify the maximum number of transitions per episode max_ep_transitions. :param sampling_type: can be uniform or behavioral :type sampling_type: str :param states: a batch of states to sample on :type states: ndarray :param actions: a batch of actions to sample each state on :type actions: ndarray :param transform_to_internal_state: transform the states or initial state to an internal state of the mdp :type transform_to_internal_state: function :param policy: behavioral policy outputing an action given a state :type policy: callable function :param n_samples: number of samples to collect (EXCLUSIVE between n_samples and n_trajectories) :type n_samples: int :param n_trajectories: number of trajectories to collect (EXCLUSIVE between n_samples and n_trajectories) :type n_trajectories: int :param initial_state: initial state to start sampling from (e.g., in the pendulum we want to start from the upright position) :type initial_state: ndarray :param max_ep_transitions: maximum number of transitions before reseting to the initial state :type max_ep_transitions: int :param render: render the environment :type render: int :param press_enter_to_start: wait for user input to start collecting trajectories :type press_enter_to_start: bool :param dataset: if a dataset is provided add trajectories to it, else start an empty one :type dataset: Dataset :param results_dir: provide a directory path to save intermediate results :type results_dir: str :return: a dataset containing the collected trajectories """ dataset = dataset if dataset is not None else Dataset( results_dir=results_dir) if sampling_type == 'uniform': for state in states: for action in actions: self._env.reset() if self._quanser_robots: self._env.env._sim_state = np.copy( transform_to_internal_state(state)) else: self._env.env.state = np.copy( transform_to_internal_state(state)) state_next, rew, done, _ = self._env.step(action) dataset.add_trajectory([(state, action, rew, state_next, done)]) elif sampling_type == 'behavioral': behavioral_policy = policy i = 0 n_samples_collected = 0 criteria = 0 if n_samples is not None: criteria = n_samples elif n_trajectories is not None: criteria = n_trajectories while i < criteria: if press_enter_to_start: if render: self._env.render() input("Press ENTER to start") state = self._env.reset() if initial_state is not None: state = initial_state if self._quanser_robots: self._env.env._sim_state = np.copy( transform_to_internal_state(state)) else: self._env.env.state = np.copy( transform_to_internal_state(state)) trajectory = [] for j in range(max_ep_transitions): if render: self._env.render() action = behavioral_policy(x=state) state_next, rew, done, _ = self._env.step(action) trajectory.append((state, action, rew, state_next, done)) n_samples_collected += 1 print("\r{}/{} - {}".format(i + 1, criteria, n_samples_collected), end=' ', flush=True) if n_samples is not None: i += 1 if done or (i >= n_samples if n_samples is not None else False): break state = state_next dataset.add_trajectory(trajectory) if n_trajectories is not None: i += 1 if i >= n_trajectories: break self._env.close() return dataset
def test_split(self): return Dataset(self.test_portion, self.test_labels)
epochs=args.epochs, device=torch.device(args.device), tokenizer=transformers.AutoTokenizer.from_pretrained(args.model), ) """ model = TransformerWrapper.load_pretrained( args.pretrained_model, params=configuration, pooler = BertPoolingStrategy(configuration), loss = SoftmaxLoss(configuration)) """ model = DistilBertForSequenceClassification.from_pretrained( args.pretrained_model, num_labels=len(LABELS_TO_ID)) train_split, valid_split = dataset.split_dataset(test_perc=0.1) train_dataset = Dataset(train_split) valid_dataset = Dataset(valid_split) print(f"train dataset size: {len(train_dataset)}") print(f"valid dataset size: {len(valid_dataset)}") valid_data_loader = SmartParaphraseDataloader.build_batches( valid_dataset, 16, mode="sequence", config=configuration) evaluator = ClassificationEvaluator(params=configuration, model=model, data_loader=valid_data_loader, device=args.device, metrics=metrics, fp16=True, verbose=True)
# Use the GPU if available, or if the memory is insufficient use only the CPU # DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu') DEVICE = torch.device('cpu') ########################################################################################## # Create the Environment (MDP) env = gym.make('MountainCarContinuous-v0') mdp = MDP(env) ########################################################################################## # Gather an Off-Policy Dataset # Load trajectories from file filename = '../datasets/mountaincar/10_trajectories.npy' dataset = Dataset() dataset.load_trajectories_from_file(filename, n_trajectories=5) dataset.update_dataset_internal() s_band_factor = [2., 2.] s_n_band_factor = s_band_factor a_band_factor = [50.] dataset.kde_bandwidths_internal(s_band_factor=s_band_factor, a_band_factor=a_band_factor, s_n_band_factor=s_n_band_factor) ########################################################################################## # Define the Policy Network policy_params = { 'policy_class': 'stochastic', 'neurons': [mdp.s_dim, 50,
TEST_PATH = DATA / 'test' TEST = TEST_PATH / 'Covid/0000.png' CWD = Path.cwd() OUTPUT_PATH = CWD / 'outputs' CLEAR = False LABELS = ['Covid','Normal','Pneumonia'] if CLEAR: remove_folder([OUTPUT_PATH, Path('./logs'), Path('./build')]) nets_path = create_folders(name=OUTPUT_PATH, nets=NETS) # %% [code] # np.random.seed(seed=42) labels = listdir(TRAIN_PATH) ds_train = Dataset(path_data=TRAIN_PATH, train=False) ds_test = Dataset(path_data=TEST_PATH, train=False) part_param = {'tamanho': 10} train, validation = ds_train.partition(val_size=0.2, **part_param) test_values, _test_val_v = ds_test.partition(val_size=1e-5, **part_param) params = { 'dim': DIM_SPLIT, 'batch_size': BATCH_SIZE, 'n_class': len(labels), 'channels': CHANNELS } train_generator = DataGenerator(x_set=train[0], y_set=train[1], **params) val_generator = DataGenerator(x_set=validation[0], y_set=validation[1], **params) test_generator = DataGenerator(x_set=test_values[0], y_set=test_values[1], **params)
centre=(0.0, 0.5), axis_ratio=0.75, phi=45.0, intensity=0.1, effective_radius=0.5, sersic_index=1.0, ) image = model.profile_image_from_grid(grid=grid) # plt.figure() # plt.imshow(image.in_2d) # plt.show() # exit() noise_map = np.random.normal(loc=0.0, scale=10**-2.0, size=grid.shape_2d) dataset = Dataset(data=np.add(image.in_2d, noise_map), noise_map=noise_map) # plt.figure() # plt.imshow(dataset.data) # plt.show() # exit() # masked_dataset = MaskedDataset( # dataset=dataset, # mask=mask # ) # likelihood = fit.likelihood_from_chi_squared_and_noise_normalization( # chi_squared=fit.chi_squared_from_chi_squared_map_and_mask( # chi_squared_map=fit.chi_squared_map_from_residual_map_noise_map_and_mask( # residual_map=fit.residual_map_from_data_model_data_and_mask( # data=dataset.data,
from quanser_robots import GentlyTerminating from quanser_robots.qube import SwingUpCtrl from src.dataset.dataset import Dataset register(id='Qube-100-v1', entry_point='quanser_robots.qube.qube:Qube', max_episode_steps=1500, kwargs={ 'fs': 200.0, 'fs_ctrl': 200.0 }) env = GentlyTerminating(gym.make('Qube-100-v1')) dataset = Dataset() n_trajectories = 1 for traj in range(n_trajectories): trajectory = [] ctrl = SwingUpCtrl() obs = env.reset() done = False while not done: env.render() act = ctrl(obs) obs_n, r, done, _ = env.step(act) trajectory.append((obs, act, r, obs_n, done)) if done: break obs = np.copy(obs_n) env.close()
shape_3d=grid_3d.shape_3d, z_step_kms=z_step_kms ) # plot_utils.plot_cube( # cube=cube, # ncols=8 # ) noise_map = np.random.normal( loc=0.0, scale=10**-4.0, size=grid_3d.shape_3d ) dataset = Dataset( data=np.add( cube, noise_map ), noise_map=noise_map, z_step_kms=z_step_kms ) # plot_utils.plot_cube( # cube=dataset.data, # ncols=8 # ) # exit() masked_dataset = MaskedDataset( dataset=dataset, mask=mask_3d )
transformer = transformer_class(uv_wavelengths=uv_wavelengths, grid=grid.in_radians) visibilities = tracer.profile_visibilities_from_grid_and_transformer( grid=grid, transformer=transformer) if data_with_phase_errors: visibilities = corrupt_visibilities_from_f_matrix_and_phase_errors( visibilities=visibilities, f=f, phase_errors=phase_errors) noise_map = np.random.normal(loc=0.0, scale=1.0 * 10**-2.0, size=visibilities.shape) dataset = Dataset(uv_wavelengths=uv_wavelengths, visibilities=np.add(visibilities, noise_map), noise_map=noise_map, antennas=antennas) xy_mask = al.Mask.unmasked( shape_2d=grid.shape_2d, pixel_scales=grid.pixel_scales, sub_size=grid.sub_size, ) def test(dataset, xy_mask, tracer, self_calibration, transformer_class=al.TransformerFINUFFT): masked_dataset = MaskedDataset(dataset=dataset, xy_mask=xy_mask)
loss.backward() optimizer.step() return test(model, validate_loader) if __name__ == '__main__': random.seed(SEED) os.environ['PYTHONHASHSEED'] = str(SEED) np.random.seed(SEED) torch.manual_seed(SEED) if torch.cuda.is_available(): torch.cuda.manual_seed_all(SEED) reader = DataReader('isear_v2/') reader.read_datas() train_dataset = Dataset(reader.train_datas, reader.word_num) validate_dataset = Dataset(reader.validate_datas, reader.word_num) test_dataset = Dataset(reader.test_datas, reader.word_num) train_loader = DataLoader(dataset=train_dataset, batch_size=args.batch_size, shuffle=True, collate_fn=collate_data) validate_loader = DataLoader(dataset=validate_dataset, batch_size=len(validate_dataset), collate_fn=collate_data) test_loader = DataLoader(dataset=test_dataset, batch_size=len(test_dataset), collate_fn=collate_data) model = eval(args_ori.model)(reader.word_num, reader.label_num, **vars(args))
# visibilities=visibilities, # transformers=transformers, # shape=cube.shape # ), # ncols=8, # cube_contours=cube, # ) # exit() noise_map = np.random.normal( loc=0.0, scale=5.0 * 10**-1.0, size=visibilities.shape ) dataset = Dataset( uv_wavelengths=uv_wavelengths, visibilities=np.add( visibilities, noise_map ), noise_map=noise_map, z_step_kms=z_step_kms ) # plot_utils.plot_cube( # cube=dirty_cube_from_visibilities( # visibilities=dataset.visibilities, # transformers=transformers, # shape=cube.shape, # invert=True # ), # ncols=8, # cube_contours=cube, # # ) # exit()
def train_split(self): return Dataset(self.examples, self.labels)