我如何在 tensorflow.js 中定义我自己的标签 [英] How may I define my own labels in tensorflow.js

查看:40
本文介绍了我如何在 tensorflow.js 中定义我自己的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有任何传感器的三维数据,比如 x,y,z.我正在创建这些值的 tensor,例如 tf.tensor3d([[[x1], [y1], [z1]], [[x2], [y2], [z3]], .... 等等]) .但是我只有两个不是数值的标签,例如 [standing , sating].我想为 x,y,z 的三个值的组合分配一个 label.如何使用我自己的标签在 tensorflow.js 中训练我的 model ?

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 ?

推荐答案

首先要创建标签的索引.

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}

标签张量应该是一个onehot编码.这是如何创建它的示例.

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  |

标签索引数组应该是[0, 1, 1](索引取自上面的对象).标签张量是索引的onehot编码

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

然后可以通过model.fit(featuresTensor, labelsTensor)

这篇关于我如何在 tensorflow.js 中定义我自己的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