【深度学习图像识别课程】神经网络系列:(7)tensorflow中dropout应用

本文介绍了一种防止神经网络过拟合的技术——Dropout,并通过Keras展示了其实现方式。利用ReLU激活函数和Dropout层构建了一个模型,设置keep_prob为0.5,并展示了如何在TensorFlow会话中运行此模型以获取logits。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、dropout原理

论文:Dropout: A Simple Way to Prevent Neural Networks fromOverfitting

防止过拟合的正则化技术

 

2、keras实现

tf.nn.dropout(hidden_layer, keep_prob)有2个参数:

(1)hidden_layer:应用dropout的tensor

(2)keep_prob:留存率。一般训练时初始留存率设置为0.5,测试时设为1.0,最大化模型的能力。

 

要求:用 ReLU 层和 dropout 层构建一个模型,keep_prob值设为 0.5。打印这个模型的 logits。

import tensorflow as tf

hidden_layer_weights = [
    [0.1, 0.2, 0.4],
    [0.4, 0.6, 0.6],
    [0.5, 0.9, 0.1],
    [0.8, 0.2, 0.8]]
out_weights = [
    [0.1, 0.6],
    [0.2, 0.1],
    [0.7, 0.9]]

# Weights and biases
weights = [
    tf.Variable(hidden_layer_weights),
    tf.Variable(out_weights)]
biases = [
    tf.Variable(tf.zeros(3)),
    tf.Variable(tf.zeros(2))]

# Input
features = tf.Variable([[0.0, 2.0, 3.0, 4.0], [0.1, 0.2, 0.3, 0.4], [11.0, 12.0, 13.0, 14.0]])

# TODO: Create Model with Dropout
keep_prob = tf.placeholder(tf.float32)
hidden_layer = tf.add(tf.matmul(features, weights[0]), biases[0])
hidden_layer = tf.nn.relu(hidden_layer)
hidden_layer = tf.nn.dropout(hidden_layer, keep_prob)

logits = tf.add(tf.matmul(hidden_layer, weights[1]), biases[1])
# TODO: Print logits from a session
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(sess.run(logits, feed_dict={keep_prob:0.5}))

3个输入,每个输入给出2个结果。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值