锅碗瓢
锅碗瓢

Reputation: 1

How to use tensorflow to slice a Tensor phase

How do I use tensorflow to slice a Tensor phase:

tensor = tf.constant([[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5] ] )
    list = [0,2]

I want to get the result tensor:

result_tensor = [[1,2,3],[1,2,3],[1,2,3]]

Upvotes: 0

Views: 86

Answers (3)

Rxma
Rxma

Reputation: 312


    import tensorflow as tf
    sess=tf.Session()
    def sess_print(tensor):
        print(sess.run(tensor))
    
    tensor = tf.constant([[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5] ] )
    sess_print(tf.slice(tensor,[0,0],[3,3]))

[[1 2 3]
 [1 2 3]
 [1 2 3]]

Upvotes: 0

aryanknp
aryanknp

Reputation: 1167

tensor =tf.constant([[6,2,3,4,5],[1,7,3,4,5],[1,2,3,4,5] ] )

new_tensor = tf.slice(tensor,[0,0],[-1,3])
print(new_tensor)

This outputs

tf.Tensor(
[[6 2 3]
 [1 7 3]
 [1 2 3]], shape=(3, 3), dtype=int32)

Upvotes: 0

Courage
Courage

Reputation: 792

tensor =tf.constant([[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5] ] )
lst = [0,2]
tensor[:, lst[0]:lst[1]+1]

Upvotes: 1

Related Questions