So I’m trying to run a tabular data only model as a base line for a CNN+data model. I’m getting a NotImplementedError: somewhere in the forward and I’m not sure where I’m messing up. Here is the model:
class Data_Only_Model(nn.Module):
def __init__(self, embedding_size, num_numerical_cols, output_size, layers, p = 0.4):
'''
Args
---------------------------
embedding_size: Contains the embedding size for the categorical columns
num_numerical_cols: Stores the total number of numerical columns
output_size: The size of the output layer or the number of possible outputs.
layers: List which contains number of neurons for all the layers.
p: Dropout with the default value of 0.5
'''
super().__init__()
#list of ModuleList objects for all categorical columns
self.all_embeddings = nn.ModuleList([nn.Embedding(ni, nf) for ni, nf in embedding_size])
#drop out value for all layers
self.embedding_dropout = nn.Dropout(p)
#list of 1 dimension batch normalization objects for all numerical columns
self.batch_norm_num = nn.BatchNorm1d(num_numerical_cols)
#the number of categorical and numerical columns are added together and stored in input_size
all_layers = []
num_categorical_cols = sum((nf for ni, nf in embedding_size))
input_size = num_categorical_cols + num_numerical_cols
#loop iterates to add corresonding layers to all_layers list above
for i in layers:
all_layers.append(nn.Linear(input_size, i))
all_layers.append(nn.ReLU(inplace=True))
all_layers.append(nn.BatchNorm1d(i))
all_layers.append(nn.Dropout(p))
input_size = i
#append output layer to list of layers
all_layers.append(nn.Linear(layers[-1], output_size))
#pass all layers to the sequential class
self.layers = nn.Sequential(*all_layers)
#define the foward method
def forward(self, x_categorical, x_numerical):
#this starts the embedding of categorical columns
embeddings = []
for i,e in enumerate(self.all_embeddings):
embeddings.append(e(x_categorical[:,i]))
x = torch.cat(embeddings, 1)
x = self.embedding_dropout(x)
#normalizing numerical columns
x_numerical = self.batch_norm_num(x_numerical)
#concatenating numerical and categorical columns
x = torch.cat([x, x_numerical], 1)
x = self.layers(x)
return x
loss_function = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(d_o_model.parameters(), lr=0.001)
epochs = 300
aggregated_losses =
for i in range(epochs):
i += 1
y_pred = d_o_model(categorical_data, numerical_data)
single_loss = loss_function(y_pred, target_columns)
aggregated_losses.append(single_loss)
if i%25 == 1:
print(f'epoch: {i:3} loss: {single_loss.item():10.8f}')
optimizer.zero_grad()
single_loss.backward()
optimizer.step()
print(f'epoch: {i:3} loss: {single_loss.item():10.10f}')
Here is a full traceback of the error:
NotImplementedError Traceback (most recent call last)
in
4 for i in range(epochs):
5 i += 1
----> 6 y_pred = d_o_model(categorical_data, numerical_data)
7 single_loss = loss_function(y_pred, target_columns)
8 aggregated_losses.append(single_loss)C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\module.py in call(self, *input, **kwargs)
539 result = self._slow_forward(*input, **kwargs)
540 else:
→ 541 result = self.forward(*input, **kwargs)
542 for hook in self._forward_hooks.values():
543 hook_result = hook(self, input, result)C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\module.py in forward(self, *input)
95 registered hooks while the latter silently ignores them.
96 “”"
—> 97 raise NotImplementedError
98
99 def register_buffer(self, name, tensor):NotImplementedError: