import torch
import torch.nn as nn
from torch.utils.data import Dataset
import numpy as np
# 自定义数据集类
class CustomDataset(Dataset):
def __init__(self, x_data, y_data):
self.x_data = torch.from_numpy(x_data).float()
self.y_data = torch.from_numpy(y_data).float()
def __len__(self):
return len(self.x_data)
def __getitem__(self, index):
return self.x_data[index], self.y_data[index]
# 逻辑回归模型
class LogisticRegressionModel(nn.Module):
def __init__(self, input_dim):
super(LogisticRegressionModel, self).__init__()
self.linear = nn.Linear(input_dim, 1) # 输出维度为1,因为这是二分类问题
def forward(self, x):
y_pred = torch.sigmoid(self.linear(x))