简体   繁体   English

使用Tensorflow进行Numpy索引操作

[英]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. 似乎ldream是一个tensorflow变量,遗憾的是它不能像numpy数组那样直接赋值。

To update the value of a tensor in tensorflow, you can make an assignment operation and then run (eval) that operation. 要更新张量流中的张量值,可以进行赋值操作,然后运行(eval)该操作。 Here is an example on how to do this: 以下是如何执行此操作的示例:

Tensorflow: How to modify the value in tensor Tensorflow:如何修改张量值

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 ); 这样做的原因是当你编写你的tf变量和操作时,你实际上是“暂存”它们以后发生(例如当你做sess.run ); like a blueprint of how tensorflow will actually run. 就像tensorflow实际运行的蓝图一样。

Fixed this by creating a boolean mask using a numpy array: 通过使用numpy数组创建布尔掩码来解决此问题:

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

Then selecting the desired indices and marking them as True 然后选择所需的索引并将其标记为True

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

Then using tf.where to update the desired indices using ldream_mask 然后使用tf.where使用ldream_mask更新所需的索引

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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