Pytorch:全连接神经网络-MLP分类

Pytorch: 全连接神经网络-多层感知机解决分类问题

copyright: Jingmin Wei, Automation 1801, School of Artificial and Intelligence, Huazhong University of Science and Technology

Pytorch教程专栏链接

文章目录

*

+ Pytorch: 全连接神经网络-多层感知机解决分类问题
@[toc]

+
* MLP 垃圾邮件分类
* 数据准备与探索
* 搭建网络并可视化
* 使用预处理后的数据训练模型
* 获取中间层的输出并可视化
*
使用中间层的输出
使用钩子获取中间层的输出

全连接神经网络MLP,或者叫多层感知机,采用BP算法实现,也叫BP神经网络,属于前馈神经网络。由输入层,输出层和隐藏层构成。输入层的神经元个数与输入的特征数量相同,隐藏层和输出层神经元对信号进行加工处理,最终结果由输出层神经元输出。

接下来探讨MLP在分类和回归任务中的应用。

import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler, MinMaxScaler
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report
from sklearn.manifold import TSNE

import torch
import torch.nn as nn
from torch.optim import SGD, Adam
import torch.utils.data as Data
import matplotlib.pyplot as plt
import seaborn as sns
import hiddenlayer as hl
from torchviz import make_dot

MLP 垃圾邮件分类

数据准备与探索

数据集下载地址:http://archive.ics.uci.edu/ml/datasets/Spambase

数据库说明:48个关键词频率,6个关键字符,1个大写字母不间断的平均长度,1个大写字母不间断的最大长度,一个大写字母变量,最后1个为带预测目标变量。

spam = pd.read_csv('./data/spambase/spambase.data')
spam.head()

word_freq_makeword_freq_addressword_freq_allword_freq_3dword_freq_ourword_freq_overword_freq_removeword_freq_internetword_freq_orderword_freq_mail…char_freq_;char_freq_(char_freq_[char_freq_!char_freq_$char_freq_#capital_run_length_averagecapital_run_length_longestcapital_run_length_totallabel00.000.640.640.00.320.000.000.000.000.00…0.000.0000.00.7780.0000.0003.75661278110.210.280.500.00.140.280.210.070.000.94…0.000.1320.00.3720.1800.0485.1141011028120.060.000.710.01.230.190.190.120.640.25…0.010.1430.00.2760.1840.0109.8214852259130.000.000.000.00.630.000.310.630.310.63…0.000.1370.00.1370.0000.0003.53740191140.000.000.000.00.630.000.310.630.310.63…0.000.1350.00.1350.0000.0003.537401911

5 rows × 58 columns


pd.value_counts(spam.label)
0    2788
1    1813
Name: label, dtype: int64

X = spam.iloc[:,0: 57].values
y = spam.label.values
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=123)

scales = MinMaxScaler(feature_range=(0, 1))
X_train_s = scales.fit_transform(X_train)

X_test_s = scales.fit_transform(X_test)

colname = spam.columns.values[:-1]
plt.figure(figsize=(20, 14))
for ii in range(len(colname)):
    plt.subplot(7, 9, ii + 1)
    sns.boxplot(x = y_train, y = X_train_s[:, ii])
    plt.title(colname[ii])
plt.subplots_adjust(hspace = 0.4)
plt.show()


Pytorch:全连接神经网络-MLP分类

有些特征在两种类型的分布上有较大差异

搭建网络并可视化


class myMLP(nn.Module):
    def __init__(self):
        super(myMLP, self).__init__()

        self.hidden1 = nn.Sequential(
            nn.Linear(in_features=57,
                      out_features=30,
                      bias=True
                      ),
            nn.ReLU()
        )

        self.hidden2 = nn.Sequential(
            nn.Linear(30, 10),
            nn.ReLU()
        )

        self.classify = nn.Sequential(
            nn.Linear(10, 2),
            nn.Sigmoid()
        )

    def forward(self, x):
        fc1 = self.hidden1(x)
        fc2 = self.hidden2(fc1)
        output = self.classify(fc2)
        return fc1, fc2, output

from torchsummary import summary
testnet = myMLP()
summary(testnet, input_size=(1, 57))

`
Input size (MB): 0.00
Forward/backward pass size (MB): 0.00
Params size (MB): 0.01
Estimated Total Size (MB): 0.01

Original: https://blog.csdn.net/weixin_44979150/article/details/122778457
Author: 宅家的小魏
Title: Pytorch:全连接神经网络-MLP分类

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/661857/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球