Long Short Term Memory Multivariate Time Series with Tensorflow

### Hierarchy

- LSTMTimeSeries
  - LSTMMultivariateTimeSeries

## Index

### Constructors

- constructor

### Properties

- compiled
- createDataset
- drop
- getDropableColumns
- getInputShape
- getTimeseriesDataSet
- getTimeseriesShape
- layers
- loss
- model
- reshape
- seriesToSupervised
- settings
- tf
- tokenizer
- trained
- type
- xShape
- yShape

### Methods

- calculate
- exportConfiguration
- generateLayers
- importConfiguration
- loadModel
- predict
- saveModel
- train
- createDataset
- drop
- getDropableColumns
- getInputShape
- getTimeseriesDataSet
- getTimeseriesShape
- reshape
- seriesToSupervised

## Constructors

### constructor

- new LSTMMultivariateTimeSeries(options?: TensorScriptOptions, properties?: TensorScriptProperties): LSTMMultivariateTimeSeries

- Overrides LSTMTimeSeries.constructor

  - Defined in src/lstm_multivariate_time_series.ts:277

  #### Parameters

  - ##### Default value options: TensorScriptOptions = {}

    neural network configuration and tensorflow model hyperparameters

  - ##### Optional properties: TensorScriptProperties

    extra instance properties

  #### Returns LSTMMultivariateTimeSeries

## Properties

### compiled

compiled: boolean

Inherited from TensorScriptModelInterface.compiled

- Defined in src/model_interface.ts:220

### createDataset

createDataset: (...args: any[]) => NestedArray<number>

Overrides LSTMTimeSeries.createDataset

- Defined in src/lstm_multivariate_time_series.ts:272

#### Type declaration

- - (...args: any[]): NestedArray<number>

  - #### Parameters

    - ##### Rest ...args: any[]

    #### Returns NestedArray<number>

### drop

drop: (...args: any[]) => any

- Defined in src/lstm_multivariate_time_series.ts:275

#### Type declaration

- - (...args: any[]): any

  - #### Parameters

    - ##### Rest ...args: any[]

    #### Returns any

### getDropableColumns

getDropableColumns: (...args: any[]) => any

- Defined in src/lstm_multivariate_time_series.ts:274

#### Type declaration

- - (...args: any[]): any

  - #### Parameters

    - ##### Rest ...args: any[]

    #### Returns any

### getInputShape

getInputShape: (...args: any[]) => any

Inherited from TensorScriptModelInterface.getInputShape

- Defined in src/model_interface.ts:222

#### Type declaration

- - (...args: any[]): any

  - #### Parameters

    - ##### Rest ...args: any[]

    #### Returns any

### getTimeseriesDataSet

getTimeseriesDataSet: (...args: any[]) => any

Overrides LSTMTimeSeries.getTimeseriesDataSet

- Defined in src/lstm_multivariate_time_series.ts:277

#### Type declaration

- - (...args: any[]): any

  - #### Parameters

    - ##### Rest ...args: any[]

    #### Returns any

### getTimeseriesShape

getTimeseriesShape: (...args: any[]) => any

Overrides LSTMTimeSeries.getTimeseriesShape

- Defined in src/lstm_multivariate_time_series.ts:276

#### Type declaration

- - (...args: any[]): any

  - #### Parameters

    - ##### Rest ...args: any[]

    #### Returns any

### Optional layers

layers: TensorScriptSavedLayers

Inherited from LSTMTimeSeries.layers

Overrides TensorScriptModelInterface.layers

- Defined in src/lstm_time_series.ts:16

### Optional loss

loss: undefined | number

Inherited from TensorScriptModelInterface.loss

- Defined in src/model_interface.ts:227

### model

model: any

Inherited from TensorScriptModelInterface.model

- Defined in src/model_interface.ts:216

### reshape

reshape: (...args: any[]) => any

Inherited from TensorScriptModelInterface.reshape

- Defined in src/model_interface.ts:221

#### Type declaration

- - (...args: any[]): any

  - #### Parameters

    - ##### Rest ...args: any[]

    #### Returns any

### seriesToSupervised

seriesToSupervised: (...args: any[]) => Array<number>

- Defined in src/lstm_multivariate_time_series.ts:273

