How may I define my own labels in tensorflow.js

谁说我不能喝 提交于 2020-02-25 07:17:06

问题


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 ?


回答1:


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)



来源:https://stackoverflow.com/questions/59127861/how-may-i-define-my-own-labels-in-tensorflow-js

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!