Skip to content

Commit cc839d6

Browse files
committed
fix: if layers == 0, layers were not initialized
1 parent 622078a commit cc839d6

1 file changed

Lines changed: 11 additions & 9 deletions

File tree

model2vec/train/classifier.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,17 @@ def classes(self) -> np.ndarray:
6666

6767
def construct_head(self) -> nn.Sequential:
6868
"""Constructs a simple classifier head."""
69-
if self.n_layers == 0:
70-
return nn.Sequential(nn.Linear(self.embed_dim, self.out_dim))
71-
modules = [
72-
nn.Linear(self.embed_dim, self.hidden_dim),
73-
nn.ReLU(),
74-
]
75-
for _ in range(self.n_layers - 1):
76-
modules.extend([nn.Linear(self.hidden_dim, self.hidden_dim), nn.ReLU()])
77-
modules.extend([nn.Linear(self.hidden_dim, self.out_dim)])
69+
modules = []
70+
if self.n_layers > 0:
71+
# If we have a hidden layer, we should first project to hidden_dim
72+
modules = [
73+
nn.Linear(self.embed_dim, self.hidden_dim),
74+
nn.ReLU(),
75+
]
76+
for _ in range(self.n_layers - 1):
77+
modules.extend([nn.Linear(self.hidden_dim, self.hidden_dim), nn.ReLU()])
78+
# We always have a layer mapping from hidden to out.
79+
modules.append(nn.Linear(self.hidden_dim, self.out_dim))
7880

7981
for module in modules:
8082
if isinstance(module, nn.Linear):

0 commit comments

Comments
 (0)