import { Order } from 'blockly/javascript';
/**
* @category AI
* @subcategory Layer
* @module BasicLayer
*/
/**
* Creates a dense (fully connected) layer.
*
* @param {Number} units Positive integer, dimensionality of the output space.
* @param {Number[] | null} inputShape If defined, will be used to create an input layer to insert before this layer. If both inputShape and batchInputShape are defined, batchInputShape will be used. This argument is only applicable to input layers (the first layer of a model).
* @param {String} activation ('elu'|'hardSigmoid'|'linear'|'relu'|'relu6'| 'selu'|'sigmoid'|'softmax'|'softplus'|'softsign'|'tanh'|'swish'|'mish'|'gelu'|'gelu_new') Activation function to use.
* @returns {Dense} tf.layers.dense({
* units, inputShape, activation}).
*/
function denseLayer(block, generator) {
const units = generator.valueToCode(block, 'UNITS', Order.NONE) || '';
const inputShape = generator.valueToCode(block, 'INPUT_SHAPE', Order.NONE) || '';
const activation = generator.valueToCode(block, 'ACTIVATION', Order.NONE) || '';
return [
`tf.layers.dense({units: ${units} ${inputShape ? `, inputShape: ${inputShape}` : ''} ${activation ? `, activation: ${activation}` : ''}})`,
Order.VOID,
];
}
/**
* Flattens the input. Does not affect the batch size.
*
* @param {Number} inputShape If defined, will be used to create an input layer to insert before this layer. If both inputShape and batchInputShape are defined, batchInputShape will be used. This argument is only applicable to input layers (the first layer of a model).
* @returns {Flatten} tf.layers.flatten({inputShape}).
*/
function flattenLayer(block, generator) {
const inputShape = generator.valueToCode(block, 'SHAPE', Order.NONE) || '';
return [`tf.layers.flatten({ ${inputShape ? `, inputShape: ${inputShape}` : ''}})`, Order.VOID];
}
/**
* Applies dropout to the input.
*
* @param {Number} rate Float between 0 and 1. Fraction of the input units to drop.
* @returns {Flatten} tf.layers.dropout({rate}).
*/
function dropoutLAyer(block, generator) {
const rate = generator.valueToCode(block, 'RATE', Order.NONE) || '';
return [`tf.layers.dropout({ rate: ${rate}})`, Order.VOID];
}
/**
* Maps positive integers (indices) into dense vectors of fixed size.
*
* @param {Number} inputDime Integer > 0. Size of the vocabulary, i.e. maximum integer index + 1.
* @param {Number} outputDim Integer >= 0. Dimension of the dense embedding.
* @param {Number | Number[]} inputLength Length of input sequences, when it is constant.
* @param {Boolean} trainable Whether the weights of this layer are updatable by fit. Defaults to true.
* @returns {Dense} tf.layers.embedding({ inputDim, outputDim, inputLength, trainable })
*/
function embeddingLayer(block, generator) {
const inputDim = generator.valueToCode(block, 'INPUT_DIM', Order.NONE) || '';
const outputDim = generator.valueToCode(block, 'OUTPUT_DIM', Order.NONE) || '';
const inputLength = generator.valueToCode(block, 'INPUT_LEN', Order.NONE) || '';
const isTrainable = block.getFieldValue('IS_TRAINABLE') || '';
return [
`tf.layers.embedding({ inputDim: ${inputDim}, outputDim: ${outputDim}, inputLength: ${inputLength}, trainable: ${isTrainable}, })`,
Order.VOID,
];
}
export const layerBlockGenerator = {
['add_dense_layer']: denseLayer,
['add_flatten_layer']: flattenLayer,
['add_dropout_layer']: dropoutLAyer,
['add_embedding_layer']: embeddingLayer,
};
//# sourceMappingURL=generators.js.map
Source