api(network): don't accept n_input_units in constructor for polymorphism
shape checking isn't performed either
@@ -52,6 +52,7 @@ class Resnet(nn.Module):
:param actv: the activation layer constructor after each hidden layer, defaults to `torch.nn.Tanh`.
:type actv: class
"""
+
def __init__(self, n_input_units=1, n_output_units=1, n_hidden_units=32, n_hidden_layers=1, actv=nn.Tanh):
super(Resnet, self).__init__()
@@ -72,15 +73,13 @@ class Resnet(nn.Module):
class MonomialNN(nn.Module):
"""A network that expands its input to a given list of monomials
Its output shape will be (n_samples, n_input_units * n_degrees)
- :param n_input_units: number of input features
- :type n_input_units: int
:param degrees: max degree to be included, or a list of degrees that will be used
:type degrees: int or list[int] or tuple[int]
"""
- def __init__(self, n_input_units, degrees=1):
+
+ def __init__(self, degrees):
super(MonomialNN, self).__init__()
- self.n_input_units = n_input_units
if isinstance(degrees, int):
degrees = [d for d in range(1, degrees + 1)]
self.degrees = tuple(degrees)
@@ -93,16 +92,10 @@ class MonomialNN(nn.Module):
warn(f"Duplicate degrees found: {self.degrees}")
def forward(self, x):
- if len(x.shape) != 2 or x.shape[1] != self.n_input_units:
- raise ValueError(f"Expected input shape (-1, {self.n_input_units}), got {x.shape} instead")
return torch.cat([x ** d for d in self.degrees], dim=1)
def __repr__(self):
- return f"{self.__class__.__name__}(" \
- f"n_input_units={self.n_input_units}" \
- f", " \
- f"degrees={self.degrees}" \
- f")"
+ return f"{self.__class__.__name__}(degrees={self.degrees})"
def __str__(self):
return self.__repr__()
