I have a tensor object, and I want to slice part of it.
tf_a1 = tf.Variable([ [9.968594, 8.655439, 0., 0. ],
[0., 8.3356, 0., 8.8974 ],
[0., 0., 6.103182, 7.330564 ],
[6.609862, 0., 3.0614321, 0. ],
[9.497023, 0., 3.8914037, 0. ],
[0., 8.457685, 8.602337, 0. ],
[0., 0., 5.826657, 8.283971 ],
[0., 0., 0., 0. ]])
Also, I have this array:
tf_a2 = tf.constant([[1, 2, 5],
[1, 4, 6],
[0, 7, 7],
[2, 3, 6],
[2, 4, 7]])
I want to do this numpy like slicing:
tf_a1[tf_a2]
The expected output from the numpy code will be like this:
[[[0. 8.3356 0. 8.8974 ]
[0. 0. 6.103182 7.330564 ]
[0. 8.457685 8.602337 0. ]]
[[0. 8.3356 0. 8.8974 ]
[9.497023 0. 3.8914037 0. ]
[0. 0. 5.826657 8.283971 ]]
[[9.968594 8.655439 0. 0. ]
[0. 0. 0. 0. ]
[0. 0. 0. 0. ]]
[[0. 0. 6.103182 7.330564 ]
[6.609862 0. 3.0614321 0. ]
[0. 0. 5.826657 8.283971 ]]
[[0. 0. 6.103182 7.330564 ]
[9.497023 0. 3.8914037 0. ]
[0. 0. 0. 0. ]]]
I thought I can do a similar operation in tensorflow using:
tf.gather_nd(tf_a1, tf_a2)
but it raises this error:
tensorflow.python.framework.errors_impl.InvalidArgumentError: index innermost dimension length must be <= params rank; saw: 3 vs. 2 [Op:GatherNd]
Any help is appreciated:)