mawiisurv 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mawiisurv-0.1.0/PKG-INFO +55 -0
- mawiisurv-0.1.0/README.md +39 -0
- mawiisurv-0.1.0/pyproject.toml +23 -0
- mawiisurv-0.1.0/setup.cfg +4 -0
- mawiisurv-0.1.0/src/ivcensor/__init__.py +13 -0
- mawiisurv-0.1.0/src/ivcensor/demo.py +103 -0
- mawiisurv-0.1.0/src/ivcensor/dnn.py +242 -0
- mawiisurv-0.1.0/src/ivcensor/func.py +251 -0
- mawiisurv-0.1.0/src/ivcensor/genius_censor.py +262 -0
- mawiisurv-0.1.0/src/mawii-surv/__init__.py +13 -0
- mawiisurv-0.1.0/src/mawii-surv/demo.py +103 -0
- mawiisurv-0.1.0/src/mawii-surv/dnn.py +242 -0
- mawiisurv-0.1.0/src/mawii-surv/func.py +252 -0
- mawiisurv-0.1.0/src/mawii-surv/mawii_surv.py +253 -0
- mawiisurv-0.1.0/src/mawiisurv/__init__.py +13 -0
- mawiisurv-0.1.0/src/mawiisurv/demo.py +103 -0
- mawiisurv-0.1.0/src/mawiisurv/dnn.py +242 -0
- mawiisurv-0.1.0/src/mawiisurv/func.py +252 -0
- mawiisurv-0.1.0/src/mawiisurv/mawii_surv.py +253 -0
- mawiisurv-0.1.0/src/mawiisurv.egg-info/PKG-INFO +55 -0
- mawiisurv-0.1.0/src/mawiisurv.egg-info/SOURCES.txt +22 -0
- mawiisurv-0.1.0/src/mawiisurv.egg-info/dependency_links.txt +1 -0
- mawiisurv-0.1.0/src/mawiisurv.egg-info/requires.txt +6 -0
- mawiisurv-0.1.0/src/mawiisurv.egg-info/top_level.txt +3 -0
mawiisurv-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: mawiisurv
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Semiparametric Causal Inference for Right-Censored Outcomes with Many Weak Invalid Instruments
|
|
5
|
+
Author-email: Qiushi Bu <buqiushi17@mails.ucas.ac.cn>
|
|
6
|
+
License: MIT
|
|
7
|
+
Keywords: Censored outcomes,Deep neural networks,instrumental variable,generalized empirical likelihood,Mendelian randomization,Over-identification test,Semiparametric theory,Weak and invalid instruments
|
|
8
|
+
Requires-Python: >=3.8
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
Requires-Dist: numpy>=1.19
|
|
11
|
+
Requires-Dist: torch>=1.8
|
|
12
|
+
Requires-Dist: scipy>=1.5
|
|
13
|
+
Requires-Dist: scikit-learn>=0.24
|
|
14
|
+
Requires-Dist: xgboost>=1.3
|
|
15
|
+
Requires-Dist: numba>=0.53
|
|
16
|
+
|
|
17
|
+
# mawiisurv
|
|
18
|
+
|
|
19
|
+
`mawiisurv` implements G‐estimation methods for treatment effects under endogeneity, both with and without right‐censoring, using a variety of machine‐learning and classical estimators.
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
## Features
|
|
24
|
+
|
|
25
|
+
- **Complete‐data G‐estimator** (`mawii_noncensor`)
|
|
26
|
+
- **Right‐censoring G‐estimator** (`mawii_censor`)
|
|
27
|
+
- Multiple model backends:
|
|
28
|
+
- Neural networks
|
|
29
|
+
- Linear regression
|
|
30
|
+
- Random forests
|
|
31
|
+
- XGBoost
|
|
32
|
+
- Choice of Generalized Empirical Likelihood (GEL) functions:
|
|
33
|
+
- Empirical Tilting (ET)
|
|
34
|
+
- Empirical Likelihood (EL)
|
|
35
|
+
- Continuous Updating Estimator (CUE)
|
|
36
|
+
|
|
37
|
+
---
|
|
38
|
+
|
|
39
|
+
## Installation
|
|
40
|
+
|
|
41
|
+
Install from PyPI:
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
pip install mawiisurv
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
Dependencies
|
|
49
|
+
Make sure you have the following installed (the minimal compatible versions shown):
|
|
50
|
+
numpy>=1.19
|
|
51
|
+
torch>=1.8
|
|
52
|
+
scipy>=1.5
|
|
53
|
+
scikit-learn>=0.24
|
|
54
|
+
xgboost>=1.3
|
|
55
|
+
numba>=0.53
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# mawiisurv
|
|
2
|
+
|
|
3
|
+
`mawiisurv` implements G‐estimation methods for treatment effects under endogeneity, both with and without right‐censoring, using a variety of machine‐learning and classical estimators.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- **Complete‐data G‐estimator** (`mawii_noncensor`)
|
|
10
|
+
- **Right‐censoring G‐estimator** (`mawii_censor`)
|
|
11
|
+
- Multiple model backends:
|
|
12
|
+
- Neural networks
|
|
13
|
+
- Linear regression
|
|
14
|
+
- Random forests
|
|
15
|
+
- XGBoost
|
|
16
|
+
- Choice of Generalized Empirical Likelihood (GEL) functions:
|
|
17
|
+
- Empirical Tilting (ET)
|
|
18
|
+
- Empirical Likelihood (EL)
|
|
19
|
+
- Continuous Updating Estimator (CUE)
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
Install from PyPI:
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pip install mawiisurv
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
Dependencies
|
|
33
|
+
Make sure you have the following installed (the minimal compatible versions shown):
|
|
34
|
+
numpy>=1.19
|
|
35
|
+
torch>=1.8
|
|
36
|
+
scipy>=1.5
|
|
37
|
+
scikit-learn>=0.24
|
|
38
|
+
xgboost>=1.3
|
|
39
|
+
numba>=0.53
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "mawiisurv"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Semiparametric Causal Inference for Right-Censored Outcomes with Many Weak Invalid Instruments"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.8"
|
|
11
|
+
keywords = ["Censored outcomes", "Deep neural networks", "instrumental variable", "generalized empirical likelihood", "Mendelian randomization","Over-identification test","Semiparametric theory", "Weak and invalid instruments"]
|
|
12
|
+
authors = [
|
|
13
|
+
{ name = "Qiushi Bu", email = "buqiushi17@mails.ucas.ac.cn" },
|
|
14
|
+
]
|
|
15
|
+
license = { text = "MIT" }
|
|
16
|
+
dependencies = [
|
|
17
|
+
"numpy>=1.19",
|
|
18
|
+
"torch>=1.8",
|
|
19
|
+
"scipy>=1.5",
|
|
20
|
+
"scikit-learn>=0.24",
|
|
21
|
+
"xgboost>=1.3",
|
|
22
|
+
"numba>=0.53",
|
|
23
|
+
]
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"""
|
|
2
|
+
genius_censor package
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
try:
|
|
6
|
+
from importlib.metadata import version
|
|
7
|
+
except ImportError:
|
|
8
|
+
from importlib_metadata import version
|
|
9
|
+
__version__ = version(__package__ or "ivcensor")
|
|
10
|
+
|
|
11
|
+
from .genius_censor import genius_censor,genius_noncensor
|
|
12
|
+
|
|
13
|
+
__all__ = ["genius_censor", "genius_noncensor"]
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# demo
|
|
2
|
+
|
|
3
|
+
# pip install ivcensor==0.4.0
|
|
4
|
+
import numpy as np
|
|
5
|
+
import ivcensor
|
|
6
|
+
import torch
|
|
7
|
+
|
|
8
|
+
# function input
|
|
9
|
+
'''
|
|
10
|
+
Main input variables:
|
|
11
|
+
X: (n,p) array, covariates
|
|
12
|
+
Z: (n,m) array, instrumental variables
|
|
13
|
+
A: (n,) array, treatment
|
|
14
|
+
Y: (n,) array, outcome
|
|
15
|
+
censor_delta: (n,) array, censor indicator, 1 for uncensor and 0 for censor
|
|
16
|
+
h: window for local KM estimation, and default setting is 1
|
|
17
|
+
model_types: ['neural_network','linear_regression','random_forest','xgboost'], 4 choices of comparsion models, and default setting is ['neural_network']
|
|
18
|
+
rho_function_names: ['ET','EL','CUE'], GEL functions, and default setting is ['EL']
|
|
19
|
+
|
|
20
|
+
Other DNN setting:
|
|
21
|
+
hidden_layers=[50, 50],
|
|
22
|
+
learning_rate=0.0005,
|
|
23
|
+
weight_decay=0.0001,
|
|
24
|
+
batch_size=256,
|
|
25
|
+
dropout_rate=0,
|
|
26
|
+
patience=5,
|
|
27
|
+
epochs=1000,
|
|
28
|
+
validation_split=0.05,
|
|
29
|
+
shuffle=False,
|
|
30
|
+
device='cpu'
|
|
31
|
+
'''
|
|
32
|
+
|
|
33
|
+
# two main functions:
|
|
34
|
+
'''
|
|
35
|
+
'genius_noncensor' for complete data
|
|
36
|
+
'genius_censor' for right censor data
|
|
37
|
+
'''
|
|
38
|
+
|
|
39
|
+
# function output
|
|
40
|
+
'''
|
|
41
|
+
function will return a list of result, including
|
|
42
|
+
'beta' : estimation of treatment effect
|
|
43
|
+
'se' : estimated standard error
|
|
44
|
+
'test' : overidentification test statistics
|
|
45
|
+
'''
|
|
46
|
+
|
|
47
|
+
# complete data
|
|
48
|
+
n = 10000
|
|
49
|
+
m = 20
|
|
50
|
+
p = 5
|
|
51
|
+
beta_0 = 0.4
|
|
52
|
+
device = 'cuda:0' if torch.cuda.is_available() else 'cpu'
|
|
53
|
+
|
|
54
|
+
X = np.random.uniform(0, 1, size=(n, p))
|
|
55
|
+
p_Z = [0.25, 0.5, 0.25]
|
|
56
|
+
Z = np.random.choice([0, 1, 2], size=(n, m), p=p_Z)
|
|
57
|
+
|
|
58
|
+
gamma = np.sqrt(0.2 / (1.5*m)) * np.random.normal(0, 1, size=m)
|
|
59
|
+
delta = np.sqrt(0.2 / (1.5*m)) * np.random.normal(0, 1, size=m)
|
|
60
|
+
|
|
61
|
+
epsilon_A = np.random.normal(0, 0.4, size=n)
|
|
62
|
+
epsilon_Y = np.random.normal(0, 0.4, size=n)
|
|
63
|
+
U = np.random.normal(0, 0.6, size=n)
|
|
64
|
+
|
|
65
|
+
A = Z @ gamma + U + (1 + Z @ delta)*epsilon_A
|
|
66
|
+
Y = beta_0*A + np.sum(X,axis=1) - U + epsilon_Y
|
|
67
|
+
|
|
68
|
+
result_noncensor = ivcensor.genius_noncensor(X, Z, A, Y,
|
|
69
|
+
model_types=['neural_network','linear_regression','random_forest','xgboost'],
|
|
70
|
+
rho_function_names=['ET','EL','CUE'],
|
|
71
|
+
device = device)
|
|
72
|
+
|
|
73
|
+
print(result_noncensor['neural_network']['ET']['beta'])
|
|
74
|
+
print(result_noncensor['neural_network']['ET']['se'])
|
|
75
|
+
print(result_noncensor['neural_network']['ET']['test'])
|
|
76
|
+
|
|
77
|
+
# censor data
|
|
78
|
+
T = beta_0*A + np.sum(X,axis=1) - U + epsilon_Y
|
|
79
|
+
censor_rate = 0.4
|
|
80
|
+
rr = 0
|
|
81
|
+
|
|
82
|
+
while True:
|
|
83
|
+
C = np.random.uniform(0+rr,5+rr, size=n)
|
|
84
|
+
censor_delta = np.where(T <= C, 1, 0)
|
|
85
|
+
if np.mean(1-censor_delta) >=censor_rate+0.03:
|
|
86
|
+
rr = rr + 0.1
|
|
87
|
+
elif np.mean(1-censor_delta)<=censor_rate-0.03:
|
|
88
|
+
rr = rr - 0.1
|
|
89
|
+
else:
|
|
90
|
+
break
|
|
91
|
+
|
|
92
|
+
Y = np.minimum(T,C)
|
|
93
|
+
|
|
94
|
+
result_censor = ivcensor.genius_censor(X, Z, A, Y, censor_delta, h = 1,
|
|
95
|
+
model_types=['neural_network','linear_regression','random_forest','xgboost'],
|
|
96
|
+
rho_function_names=['ET','EL','CUE'],
|
|
97
|
+
device = device)
|
|
98
|
+
|
|
99
|
+
print(result_censor['neural_network']['ET']['beta'])
|
|
100
|
+
print(result_censor['neural_network']['ET']['se'])
|
|
101
|
+
print(result_censor['neural_network']['ET']['test'])
|
|
102
|
+
|
|
103
|
+
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
# dnn.py
|
|
2
|
+
|
|
3
|
+
import torch
|
|
4
|
+
import torch.nn as nn
|
|
5
|
+
import torch.optim as optim
|
|
6
|
+
from torch.utils.data import DataLoader, TensorDataset, random_split
|
|
7
|
+
import numpy as np
|
|
8
|
+
|
|
9
|
+
# Neural network model
|
|
10
|
+
class RegressionNeuralNetwork(nn.Module):
|
|
11
|
+
def __init__(self, input_size, hidden_layers, output_size, dropout_rate=0.0):
|
|
12
|
+
super(RegressionNeuralNetwork, self).__init__()
|
|
13
|
+
layers = []
|
|
14
|
+
prev_size = input_size
|
|
15
|
+
|
|
16
|
+
for h in hidden_layers:
|
|
17
|
+
linear_layer = nn.Linear(prev_size, h)
|
|
18
|
+
layers.append(linear_layer)
|
|
19
|
+
layers.append(nn.ReLU())
|
|
20
|
+
if dropout_rate > 0:
|
|
21
|
+
layers.append(nn.Dropout(dropout_rate))
|
|
22
|
+
prev_size = h
|
|
23
|
+
|
|
24
|
+
output_layer = nn.Linear(prev_size, output_size)
|
|
25
|
+
layers.append(output_layer)
|
|
26
|
+
|
|
27
|
+
self.network = nn.Sequential(*layers)
|
|
28
|
+
|
|
29
|
+
def forward(self, x):
|
|
30
|
+
return self.network(x)
|
|
31
|
+
|
|
32
|
+
# Training function with multiple initializations
|
|
33
|
+
def train_regression_model(X, Y, config, num_initializations=1):
|
|
34
|
+
# Dynamically get input_size and output_size
|
|
35
|
+
input_size = X.shape[1] # Number of input features
|
|
36
|
+
output_size = Y.shape[1] # Output size
|
|
37
|
+
|
|
38
|
+
# Convert to Tensor
|
|
39
|
+
X_tensor = torch.tensor(X, dtype=torch.float32)
|
|
40
|
+
Y_tensor = torch.tensor(Y, dtype=torch.float32)
|
|
41
|
+
|
|
42
|
+
# Move tensors to device only when creating DataLoader to avoid CUDA memory issues
|
|
43
|
+
dataset = TensorDataset(X_tensor, Y_tensor)
|
|
44
|
+
|
|
45
|
+
# Split dataset into training and validation sets
|
|
46
|
+
val_size = int(len(dataset) * config['validation_split'])
|
|
47
|
+
train_size = len(dataset) - val_size
|
|
48
|
+
# Ensure reproducibility in dataset splitting
|
|
49
|
+
train_dataset, val_dataset = random_split(dataset, [train_size, val_size],
|
|
50
|
+
generator=torch.Generator().manual_seed(config.get('seed', 0)))
|
|
51
|
+
|
|
52
|
+
best_model_state = None
|
|
53
|
+
best_val_loss_overall = float('inf')
|
|
54
|
+
best_model_init = None
|
|
55
|
+
|
|
56
|
+
for init in range(num_initializations):
|
|
57
|
+
# Set different seeds for different initializations
|
|
58
|
+
seed = config.get('seed', 0) + init # Modify seed for each initialization
|
|
59
|
+
torch.manual_seed(seed)
|
|
60
|
+
torch.cuda.manual_seed_all(seed)
|
|
61
|
+
np.random.seed(seed)
|
|
62
|
+
# Define the model
|
|
63
|
+
model = RegressionNeuralNetwork(input_size, config['hidden_layers'], output_size, config['dropout_rate']).to(config['device'])
|
|
64
|
+
|
|
65
|
+
# Define loss function and optimizer
|
|
66
|
+
criterion = nn.MSELoss(reduction='mean')
|
|
67
|
+
optimizer = optim.Adam(model.parameters(), lr=config['learning_rate'], weight_decay=config['weight_decay'])
|
|
68
|
+
|
|
69
|
+
# Create DataLoader for training and validation (move data to device here)
|
|
70
|
+
train_loader = DataLoader(train_dataset, batch_size=config['batch_size'], shuffle=config['shuffle'])
|
|
71
|
+
val_loader = DataLoader(val_dataset, batch_size=config['batch_size'])
|
|
72
|
+
|
|
73
|
+
best_val_loss = float('inf')
|
|
74
|
+
patience_counter = 0
|
|
75
|
+
best_model_state_init = None
|
|
76
|
+
|
|
77
|
+
# Training loop
|
|
78
|
+
for epoch in range(config['epochs']):
|
|
79
|
+
model.train()
|
|
80
|
+
total_loss = 0
|
|
81
|
+
|
|
82
|
+
# Training step
|
|
83
|
+
for X_batch, Y_batch in train_loader:
|
|
84
|
+
X_batch = X_batch.to(config['device'])
|
|
85
|
+
Y_batch = Y_batch.to(config['device'])
|
|
86
|
+
optimizer.zero_grad()
|
|
87
|
+
outputs = model(X_batch)
|
|
88
|
+
loss = criterion(outputs, Y_batch)
|
|
89
|
+
loss.backward()
|
|
90
|
+
optimizer.step()
|
|
91
|
+
total_loss += loss.item()
|
|
92
|
+
|
|
93
|
+
# Validation step
|
|
94
|
+
model.eval()
|
|
95
|
+
val_loss = 0
|
|
96
|
+
with torch.no_grad():
|
|
97
|
+
for X_val, Y_val in val_loader:
|
|
98
|
+
X_val = X_val.to(config['device'])
|
|
99
|
+
Y_val = Y_val.to(config['device'])
|
|
100
|
+
outputs = model(X_val)
|
|
101
|
+
loss = criterion(outputs, Y_val)
|
|
102
|
+
val_loss += loss.item()
|
|
103
|
+
|
|
104
|
+
val_loss /= len(val_loader)
|
|
105
|
+
# print(f'Initialization {init+1}, Epoch {epoch+1}, Validation Loss: {val_loss:.6f}, Training Loss: {total_loss/len(train_loader):.6f}')
|
|
106
|
+
|
|
107
|
+
# Early stopping mechanism
|
|
108
|
+
if val_loss < best_val_loss:
|
|
109
|
+
best_val_loss = val_loss
|
|
110
|
+
patience_counter = 0
|
|
111
|
+
|
|
112
|
+
# Save the best model state for this initialization
|
|
113
|
+
best_model_state_init = {
|
|
114
|
+
'epoch': epoch + 1,
|
|
115
|
+
'model_state_dict': model.state_dict(),
|
|
116
|
+
'optimizer_state_dict': optimizer.state_dict(),
|
|
117
|
+
'val_loss': best_val_loss,
|
|
118
|
+
}
|
|
119
|
+
else:
|
|
120
|
+
patience_counter += 1
|
|
121
|
+
|
|
122
|
+
if patience_counter >= config['patience']:
|
|
123
|
+
# print(f"Early stopping at epoch {epoch+1}")
|
|
124
|
+
break
|
|
125
|
+
|
|
126
|
+
# After training for this initialization, check if this is the best overall
|
|
127
|
+
if best_val_loss < best_val_loss_overall:
|
|
128
|
+
best_val_loss_overall = best_val_loss
|
|
129
|
+
best_model_state = best_model_state_init
|
|
130
|
+
best_model_init = init
|
|
131
|
+
|
|
132
|
+
# After all initializations, load the best model state into a new model
|
|
133
|
+
if best_model_state is not None:
|
|
134
|
+
# Re-initialize the model with the seed corresponding to the best initialization
|
|
135
|
+
seed = config.get('seed', 0) + best_model_init
|
|
136
|
+
torch.manual_seed(seed)
|
|
137
|
+
torch.cuda.manual_seed_all(seed)
|
|
138
|
+
np.random.seed(seed)
|
|
139
|
+
best_model = RegressionNeuralNetwork(input_size, config['hidden_layers'], output_size, config['dropout_rate']).to(config['device'])
|
|
140
|
+
best_model.load_state_dict(best_model_state['model_state_dict'])
|
|
141
|
+
# Optionally, you can also return the optimizer state if needed
|
|
142
|
+
# optimizer.load_state_dict(best_model_state['optimizer_state_dict'])
|
|
143
|
+
# print(f"Loaded best model from initialization {best_model_init+1} with validation loss {best_val_loss_overall:.6f}")
|
|
144
|
+
else:
|
|
145
|
+
print("No valid model was found.")
|
|
146
|
+
return None
|
|
147
|
+
|
|
148
|
+
return best_model
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
# Classification Neural Network
|
|
152
|
+
class ClassificationNeuralNetwork(nn.Module):
|
|
153
|
+
def __init__(self, input_size, hidden_layers, num_classes, dropout_rate=0.0):
|
|
154
|
+
super(ClassificationNeuralNetwork, self).__init__()
|
|
155
|
+
layers = []
|
|
156
|
+
prev_size = input_size
|
|
157
|
+
|
|
158
|
+
# Add hidden layers
|
|
159
|
+
for h in hidden_layers:
|
|
160
|
+
layers.append(nn.Linear(prev_size, h))
|
|
161
|
+
layers.append(nn.ReLU())
|
|
162
|
+
if dropout_rate > 0:
|
|
163
|
+
layers.append(nn.Dropout(dropout_rate))
|
|
164
|
+
prev_size = h
|
|
165
|
+
|
|
166
|
+
# Add output layer
|
|
167
|
+
layers.append(nn.Linear(prev_size, num_classes))
|
|
168
|
+
|
|
169
|
+
self.network = nn.Sequential(*layers)
|
|
170
|
+
|
|
171
|
+
def forward(self, x):
|
|
172
|
+
return self.network(x)
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
# Training function for classification
|
|
177
|
+
def train_classification_model(X, Y, config):
|
|
178
|
+
# Dynamically get input_size and num_classes
|
|
179
|
+
input_size = X.shape[1]
|
|
180
|
+
# Convert Y to tensor before calling torch.unique
|
|
181
|
+
Y_tensor = torch.tensor(Y, dtype=torch.long).to(config['device']).view(-1) # For classification, Y should be long tensor
|
|
182
|
+
|
|
183
|
+
num_classes = len(torch.unique(Y_tensor))
|
|
184
|
+
|
|
185
|
+
# Create dataset
|
|
186
|
+
dataset = TensorDataset(torch.tensor(X, dtype=torch.float32).to(config['device']), Y_tensor)
|
|
187
|
+
|
|
188
|
+
# Split dataset into training and validation sets
|
|
189
|
+
val_size = int(len(dataset) * config['validation_split'])
|
|
190
|
+
train_size = len(dataset) - val_size
|
|
191
|
+
train_dataset, val_dataset = random_split(dataset, [train_size, val_size])
|
|
192
|
+
|
|
193
|
+
# Create DataLoader for training and validation
|
|
194
|
+
train_loader = DataLoader(train_dataset, batch_size=config['batch_size'], shuffle=config['shuffle'])
|
|
195
|
+
val_loader = DataLoader(val_dataset, batch_size=config['batch_size'])
|
|
196
|
+
|
|
197
|
+
# Define the model
|
|
198
|
+
model = ClassificationNeuralNetwork(input_size, config['hidden_layers'], num_classes, config['dropout_rate']).to(config['device'])
|
|
199
|
+
|
|
200
|
+
# Define loss function and optimizer
|
|
201
|
+
criterion = nn.CrossEntropyLoss()
|
|
202
|
+
optimizer = optim.Adam(model.parameters(), lr=config['learning_rate'], weight_decay=config['weight_decay'])
|
|
203
|
+
|
|
204
|
+
best_val_loss = float('inf')
|
|
205
|
+
patience_counter = 0
|
|
206
|
+
|
|
207
|
+
# Training loop
|
|
208
|
+
for epoch in range(config['epochs']):
|
|
209
|
+
model.train()
|
|
210
|
+
total_loss = 0
|
|
211
|
+
|
|
212
|
+
# Training step
|
|
213
|
+
for X_batch, Y_batch in train_loader:
|
|
214
|
+
optimizer.zero_grad()
|
|
215
|
+
outputs = model(X_batch)
|
|
216
|
+
loss = criterion(outputs, Y_batch)
|
|
217
|
+
loss.backward()
|
|
218
|
+
optimizer.step()
|
|
219
|
+
total_loss += loss.item()
|
|
220
|
+
|
|
221
|
+
# Validation step
|
|
222
|
+
model.eval()
|
|
223
|
+
val_loss = 0
|
|
224
|
+
with torch.no_grad():
|
|
225
|
+
for X_val, Y_val in val_loader:
|
|
226
|
+
outputs = model(X_val)
|
|
227
|
+
loss = criterion(outputs, Y_val)
|
|
228
|
+
val_loss += loss.item()
|
|
229
|
+
|
|
230
|
+
val_loss /= len(val_loader)
|
|
231
|
+
# print(val_loss)
|
|
232
|
+
# Early stopping mechanism
|
|
233
|
+
if val_loss < best_val_loss:
|
|
234
|
+
best_val_loss = val_loss
|
|
235
|
+
patience_counter = 0
|
|
236
|
+
else:
|
|
237
|
+
patience_counter += 1
|
|
238
|
+
|
|
239
|
+
if patience_counter >= config['patience']:
|
|
240
|
+
break
|
|
241
|
+
|
|
242
|
+
return model
|