简体   繁体   中英

Numpy Indexing Operations with Tensorflow

I want to update the values of a tensor like:

ldream[w,:,x,y,z] = 0

But keep getting an error saying:

TypeError: 'Tensor' object does not support item assignment

It seems ldream is a tensorflow variable, which unfortunately cannot be directly assigned values the same way as numpy arrays.

To update the value of a tensor in tensorflow, you can make an assignment operation and then run (eval) that operation. Here is an example on how to do this:

Tensorflow: How to modify the value in tensor

The reason for this is that when you code your tf variables and operations, you are actually "staging" them to happen at a later time (such as when you do sess.run ); like a blueprint of how tensorflow will actually run.

Fixed this by creating a boolean mask using a numpy array:

ldream_mask = np.zeros(ldream.shape, dtype=np.bool)

Then selecting the desired indices and marking them as True

ldream_mask[w,:,x,y,z] =  True

Then using tf.where to update the desired indices using ldream_mask

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM