Multi-Layer Neural Network Errors

Viewed 40

I have posted before, but I am posting a different neural network that I am trying to run on the same dataset. I am trying to run on a sklearn dataset. This is the code that I have written thus far:

X, y = make_moons(200, noise=0.2, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30, random_state=1, stratify = y)
class MultiLayerPerceptron(nn.Module):
    def __init__(self):
        super(MultiLayerPerceptron, self).__init__()
        self.fc1 = nn.Sigmoid()
        self.fc2 = nn.Sigmoid()
        self.fc3 = nn.Sigmoid()

    def forward(self, x):
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

model = MultiLayerPerceptron)
optimizer = torch.optim.Adam(model.parameters(), lr = 0.001)
loss_fn = nn.CrossEntropyLoss()
epochs = 1000

def print_(loss):
    print("Loss", loss)

x, y = Variable (torch.from_numpy(X_train)).float(), Variable(torch.from_numpy(y_train)).long()

for epoch in range(1, epochs + 1):
    print("Epoch #", epoch)
    y_pred = model(x)
    loss = loss_fn(y_pred, y)
    print_(loss.item())

    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

x_test = Variable(torch.from_numpy(X_test)).float()
model.eval()
pred = model(x_test)
pred = pred.detach().numpy()

print("Accuracy", accuracy_score(y_test, np.argmax(pred, axis = 1)))

But when I run this I get this error:

line 47, in __init__
    raise ValueError("optimizer got an empty parameter list")
ValueError: optimizer got an empty parameter list

I am trying to run a 3-layer neural network to classify the dataset and I do not know what is stopping it from running.

1 Answers

You pass two parameters into constructor of your model :

model = MultiLayerPerceptron(X_train.shape[1], 1)

But the constructor of your model doesn't accept parameters:

def __init__(self):

If you really need it add it to constructor like this:

def __init__(self,par1,par2):
Related