【机器学习基础】nn.Dropout的用法

1.nn.Dropout用法一

一句话总结:Dropout的是为了防止过拟合而设置

  • 详解:
    1.Dropout是为了防止过拟合而设置的
    2.Dropout顾名思义有丢掉的意思
    3.nn.Dropout(p = 0.3) # 表示每个神经元有0.3的可能性不被激活
    4.Dropout只能用在训练部分而不能用在测试部分
    5.Dropout一般用在全连接神经网络映射层之后,如代码的nn.Linear(20, 30)之后

代码部分:

python 复制代码
class Dropout(nn.Module):
	def __init__(self):
		super(Dropout, self).__init__()
		self.linear = nn.Linear(20, 40)
		self.dropout = nn.Dropout(p = 0.3) # p=0.3表示下图(a)中的神经元有p = 0.3的概率不被激活

	def forward(self, inputs):
		out = self.linear(inputs)
		out = self.dropout(out)
		return out

net = Dropout()
# Dropout只能用在train而不能用在test	

2.nn.Dropout用法二

python 复制代码
import torch
import torch.nn as nn

a = torch.randn(4, 4)
print(a)
"""
tensor([[ 1.2615, -0.6423, -0.4142,  1.2982],
        [ 0.2615,  1.3260, -1.1333, -1.6835],
        [ 0.0370, -1.0904,  0.5964, -0.1530],
        [ 1.1799, -0.3718,  1.7287, -1.5651]])
"""
dropout = nn.Dropout()
b = dropout(a)
print(b)
"""
tensor([[ 2.5230, -0.0000, -0.0000,  2.5964],
        [ 0.0000,  0.0000, -0.0000, -0.0000],
        [ 0.0000, -0.0000,  1.1928, -0.3060],
        [ 0.0000, -0.7436,  0.0000, -3.1303]])
"""

由以上代码可知Dropout还可以将部分tensor中的值置为0

https://blog.csdn.net/weixin_47050107/article/details/122722516

相关推荐
wflynn几秒前
语言判别增强多语言语音模型的语言学习能力
人工智能·ai
neocheng_5221 分钟前
自学、培训、项目还是认证?HR 学习 AI 的四种路径如何组合
人工智能
2501_933670795 分钟前
2027风控策略岗秋招准备:SQL、Excel、建模的优先级与项目路径
人工智能·sql·excel
小宋102113 分钟前
AI 生成内容如何证明来源:C2PA、Content Credentials 与签名校验
人工智能
zhousenshan14 分钟前
AgentScope 和 Spring AI Alibaba的区别
人工智能
奈落2415 分钟前
【RAG 深度修炼】专栏 · 第 9 期(收官):安全专题与生产 Checklist 总集——从能跑的 Demo 到睡得着觉的生产系统
大数据·网络·人工智能·安全·ai编程
超大青花鱼15 分钟前
Ubuntu20.04安装CUDA11.8教程
人工智能·深度学习·计算机视觉
golang学习记24 分钟前
Jetbrains 正式官宣:全新AI IDE正式发布!
ide·人工智能·intellij-idea·air
HZjiangzi25 分钟前
深耕精密驱动二十六载,捷昌驱动助力机器人产业升级发展
人工智能·算法
鬓戈34 分钟前
Claude Code frontend-design 插件调研 与 Vue 旧系统风格一致性方案
前端·vue.js·人工智能