ibmfl.util

Abstract Agent

class ibmfl.util.custom_rl_core.agent.Agent(environment, hyperparams, memory)[source]

Abstract base class for all agents.

abstract export_checkpoint(name)[source]

Export the model checkpoint for rollout

Parameters

name – filename

abstract get_action(state, *args)[source]

Returns the action to be taken given a state.

Parameters
  • state – environment state in numpy array format

  • args – optional params

Returns

action

abstract get_weights() → {}[source]

Return model weights as dictionary, key is network id and value is weights in numpy array

Returns

dictionary of model weights

abstract load_state(name)[source]

Loads the agent state from the specified file

Parameters

name – filename

abstract save_state(name)[source]

Saves the agent state to the specified file

Parameters

name – filename

abstract set_weights(weights: {}) → None[source]

Iterate the dictionary and set weights for model using layer id

Parameters

weights – weights of network in dictionary format

abstract train() → {}[source]

Trains the agent and return dictionary of train results

Returns

dictionary of train results

Abstract Replay Memory

class ibmfl.util.custom_rl_core.memory.Replay(params)[source]

Abstract base class for replay memory. A memory stores interaction sequences between an agent and environment.

abstract add(*args, **kwargs)[source]

Saves experiences like (state, action, reward, next_state, done) in replay memory

abstract sample(*args, **kwargs)[source]

retrieves experiences from replay memory using sampling

abstract size()[source]

retrieves length of replay memory

Returns

length of replay memory

Abstract Rollout

class ibmfl.util.custom_rl_core.rollout.Rollout(environment, checkpoint, params)[source]

Abstract base class for policy rollout

abstract evaluate() → {}[source]

Evaluate the policy with test environment and return dictionary of test results

Returns

dictionary of test results

DQN Agent

class ibmfl.util.custom_rl_examples.cartpole_dqn.dqn_agent.DQNAgent(environment, hyperparams, memory)[source]

DQN Agent Implementation Taken from https://github.com/keon/deep-q-learning/blob/master/dqn.py

export_checkpoint(name)[source]

Export the model checkpoint for rollout

Parameters

name – filename

get_action(state)[source]

Returns the action to be taken given a state.

Parameters
  • state – environment state in numpy array format

  • args – optional params

Returns

action

get_weights() → {}[source]

Return model weights as dictionary, key is network id and value is weights in numpy array

Returns

dictionary of model weights

load_state(name)[source]

Loads the agent state from the specified file

Parameters

name – filename

save_state(name)[source]

Saves the agent state to the specified file

Parameters

name – filename

set_weights(weights)[source]

Iterate the dictionary and set weights for model using layer id

Parameters

weights – weights of network in dictionary format

train()[source]

Trains the agent and return dictionary of train results

Returns

dictionary of train results

DDPG Agent

class ibmfl.util.custom_rl_examples.pendulum_ddpg.ddpg_agent.DDPGAgent(environment, hyperparams, memory)[source]

DDPG Agent Implementation Taken from https://gist.github.com/cyoon1729/542edc824bfa3396e9767e3b59183cae#file-ddpg-py

export_checkpoint(name)[source]

Export the model checkpoint for rollout

Parameters

name – filename

get_action(state)[source]

Returns the action to be taken given a state.

Parameters
  • state – environment state in numpy array format

  • args – optional params

Returns

action

get_weights() → {}[source]

Return model weights as dictionary, key is network id and value is weights in numpy array

Returns

dictionary of model weights

load_state(name)[source]

Loads the agent state from the specified file

Parameters

name – filename

parameters_to_numpy(params)[source]

Transforms parameter tensors to numpy arrays

Parameters

params – The parameter tensor to be transformed

Returns

numpy array of parameters

save_state(name)[source]

Saves the agent state to the specified file

Parameters

name – filename

set_weights(weights: {}) → None[source]

Iterate the dictionary and set weights for model using layer id

Parameters

weights – weights of network in dictionary format

train()[source]

Trains the agent and return dictionary of train results

Returns

dictionary of train results

Critic Network

class ibmfl.util.custom_rl_examples.pendulum_ddpg.models.Critic(input_size, hidden_size, output_size)[source]

Definition of critic network

forward(state, action)[source]

Params state and actions are torch tensors

