Tensorflow TypeError: Input 'pred' of 'Switch' Op has type float32 that does not match expected type of bool
Tensorflow TypeError: Input 'pred' of 'Switch' Op has type float32 that does not match expected type of bool
我目前正在使用 Tensorflow 构建深度神经网络,在实施称为 dropout 的正则化技术时遇到了一些问题(查看 Geoffrey Hinton here 的原始论文)。
Tensorflow 有一个功能可以解决这个问题,我正在学习 Aurelien Geron 的书 Hands-On Machine Learning with Scikit-Learn & Tensorflow 的教程(其中,顺便说一句,太不可思议了)。在其中,他实现 dropout 的示例代码包括声明一个 training
占位符:
training = tf.placeholder(tf.float32, shape = (), name = "training")
然后创建隐藏层丢弃对象:
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 dropout,tf.layers.dropout()
方法的 training
参数定义为
Either a Python boolean, or a TensorFlow boolean scalar tensor (e.g. a
placeholder). Whether to return the output in training mode (apply
dropout) or in inference mode (return the input untouched).
但是,在上面的代码中,我显然传入了 tf.float32
。我怀疑这是我出错的原因——它甚至在错误消息本身中也有说明。那么这只是作者的错字,还是我不了解幕后发生的事情?
我应该只用这一行替换隐藏层声明吗?
hidden1_drop = tf.layers.dropout(hidden1, dropout_rate, training = True)
我也查看了其他 SO posts with similar errors,比如这个,但答案似乎表明错误源于 Tensorflow 的过时版本,事实并非如此 - 我最近才安装几周前在我的机器上。
我会继续回答我自己的问题,因为我是个白痴。
作者没有打错字。我按照他的教程打错了字。 training
可以是 Python 布尔值或 Tensorflow 布尔张量。在书中,作者的实际代码是
training = tf.placeholder_with_default(False, shape=())
进行此切换应该可以解决问题。
我目前正在使用 Tensorflow 构建深度神经网络,在实施称为 dropout 的正则化技术时遇到了一些问题(查看 Geoffrey Hinton here 的原始论文)。
Tensorflow 有一个功能可以解决这个问题,我正在学习 Aurelien Geron 的书 Hands-On Machine Learning with Scikit-Learn & Tensorflow 的教程(其中,顺便说一句,太不可思议了)。在其中,他实现 dropout 的示例代码包括声明一个 training
占位符:
training = tf.placeholder(tf.float32, shape = (), name = "training")
然后创建隐藏层丢弃对象:
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 dropout,tf.layers.dropout()
方法的 training
参数定义为
Either a Python boolean, or a TensorFlow boolean scalar tensor (e.g. a placeholder). Whether to return the output in training mode (apply dropout) or in inference mode (return the input untouched).
但是,在上面的代码中,我显然传入了 tf.float32
。我怀疑这是我出错的原因——它甚至在错误消息本身中也有说明。那么这只是作者的错字,还是我不了解幕后发生的事情?
我应该只用这一行替换隐藏层声明吗?
hidden1_drop = tf.layers.dropout(hidden1, dropout_rate, training = True)
我也查看了其他 SO posts with similar errors,比如这个,但答案似乎表明错误源于 Tensorflow 的过时版本,事实并非如此 - 我最近才安装几周前在我的机器上。
我会继续回答我自己的问题,因为我是个白痴。
作者没有打错字。我按照他的教程打错了字。 training
可以是 Python 布尔值或 Tensorflow 布尔张量。在书中,作者的实际代码是
training = tf.placeholder_with_default(False, shape=())
进行此切换应该可以解决问题。