Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. X = torch.randn((20,20,20,20,10))
  2. linear_layer = nn.Linear(10,5)
  3. output = linear_layer(X)
  4. print(output.shape)
  5. >>> torch.Size([20, 20, 20, 20, 5])
  6.  
  7. class Linear(Module):
  8. __constants__ = ['bias']
  9.  
  10. def __init__(self, in_features, out_features, bias=True):
  11. super(Linear, self).__init__()
  12. self.in_features = in_features
  13. self.out_features = out_features
  14. self.weight = Parameter(torch.Tensor(out_features, in_features))
  15. if bias:
  16. self.bias = Parameter(torch.Tensor(out_features))
  17. else:
  18. self.register_parameter('bias', None)
  19. self.reset_parameters()
  20.  
  21. def reset_parameters(self):
  22. init.kaiming_uniform_(self.weight, a=math.sqrt(5))
  23. if self.bias is not None:
  24. fan_in, _ = init._calculate_fan_in_and_fan_out(self.weight)
  25. bound = 1 / math.sqrt(fan_in)
  26. init.uniform_(self.bias, -bound, bound)
  27.  
  28. @weak_script_method
  29. def forward(self, input):
  30. return F.linear(input, self.weight, self.bias)
  31.  
  32. def extra_repr(self):
  33. return 'in_features={}, out_features={}, bias={}'.format(
  34. self.in_features, self.out_features, self.bias is not None
  35. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement