2017-01-25 2 views
0

Tensorflow를 사용하여 텐서 인덱스에 해당하는 것을 제외한 모든 하위 요소를 추출하는 방법을 찾고 있습니다.기울어 진 텐서의 I 번째 하위 요소 제거 또는 마스킹?

(예. 다음 인덱스 1 만 보면 하위 요소 0 및 2는 본 경우)

NumPy와를 사용 this approach 매우 유사.

import tensorflow as tf 
import numpy as np 

_coordinates = np.array([ 
    [1.0, 7.0, 0.0], 
    [2.0, 7.0, 0.0], 
    [3.0, 7.0, 0.0], 
]) 

verts_coord = _coordinates 
n = verts_coord.shape[0] 

mat_loc = tf.Variable(verts_coord) 

tile = tf.tile(mat_loc, [n, 1]) 
tile = tf.reshape(tile, [n, n, n]) 

mask = tf.constant(~np.eye(n, dtype=bool)) 

result = tf.somefunc(tile, mask) #somehow extract only the elements where mask == true 

with tf.Session() as sess: 
    sess.run(tf.initialize_all_variables()) 
    print(sess.run(tile)) 
    print(sess.run(mask)) 

예 출력 텐서 : 여기

는 타일 텐서 및 부울 마스크를 만들기 위해 몇 가지 예제 코드입니다

>>> print(tile) 
[[[ 1. 7. 0.] 
    [ 2. 7. 0.] 
    [ 3. 7. 0.]] 

[[ 1. 7. 0.] 
    [ 2. 7. 0.] 
    [ 3. 7. 0.]] 

[[ 1. 7. 0.] 
    [ 2. 7. 0.] 
    [ 3. 7. 0.]]] 

>>> print(mask) 
[[False True True] 
[ True False True] 
[ True True False]] 

원하는 출력 :

>>> print(result) 
[[[ 2. 7. 0.] 
    [ 3. 7. 0.]] 

[[ 1. 7. 0.] 
    [ 3. 7. 0.]] 

[[ 1. 7. 0.] 
    [ 2. 7. 0.]]] 

나는 또한 궁금합니다 만약 큰 텐서를 만들고 그것을 마스킹하는 것과는 대조적으로 이것을하는 더 효율적인 방법이 있다면?

감사합니다.

답변

0

는 Tensorflow가 이미 :)

result = tf.boolean_mask(tile, mask) 
result = tf.reshape(result, [n, n-1, -1]) 

>>> print(result) 
[[[ 2. 7. 0.] 
    [ 3. 7. 0.]] 

[[ 1. 7. 0.] 
    [ 3. 7. 0.]] 

[[ 1. 7. 0.] 
    [ 2. 7. 0.]]] 
내장을 찾고 정확히 무엇을했다 밝혀
관련 문제