tf.One_hot返回数组中充满0&#x27

发布于 2025-02-12 20:55:05 字数 1023 浏览 0 评论 0 原文

我是深度学习的新手,我正在尝试对此张量

E = tf.constant(np.random.randint(1,100,size = 10))
E

输出

进行一个热门编码

< tf.tensor:shape =(10,),dtype = int64,numpy = array([48,85,75,75,25,28,49,3,3,51,47,47,96])>

使用tf.one_hot()后,返回的数组

tf.one_hot(E,depth=10)

在返回的张量中输出

<tf.Tensor: shape=(10, 10), dtype=float32, numpy=
array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 1., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]], dtype=float32)>

大多数热编码值是 0.,0.,0.,0。,0。,0。,0。,0。

[ 许多值的向量?

I am new to deep learning, I am trying to do one hot encoding of this tensor

E = tf.constant(np.random.randint(1,100,size = 10))
E

Output

<tf.Tensor: shape=(10,), dtype=int64, numpy=array([48, 85, 75, 25, 28, 49, 3, 51, 47, 96])>

After using tf.one_hot() the returned array is

tf.one_hot(E,depth=10)

Output

<tf.Tensor: shape=(10, 10), dtype=float32, numpy=
array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 1., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]], dtype=float32)>

In returned tensor most one hot encoded values are
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]

According to me in one hot encoding there should be unique vector for each value but here there is same vector for many values ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无边思念无边月 2025-02-19 20:55:05

如果您的随机整数在1-100范围内,则需要将深度= 100 保留在One_hot函数中。

相反,您可以将随机整数的范围降低到1-10。

这个想法是,如果您的数字范围在1-100之间,则需要尽可能多的位来表示单热编码,而您尚未通过将深度限制为10来提供。

import tensorflow as tf
import numpy as np
E = tf.constant(np.random.randint(1,10,size = 10))
tf.one_hot(E,depth=10)

输出:

<tf.Tensor: shape=(10, 10), dtype=float32, numpy=
array([[0., 0., 0., 1., 0., 0., 0., 0., 0., 0.],
   [0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
   [0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
   [0., 0., 1., 0., 0., 0., 0., 0., 0., 0.],
   [0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
   [0., 0., 0., 0., 1., 0., 0., 0., 0., 0.],
   [0., 0., 0., 0., 0., 0., 1., 0., 0., 0.],
   [0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],
   [0., 0., 0., 0., 0., 0., 1., 0., 0., 0.],
   [0., 0., 0., 0., 0., 0., 0., 0., 1., 0.]], dtype=float32)>

If your random integers are in the range 1-100, you'll need to keep depth=100 in the one_hot function.

Conversely, you can reduce the range of random integers to 1-10.

The idea is that if your number ranges between 1-100, it needs as many bits to represent the one-hot encoding, which you haven't provided by restricting the depth to 10.

import tensorflow as tf
import numpy as np
E = tf.constant(np.random.randint(1,10,size = 10))
tf.one_hot(E,depth=10)

Output:

<tf.Tensor: shape=(10, 10), dtype=float32, numpy=
array([[0., 0., 0., 1., 0., 0., 0., 0., 0., 0.],
   [0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
   [0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
   [0., 0., 1., 0., 0., 0., 0., 0., 0., 0.],
   [0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
   [0., 0., 0., 0., 1., 0., 0., 0., 0., 0.],
   [0., 0., 0., 0., 0., 0., 1., 0., 0., 0.],
   [0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],
   [0., 0., 0., 0., 0., 0., 1., 0., 0., 0.],
   [0., 0., 0., 0., 0., 0., 0., 0., 1., 0.]], dtype=float32)>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文