#### Type declaration

- - (...args: any[]): Array<number>

  - #### Parameters

    - ##### Rest ...args: any[]

    #### Returns Array<number>

### settings

settings: TensorScriptOptions

Inherited from TensorScriptModelInterface.settings

- Defined in src/model_interface.ts:215

### tf

tf: any

Inherited from TensorScriptModelInterface.tf

- Defined in src/model_interface.ts:218

### tokenizer

tokenizer: any

Inherited from TensorScriptModelInterface.tokenizer

- Defined in src/model_interface.ts:217

### trained

trained: boolean

Inherited from TensorScriptModelInterface.trained

- Defined in src/model_interface.ts:219

### type

type: string

Inherited from TensorScriptModelInterface.type

- Defined in src/model_interface.ts:214

### Optional xShape

xShape: number[]

Inherited from TensorScriptModelInterface.xShape

- Defined in src/model_interface.ts:223

### Optional yShape

yShape: number[]

Inherited from TensorScriptModelInterface.yShape

- Defined in src/model_interface.ts:224

## Methods

### calculate

- calculate(x_matrix: Matrix | Vector | InputTextArray): any

- Inherited from LSTMTimeSeries.calculate

  Overrides BaseNeuralNetwork.calculate

  - Defined in src/lstm_time_series.ts:240

  #### Parameters

  - ##### x_matrix: Matrix | Vector | InputTextArray

  #### Returns any

### exportConfiguration

- exportConfiguration(): TensorScriptContext

- Inherited from TensorScriptModelInterface.exportConfiguration

  - Defined in src/model_interface.ts:352

  #### Returns TensorScriptContext

### generateLayers

- generateLayers(x_matrix: Matrix, y_matrix: Matrix, layers: TensorScriptSavedLayers): void

- Overrides LSTMTimeSeries.generateLayers

  - Defined in src/lstm_multivariate_time_series.ts:321

  Adds dense layers to tensorflow classification model

  override

  #### Parameters

  - ##### x_matrix: Matrix

    independent variables

  - ##### y_matrix: Matrix

    dependent variables

  - ##### layers: TensorScriptSavedLayers

    model dense layer parameters

  #### Returns void

### importConfiguration

- importConfiguration(configuration: TensorScriptContext): void

- Inherited from TensorScriptModelInterface.importConfiguration

  - Defined in src/model_interface.ts:363

  #### Parameters

  - ##### configuration: TensorScriptContext

  #### Returns void

### loadModel

- loadModel(options: string): Promise<any>

- Inherited from TensorScriptModelInterface.loadModel

  - Defined in src/model_interface.ts:402

  Loads a saved tensoflow / keras model, this is an alias for

  see

    https://www.tensorflow.org/js/guide/save_load#loading_a_tfmodel

  #### Parameters

  - ##### options: string

    tensorflow load model options

  #### Returns Promise<any>

  tensorflow model

### predict

- predict(input_matrix: Matrix | Vector | InputTextArray, options?: PredictionOptions): Promise<any>

- Inherited from LSTMTimeSeries.predict

  Overrides TensorScriptModelInterface.predict

  - Defined in src/lstm_time_series.ts:246

  #### Parameters

  - ##### input_matrix: Matrix | Vector | InputTextArray

  - ##### Default value options: PredictionOptions = {}

  #### Returns Promise<any>

### saveModel

- saveModel(options: string): Promise<any>

- Inherited from TensorScriptModelInterface.saveModel

  - Defined in src/model_interface.ts:415

  saves a tensorflow model, this is an alias for

  see

    https://www.tensorflow.org/js/guide/save_load#save_a_tfmodel

  #### Parameters

  - ##### options: string

    tensorflow save model options

  #### Returns Promise<any>

  tensorflow model

### train

- train(this: TensorScriptLSTMModelContext, x_timeseries: any, y_timeseries: any, layers: TensorScriptLayers, x_test: Matrix, y_test: Matrix): Promise<any>

- Overrides LSTMTimeSeries.train

  - Defined in src/lstm_multivariate_time_series.ts:367
  override

  #### Parameters

  - ##### this: TensorScriptLSTMModelContext

  - ##### x_timeseries: any

  - ##### y_timeseries: any

  - ##### layers: TensorScriptLayers

  - ##### x_test: Matrix

  - ##### y_test: Matrix

  #### Returns Promise<any>

