tl.layers.DropoutLayer引发的“ You must feed a value for placeholder tensor 'Placeholder' ”问题及解决方法

       最近在看《一起玩转tensorlayer》,在降噪自编码器的重构图像可视化时总是报错没有给placeholder赋值。检查代码没有发现feed_dict有错误,最后查tensorlayer文档明白了问题出在tl.layers.DropoutLayer层。

       如果网络模型中用到了tl.layers.DropoutLayer层,在训练和测试的sess.run()之前必须要对feed_dict做出一定的操作。文档如下:  

Dropout 层

class tensorlayer.layers.DropoutLayer(prev_layerkeep=0.5is_fix=Falseis_train=Trueseed=Nonename='dropout_layer'源代码

The DropoutLayer class is a noise layer which randomly set some activations to zero according to a keeping probability.

参数:
  • prev_layer (Layer) -- Previous layer.
  • keep (float) -- The keeping probability. The lower the probability it is, the more activations are set to zero.
  • is_fix (boolean) -- Fixing probability or nor. Default is False. If True, the keeping probability is fixed and cannot be changed via feed_dict.
  • is_train (boolean) -- Trainable or not. If False, skip this layer. Default is True.
  • seed (int or None) -- The seed for random dropout.
  • name (str) -- A unique layer name.

Examples

Method 1: Using all_drop see tutorial_mlp_dropout1.py

import tensorflow as tf
import tensorlayer as tl
net = tl.layers.InputLayer(x, name='input_layer')
net = tl.layers.DropoutLayer(net, keep=0.8, name='drop1')
net = tl.layers.DenseLayer(net, n_units=800, act=tf.nn.relu, name='relu1')
...
# For training, enable dropout as follow.
feed_dict = {x: X_train_a, y_: y_train_a}
feed_dict.update( net.all_drop )     # enable noise layers
sess.run(train_op, feed_dict=feed_dict)
...
# For testing, disable dropout as follow.
dp_dict = tl.utils.dict_to_one( net.all_drop ) # disable noise layers
feed_dict = {x: X_val_a, y_: y_val_a}
feed_dict.update(dp_dict)
err, ac = sess.run([cost, acc], feed_dict=feed_dict)
...

Method 2: Without using all_drop see tutorial_mlp_dropout2.py

def mlp(x, is_train=True, reuse=False):
    with tf.variable_scope("MLP", reuse=reuse):
    tl.layers.set_name_reuse(reuse)
    net = tl.layers.InputLayer(x, name='input')
    net = tl.layers.DropoutLayer(net, keep=0.8, is_fix=True,
                        is_train=is_train, name='drop1')
    ...
    return net

net_train = mlp(x, is_train=True, reuse=False)
net_test = mlp(x, is_train=False, reuse=True)

      用了第一种方法在训练和测试中都加上feed_dict的修改,调试通过。得到重构图像的可视化如图:

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值