import { Order } from 'blockly/javascript';
/**
* @category AI
* @subcategory Models
* @module ModelProperties
*/
/**
* Adds a layer instance on top of the layer stack.
*
* @param {SequentialModel} model Sequential model to add layer on.
* @param {Layer} layer Layer instance.
* @returns {Void} model.add(layer)
*/
function add(block, generator) {
const model = generator.valueToCode(block, 'MODEL_NAME', Order.NONE) || '';
const layer = generator.valueToCode(block, 'LAYER', Order.NONE) || '';
return `${model}.add(${layer})`;
}
/**
* Configures and prepares the model for training and evaluation.
*
* @param {LayersModel} model The model compile.
* @param {String} optimizer An instance of tf.train.Optimizer or a string name for an Optimizer.
* @param {String | String[]} loss (string|string[]|{[outputName: string]: string}|LossOrMetricFn| LossOrMetricFn[]|{[outputName: string]: LossOrMetricFn}) Object function(s) or name(s) of object function(s). If the model has multiple outputs, you can use a different loss on each output by passing a dictionary or an Array of losses. The loss value that will be minimized by the model will then be the sum of all individual losses.
* @param {String} metrics (string|LossOrMetricFn|Array| {[outputName: string]: string | LossOrMetricFn}) List of metrics to be evaluated by the model during training and testing. Typically you will use metrics=['accuracy']. To specify different metrics for different outputs of a multi-output model, you could also pass a dictionary.
* @returns {Void} model.compile({optimizer, loss, metrics})
*/
function compile(block, generator) {
const model = generator.valueToCode(block, 'MODEL_NAME', Order.NONE) || '';
const optimizer = generator.valueToCode(block, 'OPTIMIZER', Order.NONE) || '';
const loss = generator.valueToCode(block, 'LOSS', Order.NONE) || '';
const metrics = generator.valueToCode(block, 'METRICS', Order.NONE) || '';
return `${model}.compile({optimizer: ${optimizer}, loss: ${loss}, metrics: ${metrics}})`;
}
/**
* Trains the model for a fixed number of epochs (iterations on a dataset).
*
* @param {LayersModel} model The model compile.
* @param {Tensor} x An tf.Tensor of training data, or an array of tf.Tensors if the model has multiple inputs. If all inputs in the model are named, you can also pass a dictionary mapping input names to tf.Tensors.
* @param {Tensor} y tf.Tensor of target (label) data, or an array of tf.Tensors if the model has multiple outputs. If all outputs in the model are named, you can also pass a dictionary mapping output names to tf.Tensors.
* @param {Number} epochs Integer number of times to iterate over the training data arrays.
* @param {Number} batchSize Number of samples per gradient update. If unspecified, it will default to 32.
* @returns {Promise} await model.fit(xs, ys, {epochs, batchSize})
*/
function fit(block, generator) {
const model = generator.valueToCode(block, 'MODEL_NAME', Order.NONE) || '';
const xs = generator.valueToCode(block, 'XS', Order.NONE) || '';
const ys = generator.valueToCode(block, 'YS', Order.NONE) || '';
const epochs = generator.valueToCode(block, 'EPOCH', Order.NONE) || '';
const batchSize = generator.valueToCode(block, 'BATCH', Order.NONE) || 32;
return `await ${model}.fit(${xs}, ${ys}, {epochs: ${epochs}, batchSize: ${batchSize}, callbacks:{
onEpochEnd: (epoch,logs) => {
if (${epochs} < 10) { // Always print if epochs are less than 10
print('epoch: ', epoch + 1);
print(objectToString(logs));
} else {
const epochStep = Math.floor(${epochs}/10);
if(epoch % epochStep === 0){
print('epoch: ', epoch + 1);
print(objectToString(logs));
}
}
}
}})`;
}
/**
* Execute the inference for the input tensors.
*
* @param {LayersModel} model The model compile.
* @param {Tensor} tensor The input data, as a Tensor, or an Array of tf.Tensors if the model has multiple inputs.
* @returns {Tensor} model.predict(tensor)
*/
function predict(block, generator) {
const model = generator.valueToCode(block, 'MODEL_NAME', Order.NONE) || '';
const tensor = generator.valueToCode(block, 'TENSOR', Order.NONE) || '';
return [`${model}.predict(${tensor})`, Order.NONE];
}
export const modelClassMethodsBlockGenerator = {
['add_method']: add,
['compile_method']: compile,
['fit_method']: fit,
['predict_method']: predict,
};
//# sourceMappingURL=generators.js.map
Source