张量流条件:检查张量内的值是零还是g

2024-10-01 15:41:48 发布

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

如果我有以下张量:

pmi=tf.constant([[1.5,0.0,0.0],[0.0,0.0,2.9],[1.001,5,1]])

我想要一个对应的张量Fpmi(或标量),这样当PMI张量中的元素大于0时,Fpmi中的元素应该是1,当PMI中的元素=0时,Fpmi中的元素=0.0005。在

我会很感激你的建议。在


Tags: 元素tf建议pmi标量我会constantfpmi
1条回答
网友
1楼 · 发布于 2024-10-01 15:41:48

使用^{},使用它可以有条件地从两个常量张量返回元素:

a = tf.constant(1, shape=pmi.shape, dtype=tf.float32)
b = tf.constant(0.0005, shape=pmi.shape, dtype=tf.float32)

tf.where(tf.greater(pmi, 0), a, b).eval()

#array([[  1.00000000e+00,   5.00000024e-04,   5.00000024e-04],
#       [  5.00000024e-04,   5.00000024e-04,   1.00000000e+00],
#       [  1.00000000e+00,   1.00000000e+00,   1.00000000e+00]], dtype=float32)

相关问题 更多 >

    热门问题