def sample_data(data_type, num_contexts=None): """Sample data from given 'data_type'. Args: data_type: Dataset from which to sample. num_contexts: Number of contexts to sample. Returns: dataset: Sampled matrix with rows: (context, reward_1, ..., reward_num_act). opt_rewards: Vector of expected optimal reward for each context. opt_actions: Vector of optimal action for each context. num_actions: Number of available actions. context_dim: Dimension of each context. """ if data_type == 'linear': # Create linear dataset num_actions = 8 context_dim = 10 noise_stds = [0.01 * (i + 1) for i in range(num_actions)] dataset, _, opt_linear = sample_linear_data(num_contexts, context_dim, num_actions, sigma=noise_stds) opt_rewards, opt_actions = opt_linear elif data_type == 'sparse_linear': # Create sparse linear dataset num_actions = 7 context_dim = 10 noise_stds = [0.01 * (i + 1) for i in range(num_actions)] num_nnz_dims = int(context_dim / 3.0) dataset, _, opt_sparse_linear = sample_sparse_linear_data( num_contexts, context_dim, num_actions, num_nnz_dims, sigma=noise_stds) opt_rewards, opt_actions = opt_sparse_linear elif data_type == 'mushroom': # Create mushroom dataset num_actions = 2 context_dim = 117 file_name = FLAGS.mushroom_data dataset, opt_mushroom = sample_mushroom_data(file_name, num_contexts) opt_rewards, opt_actions = opt_mushroom elif data_type == 'financial': num_actions = 8 context_dim = 21 num_contexts = min(3713, num_contexts) noise_stds = [0.01 * (i + 1) for i in range(num_actions)] file_name = FLAGS.financial_data dataset, opt_financial = sample_stock_data(file_name, context_dim, num_actions, num_contexts, noise_stds, shuffle_rows=True) opt_rewards, opt_actions = opt_financial elif data_type == 'jester': num_actions = 8 context_dim = 32 num_contexts = min(19181, num_contexts) file_name = FLAGS.jester_data dataset, opt_jester = sample_jester_data(file_name, context_dim, num_actions, num_contexts, shuffle_rows=True, shuffle_cols=True) opt_rewards, opt_actions = opt_jester elif data_type == 'statlog': file_name = FLAGS.statlog_data num_actions = 7 num_contexts = min(43500, num_contexts) sampled_vals = sample_statlog_data(file_name, num_contexts, shuffle_rows=True) contexts, rewards, (opt_rewards, opt_actions) = sampled_vals dataset = np.hstack((contexts, rewards)) context_dim = contexts.shape[1] elif data_type == 'adult': file_name = FLAGS.adult_data num_actions = 14 num_contexts = min(45222, num_contexts) sampled_vals = sample_adult_data(file_name, num_contexts, shuffle_rows=True) contexts, rewards, (opt_rewards, opt_actions) = sampled_vals dataset = np.hstack((contexts, rewards)) context_dim = contexts.shape[1] elif data_type == 'covertype': file_name = FLAGS.covertype_data num_actions = 7 num_contexts = min(150000, num_contexts) sampled_vals = sample_covertype_data(file_name, num_contexts, shuffle_rows=True) contexts, rewards, (opt_rewards, opt_actions) = sampled_vals dataset = np.hstack((contexts, rewards)) context_dim = contexts.shape[1] elif data_type == 'census': file_name = FLAGS.census_data num_actions = 9 num_contexts = min(150000, num_contexts) sampled_vals = sample_census_data(file_name, num_contexts, shuffle_rows=True) contexts, rewards, (opt_rewards, opt_actions) = sampled_vals dataset = np.hstack((contexts, rewards)) context_dim = contexts.shape[1] elif data_type == 'wheel': delta = 0.95 num_actions = 5 context_dim = 2 mean_v = [1.0, 1.0, 1.0, 1.0, 1.2] std_v = [0.05, 0.05, 0.05, 0.05, 0.05] mu_large = 50 std_large = 0.01 dataset, opt_wheel = sample_wheel_bandit_data(num_contexts, delta, mean_v, std_v, mu_large, std_large) opt_rewards, opt_actions = opt_wheel return dataset, opt_rewards, opt_actions, num_actions, context_dim
def sample_data(data_type, num_contexts=None): """Sample data from given 'data_type'. Args: data_type: Dataset from which to sample. num_contexts: Number of contexts to sample. Returns: dataset: Sampled matrix with rows: (context, reward_1, ..., reward_num_act). opt_rewards: Vector of expected optimal reward for each context. opt_actions: Vector of optimal action for each context. num_actions: Number of available actions. context_dim: Dimension of each context. """ if data_type == '2linear': # Create linear dataset num_actions = 2 context_dim = 10 noise_stds = [0.01 * (i + 1) for i in range(num_actions)] dataset, _, opt_linear = sample_linear_data(num_contexts, context_dim, num_actions, sigma=noise_stds) opt_rewards, opt_actions = opt_linear return dataset, opt_rewards, opt_actions, num_actions, context_dim, None if data_type == 'linear': # Create linear dataset num_actions = 8 context_dim = 10 noise_stds = [0.01 * (i + 1) for i in range(num_actions)] dataset, _, opt_linear = sample_linear_data(num_contexts, context_dim, num_actions, sigma=noise_stds) opt_rewards, opt_actions = opt_linear return dataset, opt_rewards, opt_actions, num_actions, context_dim, None elif data_type == 'sparse_linear': # Create sparse linear dataset num_actions = 7 context_dim = 10 noise_stds = [0.01 * (i + 1) for i in range(num_actions)] num_nnz_dims = int(context_dim / 3.0) dataset, _, opt_sparse_linear = sample_sparse_linear_data( num_contexts, context_dim, num_actions, num_nnz_dims, sigma=noise_stds) opt_rewards, opt_actions = opt_sparse_linear return dataset, opt_rewards, opt_actions, num_actions, context_dim, None elif data_type == 'mushroom': # Create mushroom dataset num_actions = 2 context_dim = 117 file_name = FLAGS.mushroom_data dataset, opt_mushroom = sample_mushroom_data(file_name, num_contexts) opt_rewards, opt_actions = opt_mushroom return dataset, opt_rewards, opt_actions, num_actions, context_dim, None elif data_type == 'financial': num_actions = 8 context_dim = 21 num_contexts = min(3713, num_contexts) noise_stds = [0.01 * (i + 1) for i in range(num_actions)] file_name = FLAGS.financial_data dataset, opt_financial = sample_stock_data(file_name, context_dim, num_actions, num_contexts, noise_stds, shuffle_rows=True) opt_rewards, opt_actions = opt_financial return dataset, opt_rewards, opt_actions, num_actions, context_dim, None elif data_type == 'jester': num_actions = 8 context_dim = 32 num_contexts = min(19181, num_contexts) file_name = FLAGS.jester_data dataset, opt_jester = sample_jester_data(file_name, context_dim, num_actions, num_contexts, shuffle_rows=True, shuffle_cols=True) opt_rewards, opt_actions = opt_jester return dataset, opt_rewards, opt_actions, num_actions, context_dim, None elif data_type == 'statlog': file_name = FLAGS.statlog_data num_actions = 7 num_contexts = min(43500, num_contexts) sampled_vals = sample_statlog_data(file_name, num_contexts, shuffle_rows=True) contexts, rewards, (opt_rewards, opt_actions) = sampled_vals dataset = np.hstack((contexts, rewards)) context_dim = contexts.shape[1] return dataset, opt_rewards, opt_actions, num_actions, context_dim, None elif data_type == 'adult': file_name = FLAGS.adult_data num_actions = 2 num_contexts = min(45222, num_contexts) sampled_vals = sample_adult_data(file_name, num_contexts, shuffle_rows=True) contexts, rewards, (opt_rewards, opt_actions) = sampled_vals dataset = np.hstack((contexts, rewards)) context_dim = contexts.shape[1] return dataset, opt_rewards, opt_actions, num_actions, context_dim, None elif data_type == 'covertype': file_name = FLAGS.covertype_data num_actions = 7 num_contexts = min(150000, num_contexts) sampled_vals = sample_covertype_data(file_name, num_contexts, shuffle_rows=True) contexts, rewards, (opt_rewards, opt_actions) = sampled_vals dataset = np.hstack((contexts, rewards)) context_dim = contexts.shape[1] #54 return dataset, opt_rewards, opt_actions, num_actions, context_dim, None elif data_type == 'census': file_name = FLAGS.census_data num_actions = 9 num_contexts = min(150000, num_contexts) sampled_vals = sample_census_data(file_name, num_contexts, shuffle_rows=True) contexts, rewards, (opt_rewards, opt_actions) = sampled_vals dataset = np.hstack((contexts, rewards)) context_dim = contexts.shape[1] return dataset, opt_rewards, opt_actions, num_actions, context_dim, None elif data_type == 'wheel': delta = 0.5 num_actions = 5 context_dim = 2 mean_v = [0.1, 0.1, 0.1, 0.1, 0.2] std_v = [0.1, 0.1, 0.1, 0.1, 0.1] mu_large = 0.4 std_large = 0.1 dataset, opt_wheel = sample_wheel_bandit_data(num_contexts, delta, mean_v, std_v, mu_large, std_large) opt_rewards, opt_actions = opt_wheel return dataset, opt_rewards, opt_actions, num_actions, context_dim, None elif data_type == 'wheel2': delta = 0.7 num_actions = 2 context_dim = 2 mean_v = [0.0, 1] std_v = [0.1, 0.1] mu_large = 2 std_large = 0.1 dataset, opt_wheel = sample_wheel2_bandit_data(num_contexts, delta, mean_v, std_v, mu_large, std_large) opt_rewards, opt_actions = opt_wheel return dataset, opt_rewards, opt_actions, num_actions, context_dim, None elif data_type == 'eeg': file_name = FLAGS.eeg_data num_actions = 5 num_contexts = min(11500, num_contexts) sampled_vals = sample_eeg_data(file_name, num_contexts, shuffle_rows=True) contexts, rewards, (opt_rewards, opt_actions) = sampled_vals dataset = np.hstack((contexts, rewards)) context_dim = contexts.shape[1] return dataset, opt_rewards, opt_actions, num_actions, context_dim, None elif data_type == 'diabetic': file_name = FLAGS.diabetic_data num_actions = 3 num_contexts = min(100000, num_contexts) sampled_vals = sample_diabetic_data(file_name, num_contexts, shuffle_rows=True) contexts, rewards, (opt_rewards, opt_actions) = sampled_vals dataset = np.hstack((contexts, rewards)) context_dim = contexts.shape[1] return dataset, opt_rewards, opt_actions, num_actions, context_dim, None elif data_type == 'phone': file_name = FLAGS.phone_data num_actions = 6 num_contexts = min(7767, num_contexts) sampled_vals = sample_phone_data(file_name, num_contexts, shuffle_rows=True) contexts, rewards, (opt_rewards, opt_actions) = sampled_vals dataset = np.hstack((contexts, rewards)) context_dim = contexts.shape[1] return dataset, opt_rewards, opt_actions, num_actions, context_dim, None elif data_type == 'aps': file_name = FLAGS.aps_data num_actions = 2 num_contexts = min(76000, num_contexts) sampled_vals = sample_aps_data(file_name, num_contexts, shuffle_rows=True) contexts, rewards, (opt_rewards, opt_actions) = sampled_vals dataset = np.hstack((contexts, rewards)) context_dim = contexts.shape[1] return dataset, opt_rewards, opt_actions, num_actions, context_dim, None elif data_type == 'txt': file_name = [FLAGS.positive_data_file, FLAGS.negative_data_file] num_actions = 2 num_contexts = min(10000, num_contexts) sampled_vals = sample_txt_data(file_name, num_contexts, shuffle_rows=True) contexts, rewards, (opt_rewards, opt_actions), vocab_processor = sampled_vals dataset = np.hstack((contexts, rewards)) context_dim = contexts.shape[1] return dataset, opt_rewards, opt_actions, num_actions, context_dim, vocab_processor elif data_type == 'amazon': file_name = FLAGS.amazon_data_file num_actions = 5 num_contexts = min(10000, num_contexts) sampled_vals = sample_amazon_data(file_name, num_contexts, shuffle_rows=True) contexts, rewards, (opt_rewards, opt_actions), vocab_processor = sampled_vals dataset = np.hstack((contexts, rewards)) context_dim = contexts.shape[1] return dataset, opt_rewards, opt_actions, num_actions, context_dim, vocab_processor