constrained_nn6C.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334
  1. import torch
  2. import torch.nn as nn
  3. # 定义神经网络模型
  4. class ConstrainedNN(nn.Module):
  5. def __init__(self, input_size):
  6. super(ConstrainedNN, self).__init__()
  7. self.fc1 = nn.Linear(input_size, 128)
  8. self.fc2 = nn.Linear(128, 64)
  9. self.fc3 = nn.Linear(64, 32)
  10. # self.dropout = nn.Dropout(0.5)
  11. self.fc4 = nn.Linear(32, 1)
  12. # 嵌入一元一次线性函数的参数
  13. self.a = nn.Parameter(torch.randn(1))
  14. self.b = nn.Parameter(torch.randn(1))
  15. def forward(self, x, original_x, special_feature_index):
  16. x = torch.relu(self.fc1(x))
  17. x = torch.relu(self.fc2(x))
  18. x = torch.relu(self.fc3(x))
  19. x = self.fc4(x)
  20. # 嵌入一元一次线性函数
  21. special_feature = original_x[:, special_feature_index].unsqueeze(1)
  22. x = x + self.a * special_feature + self.b
  23. return x
  24. # 自定义损失函数
  25. def custom_loss(y_pred, y_true, X, monotonic_feature_index, mean_X_special, mean_y):
  26. y_true = y_true
  27. mse_loss = nn.MSELoss()(y_pred, y_true)
  28. # 惩罚项
  29. X_special = X[:, monotonic_feature_index]
  30. punishment = torch.mean((torch.sign(X_special - mean_X_special) - torch.sign(y_pred - mean_y)) ** 2)
  31. return mse_loss + 2*punishment # 权衡 mse_loss 和惩罚项的权重