2017-10-18 7 views
1

私は、これを達成するための最良の方法は何...tf.gather_ndの形状を「平坦化」しないでください。

Input Tensor: - shape: (2, 7, 4) 
array([[[ 0., 0., 1., 2.], 
     [ 0., 0., 2., 2.], 
     [ 0., 0., 3., 3.], 
     [ 0., 0., 4., 3.], 
     [ 0., 0., 5., 4.], 
     [ 0., 0., 6., 4.], 
     [ 0., 0., 7., 5.]], 
     [[ 1., 1., 0., 2.], 
     [ 1., 2., 0., 2.], 
     [ 1., 3., 0., 3.], 
     [ 1., 4., 0., 3.], 
     [ 1., 5., 0., 4.], 
     [ 1., 6., 0., 5.], 
     [ 1., 7., 0., 5.]]], dtype=float32) 

Indices returned by tf.where op: - shape: (3, 2) 
array([[0, 0], 
     [0, 1], 
     [1, 0]]) 

tf.gather results: (shape = [3, 4]) 
array([[ 0., 0., 1., 2.], 
     [ 0., 0., 2., 2.], 
     [ 1., 1., 0., 2.]], dtype=float32) 

desired results: = (2, sparse, 4) 
array([[[ 0., 0., 1., 2.], 
     [ 0., 0., 2., 2.]], 
     [[ 1., 1., 0., 2.]]], dtype=float32) 

まだtensorflowで遊んだし、gather_ndオペアンプを使用しようとしてきたが、戻り値は、私が欲しい形状/フォーマットではありませんtf.where =動的な形状であり、第2次元(軸= 1)にわたって形状の一貫性が保障されないことを覚えていますか?

NB:この質問を無視 - 私はそのTensorflowバージョンの問題を考える私の答え

答えて

0

を参照してください。私のバージョン(1.2.1)では、あなたの入力から正確な出力が得られます。しかし、私は古いバージョンに従って次のコードも試しました。

import tensorflow as tf 

indices = [[[0, 0, 0], [0, 0, 1], [0, 0, 2], [0, 0, 3]], 
      [[0, 1, 0], [0, 1, 1], [0, 1, 2], [0, 1, 3]], 
      [[1, 0, 0], [1, 0, 1], [1, 0, 2], [1, 0, 3]]] 

params = [[[ 0., 0., 1., 2.], 
      [ 0., 0., 2., 2.], 
      [ 0., 0., 3., 3.], 
      [ 0., 0., 4., 3.], 
      [ 0., 0., 5., 4.], 
      [ 0., 0., 6., 4.], 
      [ 0., 0., 7., 5.]], 
      [[ 1., 1., 0., 2.], 
      [ 1., 2., 0., 2.], 
      [ 1., 3., 0., 3.], 
      [ 1., 4., 0., 3.], 
      [ 1., 5., 0., 4.], 
      [ 1., 6., 0., 5.], 
      [ 1., 7., 0., 5.]]] 

output = tf.gather_nd(params, indices) 

with tf.Session()as sess: 
    print (sess.run(output)) 

これが役に立ちます。

0

私は自分の質問の馬鹿馬鹿しいことを実現しました。

# of tuples when 1st dim is 0 != # of tuples when 1st dim is 1 

私は私が求めていることは実現可能であることを確認しないんだけど...

関連する問題