用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
直接命令不会经过审查 Prompt;运行前请先检查来源。
npx skills add https://github.com/cxcscmu/SkillLearnBench --skill pytorch-loss-implementation命令会保持在同一行。复制前请横向滚动并检查完整内容。
想先保存到本地?可下载 SkillsMP 当前能够提供的文件。
Handles reading, populating, and saving .docx files using the python-docx library. Use this skill for any tasks involving template filling or modifying Word documents.
Perform various data analysis on SEC 13-F and obtain some insights of fund activities such as number of holdings, AUM, and change of holdings between two quarters.
This skill includes search capability in 13F, such as fuzzy search a fund information using possibly inaccurate name, or fuzzy search a stock cusip info using its name.
基于 SOC 职业分类
正在显示 SKILL.md
| name | pytorch-loss-implementation |
| description | Implement loss functions in PyTorch with proper tensor operations. |
import torch
# Sigmoid function
sigmoid_output = torch.sigmoid(input_tensor)
# Log function
log_output = torch.log(input_tensor)
# Mean reduction
mean_loss = loss.mean()
# Sum reduction
sum_loss = loss.sum()
# Batch dimension handling
batch_size = tensor.shape[0]
x = tensor[:batch_size//2] # First half
y = tensor[batch_size//2:] # Second half
# Ensure same device and dtype
tensor = tensor.to(device=model.device, dtype=torch.float32)
Log-Sigmoid Stability
# Avoid: log(sigmoid(x)) can cause numerical issues
# Instead use:
stable_loss = torch.nn.functional.logsigmoid(x)
# Or manually:
loss = -torch.log(torch.sigmoid(x) + 1e-10)
Handling Small Values
# Add epsilon to avoid log(0)
safe_log = torch.log(value + 1e-8)
# Clamp to valid range
clamped = torch.clamp(value, min=1e-10, max=1.0)
# Ensure all tensors on same device
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
tensor = tensor.to(device)
# Or get from model
device = next(model.parameters()).device
tensor = tensor.to(device)
def compute_loss(logits, labels, temperature=1.0, margin=0.5):
# 1. Normalize/compute rewards
rewards = logits / (sequence_length + 1e-8)
# 2. Compute differences
diff = temperature * rewards[:n//2] - temperature * rewards[n//2:] - margin
# 3. Apply objective
loss_per_pair = -torch.log(torch.sigmoid(diff) + 1e-10)
# 4. Reduce
loss = loss_per_pair.mean()
return loss
loss.backward()tensor.log_()