PyTorch深度学习实践 Lecture07 多维特征的输入


🤵 AuthorHorizon John

编程技巧篇各种操作小结

🎇 机器视觉篇会变魔术 OpenCV

💥 深度学习篇简单入门 PyTorch

🏆 神经网络篇经典网络模型

💻 算法篇再忙也别忘了 LeetCode


视频链接:Lecture 07 Multiple_Dimension_Input
文档资料:

//Here is the link:
课件链接:https://pan.baidu.com/s/1vZ27gKp8Pl-qICn_p2PaSw
提取码:cxe4

Multiple_Dimension_Input(多维特征的输入)

概述

在前面我们所说的都是 一维数据 的输入和输出

即:一个输入 X 对应 一个输出 Y

当处理多维数据时,即:多个输入 X 对应 一个输出 Y(如下图所示)




利用矩阵知识,可以得到下式之间的转变:

N 个 Samples

以及 Mini-Batch




激活函数使用的是 Sigmoid


使用 PyTorch 来构建:



Artificial Neural Network

构建以下 网络模型 对上面所述的 多维数据 进行建模分析:



Code

# Here is the code:

import numpy as np
import torch
import matplotlib.pyplot as plt
 
 
# 1 prepare dataset

xy = np.loadtxt('./diabetes.csv.gz', delimiter=',', dtype=np.float32)
x_data = torch.from_numpy(xy[:, :-1])     # 数组操作,读取至倒数第2行,即:所有的 X 值
y_data = torch.from_numpy(xy[:, [-1]])    # [-1] 为最后一列,需要添加[]使其仍为一个列表,不然输出为元组格式( )
 
 
# 2 design model using class

class Model(torch.nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        self.linear1 = torch.nn.Linear(8, 6)   # 输入特征 X 维度为8维,输出特征维度为6维
        self.linear2 = torch.nn.Linear(6, 4)
        self.linear3 = torch.nn.Linear(4, 1)   # 最后输出特征的维度为1维对应着 y_hat 的维度
        self.sigmoid = torch.nn.Sigmoid()      # 激活函数 使用Sigmoid
 
    def forward(self, x):      # 前向传播
        x = self.sigmoid(self.linear1(x))
        x = self.sigmoid(self.linear2(x))
        x = self.sigmoid(self.linear3(x))
        return x
        
model = Model()
 
 
# 3 construct loss and optimizer

criterion = torch.nn.BCELoss(reduction='mean')    # loss结果对mini-batch取平均值,即loss / mini-batch
optimizer = torch.optim.SGD(model.parameters(), lr=0.1)
 
epoch_list = []
loss_list = []


# 4 training cycle (forward  backward  update)

for epoch in range(100):
    y_pred = model(x_data)
    loss = criterion(y_pred, y_data)
    print(epoch, loss.item())
    epoch_list.append(epoch)
    loss_list.append(loss.item())

    optimizer.zero_grad()
    loss.backward()

    optimizer.step()

plt.plot(epoch_list, loss_list)
plt.ylabel('loss')
plt.xlabel('epoch')
plt.show()

函数链接: tensor.from_numpy()


运行结果


数据集下载: diabetes.csv.gz
提取码:6666


Exercise

使用 ReLU 作为激活函数


运行结果


附录:相关文档资料

PyTorch 官方文档: PyTorch Documentation
PyTorch 中文手册: PyTorch Handbook


《PyTorch深度学习实践》系列链接:

  Lecture01 Overview
  Lecture02 Linear_Model
  Lecture03 Gradient_Descent
  Lecture04 Back_Propagation
  Lecture05 Linear_Regression_with_PyTorch
  Lecture06 Logistic_Regression
  Lecture07 Multiple_Dimension_Input
  Lecture08 Dataset_and_Dataloader
  Lecture09 Softmax_Classifier
  Lecture10 Basic_CNN
  Lecture11 Advanced_CNN
  Lecture12 Basic_RNN
  Lecture13 RNN_Classifier

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Horizon John

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值