Spaces:
Sleeping
Sleeping
Download model.py from CaBeSh/CrimeAnomalyDetection: direct link, hf CLI and curl.
- Browser
- Download file 1.81 kB
-
https://huggingface.co/spaces/CaBeSh/CrimeAnomalyDetection/resolve/main/model.py
- Command line
-
hf download hf://spaces/CaBeSh/CrimeAnomalyDetection/model.py
-
curl -L -o model.py https://huggingface.co/spaces/CaBeSh/CrimeAnomalyDetection/resolve/main/model.py
1.81 kB
| import torch | |
| from torch import nn | |
| class AdvancedCrimeDetectionModel(nn.Module): | |
| def __init__(self, input_dim=1536, hidden=512): | |
| super().__init__() | |
| self.input_proj = nn.Sequential( | |
| nn.Linear(input_dim, hidden), | |
| nn.LayerNorm(hidden), | |
| nn.ReLU(), | |
| nn.Dropout(0.3) | |
| ) | |
| encoder_layer = nn.TransformerEncoderLayer( | |
| d_model=hidden, | |
| nhead=8, | |
| dim_feedforward=hidden * 2, | |
| dropout=0.2, | |
| batch_first=True | |
| ) | |
| self.transformer = nn.TransformerEncoder(encoder_layer, num_layers=3) | |
| self.gru = nn.GRU( | |
| hidden, | |
| hidden // 2, | |
| num_layers=3, | |
| bidirectional=True, | |
| batch_first=True, | |
| dropout=0.3 | |
| ) | |
| self.attention = nn.MultiheadAttention( | |
| embed_dim=hidden, | |
| num_heads=8, | |
| dropout=0.2, | |
| batch_first=True | |
| ) | |
| self.fc1 = nn.Linear(hidden, 384) | |
| self.bn1 = nn.BatchNorm1d(384) | |
| self.fc2 = nn.Linear(384, 192) | |
| self.bn2 = nn.BatchNorm1d(192) | |
| self.fc3 = nn.Linear(192, 96) | |
| self.bn3 = nn.BatchNorm1d(96) | |
| self.fc4 = nn.Linear(96, 1) | |
| self.dropout = nn.Dropout(0.4) | |
| self.relu = nn.ReLU() | |
| def forward(self, x): | |
| x = self.input_proj(x) | |
| x = self.transformer(x) | |
| gru_out, _ = self.gru(x) | |
| attn_out, _ = self.attention(gru_out, gru_out, gru_out) | |
| pooled = torch.mean(attn_out, dim=1) | |
| x1 = self.dropout(self.relu(self.bn1(self.fc1(pooled)))) | |
| x2 = self.dropout(self.relu(self.bn2(self.fc2(x1)))) | |
| x3 = self.dropout(self.relu(self.bn3(self.fc3(x2)))) | |
| out = self.fc4(x3) | |
| return out.squeeze(1) | |