Diabetes Dataset:
逻辑回归模型:
y
^
(
i
)
=
σ
(
∑
n
=
1
8
x
n
(
i
)
∗
ω
n
+
b
)
\hat{y}^{(i)}=\sigma(\sum_{n=1}^8x_n^{(i)}*\omega_n+b)
y^(i)=σ(∑n=18xn(i)∗ωn+b)
∑
n
=
1
8
x
n
(
i
)
⋅
ω
n
=
[
x
1
(
i
)
⋯
x
N
(
i
)
]
[
ω
1
⋮
ω
8
]
\sum_{n=1}^{8} x_{n}^{(i)} \cdot \omega_{n}=\left[x_{1}^{(i)} \quad \cdots \quad x_{N}^{(i)}\right]\left[\begin{array}{c} \omega_{1} \\ \vdots \\ \omega_{8} \end{array}\right]
∑n=18xn(i)⋅ωn=[x1(i)⋯xN(i)]⎣
⎡ω1⋮ω8⎦
⎤
y
^
(
i
)
=
σ
(
[
x
1
(
i
)
⋯
x
8
(
i
)
]
[
ω
1
⋮
ω
8
]
+
b
)
=
σ
(
z
(
i
)
)
\begin{aligned} \hat{y}^{(i)} &=\sigma\left(\left[\begin{array}{lll} x_{1}^{(i)} & \cdots & x_{8}^{(i)} \end{array}\right]\left[\begin{array}{c} \omega_{1} \\ \vdots \\ \omega_{8} \end{array}\right]+b\right) \\ &=\sigma\left(z^{(i)}\right) \end{aligned}
y^(i)=σ⎝
⎛[x1(i)⋯x8(i)]⎣
⎡ω1⋮ω8⎦
⎤+b⎠
⎞=σ(z(i))
模型:
import torch
import numpy as np
xy = np.loadtxt('E:\\learning\\coding\\jupyter\\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,4)
self.linear3 = torch.nn.Linear(4,1)
self.sigmoid = torch.nn.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()
criterion = torch.nn.BCELoss(size_average=True)
optimizer = torch.optim.SGD(model.parameters(),lr=0.1)
for epoch in range(100):
#前向传播
y_pred = model(x_data)
loss = criterion(y_pred,y_data)
print(epoch,loss.item())
#反向传播
optimizer.zero_grad()
loss.backward()
#更新
optimizer.step()
结果: