tensorflow / tensorflow

An Open Source Machine Learning Framework for Everyone
https://tensorflow.org
Apache License 2.0
186.16k stars 74.28k forks source link

An easy way to update tensors that complies with numpy syntax #51582

Open ziofil opened 3 years ago

ziofil commented 3 years ago

System information

Describe the feature and the current behavior/state. I would love to be able to do something like the following:

t1 = np.zeros((2, 4))
t2 = np.ones((2, 3))

t1[:, 1:4] += t2
t1[:, [3, 1, 1]] += t2

without having to use methods like tensor_scatter_nd_update over multiple lines of code...

Will this change the current api? How? yes, it will be possible to use a syntax like in numpy, that everyone is familiar with.

Who will benefit with this feature? everyone who needs to build tensors manually

mihaimaruseac commented 3 years ago

TF tensors are supposed to be immutable

mohantym commented 3 years ago

Hi @ziofil !
We see that the issue template has not been filled, could you please do so as it helps us analyse the issue .Thanks

ziofil commented 3 years ago

TF tensors are supposed to be immutable

I know, but then again there are methods like tf.tensor_scatter_nd_update, so effectively tensor are mutable if one uses the right methods. So why not allowing a clearer syntax?

ziofil commented 3 years ago

Hi @ziofil ! We see that the issue template has not been filled, could you please do so as it helps us analyse the issue .Thanks

Done.

mohantym commented 3 years ago

Hi @ziofil , Got workaround on above operation using .numpy() and convert_to_tensor() method.

import tensorflow as tf
import numpy as np
t1 = tf.constant(np.zeros((2, 4)))
t2 = tf.constant(np.ones((2, 3)))
t1 = t1.numpy()
t2 = t2.numpy()

t1[:, 1:4] += t2
print(t1)
print("tensor_format")
t3=tf.convert_to_tensor(t1)
print(t3)
t1[:, [3, 1, 1]] += t2
print(t1)
print("tensor_format")
t3=tf.convert_to_tensor(t1)
print(t3)

Please find the gist for reference

ziofil commented 3 years ago

Thanks for the idea, but this is not differentiable...

mohantym commented 3 years ago

Hi @sanatmpa1 ,Could you please look into this issue!

cantonios commented 1 year ago

An RFC tp address this has been up for a bit. Adding a link here in case anyone wants to provide input.

Comments are invited to the pull request. You can view the design doc here, and also leave comments inline on the document source.

tilakrayal commented 1 week ago

Hi,

Thank you for opening this issue. Since this issue has been open for a long time, the code/debug information for this issue may not be relevant with the current state of the code base.

The Tensorflow team is constantly improving the framework by fixing bugs and adding new features. We suggest you try the latest TensorFlow version with the latest compatible hardware configuration which could potentially resolve the issue. If you are still facing the issue, please create a new GitHub issue with your latest findings, with all the debugging information which could help us investigate.

Please follow the release notes to stay up to date with the latest developments which are happening in the Tensorflow space.

github-actions[bot] commented 4 days ago

This issue is stale because it has been open for 7 days with no activity. It will be closed if no further activity occurs. Thank you.