Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

习题4-3 #10

Open
fecet opened this issue Mar 29, 2020 · 4 comments
Open

习题4-3 #10

fecet opened this issue Mar 29, 2020 · 4 comments

Comments

@fecet
Copy link

fecet commented Mar 29, 2020

4-3

@AOnlyQ
Copy link

AOnlyQ commented May 25, 2021

4-3
解决方法:

  1. 使用带泄露的ReLU
  2. 使用带参数的ReLU
  3. 使用ELU函数
  4. 使用Softplus函数

@woqun1
Copy link

woqun1 commented Aug 2, 2022

解决方法补充:
1.归一化
2.初始化参数时be careful

@henghengxiong
Copy link

解决方法补充,加一个偏置b

@mJyo
Copy link

mJyo commented Sep 14, 2024

QQ20240914-154658

代码上的一些验证

import torch
import torch.nn as nn
import torch.optim as optim

# 定义简单的模型,使用ReLU激活函数
class SimpleModel(nn.Module):
    def __init__(self):
        super(SimpleModel, self).__init__()
        self.linear = nn.Linear(2, 1)  # 输入2个特征,输出1个
        self.relu = nn.ReLU()  # ReLU 激活函数
    
    def forward(self, x):
        z = self.linear(x)
        a = self.relu(z)  # 使用ReLU激活
        return a

# 创建模型实例
model = SimpleModel()

# 定义损失函数和优化器
loss_fn = nn.MSELoss()
optimizer = optim.SGD(model.parameters(), lr=0.01)

# 生成输入数据,确保输入x包含负值,来触发ReLU的“死亡”
x = torch.tensor([[-1.0, -2.0]], requires_grad=True)  # 负输入
y_true = torch.tensor([[1.0]])

# 训练模型
for epoch in range(10):  # 训练多次以体现现象
    # 前向传播
    y_pred = model(x)
    
    # 计算损失
    loss = loss_fn(y_pred, y_true)
    
    # 反向传播
    optimizer.zero_grad()
    loss.backward()

    # 检查 z 和 a 的梯度
    z_value = model.linear(x)
    print(f"Epoch {epoch+1}, z: {z_value.detach().numpy()}, ReLU Output (a): {y_pred.detach().numpy()}")
    print(f"Weight Gradients: {model.linear.weight.grad.numpy()}")

    # 更新权重
    optimizer.step()
    
    # 模拟后期的ReLU死亡现象,查看ReLU输出是否持续为0
    if y_pred.item() == 0:
        print(f"Neuron is dead at epoch {epoch+1}")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants