训练代码如下:
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
KaliZ 2023-10-31 17:20:37 +08:00 1
ip 和 phone 这两个特征我理解是类别类型的离散型特征,直接把数值喂给模型的话会错当成连续型特征进行运算。
试试对这俩特征 embedding |