liveBook · Manning

Torch.randn

As explained in Deep Learning with PyTorch, Second Edition

A PyTorch function that samples random values from a normal distribution into a tensor.

torch.randn generates a tensor of random values drawn from a normal distribution with mean 0 and standard deviation 1. In a diffusion process, these values provide the noise added to progressively distort data. section 10.5

Adding noise during diffusion

def diffuse_points(points, beta): new_mean = torch.sqrt(1-beta) * points noise = torch.randn(points.shape) perturbation = torch.sqrt(beta) * noise points = new_mean + perturbation return points, noise The scaled original points and scaled random noise are added to produce the next state. section 10.5

def diffuse_points(points, beta):
    new_mean = torch.sqrt(1-beta) * points
    noise = torch.randn(points.shape)
    perturbation = torch.sqrt(beta) * noise
    points = new_mean + perturbation
    return points, noise

Inspect the complete helper that creates normally distributed noise, scales it by beta, and adds it to the input points. section 10.5

How Deep Learning with PyTorch, Second Edition covers it

How deep it goes
Defines the distribution and demonstrates one implementation, but does not detail random-number generation internals.
Examples
Uses diffuse_points to perturb points and follows their movement across repeated diffusion steps.
What you can do after
Apply it via PyTorch to create Gaussian noise matching a specified tensor shape.
How it connects
Connects tensors, normal distribution, noise, and the forward diffusion process.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with PyTorch, Second Edition. The section links point to the parts of the book it draws on.