|
| 1 | +import * as tf from '@tensorflow/tfjs'; |
| 2 | +import { LayerJson, NeuralNetworkOptions } from "./types"; |
| 3 | + |
| 4 | +/** |
| 5 | + * Separate all task-dependent logic into separate task objects to minimize if/else behavior |
| 6 | + * in the main Neural Network class and make it easier to potentially add more tasks in the future. |
| 7 | + * May want these to be classes which get the nn instance in the constructor. |
| 8 | + */ |
| 9 | + |
| 10 | +export type TaskName = 'classification' | 'regression' | 'imageClassification'; |
| 11 | + |
| 12 | +export interface NNTask { |
| 13 | + name: TaskName; |
| 14 | + |
| 15 | + // Can optionally override the standard defaults with custom defaults |
| 16 | + getDefaultOptions?(): Partial<NeuralNetworkOptions>; |
| 17 | + |
| 18 | + // Note: learningRate is always the first arg of the optimizer, but some optimizers support other optional args as well |
| 19 | + getCompileOptions(learningRate: number): tf.ModelCompileArgs; |
| 20 | + |
| 21 | + createLayers(inputShape: tf.Shape, hiddenUnits: number, outputUnits: number): LayerJson[]; |
| 22 | + |
| 23 | + getSampleData(inputs: number | string[] | number[], outputs: number | string[]): { xs: number[], ys: (string | number)[] }[] |
| 24 | + |
| 25 | + // TODO: parseInputs and parseOutputs |
| 26 | +} |
| 27 | + |
| 28 | +// TODO: move elsewhere |
| 29 | +function isStringArray(value: any): value is string[] { |
| 30 | + return Array.isArray(value) && value.some(v => typeof v === 'string'); |
| 31 | +} |
| 32 | + |
| 33 | +// Handling of input sample is the same for all tasks. |
| 34 | +function getSampleInput(inputs: number | string[] | number[]): number[] { |
| 35 | + if (isStringArray(inputs)) { |
| 36 | + throw new Error(`'inputs' cannot be an array of property names when using option 'noTraining'. You must specify the number of inputs.`); |
| 37 | + } |
| 38 | + const inputSize = Array.isArray(inputs) ? inputs.reduce((a, b) => a * b) : inputs; |
| 39 | + return new Array(inputSize).fill(0); |
| 40 | +} |
| 41 | + |
| 42 | +const classificationTask: NNTask = { |
| 43 | + name: 'classification', |
| 44 | + getCompileOptions(learningRate) { |
| 45 | + return { |
| 46 | + loss: 'categoricalCrossentropy', |
| 47 | + optimizer: tf.train.sgd(learningRate), |
| 48 | + metrics: ['accuracy'], |
| 49 | + } |
| 50 | + }, |
| 51 | + createLayers(inputShape, hiddenUnits, outputUnits) { |
| 52 | + return [ |
| 53 | + { |
| 54 | + type: 'dense', |
| 55 | + units: hiddenUnits, |
| 56 | + activation: 'relu', |
| 57 | + inputShape |
| 58 | + }, |
| 59 | + { |
| 60 | + type: 'dense', |
| 61 | + activation: 'softmax', |
| 62 | + units: outputUnits, |
| 63 | + }, |
| 64 | + ]; |
| 65 | + }, |
| 66 | + getSampleData(inputs, outputs) { |
| 67 | + if (!isStringArray(outputs)) { |
| 68 | + throw new Error(`Invalid outputs ${outputs}. Outputs must be an array of label names when using option 'noTraining' with task 'classification'.`); |
| 69 | + } |
| 70 | + const xs = getSampleInput(inputs); |
| 71 | + return outputs.map(label => ({ xs, ys: [label] })); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +const imageClassificationTask: NNTask = { |
| 76 | + name: 'imageClassification', |
| 77 | + getDefaultOptions() { |
| 78 | + return { |
| 79 | + learningRate: 0.02 |
| 80 | + } |
| 81 | + }, |
| 82 | + getCompileOptions: classificationTask.getCompileOptions, |
| 83 | + createLayers(inputShape, hiddenUnits, outputUnits) { |
| 84 | + return [ |
| 85 | + { |
| 86 | + type: 'conv2d', |
| 87 | + filters: 8, |
| 88 | + kernelSize: 5, |
| 89 | + strides: 1, |
| 90 | + activation: 'relu', |
| 91 | + kernelInitializer: 'varianceScaling', |
| 92 | + inputShape, |
| 93 | + }, |
| 94 | + { |
| 95 | + type: 'maxPooling2d', |
| 96 | + poolSize: [2, 2], |
| 97 | + strides: [2, 2], |
| 98 | + }, |
| 99 | + { |
| 100 | + type: 'conv2d', |
| 101 | + filters: 16, |
| 102 | + kernelSize: 5, |
| 103 | + strides: 1, |
| 104 | + activation: 'relu', |
| 105 | + kernelInitializer: 'varianceScaling', |
| 106 | + }, |
| 107 | + { |
| 108 | + type: 'maxPooling2d', |
| 109 | + poolSize: [2, 2], |
| 110 | + strides: [2, 2], |
| 111 | + }, |
| 112 | + { |
| 113 | + type: 'flatten', |
| 114 | + }, |
| 115 | + { |
| 116 | + type: 'dense', |
| 117 | + kernelInitializer: 'varianceScaling', |
| 118 | + activation: 'softmax', |
| 119 | + units: outputUnits, |
| 120 | + }, |
| 121 | + ]; |
| 122 | + }, |
| 123 | + getSampleData: classificationTask.getSampleData |
| 124 | +} |
| 125 | + |
| 126 | +const regressionTask: NNTask = { |
| 127 | + name: 'regression', |
| 128 | + getCompileOptions(learningRate) { |
| 129 | + return { |
| 130 | + loss: 'meanSquaredError', |
| 131 | + optimizer: tf.train.adam(learningRate), |
| 132 | + metrics: ['accuracy'], |
| 133 | + }; |
| 134 | + }, |
| 135 | + createLayers(inputShape, hiddenUnits, outputUnits) { |
| 136 | + return [ |
| 137 | + { |
| 138 | + type: 'dense', |
| 139 | + units: hiddenUnits, |
| 140 | + activation: 'relu', |
| 141 | + inputShape |
| 142 | + }, |
| 143 | + { |
| 144 | + type: 'dense', |
| 145 | + activation: 'sigmoid', |
| 146 | + units: outputUnits, |
| 147 | + }, |
| 148 | + ]; |
| 149 | + }, |
| 150 | + getSampleData(inputs, outputs) { |
| 151 | + if (typeof outputs !== 'number') { |
| 152 | + throw new Error(`Invalid outputs ${outputs}. Outputs must be a number when using option 'noTraining' with task 'regression'.`); |
| 153 | + } |
| 154 | + return [{ |
| 155 | + xs: getSampleInput(inputs), |
| 156 | + ys: new Array(outputs).fill(0) |
| 157 | + }] |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +/** |
| 162 | + * Mapping of supported task configurations and their task names. |
| 163 | + * Use lowercase keys to make the lookup case-insensitive. |
| 164 | + */ |
| 165 | +const TASKS: Record<Lowercase<TaskName>, NNTask> = { |
| 166 | + regression: regressionTask, |
| 167 | + classification: classificationTask, |
| 168 | + imageclassification: imageClassificationTask, |
| 169 | +} |
| 170 | + |
| 171 | +/** |
| 172 | + * Get the correct task object based on the task name. |
| 173 | + */ |
| 174 | +export default function getTask(name: TaskName | string): NNTask { |
| 175 | + const task = TASKS[name.toLowerCase()]; |
| 176 | + if (!task) { |
| 177 | + throw new Error(`Unknown task name '${name}'. Task must be one of ${Object.keys(TASKS).join(', ')}`); |
| 178 | + } |
| 179 | + return task; |
| 180 | +} |
0 commit comments