Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- X = torch.randn((20,20,20,20,10))
- linear_layer = nn.Linear(10,5)
- output = linear_layer(X)
- print(output.shape)
- >>> torch.Size([20, 20, 20, 20, 5])
- class Linear(Module):
- __constants__ = ['bias']
- def __init__(self, in_features, out_features, bias=True):
- super(Linear, self).__init__()
- self.in_features = in_features
- self.out_features = out_features
- self.weight = Parameter(torch.Tensor(out_features, in_features))
- if bias:
- self.bias = Parameter(torch.Tensor(out_features))
- else:
- self.register_parameter('bias', None)
- self.reset_parameters()
- def reset_parameters(self):
- init.kaiming_uniform_(self.weight, a=math.sqrt(5))
- if self.bias is not None:
- fan_in, _ = init._calculate_fan_in_and_fan_out(self.weight)
- bound = 1 / math.sqrt(fan_in)
- init.uniform_(self.bias, -bound, bound)
- @weak_script_method
- def forward(self, input):
- return F.linear(input, self.weight, self.bias)
- def extra_repr(self):
- return 'in_features={}, out_features={}, bias={}'.format(
- self.in_features, self.out_features, self.bias is not None
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement