简体   繁体   English

TypeError: 'tensorflow.python.framework.ops.EagerTensor' 对象不支持项目分配

[英]TypeError: 'tensorflow.python.framework.ops.EagerTensor' object does not support item assignment

I have a situation where I take a slice of a tf tensor, I convert it to numpy, I do some calculations and finally I want to place this slice back where it came from ie the tf tensor.我有一种情况,我取了一个 tf 张量的切片,我将它转换为 numpy,我做了一些计算,最后我想把这个切片放回它来自的地方,即 tf 张量。 To be specific this is all part of a mini-batch generation / alteration procedure.具体来说,这是小批量生成/更改程序的一部分。

mini_batch.shape

would produce the following tensor shape将产生以下张量形状

TensorShape([#samples, 640, 1152, 3])

the np_slice that I process comes from the tensor above我处理的np_slice来自上面的张量

np_slice = mini_batch[sample_index][:, :, 2].numpy()

and I try to re-insert it as such我尝试重新插入它

mini_batch[sample_index][:, :, 2] = tf.convert_to_tensor(np_slice, dtype=tf.float32)

note that np_slice has shape (640, 1152) ie a one channel image请注意, np_slice具有形状(640, 1152)即单通道图像

As I understand tf does not allow this kind of assignment, hence my error据我了解 tf 不允许这种分配,因此我的错误

TypeError: 'tensorflow.python.framework.ops.EagerTensor' object does not support item assignment

It would appear that I need to make use of tf.tensor_scatter_nd_update看来我需要使用tf.tensor_scatter_nd_update

This is what I tried so far but it does not work as I require这是我迄今为止尝试过的,但它不能按我的要求工作

indices = tf.constant([[sample_index]])
updates = tf.convert_to_tensor(np_slice, dtype=tf.float32)
mini_batch = tf.tensor_scatter_nd_update(mini_batch, indices, updates)

Which produces the following产生以下

tensorflow.python.framework.errors_impl.InvalidArgumentError: Outer dimensions of indices and update must match. Indices shape: [1,1], updates shape:[640,1152] [Op:TensorScatterUpdate]

Tensorflow Tensors are immutable ( docs ): Tensorflow 张量是不可变的(文档):

All tensors are immutable like Python numbers and strings: you can never update the contents of a tensor, only create a new one.所有张量都是不可变的,就像 Python 数字和字符串一样:您永远无法更新张量的内容,只能创建一个新的内容。

You could use a Tensorflow Variable or do the slicing and the calculations in a numpy array and convert it to a Tensor at the end:您可以使用Tensorflow 变量或在 numpy 数组中进行切片和计算,最后将其转换为张量:

mini_batch_np=mini_batch.numpy()
np_slice = mini_batch_np[sample_index][:, :, 2]

#some calculations with np_slice

mini_batch_np[sample_index][:, :, 2] = np_slice

#convert back to tensor
mini_batch=tf.convert_to_tensor(mini_batch_np, dtype=tf.float32)

Yes, daveh's solution can help if you don't worry about the gradient back-propagation flow being truncated.是的,如果您不担心梯度反向传播流被截断,daveh 的解决方案会有所帮助。

I offer an alternative solution, where you can assign value to EagerTensor with some trick about tf.concat.我提供了一个替代解决方案,您可以在其中使用有关 tf.concat 的一些技巧为 EagerTensor 赋值。

import tensorflow as tf

# data simulate
samples=10
dim1=10
dim2=20
dim3=3
mini_batch=tf.random.uniform([samples, dim1, dim2, dim3])


# the new value you want to assign. 
# In this solution there is no need to convert to numpy.
np_slice = mini_batch[5, :, :, 2]

# the sample/dim index of the tensor you want to update
# note I use tf.convert_to_tensor if you want to assign numpy value
sample_index_to_replace=5
dim3_idx_to_replace=2

sample_tensor_to_replace = tf.concat(
    [
    mini_batch[sample_index_to_replace, :, :, :dim3_idx_to_replace],
    tf.convert_to_tensor(np_slice)[:,:, None],
    mini_batch[sample_index_to_replace, :, : , dim3_idx_to_replace+1:]
    ],
    axis=-1
)

mini_batch_new = tf.concat(
    [
        mini_batch[:sample_index_to_replace][:, :, :],
        tf.convert_to_tensor(sample_tensor_to_replace)[None, :, :, :],
        mini_batch[sample_index_to_replace+1:][:, :, :],
    ],
    axis=0,
)

tf.print(mini_batch_new)
# in this case, no value is modified
tf.debugging.assert_equal(mini_batch, mini_batch_new)

暂无
暂无

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

相关问题 项目分配 Tensorflow 2.0 - TypeError: 'tensorflow.python.framework.ops.EagerTensor' object 不支持项目分配 - Item Assignment Tensorflow 2.0 - TypeError: 'tensorflow.python.framework.ops.EagerTensor' object does not support item assignment &#39;tensorflow.python.framework.ops.EagerTensor&#39; 对象没有属性 &#39;squeeze&#39; - 'tensorflow.python.framework.ops.EagerTensor' object has no attribute 'squeeze' 类型 &#39;tensorflow.python.framework.ops.EagerTensor&#39; 没有 len() - type 'tensorflow.python.framework.ops.EagerTensor' has no len() AttributeError: &#39;tensorflow.python.framework.ops.EagerTensor&#39; 对象没有属性 &#39;to_tensor&#39; - AttributeError: 'tensorflow.python.framework.ops.EagerTensor' object has no attribute 'to_tensor' AttributeError: 'tensorflow.python.framework.ops.EagerTensor' object 没有属性 'ravel' - AttributeError: 'tensorflow.python.framework.ops.EagerTensor' object has no attribute 'ravel' AttributeError: &#39;tensorflow.python.framework.ops.EagerTensor&#39; 对象没有属性 &#39;decode&#39; - AttributeError: 'tensorflow.python.framework.ops.EagerTensor' object has no attribute 'decode' 'tensorflow.python.framework.ops.EagerTensor' object 没有属性 '_in_graph_mode' - 'tensorflow.python.framework.ops.EagerTensor' object has no attribute '_in_graph_mode' AttributeError: 'tensorflow.python.framework.ops.EagerTensor' object 没有属性 'assign' - AttributeError: 'tensorflow.python.framework.ops.EagerTensor' object has no attribute 'assign' 类型错误:<tf.tensor: shape ...> 有类型<class 'tensorflow.python.framework.ops.eagertensor'> ,但预期其中之一:(<class 'int'> ,)</class></class></tf.tensor:> - TypeError: <tf.Tensor: shape ... > has type <class 'tensorflow.python.framework.ops.EagerTensor'>, but expected one of: (<class 'int'>,) 类型错误: <tf.tensor ... has type <class 'tensorflow.python.framework.ops.eagertensor'> ,但期望其中之一:numbers.Real</tf.tensor> - TypeError: <tf.Tensor ... has type <class 'tensorflow.python.framework.ops.EagerTensor'>, but expected one of: numbers.Real
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM