Reputation: 159
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
Upvotes: 1
Views: 3129
Reputation: 159
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
Upvotes: 0
Reputation: 59691
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. ]]]]]
Upvotes: 1