Atia Riaz
Atia Riaz

Reputation: 97

How may I define my own labels in tensorflow.js

I have a three dimensional data say x,y,z of any sensor. I'm creating tensorof these values like tf.tensor3d([[[x1], [y1], [z1]], [[x2], [y2], [z3]], ....... so on]) . But I have just two labels that are not numeric values like [standing , sitting]. I want to assign a single label to the combination of three values of x,y,z. How may I train my model in tensorflow.js using my own labels ?

Upvotes: 1

Views: 660

Answers (1)

edkeveked
edkeveked

Reputation: 18371

The first thing is to create an index of the label.

ES2019

const labelArray = ["standing", "sitting"]
const mapIndexLabel = Object.fromEntries(Object.entries({...labelArray}).map(([a, b]) => [b, +a])) // {standing: 0, sitting: 1}

The label tensor should be a onehot encoding. Here is an example of how to create it.

|features   | labels   |
|-----------|----------|
| feature0  | standing |
| feature1  | sitting  |
| feature1  | sitting  |

The array of labels index should be [0, 1, 1] (the indexes are taken from the object above). The label tensor is a onehot encoding of the indexes

labelsTensor = tf.onehot([0, 1, 1], numberOfUniqueLabels) // numberOfUniqueLabels = 2 in this case

Then after the model can be trained by model.fit(featuresTensor, labelsTensor)

Upvotes: 1

Related Questions