mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-07 12:21:27 +01:00
* Improvize documentation 1. Add formula for erf, erfinv 2. Make exp, expm1 similar to log, log1p 3. Symbol change in ge, le, ne, isnan * Fix minor nit in the docstring * More doc improvements 1. Added some formulae 2. Complete scanning till "Other Operations" in Tensor docs * Add more changes 1. Modify all torch.Tensor wherever required * Fix Conv docs 1. Fix minor nits in the references for LAPACK routines * Improve Pooling docs 1. Fix lint error * Improve docs for RNN, Normalization and Padding 1. Fix flake8 error for pooling * Final fixes for torch.nn.* docs. 1. Improve Loss Function documentation 2. Improve Vision Layers documentation * Fix lint error * Improve docstrings in torch.nn.init * Fix lint error * Fix minor error in torch.nn.init.sparse * Fix Activation and Utils Docs 1. Fix Math Errors 2. Add explicit clean to Makefile in docs to prevent running graph generation script while cleaning 3. Fix utils docs * Make PYCMD a Makefile argument, clear up prints in the build_activation_images.py * Fix batch norm doc error
123 lines
4.1 KiB
Python
123 lines
4.1 KiB
Python
import math
|
|
|
|
import torch
|
|
from torch.nn.parameter import Parameter
|
|
from .. import functional as F
|
|
from .module import Module
|
|
|
|
|
|
class Linear(Module):
|
|
r"""Applies a linear transformation to the incoming data: :math:`y = Ax + b`
|
|
|
|
Args:
|
|
in_features: size of each input sample
|
|
out_features: size of each output sample
|
|
bias: If set to False, the layer will not learn an additive bias.
|
|
Default: ``True``
|
|
|
|
Shape:
|
|
- Input: :math:`(N, *, in\_features)` where :math:`*` means any number of
|
|
additional dimensions
|
|
- Output: :math:`(N, *, out\_features)` where all but the last dimension
|
|
are the same shape as the input.
|
|
|
|
Attributes:
|
|
weight: the learnable weights of the module of shape
|
|
`(out_features x in_features)`
|
|
bias: the learnable bias of the module of shape `(out_features)`
|
|
|
|
Examples::
|
|
|
|
>>> m = nn.Linear(20, 30)
|
|
>>> input = torch.randn(128, 20)
|
|
>>> output = m(input)
|
|
>>> print(output.size())
|
|
"""
|
|
|
|
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):
|
|
stdv = 1. / math.sqrt(self.weight.size(1))
|
|
self.weight.data.uniform_(-stdv, stdv)
|
|
if self.bias is not None:
|
|
self.bias.data.uniform_(-stdv, stdv)
|
|
|
|
def forward(self, input):
|
|
return F.linear(input, self.weight, self.bias)
|
|
|
|
def __repr__(self):
|
|
return self.__class__.__name__ + '(' \
|
|
+ 'in_features=' + str(self.in_features) \
|
|
+ ', out_features=' + str(self.out_features) \
|
|
+ ', bias=' + str(self.bias is not None) + ')'
|
|
|
|
|
|
class Bilinear(Module):
|
|
r"""Applies a bilinear transformation to the incoming data:
|
|
:math:`y = x_1 A x_2 + b`
|
|
|
|
Args:
|
|
in1_features: size of each first input sample
|
|
in2_features: size of each second input sample
|
|
out_features: size of each output sample
|
|
bias: If set to False, the layer will not learn an additive bias.
|
|
Default: ``True``
|
|
|
|
Shape:
|
|
- Input: :math:`(N, in1\_features)`, :math:`(N, in2\_features)`
|
|
- Output: :math:`(N, out\_features)`
|
|
|
|
Attributes:
|
|
weight: the learnable weights of the module of shape
|
|
`(out_features x in1_features x in2_features)`
|
|
bias: the learnable bias of the module of shape `(out_features)`
|
|
|
|
Examples::
|
|
|
|
>>> m = nn.Bilinear(20, 30, 40)
|
|
>>> input1 = torch.randn(128, 20)
|
|
>>> input2 = torch.randn(128, 30)
|
|
>>> output = m(input1, input2)
|
|
>>> print(output.size())
|
|
"""
|
|
|
|
def __init__(self, in1_features, in2_features, out_features, bias=True):
|
|
super(Bilinear, self).__init__()
|
|
self.in1_features = in1_features
|
|
self.in2_features = in2_features
|
|
self.out_features = out_features
|
|
self.weight = Parameter(torch.Tensor(out_features, in1_features, in2_features))
|
|
|
|
if bias:
|
|
self.bias = Parameter(torch.Tensor(out_features))
|
|
else:
|
|
self.register_parameter('bias', None)
|
|
self.reset_parameters()
|
|
|
|
def reset_parameters(self):
|
|
stdv = 1. / math.sqrt(self.weight.size(1))
|
|
self.weight.data.uniform_(-stdv, stdv)
|
|
if self.bias is not None:
|
|
self.bias.data.uniform_(-stdv, stdv)
|
|
|
|
def forward(self, input1, input2):
|
|
return F.bilinear(input1, input2, self.weight, self.bias)
|
|
|
|
def __repr__(self):
|
|
return self.__class__.__name__ + '(' \
|
|
+ 'in1_features=' + str(self.in1_features) \
|
|
+ ', in2_features=' + str(self.in2_features) \
|
|
+ ', out_features=' + str(self.out_features) \
|
|
+ ', bias=' + str(self.bias is not None) + ')'
|
|
|
|
# TODO: PartialLinear - maybe in sparse?
|