簡體   English   中英

基於 TensorFlow 中的 boolean 掩碼的部分更新張量

[英]Partial update tensor based on boolean mask in TensorFlow

我想根據某些條件更新部分張量。

我知道 TensorFlow 張量是不可變的,所以創建一個新的張量對我來說是可以的。 我嘗試tensor_scatter_nd_update方法,但無法使其工作

這是我想在 NumPy 中編寫的 TensorFlow 中復制的代碼。

import numpy as np

a = np.random.random((1, 3))
b = np.array([[0, 1, 0]])

c = np.zeros_like(a)
mask = b == 1
c[mask] = np.log(a[mask])

在 TensorFlow 中,我們不更新實際上是不可變對象的張量。 相反,我們從其他張量(如函數式語言)創建新張量。

import tensorflow as tf

a = tf.random.uniform(shape=(1, 3))
b = tf.constant([[0, 1, 0]], dtype=tf.int32)

c = tf.zeros_like(a)
mask = b == 1
c_updated = tf.where(mask, tf.math.log(a), c)
# [[ 0.      , -4.175911,  0.      ]]```

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM