UNPKG

simple-tensorflow

Version:

Simplificación de la libreria tensorflow.js para que sea mas facil de usar.

114 lines (113 loc) 5.97 kB
const tf = require('@tensorflow/tfjs-node') let activaciones = [ "elu","hardSigmoid","linear","relu","relu6","selu","sigmoid","softmax","softplus","softsign","tanh","swish","mish"] let errores = ["meanAbsoluteError", "meanAbsolutePercentageError", "meanSquaredLogarithmicError", "squaredHinge", "hinge", "categoricalHinge", "meanSquaredError", "logcosh", "categoricalCrossentropy","sparseCategoricalCrossentropy","binaryCrossentropy","kullbackLeiblerDivergence","poisson","cosineProximity"] let optimizadores = ["sgd", "momentum", "adagrad", "adadelta", "adam", "adamax", "rmsprop"] module.exports = { Modelo: function (capas, error, optimizador) { if (capas == undefined || !Array.isArray(capas)) throw new Error("Debe proporcionar un array de capas") if (error == undefined || errores.indexOf(error) == -1) throw new Error("Debe proporcionar un tipo de error valido") if (optimizador == undefined || optimizadores.indexOf(optimizador) == -1) throw new Error("Debe proporcionar un tipo de optimizador valido") this.model = tf.sequential() capas.forEach(capa => { this.model.add(capa) }) this.model.compile({ loss: error, optimizer: optimizador }); this.entrenar = async function (entrada, salida, opciones) { if (entrada == undefined || !Array.isArray(entrada)) throw new Error("Debe proporcionar un array unidimensional/multidimensional de datos de entrada") if (salida == undefined || !Array.isArray(salida)) throw new Error("Debe proporcionar un array de salidas unidimensional/multidmensional a los datos de entrada") if (opciones == undefined) { opciones = { iteraciones: 10, logs: 1 } } if (opciones.iteraciones == undefined) { opciones.iteraciones = 10 } if (typeof opciones.iteraciones !== 'number') throw new Error("Debe proporcionar un numero de iteraciones y no algo distinto.") if (opciones.iteraciones < 1) { opciones.iteraciones = 1 } if (opciones.logs == undefined) { opciones.logs = 1 } if (typeof opciones.logs !== 'number' || opciones.logs < 0 || opciones.logs > 2) throw new Error("Logs debe ser un numero entre 0 y 2") if (opciones.aleatorio == undefined) { opciones.aleatorio = false } if (typeof opciones.aleatorio !== 'boolean') throw new Error("Logs debe ser true o false") entrada = tf.tensor(entrada) salida = tf.tensor(salida) await this.model.fit(entrada, salida, { epochs: opciones.iteraciones, verbose: opciones.logs, shuffle: opciones.aleatorio }).then(() => { return "Entrenamiento Completado" }) } this.predecir = function (entrada) { if (entrada == undefined || !Array.isArray(entrada)) throw new Error("Debe proporcionar un array unidimensional/multidimensional de datos de entrada") entrada = tf.tensor(entrada) return this.model.predict(entrada).arraySync() } }, Dense: function (info) { if (info == undefined || typeof info !== 'object') throw new Error("Debe proporcionar un objeto con la configuración basica.") if (info.neuronas == undefined || typeof info.neuronas !== 'number') throw new Error("Debe proporcionar un numero de neuronas para esta capa.") if (info.activacion !== undefined && typeof info.activacion !== 'string') throw new Error("Debe proporcionar un texto con la función de activación.") this.neuronas = info.neuronas this.entrada = info.entrada this.activation = info.activacion if (this.activation == undefined) { this.activation = 'sigmoid' } if(activaciones.indexOf(this.activation) == -1) throw new Error("No es una función de activación valida.") if (this.entrada == undefined) { return tf.layers.dense({ units: this.neuronas, activation: this.activation }) } else { return tf.layers.dense({ units: this.neuronas, inputShape: this.entrada, activation: this.activation }) } }, BatchNormalization: function(momentum,entrada){ this.momento = momentum this.entrada = entrada if(this.momento == undefined && this.entrada == undefined){ return tf.layers.batchNormalization() } else if(this.momento !== undefined && this.entrada == undefined){ return tf.layers.batchNormalization({momentum:this.momento}) } else if(this.momento == undefined && this.entrada !== undefined){ return tf.layers.batchNormalization({inputShape:this.entrada}) } else { return tf.layers.batchNormalization({momentum:this.momento,inputShape:this.entrada}) } }, Reshape: function(objetivo,entrada){ if (objetivo == undefined) throw new Error("Debe proporcionar una forma para reestructurar los datos.") this.entrada = entrada this.objetivo = objetivo if(this.entrada == undefined){ return tf.layers.reshape({targetShape:this.objetivo}) } else { return tf.layers.reshape({targetShape:this.objetivo,inputShape:this.entrada}) } }, Conv2D: function(entrada){ this.entrada = entrada if(this.entrada == undefined){ return tf.layers.conv2d() } else { return tf.layers.conv2d({inputShape:this.entrada}) } } }