V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
xing393939
V2EX  ›  Torch

写了一个简单的二分类模型,但是训练了 N 次模型参数都没有动静

  •  
  •   xing393939 · 2023-10-31 14:20:25 +08:00 · 1072 次点击
    这是一个创建于 387 天前的主题,其中的信息可能已经有所发展或是发生改变。

    训练代码如下:

    import numpy as np
    import torch
    
    # 1.prepare dataset
    xy = np.loadtxt("redPacket_2.csv", skiprows=1, delimiter=",", dtype=np.float32)
    x_data = torch.from_numpy(xy[:, :-1])
    y_data = torch.from_numpy(xy[:, [-1]])
    
    # 2.design model using class
    class Model(torch.nn.Module):
        def __init__(self):
            super(Model, self).__init__()
            self.linear1 = torch.nn.Linear(4, 2)
            self.linear2 = 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.sigmoid(self.linear2(x))
            return x
    
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    model = Model().to(device)
    x_data = x_data.to(device)
    y_data = y_data.to(device)
    
    # 3.construct loss and optimizer
    criterion = torch.nn.BCELoss(reduction="mean")
    optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
    
    # 4.training cycle forward, backward, update
    for epoch in range(10000):
        y_pred = model(x_data)
        loss = criterion(y_pred, y_data)
        if epoch % 100 == 0:
            print(
                "epoch %9d loss %.3f" % (epoch, loss.item()),
                model.linear2.weight.data,
                model.linear2.bias.data,
            )
    
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
    

    训练集下载在这里,我每隔 100 周期打印模型的损失值和模型参数,结果如下:

    epoch         0 loss 50.000 tensor([[0.0944, 0.1484]], device='cuda:0') tensor([0.4391], device='cuda:0')
    epoch       100 loss 50.000 tensor([[0.0944, 0.1484]], device='cuda:0') tensor([0.4391], device='cuda:0')
    epoch       200 loss 50.000 tensor([[0.0944, 0.1484]], device='cuda:0') tensor([0.4391], device='cuda:0')
    epoch       300 loss 50.000 tensor([[0.0944, 0.1484]], device='cuda:0') tensor([0.4391], device='cuda:0')
    epoch       400 loss 50.000 tensor([[0.0944, 0.1484]], device='cuda:0') tensor([0.4391], device='cuda:0')
    epoch       500 loss 50.000 tensor([[0.0944, 0.1484]], device='cuda:0') tensor([0.4391], device='cuda:0')
    epoch       600 loss 50.000 tensor([[0.0944, 0.1484]], device='cuda:0') tensor([0.4391], device='cuda:0')
    epoch       700 loss 50.000 tensor([[0.0944, 0.1484]], device='cuda:0') tensor([0.4391], device='cuda:0')
    epoch       800 loss 50.000 tensor([[0.0944, 0.1484]], device='cuda:0') tensor([0.4391], device='cuda:0')
    epoch       900 loss 50.000 tensor([[0.0944, 0.1484]], device='cuda:0') tensor([0.4391], device='cuda:0')
    epoch      1000 loss 50.000 tensor([[0.0944, 0.1484]], device='cuda:0') tensor([0.4391], device='cuda:0')
    epoch      1100 loss 50.000 tensor([[0.0944, 0.1484]], device='cuda:0') tensor([0.4391], device='cuda:0')
    epoch      1200 loss 50.000 tensor([[0.0944, 0.1484]], device='cuda:0') tensor([0.4391], device='cuda:0')
    epoch      1300 loss 50.000 tensor([[0.0944, 0.1484]], device='cuda:0') tensor([0.4391], device='cuda:0')
    

    不知道为什么会不收敛,是哪里需要改进吗?

    1 条回复    2023-10-31 17:20:37 +08:00
    KaliZ
        1
    KaliZ  
       2023-10-31 17:20:37 +08:00   ❤️ 1
    ip 和 phone 这两个特征我理解是类别类型的离散型特征,直接把数值喂给模型的话会错当成连续型特征进行运算。

    试试对这俩特征 embedding
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5141 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 19ms · UTC 09:31 · PVG 17:31 · LAX 01:31 · JFK 04:31
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.