### Static createDataset

- createDataset(this: TensorScriptModelContext, dataset?: NestedArray<Array<number>>, look_back?: number): NestedArray<number>

- Overrides LSTMTimeSeries.createDataset

  - Defined in src/lstm_multivariate_time_series.ts:49

  Creates dataset data

  example

    const ds = [ [10, 20, 30, 40, 50, 60, 70, 80, 90,], [11, 21, 31, 41, 51, 61, 71, 81, 91,], [12, 22, 32, 42, 52, 62, 72, 82, 92,], [13, 23, 33, 43, 53, 63, 73, 83, 93,], [14, 24, 34, 44, 54, 64, 74, 84, 94,], [15, 25, 35, 45, 55, 65, 75, 85, 95,], [16, 26, 36, 46, 56, 66, 76, 86, 96,], [17, 27, 37, 47, 57, 67, 77, 87, 97,], [18, 28, 38, 48, 58, 68, 78, 88, 98,], [19, 29, 39, 49, 59, 69, 79, 89, 99,], ]; LSTMMultivariateTimeSeries.createDataset(ds,1) // => // [ // [ // [ 20, 30, 40, 50, 60, 70, 80, 90 ], // [ 21, 31, 41, 51, 61, 71, 81, 91 ], // [ 22, 32, 42, 52, 62, 72, 82, 92 ], // [ 23, 33, 43, 53, 63, 73, 83, 93 ], // [ 24, 34, 44, 54, 64, 74, 84, 94 ], // [ 25, 35, 45, 55, 65, 75, 85, 95 ], // [ 26, 36, 46, 56, 66, 76, 86, 96 ], // [ 27, 37, 47, 57, 67, 77, 87, 97 ], // [ 28, 38, 48, 58, 68, 78, 88, 98 ] // ], //x_matrix // [ 11, 12, 13, 14, 15, 16, 17, 18, 19 ], //y_matrix // 8 //features // ]

  override

  #### Parameters

  - ##### this: TensorScriptModelContext

  - ##### Default value dataset: NestedArray<Array<number>> = []

    array of values

  - ##### Default value look_back: number = 1

    number of values in each feature

  #### Returns NestedArray<number>

  returns x matrix and y matrix for model trainning

### Static drop

- drop(data: NestedArray<Array<number>>, columns: Array<number>): NestedArray<Array<number>> | number[]

- - Defined in src/lstm_multivariate_time_series.ts:105

  Drops columns by array index

  example

    const data = [ [ 10, 20, 30, 40, 50, 60, 70, 80, 90, 11, 21, 31, 41, 51, 61, 71, 81, 91 ], [ 11, 21, 31, 41, 51, 61, 71, 81, 91, 12, 22, 32, 42, 52, 62, 72, 82, 92 ], [ 12, 22, 32, 42, 52, 62, 72, 82, 92, 13, 23, 33, 43, 53, 63, 73, 83, 93 ], [ 13, 23, 33, 43, 53, 63, 73, 83, 93, 14, 24, 34, 44, 54, 64, 74, 84, 94 ], [ 14, 24, 34, 44, 54, 64, 74, 84, 94, 15, 25, 35, 45, 55, 65, 75, 85, 95 ], [ 15, 25, 35, 45, 55, 65, 75, 85, 95, 16, 26, 36, 46, 56, 66, 76, 86, 96 ], [ 16, 26, 36, 46, 56, 66, 76, 86, 96, 17, 27, 37, 47, 57, 67, 77, 87, 97 ], [ 17, 27, 37, 47, 57, 67, 77, 87, 97, 18, 28, 38, 48, 58, 68, 78, 88, 98 ], [ 18, 28, 38, 48, 58, 68, 78, 88, 98, 19, 29, 39, 49, 59, 69, 79, 89, 99 ] ]; const n_in = 1; //lookbacks const n_out = 1; const dropColumns = getDropableColumns(8, n_in, n_out); // =>[ 10, 11, 12, 13, 14, 15, 16, 17 ] const newdata = drop(data,dropColumns); //=> [ // [ 10, 20, 30, 40, 50, 60, 70, 80, 90, 11 ], // [ 11, 21, 31, 41, 51, 61, 71, 81, 91, 12 ], // [ 12, 22, 32, 42, 52, 62, 72, 82, 92, 13 ], // [ 13, 23, 33, 43, 53, 63, 73, 83, 93, 14 ], // [ 14, 24, 34, 44, 54, 64, 74, 84, 94, 15 ], // [ 15, 25, 35, 45, 55, 65, 75, 85, 95, 16 ], // [ 16, 26, 36, 46, 56, 66, 76, 86, 96, 17 ], // [ 17, 27, 37, 47, 57, 67, 77, 87, 97, 18 ], // [ 18, 28, 38, 48, 58, 68, 78, 88, 98, 19 ] //]

  #### Parameters

  - ##### data: NestedArray<Array<number>>

    data set to drop columns

  - ##### columns: Array<number>

    array of column indexes

  #### Returns NestedArray<Array<number>> | number[]

  matrix with dropped columns

