Tensorflow TypeError:输入' pred' ' Switch'操作类型为float32,与预期类型的​​bool

时间:2017-10-26 22:08:39

标签: python tensorflow data-science

我目前正致力于使用Tensorflow构建深度神经网络,并遇到一些实现称为丢失的正则化技术的问题(请参阅Geoffrey Hinton撰写的原始论文here)。

Tensorflow有一个处理这个问题的功能,我正在按照Aurelien Geron的书“使用Scikit-Learn& amp; Tensorflow (顺便说一下,这是令人难以置信的)。其中,他实现辍学的示例代码包括声明training占位符:

training = tf.placeholder(tf.float32, shape = (), name = "training")

然后创建隐藏图层dropout对象:

hidden1_drop = tf.layers.dropout(hidden1, dropout_rate, training = training)

然而,当我执行此操作时,我收到指向上一行的错误。

TypeError: Input 'pred' of 'Switch' Op has type float32 that does not match expected type of bool

我查看了Tensorflow documentation regarding dropouttf.layers.dropout()方法' training参数定义为

  

Python boolean或TensorFlow布尔标量张量(例如a   占位符)。是否在训练模式下返回输出(应用   退出)或推理模式(不改变输入)。

但是,在上面的代码中,我明确地传递了tf.float32。我怀疑这是我的错误的原因 - 它甚至在错误消息本身中说明。那么这只是作者的拼写错误,还是我不了解幕后发生的事情?

我应该用这一行代替隐藏的图层声明吗?

hidden1_drop = tf.layers.dropout(hidden1, dropout_rate, training = True)

我也调查了其他SO posts with similar errors,就像这个一样,但答案似乎表明该错误源于Tensorflow的过时版本,但事实并非如此 - 我最近才安装几个星期前在我的机器上。

1 个答案:

答案 0 :(得分:0)

我会继续回答我自己的问题,因为我是个白痴。

作者没有写错字。我按照他的教程做了一个拼写错误。 training可以是Python布尔值或Tensorflow布尔张量。在书中,作者的实际代码是

training = tf.placeholder_with_default(False, shape=())

进行此切换应该可以解决问题。