Abstract class used to create a new TensorFlow.js model and train it from a dataset DataTraining. Call
The constructor takes an object with parameters :

  • dataTraining : a DataTraining instance to load data.
  • epoch : integer number of times to iterate over the training data arrays. Default to 10
  • batchSize : number of samples per gradient update. If unspecified, it will default to 128.
  • trainingRatio : a number between 0 and 1, default to 1, the %/100 of the training set used during training.
  • testingRatio : a number between 0 and 1, default to 0.9, the %/100 of the testing set used for validation.
  • nameToSave : if specified, the model will be saved as this name with tf.LayersModel.save after training, e.g. 'downloads://my-model'.
  • nameToLoad : if specified, the model won't be trained but loaded with tf.loadLayersModel.
  • useTfjsVis : default to false, if true, the code shows additional information through tfjs-vis.
  • consoleInfo : default to false, if true, the code shows addition information through the console log.
  • optimizer : a tf.Optimizer instance for the training. If unspecified, it will default to `tf.train.adam(1e-3, .5, .999, 1e-8)`

Extend this class to create your own model logic, all you have to do is implement the method initModel that creates and returns a new Sequential model, not compiled. The method finalizeModel adds the last dense layer to the number of classes and compiles the model, you can also override this one to modify the behavior of the last layers or how to compile the model.

Hierarchy

Constructors

Properties

batchSize: number
consoleInfo: boolean
epoch: number
model: LayersModel = null
nameToLoad: string
nameToSave: string
testingRatio: number
trainingRatio: number
useTfjsVis: boolean

Methods

  • Gets a sample of test data and returns the model output, with some basic operations to reshape the output as 1d tensor.

    Returns

    A list of two elements that are 1d tensors corresponding to a list [b1, b2, ...]. These two list are 0-1 lists for each batch element to be a bot trajectory (1 means bot).

    Parameters

    • size: number = 1000

      The batch size for the sample.

    Returns Tensor1D[]

  • Add the last layer that decides the class of the input for the classifier and compile the model.
    The last layer is dense with 'varianceScaling' kernel initializer, and we look at accuracy metric.
    When there is only one class in DataTraining ([0] for human, [1] for bot), we use a binaryCrossentropy loss function and the activation of the dense layer is the sigmoid function. If we have two classes ([1,0] for human, [0,1] for bot), it is the categoricalCrossentropy loss function and softmax activation.

    Returns

    The same model as input that has been compiled.

    Parameters

    • model: Sequential

      The model returned by initModel.

    Returns Sequential

  • Get prediction and labels and return the accuracy per class.

    Parameters

    • predicts: Tensor1D

      The model prediction reshaped as 1D tensor of 0 (human) or 1 (bot).

    • labels: Tensor1D

      The true labels, 1d tensor of 0 or 1.

    Returns Promise<{
        accuracy: number;
        count: number;
    }[]>

  • Get prediction and labels and return the confusion matrix.

    Parameters

    • predicts: Tensor1D

      The model prediction reshaped as 1D tensor of 0 (human) or 1 (bot).

    • labels: Tensor1D

      The true labels, 1d tensor of 0 or 1.

    Returns Promise<number[][]>

  • Create a new sequential model with tf.sequential(), add all hidden layers and returns the result. Notice that the input shape will always be [this.data.data.getXSize(), this.data.data.getYSize()] or [null, this.data.data.getYSize()] if we want to allow different time steps for recurrent models after training (however, for training, a recurrent model such as LSTM must have a fixed input shape for gradient descent).

    Returns

    The new sequential model.

    Returns Sequential

  • Run the entire training process, from data loading to model training, then model testing with accuracy and confusion matrix. If field nameToLoad is specified, training is ignored because we directly call loadExistingModel. If field nameToSave is specified, it save the model by calling tf.LayersModel.save.

    Returns Promise<void>

  • Get the model prediction batch and its corresponding labels as 1D tensors and show with tfjs-vis or in console (according to useTfjsVis and consoleInfo) the accuracy of each class and the confusion matrix.

    Parameters

    • predicts: Tensor1D

      The model prediction reshaped as 1D tensor of 0 (human) or 1 (bot).

    • labels: Tensor1D

      The true labels, 1d tensor of 0 or 1.

    • name: string = "validation"

      Default to "validation", used in the container name of tfjs-vis.

    Returns Promise<void>

  • Train the model with tf.LayersModel.fit. We first get a fragment of training and testing data according to trainingRatio and testingRatio, default set to 1 and 0.9 respectively. Then we fit the model with the corresponding batchSize and epochs.

    Returns Promise<History>

Generated using TypeDoc