Multiple Dimension Input 处理多维特征的输入

简介: Multiple Dimension Input 处理多维特征的输入

6、Multiple Dimension Input 处理多维特征的输入

B站视频教程传送门:PyTorch深度学习实践 - 处理多维特征的输入

6.1 Revision

我们先来回顾一下回归分类

差别: 主要在于输出值

回归(Regressiom):y ∈ R

分类(Classification):y ∈ { } 离散的集合

6.2 Diabetes Dataset 糖尿病数据集

如果我们安装过sklearnpython编程安装sklearn),其中就包含糖尿病数据集,可以进入该目录(D:\Software\Anaconda\Lib\site-packages\sklearn\datasets\data)下查看,如下图所示:

6.3 Logistic Regression Model 逻辑斯蒂回归模型

由于这里的 x不再是简简单单的一维,而是 8维,所以应该看成下方两个矩阵相乘:

image.png

6.4 Mini-Batch(N samples)

import torch
class Liang(torch.nn.Module):
    def __init__(self):
        super(Liang, self).__init__()
        self.linear = nn.Linear(8, 1)
        self.sigmoid = torch.nn.Sigmoid()
    def forward(self, x):
        x = self.sigmoid(self.linear(x))
        return x
model = Liang()

6.5 Neural Network 神经网络

当输入8维,输出2维时:

self.linear = torch.nn.Linear(8, 2)

当输入8维,输出6维时:

self.linear = torch.nn.Linear(8, 6)

可以降维,可以升维,也可以一降(升)一升(降):

6.6 Diabetes Prediction 糖尿病预测

X1~X8:病人相应的指标

Y:一年后病情是否加重(预测)

6.6.1 Prepare Dataset

import numpy as np
xy = np.loadtxt('../data/diabetes.csv.gz', delimiter=',', dtype=np.float32)
x_data = torch.from_numpy(xy[:, :-1])  # 所有行,除了最后一列
y_data = torch.from_numpy(xy[:, [-1]])  # 所有行,最后一列 转为矩阵而不是向量

6.6.2 Define Model

6.6.3 Construct Loss and Optimizer

criterion = torch.nn.BCELoss(reduction='mean')
optimizer = torch.optim.SGD(model.parameters(), lr=0.1)

6.6.4 Training Cycle

for epoch in range(100):
    # Forward
    y_pred = model(x_data) # This program has not use Mini-Batch for training. We shall talk about DataLoader later.
    loss = criterion(y_pred, y_data)
    print(epoch, loss.item())
    # Backward
    optimizer.zero_grad()
    loss.backward()
    # Update
    optimizer.step()

6.6.5 Activate function

神经网络中激活函数的可视化:https://dashee87.github.io/deep%20learning/visualising-activation-functions-in-neural-networks/

PyTorch文档:https://pytorch.org/docs/stable/nn.html#non-linear-activations-weighted-sum-nonlinearity

6.6.5 完整代码

import torch
import numpy as np
import matplotlib.pyplot as plt
xy = np.loadtxt('../data/diabetes.csv.gz', delimiter=',', dtype=np.float32)
x_data = torch.from_numpy(xy[:, :-1])  # 所有行,除了最后一列
y_data = torch.from_numpy(xy[:, [-1]])  # 所有行,最后一列 转为矩阵而不是向量
class Liang(torch.nn.Module):
    def __init__(self):
        super(Liang, self).__init__()
        self.linear1 = torch.nn.Linear(8, 6)
        self.linear2 = torch.nn.Linear(6, 4)
        self.linear3 = torch.nn.Linear(4, 1)
        self.sigmoid = torch.nn.Sigmoid()  # Sigmoid
        self.tanh = torch.nn.Tanh()
    def forward(self, x):
        x = self.tanh(self.linear1(x))
        x = self.tanh(self.linear2(x))
        x = self.sigmoid(self.linear3(x))
        return x
model = Liang()
criterion = torch.nn.BCELoss(reduction='mean')
optimizer = torch.optim.SGD(model.parameters(), lr=0.1)
epoch_list = []
loss_list = []
for epoch in range(100):
    # Forward
    y_pred = model(x_data)
    loss = criterion(y_pred, y_data)
    print(epoch, loss.item())
    epoch_list.append(epoch)
    loss_list.append(loss.item())
    # Backward
    optimizer.zero_grad()
    loss.backward()
    # Update
    optimizer.step()
plt.plot(epoch_list, loss_list)
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.title('Tanh')
plt.show()

目录
相关文章
|
机器学习/深度学习 数据挖掘
【提示学习】Label prompt for multi-label text classification
  标签文本分类的关键问题之一是提取和利用标签之间的相关性,但直接建模标签之间的相关性很难。   LP-MTC设计了多标签文本分类模板,将标签整合到预训练语言模型的输入中,可以捕获标签之间的相关性以及标签与文本之间的语义信息,从而有效地提高模型的性能。
101 0
|
机器学习/深度学习 测试技术
机器学习系列 | 01:多类别分类任务(multi-class)中为何precision,recall和F1相等?
在 multi-class 分类任务中,如果使用 micro 类指标,那么 micro-precision, micro-recall和micro-F1值都是相等的。本文主要针对这个现象进行解释。
|
机器学习/深度学习 算法 数据挖掘
【多标签文本分类】SGM: Sequence Generation Model for Multi-Label Classification
【多标签文本分类】SGM: Sequence Generation Model for Multi-Label Classification
153 0
【多标签文本分类】SGM: Sequence Generation Model for Multi-Label Classification
|
算法 数据挖掘
【多标签文本分类】BERT for Sequence-to-Sequence Multi-Label Text Classification
【多标签文本分类】BERT for Sequence-to-Sequence Multi-Label Text Classification
109 0
【多标签文本分类】BERT for Sequence-to-Sequence Multi-Label Text Classification
|
机器学习/深度学习 数据挖掘
【多标签文本分类】HFT-CNN: Learning Hierarchical Category Structure for Multi-label Short Text Categorization
【多标签文本分类】HFT-CNN: Learning Hierarchical Category Structure for Multi-label Short Text Categorization
213 0
【多标签文本分类】HFT-CNN: Learning Hierarchical Category Structure for Multi-label Short Text Categorization
|
机器学习/深度学习 算法 数据挖掘
【多标签文本分类】Large Scale Multi-label Text Classification with Semantic Word Vectors
【多标签文本分类】Large Scale Multi-label Text Classification with Semantic Word Vectors
157 0
【多标签文本分类】Large Scale Multi-label Text Classification with Semantic Word Vectors
|
机器学习/深度学习 数据挖掘
【多标签文本分类】Balancing Methods for Multi-label Text Classification with Long-Tailed Class Distribution
【多标签文本分类】Balancing Methods for Multi-label Text Classification with Long-Tailed Class Distribution
135 0
【多标签文本分类】Balancing Methods for Multi-label Text Classification with Long-Tailed Class Distribution
|
机器学习/深度学习 Python
机器学习: Label vs. One Hot Encoder
机器学习: Label vs. One Hot Encoder
172 0
|
机器学习/深度学习 算法
随机装箱特征模型(Random Binning Feature Model)代码
在上述代码中,我们首先定义了一个名为RandomBinningFeatureModel的类,并在其__init__方法中接受一个参数num_bins,表示装箱的数量。然后,我们定义了一个名为fit_transform的方法,该方法接受原始输入数据x作为参数,并返回转换后的特征张量xb。
157 0