2015-12-14 50 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中不会出现核心培训示例。 –