Actor Network

class ibmfl.util.custom_rl_examples.pendulum_ddpg.models.Actor(input_size, hidden_size, output_size, learning_rate=0.0003)[source]

Definition of actor network

forward(state)[source]

Param state is a torch tensor

Ornstein-Ulhenbeck Process

class ibmfl.util.custom_rl_examples.pendulum_ddpg.ou_noise.OUNoise(action_space, mu=0.0, theta=0.15, max_sigma=0.3, min_sigma=0.3, decay_period=100000)[source]

Pendulum DDPG Rollout

class ibmfl.util.custom_rl_examples.pendulum_ddpg.pendulum_ddpg_rollout.PendulumDDPGRollout(environment, checkpoint, params)[source]

rollout for Pendulum environment using DDPG

evaluate()[source]

Evaluate the policy with test environment and return dictionary of test results

Returns

dictionary of test results

Pendulum Replay Experience

class ibmfl.util.custom_rl_examples.pendulum_ddpg.replay_buffer.ExperienceReplay(params)[source]

Fixed-size buffer to store experience tuples.

add(state, action, reward, next_state, done)[source]

Saves experiences like (state, action, reward, next_state, done) in replay memory

sample(batch_size)[source]

retrieves experiences from replay memory using sampling

size()[source]

retrieves length of replay memory

Returns

length of replay memory

Adult Decision Tree Data

class ibmfl.util.data_handlers.adult_dt_data_handler.AdultDTDataHandler(data_config=None)[source]

Data handler for adult dataset.

get_data()[source]

Read adult.data from a given dir.

Returns

A dataset structure

Return type

pandas.core.frame.DataFrame

get_dataset_info()[source]

Read adult.data.txt and extract data information

Returns

spec, a dictionary that contains list_of_features, feature_values and list_of_labels.

Return type

dict

preprocess(training_dataset)[source]

Performs the following pre-processing on the provided dataset: * Categorize age feature into 4 categories,

i.e., [0, 18] -> ‘teen’, [18, 40] -> ‘adult’, [40, 80] -> ‘old-adult’, and [80, 99] -> ‘elder’.

  • Categorize workclass feature into 3 categories,

    i.e., [“?”, “Never-worked”, “Private”, “Without-pay”] -> ‘others’, [“Federal-gov”, ” Local-gov”] -> ‘gov’, and ->[“Self-emp-inc”, “Self-emp-not-inc”] -> ‘self’.

  • Categorize education feature into 5 categories,

    i.e., [“10th”,” 11th”, ” 12th”, ” 1st-4th”, ” 5th-6th”, ” 7th-8th”, ” 9th”] -> “non_college”, [” Assoc-acdm”, ” Assoc-voc”] ->”assoc”, [” Bachelors”, ” Some-college”] -> “college”, [” Doctorate”, ” HS-grad”, ” Masters”] -> “grad”, and [” Preschool”, ” Prof-school”] ->”others”

  • Categorize education-num feature into 3 categories:

    i.e., [0, 5] -> ‘<5’, [5, 10] -> ‘5-10’, and [10, 17] -> ‘>10’.

  • Categorize capital-gain feature into 5 categories,

    i.e., [-1, 1] -> 0, [1, 39999] -> 1,[39999, 49999] ->2, [49999, 79999] ->3, and [79999, 99999] ->4.

  • Categorize capital-loss feature into 6 categories,
    i.e., [-1, 1] -0, [1, 999] ->1, [999, 1999] ->2, [1999, 2999] ->3,

    [2999, 3999] ->4, [3999, 4499] -> 5.

  • Categorize hours feature into 3 categories,

    i.e., [0, 20] -> ‘<20’, [20, 40] -> ‘20-40’, [40, 100] -> ‘>40’.

  • Categorize native-coutry into 5 categories,

    i.e., [‘ ?’,] -> ‘others’, [‘ Cambodia’, ‘ China’, ‘ Hong’, ‘ India’, ‘ Iran’, ‘ Japan’, ‘ Laos’, ‘ Philippines’, ‘ South’, ‘ Taiwan’, ‘ Thailand’, ‘ Vietnam’] -> ‘asia’, [‘ Canada’, ‘ Outlying-US(Guam-USVI-etc)’, ‘ United-States’] -> ‘north_america’, [‘ Columbia’, ‘ Cuba’, ‘ Dominican-Republic’, ‘ Ecuador’, ‘ El-Salvador’, ‘ Guatemala’, ‘ Haiti’, ‘ Honduras’, ‘ Jamaica’, ‘ Mexico’, ‘ Nicaragua’, ‘ Peru’, ‘ Puerto-Rico’, ‘ Trinadad&Tobago’] -> ‘south_america’, [‘ England’, ‘ France’, ‘ Germany’, ‘ Greece’, ‘ Holand-Netherlands’, ‘ Hungary’, ‘ Ireland’, ‘ Italy’, ‘ Poland’, ‘ Portugal’, ‘ Scotland’, ‘ Yugoslavia’] -> ‘europe’.

  • Training label column is renamed to class

