I am trying to set values to index of tensor but I am not sure which method to use.
const a = tf.variable(tf.tensor1d([1,2,3,4])
const ix = tf.tensor1d([1,2])
How can I change/mutate ix indices in a ? I am unable to find any method.
In python tensorflow I can do:
a = tf.Variable([1,2,3,4])
a = a[0].assign(100)
with tf.Sesssion() as sess:
sess.run(tf.global_variables_initializer())
print sess.run(a)
This prints [100, 2, 3, 4]. I am unable to do this in tensorflow js
a[0] returns undefined
You can use variable.assign to assign new values to the variable, this would need to be a new tensor of the same shape with your modified data, as you cannot modify individual parts of a tensor. Tensor's are immutable and Variable is a fairly thin wrapper around that to allow reassignment.
Also to get your data out of the tensor you should use .data method on tensors.
A couple more thoughts courtesy of @nsthorat.
You could use .buffer to convert your variable to a TensorBuffer, this is a mutable data structure with some handy set methods. Once the modification is done you would turn it back into a tensor and update your variable. Note that this approach does download the tensor from GPU to CPU memory.
To do it all in GPU you could probably use a combination of .mulStrict and .addStrict to mask out the values you want to change from the source tensor and add them from your target tensor. This involves creating the mask manually though.
Thanks that helps.
Most helpful comment
A couple more thoughts courtesy of @nsthorat.
You could use .buffer to convert your variable to a TensorBuffer, this is a mutable data structure with some handy set methods. Once the modification is done you would turn it back into a tensor and update your variable. Note that this approach does download the tensor from GPU to CPU memory.
To do it all in GPU you could probably use a combination of .mulStrict and .addStrict to mask out the values you want to change from the source tensor and add them from your target tensor. This involves creating the mask manually though.