### Static getDropableColumns

- getDropableColumns(features: number, n_in: number, n_out: number): number[]

- - Defined in src/lstm_multivariate_time_series.ts:208

  Calculates which columns to drop by index

  todo

    support multiple iterations in the future, also only one output variable supported in column features * lookbacks -1

  example

    const ds = [ [10, 20, 30, 40, 50, 60, 70, 80, 90,], [11, 21, 31, 41, 51, 61, 71, 81, 91,], [12, 22, 32, 42, 52, 62, 72, 82, 92,], [13, 23, 33, 43, 53, 63, 73, 83, 93,], [14, 24, 34, 44, 54, 64, 74, 84, 94,], [15, 25, 35, 45, 55, 65, 75, 85, 95,], [16, 26, 36, 46, 56, 66, 76, 86, 96,], [17, 27, 37, 47, 57, 67, 77, 87, 97,], [18, 28, 38, 48, 58, 68, 78, 88, 98,], [19, 29, 39, 49, 59, 69, 79, 89, 99,], ]; const n_in = 1; //lookbacks const n_out = 1; const dropped = getDropableColumns(8, n_in, n_out); //=> [ 10, 11, 12, 13, 14, 15, 16, 17 ]

  #### Parameters

  - ##### features: number

    number of independent variables

  - ##### n_in: number

    look backs

  - ##### n_out: number

    future iterations (currently only 1 supported)

  #### Returns number[]

  array indexes to drop

### Static getInputShape

- getInputShape(matrix?: any): Shape

