tahamajs's picture
download
raw
2.35 kB
import torch
import torch.nn as nn
import torch.nn.functional as F
from typing import List, Dict, Tuple, Any, Optional
class TheoremSelector(nn.Module):
def __init__(self, problem_dim=512, theorem_dim=256, hidden_dim=512):
super().__init__()
self.theorem_names = [
"Pythagorean Theorem",
"Angle Sum in Triangle",
"SAS Similarity",
"Inscribed Angle Theorem",
"Power of a Point",
"Circumcenter Theorem",
"Thales Theorem",
"Stewart's Theorem",
"Law of Cosines",
"Law of Sines",
]
self.theorem_embeddings = nn.Embedding(len(self.theorem_names), theorem_dim)
self.selector = nn.Sequential(
nn.Linear(problem_dim + theorem_dim, hidden_dim),
nn.ReLU(),
nn.Dropout(0.1),
nn.Linear(hidden_dim, hidden_dim),
nn.ReLU(),
nn.Linear(hidden_dim, 1),
)
def forward(self, problem_encoding):
batch_size = problem_encoding.size(0)
num_theorems = len(self.theorem_names)
theorem_indices = torch.arange(num_theorems)
theorem_embeds = self.theorem_embeddings(
theorem_indices
)
problem_expanded = problem_encoding.unsqueeze(1).expand(-1, num_theorems, -1)
theorem_expanded = theorem_embeds.unsqueeze(0).expand(batch_size, -1, -1)
combined = torch.cat([problem_expanded, theorem_expanded], dim=-1)
relevance_scores = self.selector(combined).squeeze(
-1
)
relevance_probs = F.softmax(relevance_scores, dim=-1)
return {
"scores": relevance_scores,
"probabilities": relevance_probs,
"theorem_names": self.theorem_names,
}
def select_top_theorems(self, problem_encoding, top_k=3):
with torch.no_grad():
results = self.forward(problem_encoding)
probs = results["probabilities"][0]
top_probs, top_indices = torch.topk(probs, top_k)
selected_theorems = []
for prob, idx in zip(top_probs, top_indices):
theorem_name = self.theorem_names[idx.item()]
selected_theorems.append((theorem_name, prob.item()))
return selected_theorems

Xet Storage Details

Size:
2.35 kB
·
Xet hash:
d3fd6ce4adb74bcea37b908a48e6c2eab985ffbd776bd582838d9e83edfbaa1b

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.