Reputation: 65
Say we got a tensor like thisx = [[[1,2],[3,4]],[[5,6],[7,8]]]
. I want a tensorflow operation such that it returns adding 1 to every element of first nested tensor. i.e. the result the operation will return [[[2,3],[3,4]],[[6,7],[7,8]]]
.
I know the tf.map_fn
operation, but I don't find a way to implement using this operation. How to solve this problem?
Upvotes: 3
Views: 2241
Reputation: 78536
You can split the tensor at the first axis, add one to the first tensors in the first axis and then stack the new and old tensors using tf.stack
:
>>> x = tf.constant([[[1, 2], [3, 4]],[[5, 6], [7, 8]]])
>>> with tf.Session() as sess:
... sess.run(tf.stack((x[:,0] + 1, x[:, 1]), axis=1))
...
array([[[2, 3],
[3, 4]],
[[6, 7],
[7, 8]]], dtype=int32)
Upvotes: 1