我有2个粗糙Tensor,定义如下
import tensorflow as tf
# Tensor 1
pred_score = tf.ragged.constant([
[[-0.51760715], [-0.18927467], [-0.10698503]],
[[-0.58782816], [-0.13076714], [-0.04999146], [-0.1772059], [-0.14299354]]
])
pred_score = tf.squeeze(pred_score, axis=-1)
pred_score_dist = tf.nn.softmax(pred_score, axis=-1)
print(pred_score_dist)
print(pred_score_dist.shape)
>> <tf.RaggedTensor [[0.25664675, 0.35639265, 0.38696054],
[0.1358749, 0.21460423, 0.23265839, 0.20486614, 0.21199636]]>
(2, None)
# Tensor 2
actual_score = tf.ragged.constant([
[3.0, 2.0, 2.0],
[3.0, 3.0, 1.0, 1.0, 0.0]
])
actual_score_dist = tf.nn.softmax(actual_score, axis=-1)
print(actual_score_dist)
print(actual_score_dist.shape)
<tf.RaggedTensor [[0.5761169, 0.21194157, 0.21194157],
[0.4309495, 0.4309495, 0.05832267, 0.05832267, 0.021455714]]>
(2, None)
我想逐行计算KL散度,最后计算总散度,我试着运行这个程序,但是它给出了错误
loss = tf.keras.losses.KLDivergence()
batch_loss = loss(actual_score_dist, pred_score_dist)
ValueError: TypeError: object of type 'RaggedTensor' has no len()
有人能帮帮我吗
1条答案
按热度按时间s71maibg1#
这里有一个让它运行的方法,