Skip to content

Instantly share code, notes, and snippets.

View marmakoide's full-sized avatar

Devert Alexandre marmakoide

View GitHub Profile
@louity
louity / poisson_dst.py
Created April 20, 2022 08:04
Solve poisson with homogeneous dirichlet BC using Discrete Sine Transform and PyTorch
import torch
import torch.nn.functional as F
def compute_laplace_dst(nx, ny, dx, dy, arr_kwargs):
"""Discrete sine transform of the 2D centered discrete laplacian
operator."""
x, y = torch.meshgrid(torch.arange(1,nx-1, **arr_kwargs),
torch.arange(1,ny-1, **arr_kwargs),
indexing='ij')
return 2*(torch.cos(torch.pi/(nx-1)*x) - 1)/dx**2 + 2*(torch.cos(torch.pi/(ny-1)*y) - 1)/dy**2
@fnky
fnky / ANSI.md
Last active July 22, 2024 12:03
ANSI Escape Codes

ANSI Escape Sequences

Standard escape codes are prefixed with Escape:

  • Ctrl-Key: ^[
  • Octal: \033
  • Unicode: \u001b
  • Hexadecimal: \x1B
  • Decimal: 27
@thomwolf
thomwolf / gradient_accumulation.py
Last active January 16, 2024 02:38
PyTorch gradient accumulation training loop
model.zero_grad() # Reset gradients tensors
for i, (inputs, labels) in enumerate(training_set):
predictions = model(inputs) # Forward pass
loss = loss_function(predictions, labels) # Compute loss function
loss = loss / accumulation_steps # Normalize our loss (if averaged)
loss.backward() # Backward pass
if (i+1) % accumulation_steps == 0: # Wait for several backward steps
optimizer.step() # Now we can do an optimizer step
model.zero_grad() # Reset gradients tensors
if (i+1) % evaluation_steps == 0: # Evaluate the model when we...