Function assumes dataset has column labels [‘1’, ‘2’, ‘3’…’13’, ‘class’], added in the load_adult() function in datasets.py, feature fnlwgt was also dropped in load_adult().

Parameters

training_dataset (pandas.core.frame.DataFrame) – Raw training data

Returns

Preprocessed training data

Return type

pandas.core.frame.DataFrame

Adult Sklearn Data

class ibmfl.util.data_handlers.adult_sklearn_data_handler.AdultSklearnDataHandler(data_config=None)[source]

Data handler for Adult dataset to train a Logistic Regression Classifier on scikit-learn. TEST_SIZE is set to 0.2, and RANDOM_STATE is set to 42.

get_col_names()[source]

Returns the names of the dataset columns

Returns

column names

Return type

list

get_data()[source]

Gets pre-processed adult training and testing data.

Parameters

nb_points (int) – Number of data points to be included in each set

Returns

training data

Return type

tuple

get_sa()[source]

Returns the sensitive attribute

Returns

sensitive attribute

Return type

str

get_weight()[source]

Gets pre-processed adult training and testing data, calculates weights for points weight = P-expected(sensitive_attribute & class)/P-observed(sensitive_attribute & class)

Returns

weights

Return type

np.array

preprocess(training_data)[source]

Performs the following preprocessing on adult training and testing data: * Drop following features: ‘workclass’, ‘fnlwgt’, ‘education’, ‘marital-status’, ‘occupation’,

‘relationship’, ‘capital-gain’, ‘capital-loss’, ‘hours-per-week’, ‘native-country’

  • Map ‘race’, ‘sex’ and ‘class’ values to 0/1
    • ‘ White’: 1, ‘ Amer-Indian-Eskimo’: 0, ‘ Asian-Pac-Islander’: 0, ‘ Black’: 0, ‘ Other’: 0

    • ‘ Male’: 1, ‘ Female’: 0

    • Further details in Kamiran, F. and Calders, T. Data preprocessing techniques for classification without discrimination

  • Split ‘age’ and ‘education’ columns into multiple columns based on value

Parameters

