```python import numpy as np from sklearn.linear_model import LinearRegression from sklearn.model_selection import train_test_split from sklearn.metrics import mean_squared_error
# 生成模拟数据 X = np.random.rand(100, 1) y = 2 * X + 1 + 0.1 * np.random.randn(100, 1)
def forward(self, x): x = torch.relu(self.fc1(x)) x = torch.sigmoid(self.fc2(x)) return x
model = Net() criterion = nn.BCELoss() optimizer = optim.SGD(model.parameters(), lr=0.1)
# 训练模型 for epoch in range(100): for inputs, labels in train_loader: optimizer.zero_grad() outputs = model(inputs) loss = criterion(outputs, labels) loss.backward() optimizer.step()
# 测试模型 correct = 0 total = 0 with torch.no_grad(): for inputs, labels in test_loader: outputs = model(inputs) predicted = (outputs > 0.5).float() total += labels.size(0) correct += (predicted == labels).sum().item()
文章评论(0条评论)
登录后参与讨论