use a corpus to generate features from an embedding layer with Tensorflow

implements

  {BaseNeuralNetwork}

### Hierarchy

- BaseNeuralNetwork
  - FeatureEmbedding

## Index

### Constructors

- constructor

### Properties

- compiled
- featureIds
- featureToId
- getContextPairs
- getFeatureDataSet
- getInputShape
- getMergedArray
- getTimeseriesShape
- idToFeature
- importedEmbeddings
- layers
- loss
- model
- numberOfFeatures
- reshape
- settings
- tf
- tokenizer
- trained
- type
- xShape
- yShape

### Methods

- calculate
- compileModel
- exportConfiguration
- exportEmbeddings
- findSimilarFeatures
- generateBatch
- generateLayers
- importConfiguration
- importEmbeddings
- labelWeights
- loadModel
- predict
- reduceWeights
- saveModel
- train
- trainOnBatch
- getContextPairs
- getFeatureDataSet
- getInputShape
- getMergedArray
- reshape

## Constructors

### constructor

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

- Overrides BaseNeuralNetwork.constructor

  - Defined in src/feature_embedding.ts:114

  #### Parameters

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

  - ##### Optional properties: TensorScriptProperties

  #### Returns FeatureEmbedding

## Properties

### compiled

compiled: boolean

Inherited from TensorScriptModelInterface.compiled

- Defined in src/model_interface.ts:220

### Optional featureIds

featureIds: Matrix

- Defined in src/feature_embedding.ts:22

### Optional featureToId

featureToId: FeatureToId

- Defined in src/feature_embedding.ts:20

### getContextPairs

getContextPairs: typeof getContextPairs

- Defined in src/feature_embedding.ts:114

### getFeatureDataSet

getFeatureDataSet: typeof getFeatureDataSet

- Defined in src/feature_embedding.ts:113

### 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

### getMergedArray

getMergedArray: typeof getMergedArray

- Defined in src/feature_embedding.ts:112

### Optional getTimeseriesShape

getTimeseriesShape: undefined | ((x_timeseries: NestedArray<any> | undefined) => Shape)

Inherited from TensorScriptModelInterface.getTimeseriesShape

- Defined in src/model_interface.ts:226

### Optional idToFeature

idToFeature: IdToFeature

- Defined in src/feature_embedding.ts:21

### Optional importedEmbeddings

importedEmbeddings: undefined | false | true

- Defined in src/feature_embedding.ts:25

### Optional layers

layers: TensorScriptLayers

Overrides TensorScriptModelInterface.layers

- Defined in src/feature_embedding.ts:19

### Optional loss

loss: undefined | number

Overrides TensorScriptModelInterface.loss

- Defined in src/feature_embedding.ts:24

### model

model: any

Inherited from TensorScriptModelInterface.model

- Defined in src/model_interface.ts:216

### Optional numberOfFeatures

numberOfFeatures: undefined | number

- Defined in src/feature_embedding.ts:23

### 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

### 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(): Promise<any>

- Overrides BaseNeuralNetwork.calculate

  - Defined in src/feature_embedding.ts:356

  #### Returns Promise<any>

### compileModel

- compileModel(__namedParameters?: { layers: undefined | { activation?: undefined | string; batchInputShape?: any; embeddingsInitializer?: any; inputDim?: undefined | number; inputLength?: undefined | number; inputShape?: any; kernelInitializer?: undefined | string; kernelRegularizer?: any; lambdaFunction?: undefined | string; lambdaOutputShape?: Matrix | Vector; outputDim?: undefined | number; returnSequences?: undefined | false | true; units?: undefined | number; weights?: Tensor }[] }): void

- - Defined in src/feature_embedding.ts:315

  #### Parameters

  - ##### Default value __namedParameters: { layers: undefined | { activation?: undefined | string; batchInputShape?: any; embeddingsInitializer?: any; inputDim?: undefined | number; inputLength?: undefined | number; inputShape?: any; kernelInitializer?: undefined | string; kernelRegularizer?: any; lambdaFunction?: undefined | string; lambdaOutputShape?: Matrix | Vector; outputDim?: undefined | number; returnSequences?: undefined | false | true; units?: undefined | number; weights?: Tensor }[] } = {}

    - ##### layers: undefined | { activation?: undefined | string; batchInputShape?: any; embeddingsInitializer?: any; inputDim?: undefined | number; inputLength?: undefined | number; inputShape?: any; kernelInitializer?: undefined | string; kernelRegularizer?: any; lambdaFunction?: undefined | string; lambdaOutputShape?: Matrix | Vector; outputDim?: undefined | number; returnSequences?: undefined | false | true; units?: undefined | number; weights?: Tensor }[]

  #### Returns void

