Multiple Dimension Input

传送门:https://www.bilibili.com/video/BV1Y7411d7Ys?p=7

数据集:Diabetes Dataset

下载地址:链接:https://pan.baidu.com/s/1Rh-1Xv_NzHXJNGutSEU2Wg

提取码:kfif
在这里插入图片描述
一行代表一个样本,一列代表一个特征
在这里插入图片描述

训练网络在这里插入图片描述

在这里插入图片描述

常见激活函数

在这里插入图片描述

Full Convolution全连接训练模型

在这里插入图片描述

import torch.nn
import numpy as np

xy = np.loadtxt('diabetes.csv.gz', delimiter=',', dtype=np.float32)
x_data = torch.from_numpy(xy[:,:-1])
y_data = torch.from_numpy(xy[:,[-1]]) #加[]读取出来为矩阵

class Model(torch.nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        self.linear1 = torch.nn.Linear(8, 6)
        self.linear2 = torch.nn.Linear(6, 2)
        self.linear3 = torch.nn.Linear(2, 1)
        self.activate = torch.nn.ReLU()
        self.sigmoid = torch.nn.Sigmoid()

    def forward(self, x):
        x = self.activate(self.linear1(x))
        x = self.activate(self.linear2(x))
        x = self.sigmoid(self.linear3(x))
        return x

model = Model()

criterion = torch.nn.BCELoss(size_average=True)

optimizer = torch.optim.SGD(model.parameters(), lr=0.1)

temp = []

for epoch in range(100):
    #forward
    y_pred = model(x_data)
    loss = criterion(y_pred, y_data)
    print(epoch, loss.item())
    temp.append(loss.data.item())

    #backward
    optimizer.zero_grad()
    loss.backward()

    #updata
    optimizer.step()

import os
os.environ['KMP_DUPLICATE_LIB_OK'] = 'TRUE'

import matplotlib.pyplot as plt
plt.plot(list(range(len(temp))), temp)
plt.xlabel("epoch")
plt.ylabel("loss")
plt.show()
运行结果

在这里插入图片描述

更多推荐