- Inherited from TensorScriptModelInterface.getInputShape

  - Defined in src/model_interface.ts:333

  Returns the shape of an input matrix

  function
  example

    const input = [ [ 0, 1, ], [ 1, 0, ], ]; TensorScriptModelInterface.getInputShape(input) // => [2,2]

  see

    {https://stackoverflow.com/questions/10237615/get-size-of-dimensions-in-array}

  #### Parameters

  - ##### Default value matrix: any = []

    input matrix

  #### Returns Shape

  returns the shape of a matrix (e.g. [2,2])

### Static getTimeseriesDataSet

- getTimeseriesDataSet(this: TensorScriptModelContext, timeseries: NestedArray<number[]> | undefined, look_back: any): { xShape: number | number[]; x_matrix: number[][] | number[]; yShape: number | number[]; y_matrix: number[] }

- Overrides LSTMTimeSeries.getTimeseriesDataSet

  - Defined in src/lstm_multivariate_time_series.ts:248

  Returns data for predicting values

  override

  #### Parameters

  - ##### this: TensorScriptModelContext

  - ##### timeseries: NestedArray<number[]> | undefined

  - ##### look_back: any

  #### Returns { xShape: number | number[]; x_matrix: number[][] | number[]; yShape: number | number[]; y_matrix: number[] }

  - ##### xShape: number | number[]

  - ##### x_matrix: number[][] | number[]

  - ##### yShape: number | number[]

  - ##### y_matrix: number[]

### Static getTimeseriesShape

- getTimeseriesShape(this: TensorScriptModelContext, x_timeseries: NestedArray<any> | undefined): any[]

- Overrides LSTMTimeSeries.getTimeseriesShape

  - Defined in src/lstm_multivariate_time_series.ts:230

  Reshape input to be [samples, time steps, features]

  example
  override

    LSTMTimeSeries.getTimeseriesShape([ [ [ 1 ], [ 2 ], [ 3 ] ], [ [ 2 ], [ 3 ], [ 4 ] ], [ [ 3 ], [ 4 ], [ 5 ] ], [ [ 4 ], [ 5 ], [ 6 ] ], [ [ 5 ], [ 6 ], [ 7 ] ], [ [ 6 ], [ 7 ], [ 8 ] ], ]) //=> [6, 1, 3,]

  #### Parameters

  - ##### this: TensorScriptModelContext

  - ##### x_timeseries: NestedArray<any> | undefined

    dataset array of values

  #### Returns any[]

  returns proper timeseries forecasting shape

### Static reshape

- reshape(array: Vector, shape: Shape): Matrix | Vector

- Inherited from TensorScriptModelInterface.reshape

  - Defined in src/model_interface.ts:268

  Reshapes an array

  function
  example

    const array = [ 0, 1, 1, 0, ]; const shape = [2,2]; TensorScriptModelInterface.reshape(array,shape) // => [ [ 0, 1, ], [ 1, 0, ], ];

  #### Parameters

  - ##### array: Vector

    input array

  - ##### shape: Shape

    shape array

  #### Returns Matrix | Vector

  returns a matrix with the defined shape

### Static seriesToSupervised

- seriesToSupervised(data: number[], n_in?: number, n_out?: number): number[]

- - Defined in src/lstm_multivariate_time_series.ts:151

  Converts data set to supervised labels for forecasting, the first column must be the dependent variable

  example

    const ds = [ [10, 20, 30, 40, 50, 60, 70, 80, 90,], [11, 21, 31, 41, 51, 61, 71, 81, 91,], [12, 22, 32, 42, 52, 62, 72, 82, 92,], [13, 23, 33, 43, 53, 63, 73, 83, 93,], [14, 24, 34, 44, 54, 64, 74, 84, 94,], [15, 25, 35, 45, 55, 65, 75, 85, 95,], [16, 26, 36, 46, 56, 66, 76, 86, 96,], [17, 27, 37, 47, 57, 67, 77, 87, 97,], [18, 28, 38, 48, 58, 68, 78, 88, 98,], [19, 29, 39, 49, 59, 69, 79, 89, 99,], ]; const n_in = 1; //lookbacks const n_out = 1; const series = seriesToSupervised(ds, n_in, n_out); //=> [ // [ 10, 20, 30, 40, 50, 60, 70, 80, 90, 11, 21, 31, 41, 51, 61, 71, 81, 91 ], // [ 11, 21, 31, 41, 51, 61, 71, 81, 91, 12, 22, 32, 42, 52, 62, 72, 82, 92 ], // [ 12, 22, 32, 42, 52, 62, 72, 82, 92, 13, 23, 33, 43, 53, 63, 73, 83, 93 ], // [ 13, 23, 33, 43, 53, 63, 73, 83, 93, 14, 24, 34, 44, 54, 64, 74, 84, 94 ], // [ 14, 24, 34, 44, 54, 64, 74, 84, 94, 15, 25, 35, 45, 55, 65, 75, 85, 95 ], // [ 15, 25, 35, 45, 55, 65, 75, 85, 95, 16, 26, 36, 46, 56, 66, 76, 86, 96 ], // [ 16, 26, 36, 46, 56, 66, 76, 86, 96, 17, 27, 37, 47, 57, 67, 77, 87, 97 ], // [ 17, 27, 37, 47, 57, 67, 77, 87, 97, 18, 28, 38, 48, 58, 68, 78, 88, 98 ], // [ 18, 28, 38, 48, 58, 68, 78, 88, 98, 19, 29, 39, 49, 59, 69, 79, 89, 99 ] //];

  todo

    support multiple future iterations

  #### Parameters

  - ##### data: number[]

    data set

  - ##### Default value n_in: number = 1

    look backs

  - ##### Default value n_out: number = 1

    future iterations (only 1 supported)

  #### Returns number[]

  multivariate dataset for time series