### exportConfiguration

- exportConfiguration(): TensorScriptContext

- Inherited from TensorScriptModelInterface.exportConfiguration

  - Defined in src/model_interface.ts:352

  #### Returns TensorScriptContext

### exportEmbeddings

- exportEmbeddings(): Promise<{ featureIds: undefined | number[][]; featureToId: undefined | {}; idToFeature: undefined | {}; labeledWeights: {}; numberOfFeatures: undefined | number }>

- - Defined in src/feature_embedding.ts:254

  #### Returns Promise<{ featureIds: undefined | number[][]; featureToId: undefined | {}; idToFeature: undefined | {}; labeledWeights: {}; numberOfFeatures: undefined | number }>

### findSimilarFeatures

- findSimilarFeatures(weights: Matrix, options?: SimilarFeatureOptions): Promise<{}>

- - Defined in src/feature_embedding.ts:443

  Uses either cosineProximity or Eucledian distance to rank similarity

  example

    //weights = [ [1,2,3,], [1,2,2], [0,-1,3] ] //labeledWeights = [ {car:[1,2,3,],tesla:[1,2,2],boat:[0,-1,3]}] FeatureEmbeddingInstance.findSimilarFeatures(weights,{features:['car'], limit:2,}) //=> { car:[ { comparedFeature: 'tesla', proximity: -0.5087087154388428, distance: 0.03015853278338909 }, { comparedFeature: 'boat', proximity: -0.3032159209251404, distance: 0.036241017282009125 }, ] }

  #### Parameters

  - ##### weights: Matrix

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

  #### Returns Promise<{}>

### generateBatch

- generateBatch(__namedParameters: { epoch: number }): Promise<{ loss: number }>

- - Defined in src/feature_embedding.ts:219

  #### Parameters

  - ##### __namedParameters: { epoch: number }

    - ##### epoch: number

  #### Returns Promise<{ loss: number }>

### generateLayers

- generateLayers(this: FeatureEmbedding, x_matrix: Matrix, y_matrix: Matrix, layers?: TensorScriptLayers): void

- Overrides BaseNeuralNetwork.generateLayers

  - Defined in src/feature_embedding.ts:153

  Adds dense layers to tensorflow classification model

  override

  #### Parameters

  - ##### this: FeatureEmbedding

  - ##### x_matrix: Matrix

    independent variables

  - ##### y_matrix: Matrix

    dependent variables

  - ##### Optional layers: TensorScriptLayers

    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

### importEmbeddings

- importEmbeddings(__namedParameters: { addNewWeights: boolean; featureIds: undefined | number[][]; featureToId: undefined | {}; idToFeature: undefined | {}; inputMatrixFeatures: undefined | (string | number)[][]; labeledWeights: {}; numberOfFeatures: undefined | number }): Promise<void>

- - Defined in src/feature_embedding.ts:267

  #### Parameters

  - ##### __namedParameters: { addNewWeights: boolean; featureIds: undefined | number[][]; featureToId: undefined | {}; idToFeature: undefined | {}; inputMatrixFeatures: undefined | (string | number)[][]; labeledWeights: {}; numberOfFeatures: undefined | number }

    - ##### addNewWeights: boolean

    - ##### featureIds: undefined | number[][]

    - ##### featureToId: undefined | {}

    - ##### idToFeature: undefined | {}

    - ##### inputMatrixFeatures: undefined | (string | number)[][]

    - ##### labeledWeights: {}

      - ##### [index: string]: Vector

    - ##### numberOfFeatures: undefined | number

  #### Returns Promise<void>

### labelWeights

- labelWeights(weights: Matrix): {}

