@epfml/discojs 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (104) hide show
  1. package/README.md +32 -0
  2. package/dist/aggregation.d.ts +3 -0
  3. package/dist/aggregation.js +19 -0
  4. package/dist/async_buffer.d.ts +41 -0
  5. package/dist/async_buffer.js +98 -0
  6. package/dist/async_informant.d.ts +20 -0
  7. package/dist/async_informant.js +69 -0
  8. package/dist/client/base.d.ts +36 -0
  9. package/dist/client/base.js +34 -0
  10. package/dist/client/decentralized.d.ts +23 -0
  11. package/dist/client/decentralized.js +275 -0
  12. package/dist/client/federated.d.ts +30 -0
  13. package/dist/client/federated.js +221 -0
  14. package/dist/client/index.d.ts +4 -0
  15. package/dist/client/index.js +11 -0
  16. package/dist/client/local.d.ts +8 -0
  17. package/dist/client/local.js +34 -0
  18. package/dist/dataset/data_loader/data_loader.d.ts +16 -0
  19. package/dist/dataset/data_loader/data_loader.js +10 -0
  20. package/dist/dataset/data_loader/image_loader.d.ts +14 -0
  21. package/dist/dataset/data_loader/image_loader.js +93 -0
  22. package/dist/dataset/data_loader/index.d.ts +3 -0
  23. package/dist/dataset/data_loader/index.js +9 -0
  24. package/dist/dataset/data_loader/tabular_loader.d.ts +29 -0
  25. package/dist/dataset/data_loader/tabular_loader.js +88 -0
  26. package/dist/dataset/dataset_builder.d.ts +17 -0
  27. package/dist/dataset/dataset_builder.js +80 -0
  28. package/dist/dataset/index.d.ts +2 -0
  29. package/dist/dataset/index.js +7 -0
  30. package/dist/index.d.ts +17 -0
  31. package/dist/index.js +34 -0
  32. package/dist/logging/console_logger.d.ts +18 -0
  33. package/dist/logging/console_logger.js +33 -0
  34. package/dist/logging/index.d.ts +2 -0
  35. package/dist/logging/index.js +7 -0
  36. package/dist/logging/logger.d.ts +12 -0
  37. package/dist/logging/logger.js +9 -0
  38. package/dist/logging/trainer_logger.d.ts +24 -0
  39. package/dist/logging/trainer_logger.js +59 -0
  40. package/dist/memory/base.d.ts +53 -0
  41. package/dist/memory/base.js +9 -0
  42. package/dist/memory/empty.d.ts +12 -0
  43. package/dist/memory/empty.js +69 -0
  44. package/dist/memory/index.d.ts +3 -0
  45. package/dist/memory/index.js +9 -0
  46. package/dist/memory/model_type.d.ts +4 -0
  47. package/dist/memory/model_type.js +9 -0
  48. package/dist/model_actor.d.ts +16 -0
  49. package/dist/model_actor.js +20 -0
  50. package/dist/privacy.d.ts +12 -0
  51. package/dist/privacy.js +60 -0
  52. package/dist/serialization/index.d.ts +2 -0
  53. package/dist/serialization/index.js +6 -0
  54. package/dist/serialization/model.d.ts +5 -0
  55. package/dist/serialization/model.js +55 -0
  56. package/dist/serialization/weights.d.ts +5 -0
  57. package/dist/serialization/weights.js +62 -0
  58. package/dist/task/data_example.d.ts +5 -0
  59. package/dist/task/data_example.js +24 -0
  60. package/dist/task/display_information.d.ts +15 -0
  61. package/dist/task/display_information.js +53 -0
  62. package/dist/task/index.d.ts +3 -0
  63. package/dist/task/index.js +8 -0
  64. package/dist/task/model_compile_data.d.ts +6 -0
  65. package/dist/task/model_compile_data.js +12 -0
  66. package/dist/task/task.d.ts +10 -0
  67. package/dist/task/task.js +32 -0
  68. package/dist/task/training_information.d.ts +29 -0
  69. package/dist/task/training_information.js +2 -0
  70. package/dist/tasks/cifar10.d.ts +4 -0
  71. package/dist/tasks/cifar10.js +74 -0
  72. package/dist/tasks/index.d.ts +5 -0
  73. package/dist/tasks/index.js +9 -0
  74. package/dist/tasks/lus_covid.d.ts +4 -0
  75. package/dist/tasks/lus_covid.js +48 -0
  76. package/dist/tasks/mnist.d.ts +4 -0
  77. package/dist/tasks/mnist.js +56 -0
  78. package/dist/tasks/simple_face.d.ts +4 -0
  79. package/dist/tasks/simple_face.js +88 -0
  80. package/dist/tasks/titanic.d.ts +4 -0
  81. package/dist/tasks/titanic.js +86 -0
  82. package/dist/testing/tester.d.ts +5 -0
  83. package/dist/testing/tester.js +21 -0
  84. package/dist/training/disco.d.ts +12 -0
  85. package/dist/training/disco.js +62 -0
  86. package/dist/training/index.d.ts +2 -0
  87. package/dist/training/index.js +7 -0
  88. package/dist/training/trainer/distributed_trainer.d.ts +21 -0
  89. package/dist/training/trainer/distributed_trainer.js +60 -0
  90. package/dist/training/trainer/local_trainer.d.ts +10 -0
  91. package/dist/training/trainer/local_trainer.js +37 -0
  92. package/dist/training/trainer/round_tracker.d.ts +30 -0
  93. package/dist/training/trainer/round_tracker.js +44 -0
  94. package/dist/training/trainer/trainer.d.ts +66 -0
  95. package/dist/training/trainer/trainer.js +146 -0
  96. package/dist/training/trainer/trainer_builder.d.ts +25 -0
  97. package/dist/training/trainer/trainer_builder.js +102 -0
  98. package/dist/training/training_schemes.d.ts +5 -0
  99. package/dist/training/training_schemes.js +10 -0
  100. package/dist/training_informant.d.ts +88 -0
  101. package/dist/training_informant.js +135 -0
  102. package/dist/types.d.ts +4 -0
  103. package/dist/types.js +2 -0
  104. package/package.json +48 -0
