刘二老师第7节,多个激活器图例详细代码

由于编码能力有限,略显简陋,大牛勿喷

Model.py. 主类文件

import torch
import numpy as np
import matplotlib.pyplot as plt
from activate import activate_fun

# data_set
xy = np.loadtxt('diabetes.csv.gz', delimiter=',', dtype=np.float32)  # 第二个参数相当于分割作用
x_data = torch.from_numpy(xy[:, :-1])  # (所有行,从第一行开始,最后一行不要)
y_data = torch.from_numpy(xy[:, [-1]])  # 用[]保证拿出来的是个矩阵而不是向量   (所有行,只要最后一列)
activate_f = [
    activate_fun('ReLU6'),
    activate_fun('ReLU'),
    activate_fun('ELU'),
    activate_fun('PReLU'),
    activate_fun('LeakyReLU'),
    activate_fun('Threshold', 0.2, 0.0),
    activate_fun('Hardtanh'),
    activate_fun('Sigmoid'),
    activate_fun('Tanh'),
    activate_fun('LogSigmoid'),
    activate_fun('Softplus'),
    activate_fun('Softshrink'),
    activate_fun('Softsign'),
    activate_fun('Softmin'),
    activate_fun('Softmax'),
    activate_fun('LogSoftmax')
]


# model class
class multipleLogistic(torch.nn.Module):
    def __init__(self, act):
        super(multipleLogistic, self).__init__()
        self.linear1 = torch.nn.Linear(8, 6)
        self.linear2 = torch.nn.Linear(6, 4)
        self.linear3 = torch.nn.Linear(4, 1)  # 加了3个层
        self.activate = act  # 继承的仍然还是module模块
        self.sigmoid = torch.nn.Sigmoid()

    def forward(self, x):
        x = self.activate(self.linear1(x))  # 第一层的y_pred经过激活函数输出
        x = self.activate(self.linear2(x))  # 第二层输入为第一层输出
        x = self.activate(self.linear3(x))
        x = self.sigmoid(x)
        return x


epoch_list = range(1, 101)

loss_list = [[] for _ in activate_f]

for act_f, i in zip(activate_f, range(len(activate_f))):
    model = multipleLogistic(act_f)
    # criterion optimizer
    criterion = torch.nn.BCELoss(size_average=True)
    optimizer = torch.optim.SGD(model.parameters(), lr=0.1)
    # training model
    for epoch in range(100):
        y_pred = model(x_data)
        loss = criterion(y_pred, y_data)
        loss_list[i].append(loss.item())
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

fig, ax = plt.subplots()
ax.plot(epoch_list, loss_list[0], label='ReLU6')
ax.plot(epoch_list, loss_list[1], label='ReLU')
ax.plot(epoch_list, loss_list[2], label='ELU')
ax.plot(epoch_list, loss_list[3], label='PReLU')
ax.plot(epoch_list, loss_list[4], label='LeakyReLU')
ax.plot(epoch_list, loss_list[5], label='Threshold')
ax.plot(epoch_list, loss_list[6], label='Hardtanh')
ax.plot(epoch_list, loss_list[7], label='Sigmoid')
ax.plot(epoch_list, loss_list[8], label='Tanh')
ax.plot(epoch_list, loss_list[9], label='LogSigmoid')
ax.plot(epoch_list, loss_list[10], label='Softplus')
ax.plot(epoch_list, loss_list[11], label='Softshrink')
ax.plot(epoch_list, loss_list[12], label='Softsign')
ax.plot(epoch_list, loss_list[13], label='Softmin')
ax.plot(epoch_list, loss_list[14], label='Softmax')
ax.plot(epoch_list, loss_list[15], label='LogSoftmax')
ax.set_xlabel('rounds')
ax.set_ylabel('loss')
ax.set_title('activate_fun')
ax.legend()
plt.savefig('p.png')
plt.show()

activity.py 激活函数文件

import torch


def activate_fun(fun_name, *args, **kwargs):
    if fun_name == 'ReLU6':
        return torch.nn.ReLU6(*args, **kwargs)
    elif fun_name == 'ReLU':
        return torch.nn.ReLU(*args, **kwargs)
    elif fun_name == 'ELU':
        return torch.nn.ELU(*args, **kwargs)
    elif fun_name == 'PReLU':
        return torch.nn.PReLU(*args, **kwargs)
    elif fun_name == 'LeakyReLU':
        return torch.nn.LeakyReLU(*args, **kwargs)
    elif fun_name == 'Threshold':
        return torch.nn.Threshold(*args, **kwargs)
    elif fun_name == 'Hardtanh':
        return torch.nn.Hardtanh(*args, **kwargs)
    elif fun_name == 'Sigmoid':
        return torch.nn.Sigmoid()
    elif fun_name == 'Tanh':
        return torch.nn.Tanh()
    elif fun_name == 'LogSigmoid':
        return torch.nn.LogSigmoid()
    elif fun_name == 'Softplus':
        return torch.nn.Softplus(*args, **kwargs)
    elif fun_name == 'Softshrink':
        return torch.nn.Softshrink(*args, **kwargs)
    elif fun_name == 'Softsign':
        return torch.nn.Softsign()
    elif fun_name == 'Softmin':
        return torch.nn.Softmin()
    elif fun_name == 'Softmax':
        return torch.nn.Softmax(dim=1)
    elif fun_name == 'LogSoftmax':
        return torch.nn.LogSoftmax(dim=1)
    else:
        raise ValueError(f"不支持的激活函数: {fun_name}")

由于激活函数许多有的概率映射无法到0-1范围,所以程序运算时按照刘老师的源码会出问题,所以我在模型类里初始函数和前馈函数中神经网络最后一层加了sigmoid函数,将之前的激活函数输出值在映射一次到0-1范围内,不知道这种方法是否可行,不过我的目的是为了看各种激活函数的效果,所以具体影响就没有细究。以下是图例:

在这里插入图片描述
P S:有很多不会的,必要时确实需要问gpt,但不能盲从

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值