Assigning zero to tensor at indices specified in a list
up vote
2
down vote
favorite
I have to tensors, for example
A = tf.Tensor(
[[1.0986123 0.6931472 0. 0.6931472 0. ]
[0. 0. 0. 0. 0. ]
[3.7376697 3.7612002 3.7841897 3.8066626 3.8286414]], shape=(3, 5), dtype=float32)
B = tf.Tensor(
[[2 1]
[2 2]], shape=(2, 2), dtype=int64)
Tensor B
holds indices in tensor A
. I want to update every value in tensor A
to zero that is listed in the index list B
.
So, the expected result would be
tf.Tensor(
[[1.0986123 0.6931472 0. 0.6931472 0. ]
[0. 0. 0. 0. 0. ]
[3.7376697 0 0 3.8066626 3.8286414]], shape=(3, 5), dtype=float32)
So the entries at index [2,1] and [2, 2] are set to 0.
I looked at tf.assign
but they can only be used for tf.Variable
's. tf.boolean_mask
would be a nice way to do it, but i do not know and could not find out how i can create a boolean mask with a list of indices.
I looked at the tensor flow functions i could find and related S/O answers but couldn't find a satisfying solution.
python tensorflow
add a comment |
up vote
2
down vote
favorite
I have to tensors, for example
A = tf.Tensor(
[[1.0986123 0.6931472 0. 0.6931472 0. ]
[0. 0. 0. 0. 0. ]
[3.7376697 3.7612002 3.7841897 3.8066626 3.8286414]], shape=(3, 5), dtype=float32)
B = tf.Tensor(
[[2 1]
[2 2]], shape=(2, 2), dtype=int64)
Tensor B
holds indices in tensor A
. I want to update every value in tensor A
to zero that is listed in the index list B
.
So, the expected result would be
tf.Tensor(
[[1.0986123 0.6931472 0. 0.6931472 0. ]
[0. 0. 0. 0. 0. ]
[3.7376697 0 0 3.8066626 3.8286414]], shape=(3, 5), dtype=float32)
So the entries at index [2,1] and [2, 2] are set to 0.
I looked at tf.assign
but they can only be used for tf.Variable
's. tf.boolean_mask
would be a nice way to do it, but i do not know and could not find out how i can create a boolean mask with a list of indices.
I looked at the tensor flow functions i could find and related S/O answers but couldn't find a satisfying solution.
python tensorflow
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have to tensors, for example
A = tf.Tensor(
[[1.0986123 0.6931472 0. 0.6931472 0. ]
[0. 0. 0. 0. 0. ]
[3.7376697 3.7612002 3.7841897 3.8066626 3.8286414]], shape=(3, 5), dtype=float32)
B = tf.Tensor(
[[2 1]
[2 2]], shape=(2, 2), dtype=int64)
Tensor B
holds indices in tensor A
. I want to update every value in tensor A
to zero that is listed in the index list B
.
So, the expected result would be
tf.Tensor(
[[1.0986123 0.6931472 0. 0.6931472 0. ]
[0. 0. 0. 0. 0. ]
[3.7376697 0 0 3.8066626 3.8286414]], shape=(3, 5), dtype=float32)
So the entries at index [2,1] and [2, 2] are set to 0.
I looked at tf.assign
but they can only be used for tf.Variable
's. tf.boolean_mask
would be a nice way to do it, but i do not know and could not find out how i can create a boolean mask with a list of indices.
I looked at the tensor flow functions i could find and related S/O answers but couldn't find a satisfying solution.
python tensorflow
I have to tensors, for example
A = tf.Tensor(
[[1.0986123 0.6931472 0. 0.6931472 0. ]
[0. 0. 0. 0. 0. ]
[3.7376697 3.7612002 3.7841897 3.8066626 3.8286414]], shape=(3, 5), dtype=float32)
B = tf.Tensor(
[[2 1]
[2 2]], shape=(2, 2), dtype=int64)
Tensor B
holds indices in tensor A
. I want to update every value in tensor A
to zero that is listed in the index list B
.
So, the expected result would be
tf.Tensor(
[[1.0986123 0.6931472 0. 0.6931472 0. ]
[0. 0. 0. 0. 0. ]
[3.7376697 0 0 3.8066626 3.8286414]], shape=(3, 5), dtype=float32)
So the entries at index [2,1] and [2, 2] are set to 0.
I looked at tf.assign
but they can only be used for tf.Variable
's. tf.boolean_mask
would be a nice way to do it, but i do not know and could not find out how i can create a boolean mask with a list of indices.
I looked at the tensor flow functions i could find and related S/O answers but couldn't find a satisfying solution.
python tensorflow
python tensorflow
edited Nov 4 at 15:51
Sam Comber
367212
367212
asked Nov 4 at 10:11
Falco Winkler
371618
371618
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
You can use tf.scatter_nd_update
for this. For example:
A = tf.Variable(
[[1.0986123, 0.6931472, 0. , 0.6931472, 0. ],
[0. , 0. , 0. , 0. , 0. ],
[3.7376697, 3.7612002, 3.7841897, 3.8066626, 3.8286414]], dtype=tf.float32)
B = tf.Variable(
[[2, 1],
[2, 2]], dtype=tf.int64)
C = tf.scatter_nd_update(A, B, tf.zeros(shape=tf.shape(B)[0]))
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(sess.run(C))
or
A = tf.constant(
[[1.0986123, 0.6931472, 0. , 0.6931472, 0. ],
[0. , 0. , 0. , 0. , 0. ],
[3.7376697, 3.7612002, 3.7841897, 3.8066626, 3.8286414]], dtype=tf.float32)
B = tf.constant(
[[2, 1],
[2, 2]], dtype=tf.int64)
AV = tf.Variable(A)
C = tf.scatter_nd_update(AV, B, tf.zeros(shape=tf.shape(B)[0]))
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(sess.run(C))
Thanks! My tensors come from the Dataset api so they are nottf.Variable
's. I figured i can create a variable from the incoming tensor, applyscatter_nd_update
and then convert it into a tensor again. Hacky but it works:tf.convert_to_tensor(tf.scatter_nd_update(tf.contrib.eager.Variable(input_data), index_list, tf.zeros(shape=tf.shape(index_list)[0])))
– Falco Winkler
2 days ago
i would like to continue research if there is a solution without using variables, maybe withtf.boolean_mask
and if i don't find something i can accept your answer
– Falco Winkler
2 days ago
I add code with constant instead variable. This is same behavior as tf.Tensor
– Vladimir Bystricky
2 days ago
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
You can use tf.scatter_nd_update
for this. For example:
A = tf.Variable(
[[1.0986123, 0.6931472, 0. , 0.6931472, 0. ],
[0. , 0. , 0. , 0. , 0. ],
[3.7376697, 3.7612002, 3.7841897, 3.8066626, 3.8286414]], dtype=tf.float32)
B = tf.Variable(
[[2, 1],
[2, 2]], dtype=tf.int64)
C = tf.scatter_nd_update(A, B, tf.zeros(shape=tf.shape(B)[0]))
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(sess.run(C))
or
A = tf.constant(
[[1.0986123, 0.6931472, 0. , 0.6931472, 0. ],
[0. , 0. , 0. , 0. , 0. ],
[3.7376697, 3.7612002, 3.7841897, 3.8066626, 3.8286414]], dtype=tf.float32)
B = tf.constant(
[[2, 1],
[2, 2]], dtype=tf.int64)
AV = tf.Variable(A)
C = tf.scatter_nd_update(AV, B, tf.zeros(shape=tf.shape(B)[0]))
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(sess.run(C))
Thanks! My tensors come from the Dataset api so they are nottf.Variable
's. I figured i can create a variable from the incoming tensor, applyscatter_nd_update
and then convert it into a tensor again. Hacky but it works:tf.convert_to_tensor(tf.scatter_nd_update(tf.contrib.eager.Variable(input_data), index_list, tf.zeros(shape=tf.shape(index_list)[0])))
– Falco Winkler
2 days ago
i would like to continue research if there is a solution without using variables, maybe withtf.boolean_mask
and if i don't find something i can accept your answer
– Falco Winkler
2 days ago
I add code with constant instead variable. This is same behavior as tf.Tensor
– Vladimir Bystricky
2 days ago
add a comment |
up vote
1
down vote
You can use tf.scatter_nd_update
for this. For example:
A = tf.Variable(
[[1.0986123, 0.6931472, 0. , 0.6931472, 0. ],
[0. , 0. , 0. , 0. , 0. ],
[3.7376697, 3.7612002, 3.7841897, 3.8066626, 3.8286414]], dtype=tf.float32)
B = tf.Variable(
[[2, 1],
[2, 2]], dtype=tf.int64)
C = tf.scatter_nd_update(A, B, tf.zeros(shape=tf.shape(B)[0]))
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(sess.run(C))
or
A = tf.constant(
[[1.0986123, 0.6931472, 0. , 0.6931472, 0. ],
[0. , 0. , 0. , 0. , 0. ],
[3.7376697, 3.7612002, 3.7841897, 3.8066626, 3.8286414]], dtype=tf.float32)
B = tf.constant(
[[2, 1],
[2, 2]], dtype=tf.int64)
AV = tf.Variable(A)
C = tf.scatter_nd_update(AV, B, tf.zeros(shape=tf.shape(B)[0]))
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(sess.run(C))
Thanks! My tensors come from the Dataset api so they are nottf.Variable
's. I figured i can create a variable from the incoming tensor, applyscatter_nd_update
and then convert it into a tensor again. Hacky but it works:tf.convert_to_tensor(tf.scatter_nd_update(tf.contrib.eager.Variable(input_data), index_list, tf.zeros(shape=tf.shape(index_list)[0])))
– Falco Winkler
2 days ago
i would like to continue research if there is a solution without using variables, maybe withtf.boolean_mask
and if i don't find something i can accept your answer
– Falco Winkler
2 days ago
I add code with constant instead variable. This is same behavior as tf.Tensor
– Vladimir Bystricky
2 days ago
add a comment |
up vote
1
down vote
up vote
1
down vote
You can use tf.scatter_nd_update
for this. For example:
A = tf.Variable(
[[1.0986123, 0.6931472, 0. , 0.6931472, 0. ],
[0. , 0. , 0. , 0. , 0. ],
[3.7376697, 3.7612002, 3.7841897, 3.8066626, 3.8286414]], dtype=tf.float32)
B = tf.Variable(
[[2, 1],
[2, 2]], dtype=tf.int64)
C = tf.scatter_nd_update(A, B, tf.zeros(shape=tf.shape(B)[0]))
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(sess.run(C))
or
A = tf.constant(
[[1.0986123, 0.6931472, 0. , 0.6931472, 0. ],
[0. , 0. , 0. , 0. , 0. ],
[3.7376697, 3.7612002, 3.7841897, 3.8066626, 3.8286414]], dtype=tf.float32)
B = tf.constant(
[[2, 1],
[2, 2]], dtype=tf.int64)
AV = tf.Variable(A)
C = tf.scatter_nd_update(AV, B, tf.zeros(shape=tf.shape(B)[0]))
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(sess.run(C))
You can use tf.scatter_nd_update
for this. For example:
A = tf.Variable(
[[1.0986123, 0.6931472, 0. , 0.6931472, 0. ],
[0. , 0. , 0. , 0. , 0. ],
[3.7376697, 3.7612002, 3.7841897, 3.8066626, 3.8286414]], dtype=tf.float32)
B = tf.Variable(
[[2, 1],
[2, 2]], dtype=tf.int64)
C = tf.scatter_nd_update(A, B, tf.zeros(shape=tf.shape(B)[0]))
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(sess.run(C))
or
A = tf.constant(
[[1.0986123, 0.6931472, 0. , 0.6931472, 0. ],
[0. , 0. , 0. , 0. , 0. ],
[3.7376697, 3.7612002, 3.7841897, 3.8066626, 3.8286414]], dtype=tf.float32)
B = tf.constant(
[[2, 1],
[2, 2]], dtype=tf.int64)
AV = tf.Variable(A)
C = tf.scatter_nd_update(AV, B, tf.zeros(shape=tf.shape(B)[0]))
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(sess.run(C))
edited 2 days ago
answered Nov 4 at 15:24
Vladimir Bystricky
1,0731512
1,0731512
Thanks! My tensors come from the Dataset api so they are nottf.Variable
's. I figured i can create a variable from the incoming tensor, applyscatter_nd_update
and then convert it into a tensor again. Hacky but it works:tf.convert_to_tensor(tf.scatter_nd_update(tf.contrib.eager.Variable(input_data), index_list, tf.zeros(shape=tf.shape(index_list)[0])))
– Falco Winkler
2 days ago
i would like to continue research if there is a solution without using variables, maybe withtf.boolean_mask
and if i don't find something i can accept your answer
– Falco Winkler
2 days ago
I add code with constant instead variable. This is same behavior as tf.Tensor
– Vladimir Bystricky
2 days ago
add a comment |
Thanks! My tensors come from the Dataset api so they are nottf.Variable
's. I figured i can create a variable from the incoming tensor, applyscatter_nd_update
and then convert it into a tensor again. Hacky but it works:tf.convert_to_tensor(tf.scatter_nd_update(tf.contrib.eager.Variable(input_data), index_list, tf.zeros(shape=tf.shape(index_list)[0])))
– Falco Winkler
2 days ago
i would like to continue research if there is a solution without using variables, maybe withtf.boolean_mask
and if i don't find something i can accept your answer
– Falco Winkler
2 days ago
I add code with constant instead variable. This is same behavior as tf.Tensor
– Vladimir Bystricky
2 days ago
Thanks! My tensors come from the Dataset api so they are not
tf.Variable
's. I figured i can create a variable from the incoming tensor, apply scatter_nd_update
and then convert it into a tensor again. Hacky but it works: tf.convert_to_tensor(tf.scatter_nd_update(tf.contrib.eager.Variable(input_data), index_list, tf.zeros(shape=tf.shape(index_list)[0])))
– Falco Winkler
2 days ago
Thanks! My tensors come from the Dataset api so they are not
tf.Variable
's. I figured i can create a variable from the incoming tensor, apply scatter_nd_update
and then convert it into a tensor again. Hacky but it works: tf.convert_to_tensor(tf.scatter_nd_update(tf.contrib.eager.Variable(input_data), index_list, tf.zeros(shape=tf.shape(index_list)[0])))
– Falco Winkler
2 days ago
i would like to continue research if there is a solution without using variables, maybe with
tf.boolean_mask
and if i don't find something i can accept your answer– Falco Winkler
2 days ago
i would like to continue research if there is a solution without using variables, maybe with
tf.boolean_mask
and if i don't find something i can accept your answer– Falco Winkler
2 days ago
I add code with constant instead variable. This is same behavior as tf.Tensor
– Vladimir Bystricky
2 days ago
I add code with constant instead variable. This is same behavior as tf.Tensor
– Vladimir Bystricky
2 days ago
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53139678%2fassigning-zero-to-tensor-at-indices-specified-in-a-list%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password