首页 > 解决方案 > 计算张量流中的对数不是就地

问题描述

我有以下功能(见下文)。它需要一个 (N,8,3) 张量作为参数。我需要沿轴 0 和 2 进行选择,因此我从沿轴 1 的概率中收到大小为 (N,1,3) 的选择。

我用tf.random.categorical. 遗憾的是,此函数需要tf.log(vector)预先计算沿轴 1 的向量。(我不明白原因,但这就是文档所说的)。

现在,这会就地计算日志,因此它会从外部破坏我的论点。

怎么可能计算非对数(或从没有 的概率中选择log(prop))?

谢谢

@tf.function
def convert_to_move(move):
    # copy  = tf.constant(move) <---  this is not supported by tensorflow. 
    # move = tf.math.log(move) <--- this errors if move==0. not direct but recreate with exp. is not possible
    m1 = move[:, :, 0] 
    m2 = move[:, :, 1]
    m3 = move[:, :, 2]
    x1 = tf.random.categorical(m1, num_samples=1)
    x2 = tf.random.categorical(m2, num_samples=1)
    x3 = tf.random.categorical(m3, num_samples=1)

    # move = tf.exp(move)
    # x1 = tf.argmax(movetensors[0], axis=1)
    # x2 = tf.argmax(movetensors[1], axis=1)
    # x3 = tf.argmax(movetensors[2], axis=1)

    r1 = tf.squeeze(tf.one_hot(x1, move.shape[1]))
    r2 = tf.squeeze(tf.one_hot(x2, move.shape[1]))
    r3 = tf.squeeze(tf.one_hot(x3, move.shape[1]))

    p1 = tf.boolean_mask(move[:, :, 0], r1)
    p2 = tf.boolean_mask(move[:, :, 2], r2)
    p3 = tf.boolean_mask(move[:, :, 3], r3)

    k1 = tf.stack([x1, x2, x3], axis=-1)
    k2 = tf.stack([p1, p2, p3], axis=-1)
    return tf.squeeze(k1), tf.squeeze(k2)

标签: pythontensorflowmathkeras

解决方案


推荐阅读