Upload model.py with huggingface_hub
Browse files
model.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import AutoTokenizer, GPT2Model
|
| 3 |
+
import torch.nn as nn
|
| 4 |
+
|
| 5 |
+
class ChessMoveClassifier(nn.Module):
|
| 6 |
+
def __init__(self, model_name, num_labels=4096):
|
| 7 |
+
super().__init__()
|
| 8 |
+
self.base_model = GPT2Model.from_pretrained(model_name)
|
| 9 |
+
self.dropout = nn.Dropout(0.1)
|
| 10 |
+
self.classifier = nn.Linear(self.base_model.config.n_embd, num_labels)
|
| 11 |
+
|
| 12 |
+
def forward(self, input_ids, attention_mask=None, **kwargs):
|
| 13 |
+
outputs = self.base_model(input_ids=input_ids, attention_mask=attention_mask)
|
| 14 |
+
hidden_state = outputs.last_hidden_state[:, -1, :]
|
| 15 |
+
logits = self.classifier(self.dropout(hidden_state))
|
| 16 |
+
return {"logits": logits}
|
| 17 |
+
|
| 18 |
+
def model_fn(model_dir):
|
| 19 |
+
model = ChessMoveClassifier(model_name="austindavis/ChessGPT_d12")
|
| 20 |
+
model.load_state_dict(torch.load(f"{model_dir}/model.pt", map_location="cpu"))
|
| 21 |
+
model.eval()
|
| 22 |
+
return model
|