python - 计算张量流中的对数不是就地
问题描述
我有以下功能(见下文)。它需要一个 (N,8,3) 张量作为参数。我需要沿轴 0 和 2 进行选择,因此我从沿轴 1 的概率中收到大小为 (N,1,3) 的选择。
我用tf.random.categorical
. 遗憾的是,此函数需要tf.log(vector)
预先计算沿轴 1 的向量。(我不明白原因,但这就是文档所说的)。
现在,这会就地计算日志,因此它会从外部破坏我的论点。
- 我无法复制张量。
copy = tf.constant(move)
是不可能的,因为move是一个张量。 - 我不能 exp(ln(move)) 因为我收到 NaNs 之后。
- 我不能使用 Numpy,因为它在 tf.function 中。(GPU,但 32 位)
怎么可能计算非对数(或从没有 的概率中选择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)
解决方案
推荐阅读
- feature-selection - Python中的RReliefF特征选择
- swift - bezierpath 视图上的阴影为角落添加了奇怪的笔触
- angular - Angular 库无法在包中包含第三方库
- linux - 如何限制循环执行的命令的shell脚本中的输出
- r - R 中 optim/SANN 的停止标准不起作用
- podio - Podio API:如何获取所有组织工作区的 ID(开放和私有)
- python - 如何单独操作 Django-ORM 模型?
- microservices - 功能区在 WebLogic 12c 上给出 404 响应
- css - 使用输入水平内联图像
- ios - 要在图像集中上传哪 3 张尺寸/分辨率的图像以支持所有设备?