moondream-captcha / fourier_features.py
ShooterShanky's picture
Upload 20 files
b7cd909 verified
raw
history blame contribute delete
576 Bytes
# Adopted from https://github.com/crowsonkb/k-diffusion/blob/transformer-model-v2/k_diffusion/layers.py
import torch
import torch.nn as nn
import math
class FourierFeatures(nn.Module):
def __init__(self, in_features, out_features, std=1.0):
super().__init__()
assert out_features % 2 == 0
self.register_buffer(
"weight", torch.randn([out_features // 2, in_features]) * std
)
def forward(self, input):
f = 2 * math.pi * input @ self.weight.T
return torch.cat([f.cos(), f.sin()], dim=-1)