@@ -0,0 +1,4 @@
1
+ import * as tf from '@tensorflow/tfjs';
2
+ import { Task } from '..';
3
+ export declare const task: Task;
4
+ export declare function model(): tf.LayersModel;
@@ -0,0 +1,86 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.model = exports.task = void 0;
4
+ var tslib_1 = require("tslib");
5
+ var tf = (0, tslib_1.__importStar)(require("@tensorflow/tfjs"));
6
+ exports.task = {
7
+ taskID: 'titanic',
8
+ displayInformation: {
9
+ taskTitle: 'Titanic',
10
+ summary: "Test our platform by using a publicly available <b>tabular</b> dataset. <br><br> Download the passenger list from the Titanic shipwreck <a class='underline text-primary-dark dark:text-primary-light' href='https://www.kaggle.com/c/titanic'>here</a>. <br> This model predicts the type of person most likely to survive/die based on their characteristics (sex, age, class etc.).",
11
+ overview: 'We all know the unfortunate story of the Titanic: this flamboyant new transatlantic boat that sunk in 1912 in the North Atlantic Ocean. Today, we revist this tragedy by trying to predict the survival odds of the passenger given some basic features.',
12
+ model: 'The current form of the model does not normalize the given data and applies a very simple pre-processing of the data.',
13
+ tradeoffs: 'We are using a small model for this task: 2 connected layers with few nodes and we are using no optimization techniques implemented. This allows fast training but can yield to poor performance.',
14
+ dataFormatInformation: 'This model takes as input a CSV file with 12 columns. general information about the passenger (sex, age, name ...) and specific related Titanic data such as the ticket class bought by the passenger, its cabin number ... \n pclass: A proxy for socio-economic status (SES) \n 1st = Upper \n 2nd = Middle \n 3rd = Lower \n \n age: Age is fractional if less than 1. If the age is estimated, is it in the form of xx.5 \n \n sibsp: The dataset defines family relations in this way... \n Sibling = brother, sister, stepbrother, stepsister \n Spouse = husband, wife (mistresses and fiancés were ignored) \n \n parch: The dataset defines family relations in this way.. \n Parent = mother, father \n Child = daughter, son, stepdaughter, stepson \n Some children travelled only with a nanny, therefore parch=0 for them. <br><br> The first line of the csv contains the header: <br> PassengerId, Survived, Pclass, Name, Sex, Age, SibSp, Parch, Ticket, Fare, Cabin, Embarked <br> Each susequent row contains the corresponding data.',
15
+ dataExampleText: 'Below one can find an example of a datapoint taken as input by our model. In this datapoint, the person is young man named Owen Harris that unfortunnalty perished with the Titanic. He boarded the boat in South Hamptons and was a 3rd class passenger.On the testing page, the data should not contain the class column (Survived).',
16
+ dataExample: [
17
+ { columnName: 'PassengerId', columnData: '1' },
18
+ { columnName: 'Survived', columnData: '0' },
19
+ { columnName: 'Name', columnData: 'Braund, Mr. Owen Harris' },
20
+ { columnName: 'Sex', columnData: 'male' },
21
+ { columnName: 'Age', columnData: '22' },
22
+ { columnName: 'SibSp', columnData: '1' },
23
+ { columnName: 'Parch', columnData: '0' },
24
+ { columnName: 'Ticket', columnData: '1/5 21171' },
25
+ { columnName: 'Fare', columnData: '7.25' },
26
+ { columnName: 'Cabin', columnData: 'E46' },
27
+ { columnName: 'Embarked', columnData: 'S' },
28
+ { columnName: 'Pclass', columnData: '3' }
29
+ ],
30
+ headers: [
31
+ 'PassengerId',
32
+ 'Survived',
33
+ 'Name',
34
+ 'Sex',
35
+ 'Age',
36
+ 'SibSp',
37
+ 'Parch',
38
+ 'Ticket',
39
+ 'Fare',
40
+ 'Cabin',
41
+ 'Embarked',
42
+ 'Pclass'
43
+ ]
44
+ },
45
+ trainingInformation: {
46
+ modelID: 'titanic-model',
47
+ epochs: 20,
48
+ roundDuration: 10,
49
+ validationSplit: 0.2,
50
+ batchSize: 30,
51
+ preprocessFunctions: [],
52
+ modelCompileData: {
53
+ optimizer: 'rmsprop',
54
+ loss: 'binaryCrossentropy',
55
+ metrics: ['accuracy']
56
+ },
57
+ receivedMessagesThreshold: 1,
58
+ dataType: 'tabular',
59
+ inputColumns: [
60
+ 'PassengerId',
61
+ 'Age',
62
+ 'SibSp',
63
+ 'Parch',
64
+ 'Fare',
65
+ 'Pclass'
66
+ ],
67
+ outputColumns: [
68
+ 'Survived'
69
+ ],
70
+ scheme: 'Federated'
71
+ }
72
+ };
73
+ function model() {
74
+ var model = tf.sequential();
75
+ model.add(tf.layers.dense({
76
+ inputShape: [6],
77
+ units: 124,
78
+ activation: 'relu',
79
+ kernelInitializer: 'leCunNormal'
80
+ }));
81
+ model.add(tf.layers.dense({ units: 64, activation: 'relu' }));
82
+ model.add(tf.layers.dense({ units: 32, activation: 'relu' }));
83
+ model.add(tf.layers.dense({ units: 1, activation: 'sigmoid' }));
84
+ return model;
85
+ }
86
+ exports.model = model;
@@ -0,0 +1,5 @@
1
+ import { ModelActor } from '../model_actor';
2
+ export declare class Tester extends ModelActor {
3
+ testModel(): Promise<boolean>;
4
+ }
5
+ export default Tester;
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Tester = void 0;
4
+ var tslib_1 = require("tslib");
5
+ var model_actor_1 = require("../model_actor");
6
+ var Tester = /** @class */ (function (_super) {
7
+ (0, tslib_1.__extends)(Tester, _super);
8
+ function Tester() {
9
+ return _super !== null && _super.apply(this, arguments) || this;
10
+ }
11
+ Tester.prototype.testModel = function () {
12
+ return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
13
+ return (0, tslib_1.__generator)(this, function (_a) {
14
+ return [2 /*return*/, true];
15
+ });
16
+ });
17
+ };
18
+ return Tester;
19
+ }(model_actor_1.ModelActor));
20
+ exports.Tester = Tester;
21
+ exports.default = Tester;
@@ -0,0 +1,12 @@
1
+ import { Memory } from '@/memory';
2
+ import { Client, dataset, Logger, Task, TrainingInformant, TrainingSchemes } from '..';
3
+ export declare class Disco {
4
+ readonly task: Task;
5
+ readonly logger: Logger;
6
+ readonly memory: Memory;
7
+ private readonly client;
8
+ private readonly trainer;
9
+ constructor(task: Task, logger: Logger, memory: Memory, scheme: TrainingSchemes, informant: TrainingInformant, client: Client);
10
+ startTraining(data: dataset.Data): Promise<void>;
11
+ stopTraining(): Promise<void>;
12
+ }
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Disco = void 0;
4
+ var tslib_1 = require("tslib");
5
+ var __1 = require("..");
6
+ var trainer_builder_1 = require("./trainer/trainer_builder");
7
+ // Handles the training loop, server communication & provides the user with feedback.
8
+ var Disco = /** @class */ (function () {
9
+ // client need to be connected
10
+ function Disco(task, logger, memory, scheme, informant, client) {
11
+ this.task = task;
12
+ this.logger = logger;
13
+ this.memory = memory;
14
+ this.client = client;
15
+ if (client.task !== task) {
16
+ throw new Error('client not setup for given task');
17
+ }
18
+ if (informant.taskID !== task.taskID) {
19
+ throw new Error('informant not setup for given task');
20
+ }
21
+ var trainerBuilder = new trainer_builder_1.TrainerBuilder(this.memory, this.task, informant);
22
+ this.trainer = trainerBuilder.build(this.client, scheme !== __1.TrainingSchemes.LOCAL);
23
+ }
24
+ Disco.prototype.startTraining = function (data) {
25
+ return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
26
+ return (0, tslib_1.__generator)(this, function (_a) {
27
+ switch (_a.label) {
28
+ case 0:
29
+ if (data.size === 0) {
30
+ this.logger.error('Training aborted. No uploaded file given as input.');
31
+ throw new Error('No data in dataset');
32
+ }
33
+ this.logger.success('Thank you for your contribution. Data preprocessing has started');
34
+ return [4 /*yield*/, this.trainer];
35
+ case 1: return [4 /*yield*/, (_a.sent()).trainModel(data.dataset)];
36
+ case 2:
37
+ _a.sent();
38
+ return [2 /*return*/];
39
+ }
40
+ });
41
+ });
42
+ };
43
+ // Stops the training function
44
+ //
45
+ // do not disconnect given client
46
+ Disco.prototype.stopTraining = function () {
47
+ return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
48
+ return (0, tslib_1.__generator)(this, function (_a) {
49
+ switch (_a.label) {
50
+ case 0: return [4 /*yield*/, this.trainer];
51
+ case 1: return [4 /*yield*/, (_a.sent()).stopTraining()];
52
+ case 2:
53
+ _a.sent();
54
+ this.logger.success('Training was successfully interrupted.');
55
+ return [2 /*return*/];
56
+ }
57
+ });
58
+ });
59
+ };
60
+ return Disco;
61
+ }());
62
+ exports.Disco = Disco;
@@ -0,0 +1,2 @@
1
+ export { Disco } from './disco';
2
+ export { TrainingSchemes } from './training_schemes';
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TrainingSchemes = exports.Disco = void 0;
4
+ var disco_1 = require("./disco");
5
+ Object.defineProperty(exports, "Disco", { enumerable: true, get: function () { return disco_1.Disco; } });
6
+ var training_schemes_1 = require("./training_schemes");
7
+ Object.defineProperty(exports, "TrainingSchemes", { enumerable: true, get: function () { return training_schemes_1.TrainingSchemes; } });
@@ -0,0 +1,21 @@
1
+ import * as tf from '@tensorflow/tfjs';
2
+ import { Client, Memory, Task, TrainingInformant } from '@/.';
3
+ import { Trainer } from './trainer';
4
+ /**
5
+ * Class whose role is to train a model in a distributed way with a given dataset.
6
+ */
7
+ export declare class DistributedTrainer extends Trainer {
8
+ private readonly previousRoundModel;
9
+ private readonly client;
10
+ /** DistributedTrainer constructor, accepts same arguments as Trainer and in additional also a client who takes care of communicating weights.
11
+ */
12
+ constructor(task: Task, trainingInformant: TrainingInformant, memory: Memory, model: tf.LayersModel, previousRoundModel: tf.LayersModel, client: Client);
13
+ /**
14
+ * Callback called every time a round is over
15
+ */
16
+ onRoundEnd(accuracy: number): Promise<void>;
17
+ /**
18
+ * Callback called once training is over
19
+ */
20
+ onTrainEnd(): Promise<void>;
21
+ }
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DistributedTrainer = void 0;
4
+ var tslib_1 = require("tslib");
5
+ var trainer_1 = require("./trainer");
6
+ /**
7
+ * Class whose role is to train a model in a distributed way with a given dataset.
8
+ */
9
+ var DistributedTrainer = /** @class */ (function (_super) {
10
+ (0, tslib_1.__extends)(DistributedTrainer, _super);
11
+ /** DistributedTrainer constructor, accepts same arguments as Trainer and in additional also a client who takes care of communicating weights.
12
+ */
13
+ function DistributedTrainer(task, trainingInformant, memory, model, previousRoundModel, client) {
14
+ var _this = _super.call(this, task, trainingInformant, memory, model) || this;
15
+ _this.previousRoundModel = previousRoundModel;
16
+ _this.client = client;
17
+ return _this;
18
+ }
19
+ /**
20
+ * Callback called every time a round is over
21
+ */
22
+ DistributedTrainer.prototype.onRoundEnd = function (accuracy) {
23
+ return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
24
+ var currentRoundWeights, previousRoundWeights, aggregatedWeights;
25
+ return (0, tslib_1.__generator)(this, function (_a) {
26
+ switch (_a.label) {
27
+ case 0:
28
+ currentRoundWeights = this.model.weights.map(function (w) { return w.read(); });
29
+ previousRoundWeights = this.previousRoundModel.weights.map(function (w) { return w.read(); });
30
+ return [4 /*yield*/, this.client.onRoundEndCommunication(currentRoundWeights, previousRoundWeights, this.roundTracker.round, this.trainingInformant)];
31
+ case 1:
32
+ aggregatedWeights = _a.sent();
33
+ this.previousRoundModel.setWeights(currentRoundWeights);
34
+ this.model.setWeights(aggregatedWeights);
35
+ return [4 /*yield*/, this.memory.updateWorkingModel(this.task.taskID, this.trainingInformation.modelID, this.model)];
36
+ case 2:
37
+ _a.sent();
38
+ return [2 /*return*/];
39
+ }
40
+ });
41
+ });
42
+ };
43
+ /**
44
+ * Callback called once training is over
45
+ */
46
+ DistributedTrainer.prototype.onTrainEnd = function () {
47
+ return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
48
+ return (0, tslib_1.__generator)(this, function (_a) {
49
+ switch (_a.label) {
50
+ case 0: return [4 /*yield*/, this.client.onTrainEndCommunication(this.model.weights.map(function (w) { return w.read(); }), this.trainingInformant)];
51
+ case 1:
52
+ _a.sent();
53
+ return [2 /*return*/];
54
+ }
55
+ });
56
+ });
57
+ };
58
+ return DistributedTrainer;
59
+ }(trainer_1.Trainer));
60
+ exports.DistributedTrainer = DistributedTrainer;
@@ -0,0 +1,10 @@
1
+ import { Trainer } from './trainer';
2
+ /** Class whose role is to locally train a model with a given dataset.
3
+ */
4
+ export declare class LocalTrainer extends Trainer {
5
+ /**
6
+ * Callback called every time a round is over
7
+ */
8
+ onRoundEnd(accuracy: number): Promise<void>;
9
+ onTrainEnd(): Promise<void>;
10
+ }
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LocalTrainer = void 0;
4
+ var tslib_1 = require("tslib");
5
+ var trainer_1 = require("./trainer");
6
+ /** Class whose role is to locally train a model with a given dataset.
7
+ */
8
+ var LocalTrainer = /** @class */ (function (_super) {
9
+ (0, tslib_1.__extends)(LocalTrainer, _super);
10
+ function LocalTrainer() {
11
+ return _super !== null && _super.apply(this, arguments) || this;
12
+ }
13
+ /**
14
+ * Callback called every time a round is over
15
+ */
16
+ LocalTrainer.prototype.onRoundEnd = function (accuracy) {
17
+ return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
18
+ return (0, tslib_1.__generator)(this, function (_a) {
19
+ switch (_a.label) {
20
+ case 0: return [4 /*yield*/, this.memory.updateWorkingModel(this.task.taskID, this.trainingInformation.modelID, this.model)];
21
+ case 1:
22
+ _a.sent();
23
+ return [2 /*return*/];
24
+ }
25
+ });
26
+ });
27
+ };
28
+ LocalTrainer.prototype.onTrainEnd = function () {
29
+ return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
30
+ return (0, tslib_1.__generator)(this, function (_a) {
31
+ return [2 /*return*/];
32
+ });
33
+ });
34
+ };
35
+ return LocalTrainer;
36
+ }(trainer_1.Trainer));
37
+ exports.LocalTrainer = LocalTrainer;
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Class that keeps track of the current batch in order for the trainer to query when round has ended.
3
+ *
4
+ * @remark
5
+ * In distributed training, the client trains locally for a certain amount of epochs before sharing his weights to the server/neighbor, this
6
+ * is what we call a round.
7
+ *
8
+ * The role of the RoundTracker is to keep track of when a roundHasEnded using the current batch number. The batch in the RoundTracker is cumulative whereas
9
+ * in the onBatchEnd it is not (it resets to 0 after each epoch).
10
+ *
11
+ * The roundDuration is the length of a round (in batches).
12
+ */
13
+ export declare class RoundTracker {
14
+ round: number;
15
+ batch: number;
16
+ roundDuration: number;
17
+ constructor(roundDuration: number);
18
+ /**
19
+ * Update the batch number, to be called inside onBatchEnd. (We do not use batch output of onBatchEnd since it is
20
+ * not cumulative).
21
+ */
22
+ updateBatch(): void;
23
+ /**
24
+ * Returns true if a local round has ended, false otherwise.
25
+ *
26
+ * @remark
27
+ * Returns true if (batch) mod (batches per round) == 0, false otherwise
28
+ */
29
+ roundHasEnded(): boolean;
30
+ }
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RoundTracker = void 0;
4
+ /**
5
+ * Class that keeps track of the current batch in order for the trainer to query when round has ended.
6
+ *
7
+ * @remark
8
+ * In distributed training, the client trains locally for a certain amount of epochs before sharing his weights to the server/neighbor, this
9
+ * is what we call a round.
10
+ *
11
+ * The role of the RoundTracker is to keep track of when a roundHasEnded using the current batch number. The batch in the RoundTracker is cumulative whereas
12
+ * in the onBatchEnd it is not (it resets to 0 after each epoch).
13
+ *
14
+ * The roundDuration is the length of a round (in batches).
15
+ */
16
+ var RoundTracker = /** @class */ (function () {
17
+ function RoundTracker(roundDuration) {
18
+ this.round = 0;
19
+ this.batch = 0;
20
+ this.roundDuration = roundDuration;
21
+ }
22
+ /**
23
+ * Update the batch number, to be called inside onBatchEnd. (We do not use batch output of onBatchEnd since it is
24
+ * not cumulative).
25
+ */
26
+ RoundTracker.prototype.updateBatch = function () {
27
+ this.batch += 1;
28
+ };
29
+ /**
30
+ * Returns true if a local round has ended, false otherwise.
31
+ *
32
+ * @remark
33
+ * Returns true if (batch) mod (batches per round) == 0, false otherwise
34
+ */
35
+ RoundTracker.prototype.roundHasEnded = function () {
36
+ if (this.batch === 0) {
37
+ return false;
38
+ }
39
+ var roundHasEnded = this.batch % this.roundDuration === 0;
40
+ return roundHasEnded;
41
+ };
42
+ return RoundTracker;
43
+ }());
44
+ exports.RoundTracker = RoundTracker;
@@ -0,0 +1,66 @@
1
+ import * as tf from '@tensorflow/tfjs';
2
+ import { Memory, Task, TrainingInformant, TrainingInformation } from '@/.';
3
+ import { RoundTracker } from './round_tracker';
4
+ import { TrainerLog } from '../../logging/trainer_logger';
5
+ /** Abstract class whose role is to train a model with a given dataset. This can be either done
6
+ * locally or in a distributed way. The Trainer works as follows:
7
+ *
8
+ * 1. Call trainModel(dataset) to start training
9
+ * 2. Once a batch ends, onBatchEnd is triggered, witch will then call onRoundEnd once the round has ended.
10
+ *
11
+ * The onRoundEnd needs to be implemented to specify what actions to do when the round has ended. To know when
12
+ * a round has ended we use the roundTracker object.
13
+ */
14
+ export declare abstract class Trainer {
15
+ readonly task: Task;
16
+ readonly trainingInformant: TrainingInformant;
17
+ readonly memory: Memory;
18
+ readonly model: tf.LayersModel;
19
+ readonly trainingInformation: TrainingInformation;
20
+ readonly roundTracker: RoundTracker;
21
+ private stopTrainingRequested;
22
+ private readonly trainerLogger;
23
+ /**
24
+ * Constructs the training manager.
25
+ * @param task the trained task
26
+ * @param trainingInformant the training informant
27
+ */
28
+ constructor(task: Task, trainingInformant: TrainingInformant, memory: Memory, model: tf.LayersModel);
29
+ /**
30
+ * Every time a round ends this function will be called
31
+ */
32
+ protected abstract onRoundEnd(accuracy: number): Promise<void>;
33
+ /**
34
+ * When the training ends this function will be call
35
+ */
36
+ protected abstract onTrainEnd(): Promise<void>;
37
+ /**
38
+ * Request stop training to be used from the Disco instance or any class that is taking care of the trainer.
39
+ */
40
+ stopTraining(): Promise<void>;
41
+ /**
42
+ * Start training the model with the given dataset
43
+ * @param dataset
44
+ */
45
+ trainModel(dataset: tf.data.Dataset<tf.TensorContainer>): Promise<void>;
46
+ /**
47
+ * Format accuracy
48
+ */
49
+ private roundDecimals;
50
+ /**
51
+ * We update the training graph, this needs to be done on epoch end as there is no validation accuracy onBatchEnd.
52
+ */
53
+ private onEpochEnd;
54
+ /** onBatchEnd callback, when a round ends, we call onRoundEnd (to be implemented for local and distributed instances)
55
+ */
56
+ private onBatchEnd;
57
+ /**
58
+ * reset stop training state
59
+ */
60
+ private resetStopTrainerState;
61
+ /**
62
+ * If stop training is requested, do so
63
+ */
64
+ private stopTrainModelIfRequested;
65
+ getTrainerLog(): TrainerLog;
66
+ }
@@ -0,0 +1,146 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Trainer = void 0;
4
+ var tslib_1 = require("tslib");
5
+ var round_tracker_1 = require("./round_tracker");
6
+ var trainer_logger_1 = require("../../logging/trainer_logger");
7
+ /** Abstract class whose role is to train a model with a given dataset. This can be either done
8
+ * locally or in a distributed way. The Trainer works as follows:
9
+ *
10
+ * 1. Call trainModel(dataset) to start training
11
+ * 2. Once a batch ends, onBatchEnd is triggered, witch will then call onRoundEnd once the round has ended.
12
+ *
13
+ * The onRoundEnd needs to be implemented to specify what actions to do when the round has ended. To know when
14
+ * a round has ended we use the roundTracker object.
15
+ */
16
+ var Trainer = /** @class */ (function () {
17
+ /**
18
+ * Constructs the training manager.
19
+ * @param task the trained task
20
+ * @param trainingInformant the training informant
21
+ */
22
+ function Trainer(task, trainingInformant, memory, model) {
23
+ this.task = task;
24
+ this.trainingInformant = trainingInformant;
25
+ this.memory = memory;
26
+ this.model = model;
27
+ this.stopTrainingRequested = false;
28
+ this.trainerLogger = new trainer_logger_1.TrainerLogger();
29
+ var trainingInformation = task.trainingInformation;
30
+ if (trainingInformation === undefined) {
31
+ throw new Error('round duration is undefined');
32
+ }
33
+ this.trainingInformation = trainingInformation;
34
+ this.roundTracker = new round_tracker_1.RoundTracker(trainingInformation.roundDuration);
35
+ }
36
+ //* ***************************************************************
37
+ //* Functions to be used by the training manager
38
+ //* ***************************************************************
39
+ /**
40
+ * Request stop training to be used from the Disco instance or any class that is taking care of the trainer.
41
+ */
42
+ Trainer.prototype.stopTraining = function () {
43
+ return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
44
+ return (0, tslib_1.__generator)(this, function (_a) {
45
+ this.stopTrainingRequested = true;
46
+ return [2 /*return*/];
47
+ });
48
+ });
49
+ };
50
+ /**
51
+ * Start training the model with the given dataset
52
+ * @param dataset
53
+ */
54
+ Trainer.prototype.trainModel = function (dataset) {
55
+ return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
56
+ var _this = this;
57
+ return (0, tslib_1.__generator)(this, function (_a) {
58
+ switch (_a.label) {
59
+ case 0:
60
+ this.resetStopTrainerState();
61
+ // Assign callbacks and start training
62
+ return [4 /*yield*/, this.model.fitDataset(dataset.batch(this.trainingInformation.batchSize), {
63
+ epochs: this.trainingInformation.epochs,
64
+ validationData: dataset.batch(this.trainingInformation.batchSize),
65
+ callbacks: {
66
+ onEpochEnd: function (epoch, logs) { return _this.onEpochEnd(epoch, logs); },
67
+ onBatchEnd: function (epoch, logs) { return (0, tslib_1.__awaiter)(_this, void 0, void 0, function () { return (0, tslib_1.__generator)(this, function (_a) {
68
+ switch (_a.label) {
69
+ case 0: return [4 /*yield*/, this.onBatchEnd(epoch, logs)];
70
+ case 1: return [2 /*return*/, _a.sent()];
71
+ }
72
+ }); }); }
73
+ }
74
+ })];
75
+ case 1:
76
+ // Assign callbacks and start training
77
+ _a.sent();
78
+ return [2 /*return*/];
79
+ }
80
+ });
81
+ });
82
+ };
83
+ /**
84
+ * Format accuracy
85
+ */
86
+ Trainer.prototype.roundDecimals = function (accuracy, decimalsToRound) {
87
+ if (decimalsToRound === void 0) { decimalsToRound = 2; }
88
+ return +(accuracy * 100).toFixed(decimalsToRound);
89
+ };
90
+ /**
91
+ * We update the training graph, this needs to be done on epoch end as there is no validation accuracy onBatchEnd.
92
+ */
93
+ Trainer.prototype.onEpochEnd = function (epoch, logs) {
94
+ this.trainerLogger.onEpochEnd(epoch, logs);
95
+ if (logs !== undefined && !isNaN(logs.acc) && !isNaN(logs.val_acc)) {
96
+ this.trainingInformant.updateTrainingAccuracyGraph(this.roundDecimals(logs.acc));
97
+ this.trainingInformant.updateValidationAccuracyGraph(this.roundDecimals(logs.val_acc));
98
+ }
99
+ else {
100
+ this.trainerLogger.error('onEpochEnd: NaN value');
101
+ }
102
+ };
103
+ /** onBatchEnd callback, when a round ends, we call onRoundEnd (to be implemented for local and distributed instances)
104
+ */
105
+ Trainer.prototype.onBatchEnd = function (_, logs) {
106
+ return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
107
+ return (0, tslib_1.__generator)(this, function (_a) {
108
+ switch (_a.label) {
109
+ case 0:
110
+ if (logs === undefined) {
111
+ return [2 /*return*/];
112
+ }
113
+ this.roundTracker.updateBatch();
114
+ this.stopTrainModelIfRequested();
115
+ if (!this.roundTracker.roundHasEnded()) return [3 /*break*/, 2];
116
+ return [4 /*yield*/, this.onRoundEnd(logs.acc)];
117
+ case 1:
118
+ _a.sent();
119
+ _a.label = 2;
120
+ case 2: return [2 /*return*/];
121
+ }
122
+ });
123
+ });
124
+ };
125
+ /**
126
+ * reset stop training state
127
+ */
128
+ Trainer.prototype.resetStopTrainerState = function () {
129
+ this.model.stopTraining = false;
130
+ this.stopTrainingRequested = false;
131
+ };
132
+ /**
133
+ * If stop training is requested, do so
134
+ */
135
+ Trainer.prototype.stopTrainModelIfRequested = function () {
136
+ if (this.stopTrainingRequested) {
137
+ this.model.stopTraining = true;
138
+ this.stopTrainingRequested = false;
139
+ }
140
+ };
141
+ Trainer.prototype.getTrainerLog = function () {
142
+ return this.trainerLogger.log;
143
+ };
144
+ return Trainer;
145
+ }());
146
+ exports.Trainer = Trainer;
@@ -0,0 +1,25 @@
1
+ import { Client, Task, TrainingInformant, Memory } from '../..';
2
+ import { Trainer } from './trainer';
3
+ /**
4
+ * A class that helps build the Trainer and auxiliary classes.
5
+ */
6
+ export declare class TrainerBuilder {
7
+ private readonly memory;
8
+ private readonly task;
9
+ private readonly trainingInformant;
10
+ constructor(memory: Memory, task: Task, trainingInformant: TrainingInformant);
11
+ /**
12
+ * Builds a trainer object.
13
+ *
14
+ * @param client client to share weights with (either distributed or federated)
15
+ * @param distributed whether to build a distributed or local trainer
16
+ * @returns
17
+ */
18
+ build(client: Client, distributed?: boolean): Promise<Trainer>;
19
+ /**
20
+ * If a model exists in memory, laod it, otherwise load model from server
21
+ * @returns
22
+ */
23
+ private getModel;
24
+ private updateModelInformation;
25
+ }