ml5-library icon indicating copy to clipboard operation
ml5-library copied to clipboard

🧹 NeuralNetwork cleanup pt. 3 -- tasks

Open lindapaiste opened this issue 2 years ago • 0 comments

Introduces a level of abstraction for the "task" of the neural network:

getTask.ts

  • Define an interface NNTask which specifies what information a task needs to provide:
    • Its default layers.
    • Its compile options.
    • Any overrides to the NN default options.
    • How to create sample data to use when option noTraining is set.
    • Its name (not currently not used, possible 🪓)
  • Create three concrete implementations of that interface for the three tasks: classification, regression, and imageClassification.
    • imageClassification uses some of the same functions as classification.
    • getSampleData is only possible in a limited set of circumstances (see issue #1409), so I'm throwing a lot of specific errors when those circumstances aren't met.
  • Export a function getTask to convert a string task name into a NNTask object.

NeuralNetwork/index.ts

  • Constructor sets an instance property this.task with the task object based on options.task. This defaults to regression which seems to match what was in the default layers before.
  • Remove large blocks of code in addDefaultLayers() and compile() which was moved into the task objects. Now we call a function on this.task:
    • const layers = this.task.createLayers(inputUnits, hiddenUnits, outputUnits);
    • const options = this.task.getCompileOptions(this.options.learningRate);

NeuralNetwork/NeuralNetwork.ts

  • I'm like 99% sure that using optimizer.call(this, learningRate) to set the thisArg on the optimizer doesn't actually make a difference? (Please correct me if I am wrong). I removed setOptimizerFunction(), which, despite the name, just creates an optimizer and doesn't set anything. The instantiation of the optimizer is handled in the getCompileOptions() function of the task, which gets the learningRate as an argument.

lindapaiste avatar Jun 06 '22 18:06 lindapaiste