Skip to content

Latest commit

 

History

History
764 lines (482 loc) · 16.9 KB

API-REFERENCE.md

File metadata and controls

764 lines (482 loc) · 16.9 KB

Table of Contents

Syft

Syft client for model-centric federated learning.

Parameters

  • options Object
    • options.url string Full URL to PyGrid app (ws and http schemas supported).
    • options.verbose boolean Whether to enable logging and allow unsecured PyGrid connection.
    • options.peerConfig Object [not implemented] WebRTC peer config used with RTCPeerConnection.

Examples

const client = new Syft({url: "ws://localhost:5000", verbose: true})
const job = client.newJob({modelName: "mnist", modelVersion: "1.0.0"})
job.on('accepted', async ({model, clientConfig}) => {
  // Execute training
  const training = this.train('...', { ... })
  training.on('end', async () => {
    const diff = await model.createSerializedDiffFromModel(training.currentModel)
    await this.report(diff)
  }
})
job.on('rejected', ({timeout}) => {
  // Retry later or stop
})
job.on('error', (err) => {
  // Handle errors
})
job.request()

newJob

Instantiates the new Job with given options.

Parameters

  • options Object
    • options.modelName string FL Model name.
    • options.modelVersion string FL Model version.
    • options.authToken string FL Model authentication token.

Returns Job

Job

Job represents a single training cycle done by the client.

Properties

on

Registers an event listener to the Job's event observer.

Available events: accepted, rejected, error.

Parameters

start

Starts the Job by executing following actions:

  • Authenticates for given FL model.
  • Meters connection speed to PyGrid (if requested by PyGrid).
  • Registers into training cycle on PyGrid.
  • Retrieves cycle and client parameters.
  • Downloads the model, plans, protocols from PyGrid.
  • Fires accepted event on success.

Returns Promise<void>

request

  • **See: Job.start **

Alias for Job.start

Returns Promise<void>

report

Submits the model diff to PyGrid.

Parameters

  • diff ArrayBuffer Serialized difference between original and trained model parameters.

Returns Promise<void>

train

Trains the model against specified plan and using specified parameters. Returns PlanTrainer object to have a handle on training process.

Parameters

  • trainingPlan string Training Plan name.
  • parameters Object Dictionary of training parameters.
    • parameters.inputs [PlanInputSpec] List of training Plan input arguments
    • parameters.outputs [PlanOutputSpec] List of training Plan outputs
    • parameters.data tf.Tensor Tensor containing training data
    • parameters.target tf.Tensor Tensor containing training targets
    • parameters.epochs number? Epochs to train (if not specified, taken from Job)
    • parameters.batchSize number? Batch size (if not specified, taken from Job)
    • parameters.stepsPerEpoch number? Max number of steps per epoch (if not specified, taken from Job)
    • parameters.checkpoint PlanTrainerCheckpoint? Checkpoint
    • parameters.events Object? List of event listeners
      • parameters.events.start Function? On training start listener
      • parameters.events.end Function? On training end listener
      • parameters.events.epochStart Function? On epoch start listener
      • parameters.events.epochEnd Function? On epoch end listener
      • parameters.events.batchStart Function? On batch start listener
      • parameters.events.batchEnd Function? On batch end listener

Returns PlanTrainer

Job#accepted

accepted event. Triggered when PyGrid accepts the client into training cycle.

Type: Object

Properties

  • model SyftModel Instance of SyftModel.
  • clientConfig Object Client configuration returned by PyGrid.

Job#rejected

rejected event. Triggered when PyGrid rejects the client.

Type: Object

Properties

  • timeout (number | null) Time in seconds to retry. Empty when the FL model is not trainable anymore.

Job#error

error event. Triggered for plethora of error conditions.

SyftModel

Model parameters as stored in the PyGrid.

Properties

  • params [tf.Tensor] Array of Model parameters.

toProtobuf

Returns model serialized to protobuf.

Returns Promise<ArrayBuffer>

createSerializedDiff

Calculates difference between 2 versions of the Model parameters and returns serialized diff that can be submitted to PyGrid.

Parameters

  • updatedModelParams Array<tf.Tensor> Array of model parameters (tensors).

Returns Promise<ArrayBuffer> Protobuf-serialized diff.

createSerializedDiffFromModel

Calculates difference between 2 versions of the Model and returns serialized diff that can be submitted to PyGrid.

Parameters

Returns Promise<ArrayBuffer> Protobuf-serialized diff.

Plan

Plan stores a sequence of actions (ComputationAction) in its role. A worker is assigned plans and executes the actions stored in the plans.

execute

Executes the Plan and returns its output.

The order, type and number of arguments must match to arguments defined in the PySyft Plan.

Parameters

Returns Promise<Array<tf.Tensor>>

PlanTrainer

Class that contains training loop logic.

Properties

  • originalModel SyftModel Original model.
  • currentModel SyftModel Trained model.
  • epoch number Current epoch.
  • batchIdx number Current batch.
  • stopped boolean Is the training currently stopped.

on

Registers an event listener to the PlanTrainer's event observer.

Available events: start, end, epochStart, epochEnd, batchStart, batchEnd.

Parameters

start

Starts the training loop.

Parameters

  • resume (optional, default false)

stop

Stops training loop and returns training checkpoint.

Returns Promise<PlanTrainerCheckpoint>

resume

Resume stopped training process.

createCheckpoint

Creates checkpoint using current training state.

Returns PlanTrainerCheckpoint

applyCheckpoint

Restores PlanTrainer state from checkpoint.

Parameters

PlanTrainer#start

start event. Triggered on training start.

Type: Object

PlanTrainer#end

end event. Triggered after training end.

PlanTrainer#stop

stop event. Triggered when training was stopped.

PlanTrainer#epochStart

epochStart event. Triggered before epoch start.

Type: Object

Properties

PlanTrainer#epochEnd

epochEnd event. Triggered after epoch end.

Properties

PlanTrainer#batchStart

batchStart event. Triggered before batch start.

Type: Object

Properties

PlanTrainer#batchEnd

batchEnd event. Triggered after batch end.

Type: Object

Properties

  • epoch number Current epoch.
  • batch number Current batch.
  • loss number? Batch loss.
  • metrics Object? Dictionary containing metrics (if any defined in the outputs).

PlanInputSpec

Object that describes Plan input. Parameters known to PlanTrainer (like training data, model parameters, batch size, etc.) are mapped into Plan arguments according to this object.

Parameters

  • type string Input argument type.
  • name string? Optional argument name. (optional, default null)
  • index number? Optional argument index (to take from array). (optional, default null)
  • value any? Argument value. (optional, default null)

TYPE_DATA

Represents training data (substituted with PlanTrainer's data batch)

TYPE_TARGET

Represents training targets aka labels (substituted with PlanTrainer's target batch)

TYPE_BATCH_SIZE

Represents batch size (substituted with PlanTrainer's batchSize).

TYPE_CLIENT_CONFIG_PARAM

Represents parameter from client config configured in FL model, name argument is required (substituted with parameter from PlanTrainer's clientConfig).

TYPE_VALUE

Represents any value, value argument is required.

TYPE_MODEL_PARAM

Represents model parameter (substituted with SyftModel contents).

PlanOutputSpec

Object that describes Plan output. Values returned from Plan (like loss, accuracy, model parameters, etc.) are mapped into PlanTrainer's internal state according to this object.

Parameters

  • type string Output variable type.
  • name string? Optional name. (optional, default null)
  • index number? Optional index (to put into array). (optional, default null)

TYPE_LOSS

Represents loss value (maps to PlanTrainer's loss).

TYPE_METRIC

Represents metric value, name is required (maps to PlanTrainer's metrics dictionary).

TYPE_MODEL_PARAM

Represents model parameter (maps to SyftModel parameters)

PlanTrainerCheckpoint

Object that stores PlanTrainer state, to resume training from it.

Parameters

  • parameters Object Dictionary of parameters
    • parameters.epochs number Total number of epochs
    • parameters.stepsPerEpoch number? Max steps per epoch
    • parameters.batchSize number Batch size
    • parameters.clientConfig Object Client config
    • parameters.epoch number Current epoch
    • parameters.batch number Current batch number
    • parameters.currentModel SyftModel Current state of the Model

toJSON

Returns PlanTrainerCheckpoint serialized to plain Object.

Returns Promise<Object>

fromJSON

Creates PlanTrainerCheckpoint from object.

Parameters

  • worker Syft Syft Worker
  • obj Object Object containing checkpoint data

Returns PlanTrainerCheckpoint

Dataset

Abstract class for Dataset. getItem method and length getter must be defined in the child class.

Properties

  • getItem Function Returns a sample
  • length Number Length of the datasets

Examples

class MyDataset extends Dataset {
  constructor() {
    super();
    this.data = [1, 2, 3, 4, 5].map(i => tf.tensor(i));
    this.labels = [0, 0, 1, 0, 1].map(i => tf.tensor(i));
  }

  getItem(index) {
    return [this.data[index], this.labels[index]];
  }

  get length() {
    return this.data.length;
  }
}

const ds = new MyDataset();
ds[0][0].print() // => Tensor 1
ds[0][1].print() // => Tensor 0

DataLoader

DataLoader controls fetching the data from the Dataset, including shuffling and batching. Implements iterable protocol to iterate over data samples.

Note: currently it only supports tf.Tensor data in the dataset, and collates batches using TFJS.

Parameters

  • parameters Object
    • parameters.dataset Dataset Dataset to load
    • parameters.batchSize Number Batch size for batching (optional, default 1)
    • parameters.shuffle Boolean Shuffle the Dataset (optional, default true)
    • parameters.dropLast Boolean Skip the last chunk if it is smaller than the batchSize (optional, default false)

Properties

Examples

const loader = new DataLoader({dataset, batchSize: 32})
consle.log('number of batches: ', loader.length)
for (let batch of loader) {
  // ...
}

iterator

Iterator producing data batches.

Returns any