- - Defined in src/feature_embedding.ts:384

  Converts matrix of layer weights into labeled features

  example

    const weights = [ [1.5,1,4,1.6,3.5], [4.3,3.2,5.5,6.5] ] FeatureEmbeddingInstance.labelWeights(weights) //=> weights = { car:[1.5,1,4,1.6,3.5], boat:[4.3,3.2,5.5,6.5] }

  #### Parameters

  - ##### weights: Matrix

  #### Returns {}

  - ##### [index: string]: Vector

### 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(options?: PredictionOptions): Promise<any>

- Overrides TensorScriptModelInterface.predict

  - Defined in src/feature_embedding.ts:360

  #### Parameters

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

  #### Returns Promise<any>

### reduceWeights

- reduceWeights(weights: Matrix, options?: any): Promise<any>

- - Defined in src/feature_embedding.ts:403

  Uses tSNE to reduce dimensionality of features

  example

    const weights = [ [1.5,1,4,1.6,3.5], [4.3,3.2,5.5,6.5] ] FeatureEmbeddingInstance.reduceWeights(weights) //=> [ [1,2], [2,3], ]

  #### Parameters

  - ##### weights: Matrix

  - ##### Optional options: any

  #### 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(x_matrix: Matrix, y_matrix: Matrix, layers?: DenseLayer[]): Promise<any>

- Overrides BaseNeuralNetwork.train

  - Defined in src/feature_embedding.ts:323

  #### Parameters

  - ##### x_matrix: Matrix

  - ##### y_matrix: Matrix

  - ##### Optional layers: DenseLayer[]

  #### Returns Promise<any>

### trainOnBatch

- trainOnBatch(__namedParameters: { epoch: number; trainingLoss: number; x_input_matrix: number[][]; y_output_matrix: number[][] }): Promise<{ loss: number }>

- - Defined in src/feature_embedding.ts:195

  #### Parameters

  - ##### __namedParameters: { epoch: number; trainingLoss: number; x_input_matrix: number[][]; y_output_matrix: number[][] }

    - ##### epoch: number

    - ##### trainingLoss: number

    - ##### x_input_matrix: number[][]

    - ##### y_output_matrix: number[][]

  #### Returns Promise<{ loss: number }>

### Static getContextPairs

- getContextPairs(this: any, __namedParameters: { inputMatrix: number[][]; numberOfFeatures: number; tf: any; window_size: number }): Promise<{ context_length: number; emptyXVector: any; emptyYVector: any; x: number[][]; y: number[][] }>

- - Defined in src/feature_embedding.ts:80

  #### Parameters

  - ##### this: any

  - ##### __namedParameters: { inputMatrix: number[][]; numberOfFeatures: number; tf: any; window_size: number }

    - ##### inputMatrix: number[][]

    - ##### numberOfFeatures: number

    - ##### tf: any

    - ##### window_size: number

  #### Returns Promise<{ context_length: number; emptyXVector: any; emptyYVector: any; x: number[][]; y: number[][] }>

### Static getFeatureDataSet

- getFeatureDataSet(this: any, __namedParameters: { PAD: string; initialFeatureToId: undefined | {}; initialIdToFeature: undefined | {}; inputMatrixFeatures: (string | number)[][] }): Promise<{ featureIds: number[][]; featureToId: { __computed: number }; idToFeature: {}; numberOfFeatures: number }>

- - Defined in src/feature_embedding.ts:27

  #### Parameters

  - ##### this: any

  - ##### __namedParameters: { PAD: string; initialFeatureToId: undefined | {}; initialIdToFeature: undefined | {}; inputMatrixFeatures: (string | number)[][] }

    - ##### PAD: string

    - ##### initialFeatureToId: undefined | {}

    - ##### initialIdToFeature: undefined | {}

    - ##### inputMatrixFeatures: (string | number)[][]

  #### Returns Promise<{ featureIds: number[][]; featureToId: { __computed: number }; idToFeature: {}; numberOfFeatures: number }>

### 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 getMergedArray

- getMergedArray(base?: Vector, merger?: Vector, append?: boolean, truncate?: boolean): any[]

- - Defined in src/feature_embedding.ts:70

  #### Parameters

  - ##### Default value base: Vector = []

  - ##### Default value merger: Vector = []

  - ##### Default value append: boolean = false

  - ##### Default value truncate: boolean = true

  #### Returns any[]

### 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