Exemple #1
0
def dataset_proto(name=name):
    if name == "linear":
        dataset, _, opt_linear = sample_linear_data(num_contexts,
                                                    context_dim,
                                                    num_actions,
                                                    sigma=noise_stds)
    elif name == "wheel":
        dataset, opt_linear = sample_wheel_bandit_data(num_contexts, delta,
                                                       mean_v, std_v, mu_large,
                                                       std_large)

    elif name == "mushroom":
        mush = Mushrooms(num_contexts=num_contexts)
        dataset = mush.table
        opt_rewards, opt_actions = mush.opts[:, 0], mush.opts[:, 1]
        opt_linear = (opt_rewards, opt_actions)

    elif name == "covertype":
        cov = Covertype(num_contexts=num_contexts)
        dataset = cov.table
        opt_rewards, opt_actions = cov.opts[:, 0], cov.opts[:, 1]
        opt_linear = (opt_rewards, opt_actions)

    return dataset, opt_linear
Exemple #2
0
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 dataset_proto():
     dataset, _, opt_linear = sample_linear_data(num_contexts, context_dim,
                                                 num_actions, sigma=noise_stds)
     return dataset, opt_linear
Exemple #4
0
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
Exemple #5
0
    def train(self, epochs, batch_size=128, sample_interval=50):

        # Load the dataset
        # (X_train, _), (_, _) = mnist.load_data()
        self.dirname = os.path.dirname(__file__)
        if self.file == "linear":
            num_actions = 8
            context_dim = 10
            num_contexts = 1500
            noise_stds = [0.01 * (i + 1) for i in range(num_actions)]
            noise_stds = [1 for i in range(num_actions)]
            X_train = sample_linear_data(num_contexts, context_dim,
                                         num_actions, sigma=noise_stds)[0][:, :-8]

        elif self.file == "wheel":
            num_actions = 5
            context_dim = 2
            num_contexts = 1500
            delta = 0.95
            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
            X_train = sample_wheel_bandit_data(num_contexts, delta,
                                               mean_v, std_v,
                                               mu_large, std_large)[0][:, :-5]

        else:
            _, X_train = get_labels_contexts_covertype(path=os.path.join(self.dirname, self.file))

        # Adversarial ground truths
        valid = -np.ones((batch_size, 1))
        fake = np.ones((batch_size, 1))

        for epoch in range(epochs):

            for _ in range(self.n_critic):

                # ---------------------
                #  Train Discriminator
                # ---------------------

                # Select a random batch of images
                idx = np.random.randint(0, X_train.shape[0], batch_size)
                imgs = X_train[idx]

                # Sample noise as generator input
                noise = np.random.normal(0, 1, (batch_size, self.n_noise))

                # Generate a batch of new images
                gen_imgs = self.generator.predict(noise)

                # Train the critic
                d_loss_real = self.critic.train_on_batch(imgs, valid)
                d_loss_fake = self.critic.train_on_batch(gen_imgs, fake)
                d_loss = 0.5 * np.add(d_loss_fake, d_loss_real)
                # print("piche", d_loss_real, d_loss_fake)

                # Clip critic weights
                for l in self.critic.layers:
                    weights = l.get_weights()
                    weights = [np.clip(w, -self.clip_value, self.clip_value) for w in weights]
                    l.set_weights(weights)

            # ---------------------
            #  Train Generator
            # ---------------------

            g_loss = self.combined.train_on_batch(noise, valid)