2016-12-21 9 views
-1

int32の1次元テンソルがあります。最初に出現する前の要素を0に置き換えたいと思います。1次元テンソルを特定のインデックステンソルで分割する方法は?

#This is a numpy equivalent. 
import numpy as np 
a = np.array([5, 4, 1, 3, 1, 2, 3, 3, 1, 5], np.int32) 
first_ind = np.where(a == 1)[0][0] # => 2 
result = np.concatenate((np.zeros((first_ind,)), a[first_ind:])) 
# =>[ 0. 0. 1. 3. 1. 2. 3. 3. 1. 5.] 

import tensorflow as tf 
_a = tf.convert_to_tensor(a) 
_first_ind = tf.where(tf.equal(_a, 1))[0][0] 
# But I don't know what to do next. 

答えて

0

私自身が答えを得ました。

import numpy as np 
a = np.array([5, 4, 1, 3, 1, 2, 3, 3, 1, 5], np.int32) 
first_ind = np.where(a == 1)[0][0] # => 2 
result = np.concatenate((np.zeros((first_ind,)), a[first_ind:])) 
# =>[ 0. 0. 1. 3. 1. 2. 3. 3. 1. 5.] 

import tensorflow as tf 
_a = tf.convert_to_tensor(a) 
_first_ind = tf.where(tf.equal(_a, 1))[0] 

zero_padding = tf.zeros(tf.to_int32(_first_ind), tf.int32) 
_a_back = tf.slice(_a, _first_ind, [-1]) 
out = tf.concat(0, (zero_padding, _a_back)) 
with tf.Session() as sess: 
    print out.eval() 
    #=> [0 0 1 3 1 2 3 3 1 5] 
関連する問題