张量流变换的正确应用

2024-06-17 16:06:27 发布

您现在位置:Python中文网/ 问答频道 /正文

这是在TensorFlow 1.11.0上。tft.apply_bucketsdocumentation不是很有描述性。具体来说,我读到: “桶边界:用秩2张量表示的桶边界。”

我想这一定是桶指数和桶边界?你知道吗

当我尝试下面的玩具示例时:

import tensorflow as tf
import tensorflow_transform as tft
import numpy as np

tf.enable_eager_execution()

x = np.array([-1,9,19, 29, 39])
xt = tf.cast(
        tf.convert_to_tensor(x),
        tf.float32
        )

boundaries = tf.cast(
                tf.transpose(
                    tf.convert_to_tensor([[0, 1, 2, 3], [10, 20, 30, 40]])
                    ),
                tf.float32
                )

buckets = tft.apply_buckets(xt, boundaries)

我得到:

InvalidArgumentError: Expected sorted boundaries [Op:BucketizeWithInputBoundaries] name: assign_buckets

注意,在这种情况下,xbucket_boundaries参数是:

tf.Tensor([-1.  9. 19. 29. 39.], shape=(5,), dtype=float32)
tf.Tensor(
[[ 0. 10.]
 [ 1. 20.]
 [ 2. 30.]
 [ 3. 40.]], shape=(4, 2), dtype=float32)

所以,似乎bucket_boundaries不应该是索引和边界。有人知道如何正确使用这个方法吗?你知道吗


Tags: toimportconverttftensorflowasnp边界
1条回答
网友
1楼 · 发布于 2024-06-17 16:06:27

经过一番研究,我发现bucket_boundaries应该是一个二维数组,其中条目是bucket边界,数组被包装成两列。见下例:

import tensorflow as tf
import tensorflow_transform as tft
import numpy as np

tf.enable_eager_execution()

x = np.array([-1,9,19, 29, 39])
xt = tf.cast(
        tf.convert_to_tensor(x),
        tf.float32
        )

boundaries = tf.cast(
                tf.transpose(
                    tf.convert_to_tensor([[0, 20, 40, 60], [10, 30, 50, 70]])
                    ),
                tf.float32
                )

buckets = tft.apply_buckets(xt, boundaries)

因此,预期输入为:

print (xt)
print (buckets)
print (boundaries)
tf.Tensor([-1.  9. 19. 29. 39.], shape=(5,), dtype=float32)
tf.Tensor([0 1 2 3 4], shape=(5,), dtype=int64)
tf.Tensor(
[[ 0. 10.]
 [20. 30.]
 [40. 50.]
 [60. 70.]], shape=(4, 2), dtype=float32)

相关问题 更多 >