MCPcopy Create free account
hub / github.com/OpenDriveLab/ReSim / gradient_penalty

Function gradient_penalty

sat/sgm/modules/autoencoding/magvit2_pytorch.py:119–132  ·  view source on GitHub ↗
(images, output)

Source from the content-addressed store, hash-verified

117
118
119def gradient_penalty(images, output):
120 batch_size = images.shape[0]
121
122 gradients = torch_grad(
123 outputs=output,
124 inputs=images,
125 grad_outputs=torch.ones(output.size(), device=images.device),
126 create_graph=True,
127 retain_graph=True,
128 only_inputs=True,
129 )[0]
130
131 gradients = rearrange(gradients, "b ... -> b (...)")
132 return ((gradients.norm(2, dim=1) - 1) ** 2).mean()
133
134
135def leaky_relu(p=0.1):

Callers 1

forwardMethod · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected