Download Analysis_code/deepgbm.py from bong9513/Visibility_Nowcasting: direct link, hf CLI and curl.
- Browser
- Download file 1.63 kB
-
https://huggingface.co/bong9513/Visibility_Nowcasting/resolve/refs%2Fpr%2F1/Analysis_code/deepgbm.py
- Command line
-
hf download hf://bong9513/Visibility_Nowcasting@refs/pr/1/Analysis_code/deepgbm.py
-
curl -L -o deepgbm.py https://huggingface.co/bong9513/Visibility_Nowcasting/resolve/refs%2Fpr%2F1/Analysis_code/deepgbm.py
1.63 kB
| import torch | |
| import torch.nn as nn | |
| import torch.nn.functional as F | |
| class DeepGBM(nn.Module): | |
| def __init__(self, num_features, cat_features, num_classes, d_main=128, d_hidden=64, n_blocks=4, dropout=0.2): | |
| super(DeepGBM, self).__init__() | |
| self.num_classes = num_classes | |
| # ์ฐ์ํ ๋ณ์ ์ฒ๋ฆฌ (Linear) | |
| self.num_linear = nn.Linear(num_features, d_main) | |
| # ๋ฒ์ฃผํ ๋ณ์ ์ฒ๋ฆฌ (Embedding) | |
| self.cat_embedding = nn.ModuleList([ | |
| nn.Embedding(cat_size, d_main) for cat_size in cat_features | |
| ]) | |
| # ResNet-like ๋ธ๋ก | |
| self.blocks = nn.ModuleList([ | |
| nn.Sequential( | |
| nn.Linear(d_main, d_hidden), | |
| nn.ReLU(), | |
| nn.Dropout(dropout), | |
| nn.Linear(d_hidden, d_main), | |
| nn.ReLU() | |
| ) for _ in range(n_blocks) | |
| ]) | |
| if num_classes == 2: | |
| self.output_layer = nn.Linear(d_main, 1) # Binary classification | |
| elif num_classes > 2: | |
| self.output_layer = nn.Linear(d_main, num_classes) # Multi classification | |
| def forward(self, x_num, x_cat): # ๋ ๊ฐ์ ์ ๋ ฅ์ ๋ฐ์ | |
| x_num = self.num_linear(x_num) | |
| # ๋ฒ์ฃผํ ๋ณ์๋ฅผ ์๋ฒ ๋ฉ ํ ํฉ์ฐ | |
| x_cat = [embed(x_cat[:, i]) for i, embed in enumerate(self.cat_embedding)] | |
| x_cat = torch.stack(x_cat, dim=1).sum(dim=1) | |
| x = x_num + x_cat # ์ฐ์ํ + ๋ฒ์ฃผํ ๊ฒฐํฉ | |
| for block in self.blocks: | |
| x = x + block(x) # Residual connection | |
| x = self.output_layer(x) | |
| return x | |