Created
September 11, 2025 14:55
-
-
Save dongwooklee96/196acd5c3a1b78a0df9e6015c100ce38 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import torch | |
import torch.nn as nn | |
from torchensemble import VotingClassifier | |
from sklearn.datasets import make_classification | |
from sklearn.model_selection import train_test_split | |
from sklearn.preprocessing import StandardScaler | |
import numpy as np | |
from skorch import NeuralNetClassifier | |
from sklearn.ensemble import VotingClassifier as SklearnVotingClassifier | |
# 데이터 생성 | |
X, y = make_classification(n_samples=1000, n_features=20, n_classes=2, random_state=42) | |
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) | |
# 데이터 정규화 | |
scaler = StandardScaler() | |
X_train = scaler.fit_transform(X_train) | |
X_test = scaler.transform(X_test) | |
# PyTorch 모델 정의 (서로 다른 아키텍처) | |
class Model1(nn.Module): | |
def __init__(self): | |
super(Model1, self).__init__() | |
self.fc1 = nn.Linear(20, 64) | |
self.dropout1 = nn.Dropout(0.3) | |
self.fc2 = nn.Linear(64, 32) | |
self.dropout2 = nn.Dropout(0.2) | |
self.fc3 = nn.Linear(32, 2) | |
def forward(self, x): | |
x = torch.relu(self.fc1(x)) | |
x = self.dropout1(x) | |
x = torch.relu(self.fc2(x)) | |
x = self.dropout2(x) | |
x = self.fc3(x) | |
return x | |
class Model2(nn.Module): | |
def __init__(self): | |
super(Model2, self).__init__() | |
self.fc1 = nn.Linear(20, 128) | |
self.dropout1 = nn.Dropout(0.4) | |
self.fc2 = nn.Linear(128, 64) | |
self.fc3 = nn.Linear(64, 2) | |
def forward(self, x): | |
x = torch.tanh(self.fc1(x)) # 다른 활성화 함수 | |
x = self.dropout1(x) | |
x = torch.tanh(self.fc2(x)) | |
x = self.fc3(x) | |
return x | |
class Model3(nn.Module): | |
def __init__(self): | |
super(Model3, self).__init__() | |
self.fc1 = nn.Linear(20, 32) | |
self.fc2 = nn.Linear(32, 16) | |
self.fc3 = nn.Linear(16, 2) | |
def forward(self, x): | |
x = torch.relu(self.fc1(x)) | |
x = torch.relu(self.fc2(x)) | |
x = self.fc3(x) | |
return x | |
# skorch로 각 모델을 scikit-learn 호환으로 래핑 | |
model1 = NeuralNetClassifier(Model1, max_epochs=20, lr=0.001, optimizer=torch.optim.Adam, verbose=0) | |
model2 = NeuralNetClassifier(Model2, max_epochs=20, lr=0.002, optimizer=torch.optim.Adam, verbose=0) | |
model3 = NeuralNetClassifier(Model3, max_epochs=20, lr=0.0015, optimizer=torch.optim.Adam, verbose=0) | |
# scikit-learn의 VotingClassifier 사용 | |
ensemble = SklearnVotingClassifier( | |
estimators=[ | |
('model1', model1), | |
('model2', model2), | |
('model3', model3) | |
], | |
voting='soft', | |
weights=[0.5, 0.3, 0.2] # 모델별 가중치 | |
) | |
# 모델 학습 | |
ensemble.fit(X_train.astype(np.float32), y_train) | |
# 테스트 데이터로 평가 | |
accuracy = ensemble.score(X_test.astype(np.float32), y_test) | |
print(f"Voting 앙상블 테스트 정확도: {accuracy:.4f}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment