Skip to content

Instantly share code, notes, and snippets.

@thomwolf
thomwolf / gradient_accumulation.py
Last active October 21, 2024 08:08
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...
@NikolasTzimoulis
NikolasTzimoulis / witness_lz4d.py
Last active July 11, 2024 02:18 — forked from mfgmfg/witness_lz4d.py
Extract .SOUND files from the videogame The Witness
import struct
import sys
import os
MAX_OUTPUT_SIZE = 1 << 24 # 16 megabytes
def load_binary_file(fn, nbytes=100000000):
data = []
with open(fn, "rb") as src:
byte = src.read(1)