training_dataset (`pandas.core.frame.DataFrame) – Raw training data

Returns

Preprocessed training data

Return type

pandas.core.frame.DataFrame

Airline Data

class ibmfl.util.data_handlers.airline_data_handler.AirlineDataHandler(data_config=None)[source]

Data handler for Airline Delay dataset to train a Multiclass Classification Model. TEST_SIZE is set to 0.2, and RANDOM_STATE is set to 42.

get_data()[source]

Obtains generated data and splits to test and train sets.

Parameters

nb_points (int) – Number of data points to be included in each set

Returns

training data

Return type

tuple

Binovf Data

class ibmfl.util.data_handlers.binovf_data_handler.BinovfDataHandler(data_config=None)[source]

Data handler for Binary Overfit dataset to train a Binary Classification Model. TEST_SIZE is set to 0.2, and RANDOM_STATE is set to 42.

get_data()[source]

Obtains generated data and splits to test and train sets.

Returns

training data and testing data

Return type

tuple

Diabetes Data

class ibmfl.util.data_handlers.diabetes_data_handler.DiabetesDataHandler(data_config=None)[source]

Data handler for Diabetes dataset to train a Multiclass Classification Model. TEST_SIZE is set to 0.2, and RANDOM_STATE is set to 1234.

get_data()[source]

Get pre-processed diabetes training and testing data.

Parameters

nb_points (int) – Number of data points to be included in each set

Returns

training data

Return type

tuple

Federated Clustering Data

class ibmfl.util.data_handlers.federated_clustering_data_handler.FederatedClusteringDataHandler(data_config=None)[source]

Data handler for a simulated federated clustering dataset.

get_data(**kwargs)[source]

Loads the simulated federated clustering dataset as described in the paper [https://arxiv.org/abs/1911.00218].

:param **kwargs

See below

Keyword Arguments
  • L (int) – Number of true global centroids, default 100

  • J (int) – Number of clients, default 10

  • D (int) – Data dimension, default 50

  • M (int) – Data points per group, default 500

  • mu0 (float) – Global mean, default 0.0

  • global_sd (float) – Global standard deviation, default np.sqrt(L)

  • local_sd (float) – Local standard deviation, default 0.1

Returns

(x_train, None), (x_test, None)

Return type

tuple

Higgs Data

class ibmfl.util.data_handlers.higgs_data_handler.HiggsDataHandler(data_config=None)[source]

Data handler for Higgs Boson dataset to train a Binary Classification Model. TEST_SIZE is set to 0.2, and RANDOM_STATE is set to 1234.

get_data()[source]

Gets pre-processed higgs training and testing data.

Parameters

nb_points (int) – Number of data points to be included in each set

Returns

training data

Return type

tuple

Linovf Data

class ibmfl.util.data_handlers.linovf_data_handler.LinovfDataHandler(data_config=None)[source]

Data handler for Binary Overfit dataset to train a Regression Model based on a Linear Relationship. TEST_SIZE is set to 0.2, and RANDOM_STATE is set to 42.

get_data()[source]

Obtains generated data and splits to test and train sets.

Returns

training data and testing data

Return type

tuple

MNIST Keras Data

class ibmfl.util.data_handlers.mnist_keras_data_handler.MnistKerasDataHandler(data_config=None, channels_first=False)[source]

Data handler for MNIST dataset.

get_data(nb_points=500)[source]

Gets pre-process mnist training and testing data. Because this method is for testing it takes as input the number of datapoints, nb_points, to be included in the training and testing set.

Parameters

nb_points (int) – Number of data points to be included in each set

Returns

training data

Return type

tuple

MNIST Sklearn Data

class ibmfl.util.data_handlers.mnist_sklearn_data_handler.MnistSklearnDataHandler(data_config=None)[source]

Data handler for mnist dataset to train a SGDClassifier on scikit-learn.

get_data(nb_points=500)[source]

Gets pre-process mnist training and testing data. Because this method is for testing it takes as input the number of datapoints, nb_points, to be included in the training and testing set.

Parameters

nb_points (int) – Number of data points to be included in each set

Returns

training data

Return type

tuple

Multovf Data

Multiclass Overfit Dataset Author: Yuya J. Ong (yuyajong@ibm.com)

class ibmfl.util.data_handlers.multovf_data_handler.MultovfDataHandler(data_config=None)[source]

Data handler for Multiclass Overfit dataset to train a Multiclass Classification Model. TEST_SIZE is set to 0.2, and RANDOM_STATE is set to 42.

get_data()[source]

Obtains generated data and splits to test and train sets.

Parameters

nb_points (int) – Number of data points to be included in each set

Returns

training data

Return type

tuple

Nursery Data

class ibmfl.util.data_handlers.nursery_data_handler.NurseryDataHandler(data_config=None)[source]

Data handler for nursery dataset.

get_data()[source]

Read nursery.data.txt from a given dir.

Returns

A dataset structure

Return type

pandas.core.frame.DataFrame

get_dataset_info()[source]

Read nursery.data.txt and extract data information

Returns

spec, a dictionary that contains list_of_features, feature_values and list_of_labels.

Return type

dict

Pendulum Environment

class ibmfl.util.data_handlers.pendulum_env.PendulumEnv(data=None, env_config=None, g=10.0)[source]

Github : https://github.com/openai/gym/blob/master/gym/envs/classic_control/pendulum.py Description:

Try to keep a frictionless pendulum standing up. The inverted pendulum swingup problem is a classic problem in the control literature. In this version of the

problem, the pendulum starts in a random position, and the goal is to swing it up so it stays upright

Source:

Unknown

Observation:

Type: Box(3) Num Observation Min Max 0 cos(theta) -1.0 1.0 1 sin(theta) -1.0 1.0 2 theta dot -8.0 8.0

Actions:

Type: Box(1) Num Action Min Max 0 Joint effort -2.0 2.0

Reward:

The precise equation for reward:

-(theta^2 + 0.1*theta_dt^2 + 0.001*action^2) Theta is normalized between -pi and pi. Therefore, the lowest reward is -(pi^2 + 0.1*8^2 + 0.001*2^2) = -16.2736044,

and the highest reward is 0. In essence, the goal is to remain at zero angle (vertical), with the least rotational velocity, and the least effort.

Starting State:

Random angle from -pi to pi, and random velocity between -1 and 1

Episode Termination:

There is no specified termination. Adding a maximum number of steps might be a good idea.

NOTE: Your environment object could be wrapped by the TimeLimit wrapper, if created using the “gym.make” method. In that case it will terminate after 200 steps.

close()[source]

Override close in your subclass to perform any necessary cleanup.

Environments will automatically close() themselves when garbage collected or when the program exits.

render(mode='human')[source]

Render one frame of the environment

reset()[source]

Resets the state of the environment and returns an initial observation. Returns:

observation (object): the initial observation.

seed(seed=None)[source]

Sets the seed for this env’s random number generator(s).

Note:

Some environments use multiple pseudorandom number generators. We want to capture all such seeds used in order to ensure that there aren’t accidental correlations between multiple generators.

Returns:
list<bigint>: Returns the list of seeds used in this env’s random

number generators. The first value in the list should be the “main” seed, or the value which a reproducer should pass to ‘seed’. Often, the main seed equals the provided ‘seed’, but this won’t be true if seed=None, for example.

step(u)[source]

Run one timestep of the environment’s dynamics. When end of episode is reached, you are responsible for calling reset() to reset this environment’s state. Accepts an action and returns a tuple (observation, reward, done, info). Args:

action (object): an action provided by the agent

Returns:

observation (object): agent’s observation of the current environment reward (float) : amount of reward returned after previous action done (bool): whether the episode has ended, in which case further

step() calls will return undefined results

info (dict): contains auxiliary diagnostic information (helpful for debugging, and sometimes learning)

Pendulum Environment Data

Data Handler for Pendulum Environment

class ibmfl.util.data_handlers.pendulum_env_data_handler.PendulumEnvDataHandler(data_config)[source]

Class to load pendulum environment for reinforcement learning.

get_data()[source]

Read train data and test data for reinforcement learning :return:

get_env_class_ref() → ibmfl.data.env_spec.EnvHandler[source]

Get environment reference for RL trainer, the instance is created in model class as part of trainer initialization

Rollout Save

class ibmfl.util.data_handlers.rollout.RolloutSaver(outfile=None, use_shelve=False, write_update_file=False, target_steps=None, target_episodes=None, save_info=False)[source]

Utility class for storing rollouts. Currently supports two behaviours: the original, which simply dumps everything to a pickle file once complete, and a mode which stores each rollout as an entry in a Python shelf db file. The latter mode is more robust to memory problems or crashes part-way through the rollout generation. Each rollout is stored with a key based on the episode number (0-indexed), and the number of episodes is stored with the key “num_episodes”, so to load the shelf file, use something like: with shelve.open(‘rollouts.pkl’) as rollouts:

for episode_index in range(rollouts[“num_episodes”]):

rollout = rollouts[str(episode_index)]

If outfile is None, this class does nothing.

append_step(obs, action, next_obs, reward, done, info)[source]

Add a step to the current rollout, if we are saving them

FFL Version Filter

class ibmfl.util.log_config.FFLVersionFilter(object[source]

filter(record)[source]

Parameters
  • record

Returns

boolean

Return type

bool

configure_file_logging(filename)[source]

Parameters
  • handler (method) – A routine which is invoked when scheduled.

Returns

None

configure_basic_logging(log_level)[source]

Configures logging for the session based on pre defined format. Default logging will be done on console

Parameters
  • log_level (int) – Should be a value from [DEBUG, INFO, WARNING, ERROR, CRITICAL] based on the required granularity.

Returns

None