1

I need to manipulate tensor as below

ary = np.array([82.20674918566776, 147.55325947521865, 25.804872964169384, 85.34690767735665, 1, 0]).reshape(1,1,1,1,6)

tf_array = tf.convert_to_tensor(ary, tf.float32)
value_x = 13.0
norm_value = 416.0

tf_array[...,0:4] = tf_array[...,0:4] * (value_x / norm_value)

when executed

TypeError: 'Tensor' object does not support item assignment

0

2 Answers 2

1

You cannot assign values in TensorFlow, as tensors are immutable (TensorFlow variables can have their value changed, but that is more like replacing their inner tensor with a new one). The closest thing to item assignment in TensorFlow may be tf.tensor_scatter_nd_update, which is still not assigning a new value, but creating a new tensor with some values replaced. In general, you have to find the way to compute the result that you want from the tensor that you have. In your case, you could do for example this:

import tensorflow as tf
import numpy as np

ary = np.array([82.20674918566776, 147.55325947521865,
                25.804872964169384, 85.34690767735665,
                1, 0]).reshape(1,1,1,1,6)
tf_array = tf.convert_to_tensor(ary, tf.float32)
value_x = 13.0
norm_value = 416.0

# Mask for the first four elements in the last dimension
m = tf.range(tf.shape(tf_array)[0]) < 4
# Pick the multiplication factor where the mask is true and 1 everywhere else
f = tf.where(m, value_x / norm_value, 1)
# Multiply the tensor
tf_array_multiplied = tf_array * f
# [[[[[2.568961   4.611039   0.80640227 2.667091   0.03125    0.        ]]]]]
Sign up to request clarification or add additional context in comments.

Comments

0

Thank you,

I tried workaround

ary = np.array([82.20674918566776, 147.55325947521865, 25.804872964169384, 85.34690767735665, 1, 0]).reshape(1,1,1,1,6)

tf_array = tf.convert_to_tensor(ary, tf.float32)
value_x = 13.0
norm_value = 416.0

#create array of same shape and values to be multiplied with
temp = np.array([value_x /norm_value , value_x /norm_value, value_x /norm_value, value_x /norm_value, 1, 1]).reshape(1,1,1,1,6)

#convert numpy array to tensorflow tensor
normalise_ary = tf.convert_to_tensor(temp, tf.float32)

tf_array = tf_array * normalise_ary

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.