Python Bucketize实值张量
我有这样一个矩阵:Python Bucketize实值张量,python,tensorflow,mapping,slice,Python,Tensorflow,Mapping,Slice,我有这样一个矩阵: tf_ent = tf.Variable([ [9.96, 8.65, 0.8, 0.1 ], [0.7, 8.33, 0 , 0 ], [0.9, 0, 6, 7.33], [6.60, 0, 3, 5.5 ],
tf_ent = tf.Variable([ [9.96, 8.65, 0.8, 0.1 ],
[0.7, 8.33, 0 , 0 ],
[0.9, 0, 6, 7.33],
[6.60, 0, 3, 5.5 ],
[9.49, 0., 0, 0 ],
[0.4, 8.45, 0, 0.2 ],
[0.3, 0, 5.82, 8.28]])
[[5, 5, 1, 1],
[1, 5, 0, 0],
[1, 0, 6, 5],
[4, 0, 2, 3],
[5, 0., 0, 0],
[1, 5, 0, 1],
[1, 0, 3, 5]]
我想得到一个输出,在这个输出中,相同范围内的权重得到相同的数字
换句话说,这个数字很好地代表了矩阵中的权重范围
所以我的理想输出可以是这样的:
tf_ent = tf.Variable([ [9.96, 8.65, 0.8, 0.1 ],
[0.7, 8.33, 0 , 0 ],
[0.9, 0, 6, 7.33],
[6.60, 0, 3, 5.5 ],
[9.49, 0., 0, 0 ],
[0.4, 8.45, 0, 0.2 ],
[0.3, 0, 5.82, 8.28]])
[[5, 5, 1, 1],
[1, 5, 0, 0],
[1, 0, 6, 5],
[4, 0, 2, 3],
[5, 0., 0, 0],
[1, 5, 0, 1],
[1, 0, 3, 5]]
在本例中,我使用此条件映射(假设我有映射):
首先,我尝试了tf.histogram\u fixed\u width\u bins
,但没有得到预期的结果:
这是我的代码:
value_ranges = [0, 10.0]
nbins = 5
new_f_w_t = tf.histogram_fixed_width_bins(tf_ent, value_ranges, nbins)
print(new_f_w_t)
这是这个代码的输出:
output= [[4 4 0 0]
[0 4 0 0]
[0 0 3 3]
[3 0 1 2]
[4 0 0 0]
[0 4 0 0]
[0 0 2 4]]
输出更像是获得这些浮点数的等效整数,而不是考虑任何范围
如何有效地获得矩阵的动态映射
提前感谢:)这相当于一个np.数字化操作。您可以使用数学运算。\u bucketize
:
from tensorflow.python.ops import math_ops
bins = [0.1, 1, 3, 5, 7, 10]
new_f_w_t = math_ops._bucketize(tf_ent, boundaries=bins)
new_f_w_t.numpy()
array([[5, 5, 1, 1],
[1, 5, 0, 0],
[1, 0, 4, 5],
[4, 0, 3, 4],
[5, 0, 0, 0],
[1, 5, 0, 1],
[1, 0, 4, 5]], dtype=int32)
tf.searchsorted()
调用也可用于此目的,从而避免调用隐藏的_bucketize函数。不过,您必须平铺箱子以匹配输入值的尺寸
例如:
bins = tf.tile([[0.1, 1, 3, 5, 7, 10]], (tf_ent.shape[0], 1))
new_f_w_t = tf.searchsorted(bins, tf_ent, side="right")
@sariii不能代表开发者说话,但也许他们不认为它是一个非常常用的函数,或者认为直方图*
方法就足够了。@sariii您的输入和存储箱是否与这里描述的相同?我在TensorFlow 2.0上运行了这个,它运行得很好。是的,我只是复制粘贴,也许我的TensorFlow有问题,我调查了一下,:)你的方法似乎正是我想要的,再次感谢你。这是TensorFlow的问题,非常感谢:)@sariii嗯,不。问问你自己,你会在哪个索引处将9.96插入垃圾箱中?对数组中的每个元素重复此操作。这就是bucketize
所做的。