2017-05-31 10 views
0

私はtensorflowに初心者です。私はTensorで最大値のインデックスを取得しようとしています。ここでは、コードは次のとおりargmax関数であるので寸法の複数の次元に沿ったTensorflow argmax

Tensor("Relu:0", shape=(32, 3, 3, 5), dtype=float32) 
Tensor("Sum:0", shape=(32, 3, 3), dtype=float32) 
Tensor("ArgMax:0", shape=(32, 3), dtype=int64) 
... 

= 1 Tensor("ArgMax:0") = (32,3)の形状:ここで

def select(input_layer): 

    shape = input_layer.get_shape().as_list() 

    rel = tf.nn.relu(input_layer) 
    print (rel) 
    redu = tf.reduce_sum(rel,3) 
    print (redu) 

    location2 = tf.argmax(redu, 1) 
    print (location2) 

sess = tf.InteractiveSession() 
I = tf.random_uniform([32, 3, 3, 5], minval = -541, maxval = 23, dtype = tf.float32) 
matI, matO = sess.run([I, select(I, 3)]) 
print(matI, matO) 

が出力されます。 argmaxを適用する前にを実行せずにargmax出力テンソルサイズ= (32,)を取得する方法はありますか?

+0

'' tf.reshape(のRedu、[32、-1])と何が問題なのですか? ['tf.argmax'](https://www.tensorflow.org/api_docs/python/tf/argmax)は1つの軸に沿ってのみ縮小されます – martianwars

答えて

1

複数の方向に沿ってargmaxを使用している場合は、通常、縮小されたすべてのディメンションのmaxの座標が必要なので、サイズ(32,)の出力は望ましくありません。あなたの場合は、サイズが(32,2)の出力が必要です。

あなたはこのような2次元argmaxを行うことができます。

import numpy as np 
import tensorflow as tf 

x = np.zeros((10,9,8)) 
# pick a random position for each batch image that we set to 1 
pos = np.stack([np.random.randint(9,size=10), np.random.randint(8,size=10)]) 

posext = np.concatenate([np.expand_dims([i for i in range(10)], axis=0), pos]) 
x[tuple(posext)] = 1 

a = tf.argmax(tf.reshape(x, [10, -1]), axis=1) 
pos2 = tf.stack([a // 8, tf.mod(a, 8)]) # recovered positions, one per batch image 

sess = tf.InteractiveSession() 
# check that the recovered positions are as expected 
assert (pos == pos2.eval()).all(), "it did not work" 
+0

あなたの素早い応答に感謝しますが、問題は私がテンソルフロー0.9を使用していることですそのバージョンにはtf.stackは存在しません。代わりがありますか? – Maystro

+0

申し訳ありませんが、私はバージョン0.9を持っておらず、tensorflow.orgは0.10以降のドキュメントしか持っていません。連結が存在する場合は、おそらく 'stack_dims'と組み合わせて' stack'をシミュレートすることができます。 – user1735003

関連する問題