2015-12-14 71 views
2

下面的代碼工作正常,但使用eval(),我認爲這會效率低下。有沒有更好的方法來實現相同?Tensorflow:一個熱門編碼

import tensorflow as tf 
import numpy as np 
sess = tf.Session() 
t = tf.constant([[4,5.1,6.3,5,6.5,7.2,9.3,7,1,1.4],[4,5.1,9.3,5,6.5,7.2,1.3,7,1,1.4],[4,3.1,6.3,5,6.5,3.2,5.3,7,1,1.4]]) 
print t 
a = tf.argmax(t,1).eval(session=sess) 
z = [ k==np.arange(14) for k in a] 
z1 = tf.convert_to_tensor(np.asarray(z).astype('int32')) 
print z1 
print sess.run(z1) 

輸出

Tensor("Const_25:0", shape=TensorShape([Dimension(3), Dimension(10)]), dtype=float32) 
Tensor("Const_26:0", shape=TensorShape([Dimension(3), Dimension(14)]), dtype=int32) 
[[0 0 0 0 0 0 1 0 0 0 0 0 0 0] 
[0 0 1 0 0 0 0 0 0 0 0 0 0 0] 
[0 0 0 0 0 0 0 1 0 0 0 0 0 0]] 

回答

3

一種方法來實現它是計算每行最多,然後比較各元素到該值。我沒有在這臺機器上安裝張量流量,所以不能提供給您確切的代碼,但它會沿着這條線:

z1 = tf.equal(t, tf.reduce_max(t, reduction_indices=[1], keep_dims=True)) 
+0

非常感謝。已經連續3天這樣做了,並且我寫了最近搞砸的代碼,因爲eval在編譯該步驟時並未將我的整個16Gig吃掉,並且在我遇害時仍然飢餓:( –

+0

您也可以查看如果您的數據太大而無法放入內存,那麼在Scikit Flow中不會出現核心培訓示例。 –