逻辑回归以及pytorch实现

1.逻辑回归常常被当作二分类问题。

2.线性神经元输出xw,经过sigmoid函数映射为(0,1)中,sigmoid = 1/1+e^-x

3.伯努利原理:抛硬币正:p = θ ,y=1 抛硬币反:p=1-θ,y=0

p(y|θ)=θ^y(1-θ)^1-y

4.极大似然:抛硬币每次独立,若硬币不均匀,则每次抛出的概率会偏向高概率的结果,那么将每次抛出的结果概率累起来,则会趋向最大。

p(y) = py1py2py3...pyn = θ^y1(1-θ)^1-y1....θ^yn(1-θ)^1-yn

我们有,p(yi) = sigm(x,w)= 1/1+e^-xiwi = θ

最大似然可以写作:p(y|X,w) =\prod sigm(xiw)^yi *(1-sigm(xiw))^1-yi

我们两边取对数,取负数,将最大化变最小化:

-logp(y|X,w)=- \sum(yi logsigm(xiw)*  1-yi log(1-sigm(xiw)) )

令L(w)= -logp(y|X,w) ,L(w)就是我们需要的损失函数,只需求出最小的损失函数,梯度下降后就可以得到 =w   ,pytorch中 有这样的交叉熵损失函数(用极大似然得到的函数),

nn.CrossEntropyLoss()

import matplotlib.pyplot as plt
import torch
from torch import nn,optim

# 500*2的大小
cluster = torch.ones(500, 2)
# 4,-4为期望值,2为标准差生成一堆数据
data0 = torch.normal(4 * cluster, 2)
data1 = torch.normal(-4 * cluster, 2)
label0 = torch.zeros(500)
label1 = torch.ones(500)

x = torch.cat((data0, data1), ).type(torch.FloatTensor)
y = torch.cat((label0, label1), ).type(torch.LongTensor)

plt.scatter(x.numpy()[:, 0], x.numpy()[:, 1], c=y.numpy(), s=10, lw=0, cmap='RdYlGn')
plt.show()


class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        # 输入包括两个特征,x1,x2横轴纵轴
        self.linear = nn.Linear(2, 2)

    def forward(self, x):
        x = self.linear(x)
        x = torch.sigmoid(x)
        return x


CUDA = torch.cuda.is_available()

if CUDA:
    net = Net().cuda()
    inputs = x.cuda()
    target = y.cuda()
else:
    net = Net()
    inputs = x
    target = y

criterion = torch.nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=2e-2)


def draw(output):
    if CUDA:
        output = output.cpu()
    # 清空画布
    plt.cla()
    output = torch.max((output), 1)[1]
    pred_y = output.data.numpy().squeeze()
    target_y = y.numpy()
    plt.scatter(x.numpy()[:, 0], x.numpy()[:, 1], c=pred_y, s=10, lw=0, cmap='RdYlGn')
    accuracy = sum(pred_y == target_y) / 1000.0
    plt.text(1.5, -4, 'Accuracy=%s' % (accuracy), fontdict={'size': 20, 'color': 'red'})
    plt.pause(0.1)

def Train(model,criterion,optimizer,epochs):
    for epochs in range(epochs):

        output = model(inputs)
        loss = criterion(output,target)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step() #权重更新
        if epochs % 40 == 0:
            draw(output)
    return model,loss

Train(net,criterion,optimizer,1000)

你可能感兴趣的:(逻辑回归,pytorch,算法)