在 Tensorflow 中使用不同的张量类型

发布于 2025-01-14 14:13:34 字数 732 浏览 3 评论 0原文

我正在努力处理不同的张量类型和它们之间的运算。例如,基本除法tf.divide(a, b)给我带来了以下错误:

TypeError: Failed to convert elements of SparseTensor(indices=Tensor("inputs_8_copy:0", shape=(None, 2), dtype=int64), values=Tensor("cond/Cast_1:0", shape=(None,), dtype=float64), dense_shape=Tensor("inputs_10_copy:0", shape=(2,), dtype=int64)) to Tensor. Consider casting elements to a supported type. See https://www.tensorflow.org/api_docs/python/tf/dtypes for supported TF dtypes.

我能够通过调用tf.sparse.to_dense来解决这个问题ab。但当数据集很大时,该方法无法扩展。它通常也不起作用,因为我不知道所有特征的张量类型(我正在 TFT 中的 preprocessing_fn 中工作,数据来自 BigQuery)。

这似乎是一个非常常见的问题,应该有一个简单的答案,但我无法找到有关它的任何信息。像基本的划分之类的东西不应该造成这么大的麻烦吗?

I'm struggling to work with different tensor types and operations between them. For example, a basic division tf.divide(a, b) is giving me the following error:

TypeError: Failed to convert elements of SparseTensor(indices=Tensor("inputs_8_copy:0", shape=(None, 2), dtype=int64), values=Tensor("cond/Cast_1:0", shape=(None,), dtype=float64), dense_shape=Tensor("inputs_10_copy:0", shape=(2,), dtype=int64)) to Tensor. Consider casting elements to a supported type. See https://www.tensorflow.org/api_docs/python/tf/dtypes for supported TF dtypes.

I was able to work around this by calling tf.sparse.to_dense on a and b. But the approach doesn't scale when the dataset is large. Nor does it work in general because I don't know the tensor type of all of the features (I'm working within a preprocessing_fn in TFT and the data comes from BigQuery).

This seems like a very common issue that should have a simple answer but I'm not able to find any information on it. Something like basic divisions shouldn't cause this much trouble?

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

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

发布评论

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

评论(1

可是我不能没有你 2025-01-21 14:13:34

事实上,这是一个很难的问题。
特别是对于逐元素除法,假设 ai 和 bi 是标量。如果 ai = 0 且 bi 不为零,则 ai/bi = 0,但如果 ai = 0 且 bi = 0,则 ai/bi = 呢? 0?
更糟糕的是,如果 ai 不为零且 bi = 0,则 ai/bi 为 NaN!
因此,如果除数是稀疏张量,它将产生(可能很多)NaN,除非两个稀疏矩阵的索引相同。如果将稠密矩阵除以稀疏矩阵,也会出现同样的问题。

有一个很好的解决方法可以将两个稀疏张量按元素相乘 此处,基于关系 (a+b)^2 = a^2 + b^2 + 2 ab。
还可以计算稀疏张量 C 的逆:tf.SparseTensor(indices=C.indices,values=1/C.values,dense_shape=C.dense_shape)。

因此,除法存在 NaN 问题,并且对于稠密张量和稀疏张量的混合,一种选择是将稀疏张量转换为稠密张量。但我们想避免这种情况。另一方面,如果张量不是真正稀疏的,那么将稠密张量转换为稀疏张量可能会非常无效。

这一切都说明这似乎不是一个简单的问题。

It is a difficult question, in fact.
For element-wise division in particular, let say ai and bi are scalars. if ai = 0 and bi is not zero, then ai/bi = 0, but what if ai = 0 and bi = 0, ai/bi = ? 0?
Even worse, if ai is not zero and bi = 0 then ai/bi is NaN!
So if the divisor is a sparse tensor, it will raise (possibly lots of) NaNs, unless the indices of both sparse matrices are the same. Same problem if you divide a dense matrix by a sparse matrix.

There is a nice a workaround to multiply two sparse tensors element-wise here, based on the relation (a+b)^2 = a^2 + b^2 + 2 ab.
It is also possible to compute the inverse of a sparse tensor C: tf.SparseTensor(indices=C.indices, values=1/C.values, dense_shape=C.dense_shape).

So there is this NaN issue for division, and concerning the mixture of dense tensor and sparse tensor, one option consists in converting the sparse tensor to a dense tensor. But we want to avoid this. In the other direction, from converting the dense tensor to a sparse tensor, this can be very ineffective if the tensor is not really sparse.

All this to say that it does not seem to be a simple problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文