2017-01-27 3 views
1

입력 텐서의 ~ 개의 서브 센서을 적용하여 출력 텐서를 얻고 싶습니다. NumPy와 예를 들어 TensorFlow의 인덱싱 서브 센서

,

import numpy as np 

input = np.random.random((100,5)) # matrix 
index = np.randint(5, size=(100,)) # vector 
output = data[np.arange(index.shape[0]), index] # vector 

나에게 원하는 출력 (I이의 상징적 버전을 원하는) 제공합니다. Theano에서와 비슷하게

,

import theano.tensor as T 
import theano 

input = T.matrix() # symbolic matrix 
index = T.ivector() # symbolic vector 
output = input[T.arange(index.shape[0]), index] # symbolic vector 

나에게 원하는 output을 제공합니다.

어떻게해야합니까? TensorFlow? NumPy와, index의 길이 (= input의 1 사이즈)가 고정되지 않은 예와 달리

import tensorflow as tf 
input = tf.placeholder('float32', [None, 5]) 
index = tf.placeholder('int32', [None]) 
output = ??? 

.

답변

1

당신은 tf.gather_nd과 슬라이싱을 수행 할 수 있습니다

output = tf.gather_nd(input, tf.stack((tf.range(tf.shape(index)[0]), index), -1)) 
+0

을 난 두려워하는 나에게 잘못된 결과를 제공 : codebomb

+0

내가 원하는 : [입력 [0] [인덱스 [0]], 입력 [1] [인덱스 [1]], 입력 [2] [인덱스 [2]] ...] tf. 모아주세요 : [input [index [0]], input [index [1]], ...] – codebomb

+0

@codebomb 그래, 미안해, 내가 잘못 이해했다! 나는 지금 그것을 편집했다. – jdehesa