在TensorFlow中使用dropout时出现错误

9

我正在尝试使用tensorflow中的dropout功能:

sess=tf.InteractiveSession()
initial = tf.truncated_normal([1,4], stddev=0.1)  
x = tf.Variable(initial)  
keep_prob = tf.placeholder("float") 
dx = tf.nn.dropout(x, keep_prob)
sess.run(tf.initialize_all_variables())
sess.run(dx, feed_dict={keep_prob: 0.5})
sess.close()

这个示例与教程中的操作非常相似,但是我最终遇到了以下错误:

RuntimeError: min: Conversion function <function constant at 0x7efcc6e1ec80> for type <type 'object'> returned incompatible dtype: requested = float32_ref, actual = float32

我对dtype float32_ref 有些困惑,这似乎是问题的背景。我也试过指定dtype=tf.float32,但没有解决任何问题。

我还尝试了这个例子,使用float32可以正常工作:

sess=tf.Session()
x=tf.Variable(np.array([1.0,2.0,3.0,4.0]))
sess.run(x.initializer)
x=tf.cast(x,tf.float32)
prob=tf.Variable(np.array([0.5]))
sess.run(prob.initializer)
prob=tf.cast(prob,tf.float32)
dx=tf.nn.dropout(x,prob)
sess.run(dx)
sess.close()

然而,如果我将float32转换为float64,我会得到相同的错误:

RuntimeError: min: Conversion function <function constant at 0x7efcc6e1ec80> for type <type 'object'> returned incompatible dtype: requested = float64_ref, actual = float64

编辑:

似乎这个问题只会在直接应用于变量的dropout时出现,而对于占位符和变量与占位符的乘积则不会出现此问题。例如:

sess=tf.InteractiveSession()
x = tf.placeholder(tf.float64) 

sess=tf.InteractiveSession()
initial = tf.truncated_normal([1,5], stddev=0.1,dtype=tf.float64)  
y = tf.Variable(initial) 

keep_prob = tf.placeholder(tf.float64) 
dx = tf.nn.dropout(x*y, keep_prob)
sess.run(tf.initialize_all_variables())
sess.run(dx, feed_dict={x : np.array([1.0, 2.0, 3.0, 4.0, 5.0]),keep_prob: 0.5})
sess.close()
1个回答

8

这是在实现tf.nn.dropout时的一个bug,在最近的提交中已经修复,将包含在下一个TensorFlow版本中。目前,为了避免这个问题,可以选择从源代码构建TensorFlow,或按照以下方式修改您的程序:

#dx = tf.nn.dropout(x, keep_prob)
dx = tf.nn.dropout(tf.identity(x), keep_prob)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接