Spaces:
Running
on
Zero
Running
on
Zero
File size: 600 Bytes
1ea89dd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import torch
import torch.nn as nn
from torch.autograd import Function
class ChockerFunction(Function):
@staticmethod
def forward(ctx, x, alpha):
ctx.alpha = alpha
return x
@staticmethod
def backward(ctx, grad_output):
grad_input = grad_output * ctx.alpha
return grad_input, None
class GradChoker(nn.Module):
def __init__(self, alpha):
super().__init__()
self.alpha = alpha
def forward(self, x):
alpha = torch.tensor(self.alpha, requires_grad=False, device=x.device)
return ChockerFunction.apply(x, alpha)
|