[Python] Linear FFNN Model achieving 100% accuracy in predicting sums

Discussão em 'Python' iniciado por Stack, Maio 7, 2025.

  1. Stack

    Stack Membro Participativo

    I have a model that adds numbers between -10 to positive 10. The task is simply to predict the sum of those numbers. However the train accuracy quickly reaches 100%.
    I am not sure if the model is just quickly training, or if there is something wrong and it is not properly learning. Can anyone give some insight why this happens here?

    Here is my code and minimal working example:

    import torch
    import torch.nn as nn
    import torch.nn.functional as F
    from torch.utils.data import DataLoader,TensorDataset
    from sklearn.model_selection import train_test_split

    import numpy as np

    # Create the dataset
    data = []
    labels = []

    datasetAmount = 2000

    for i in range(datasetAmount):
    x = np.random.randint(-10, 10)
    y = np.random.randint(-10,10)
    bothNumber = [x, y] # Inputs
    data.append(bothNumber)
    labels.append(x+y) # Targets

    data_np = np.array(data)
    labels_np = np.array(labels).reshape(-1,1)

    train_data, test_data, train_labels, test_labels = train_test_split(data_np, labels_np, train_size =.9)

    train_data = TensorDataset(torch.tensor(train_data),torch.tensor(train_labels))
    test_data = TensorDataset(torch.tensor(test_data),torch.tensor(test_labels))

    batchsize = 20

    train_loader = DataLoader(train_data, batch_size = batchsize, shuffle = True, drop_last = True)
    test_loader = DataLoader(test_data, batch_size = test_data.tensors[0].shape[0])

    # Model Factory
    def createModel():
    class myModel(nn.Module):
    def __init__(self):
    super().__init__()

    self.input = nn.Linear(2,8)
    self.fc1 = nn.Linear(8,8)
    self.output = nn.Linear(8,1)

    def forward(self,x):
    x = F.relu( self.input(x) )
    x = F.relu( self.fc1(x) )
    return self.output(x)

    net = myModel()
    lossfun = nn.MSELoss()
    optimizer = torch.optim.SGD(net.parameters(),lr=.001)

    return net,lossfun,optimizer


    def trainModel():

    numepochs = 100
    net,lossfun,optimizer = createModel()
    losses = torch.zeros(numepochs)
    trainacc = []
    testacc = []

    for epochi in range(numepochs):
    batchLoss = []

    for X,y in train_loader:
    X = X.float()
    y = y.float()
    yHat = net(X)

    loss = lossfun(yHat,y)
    batchLoss.append(loss.item())

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

    losses[epochi] = np.mean(batchLoss)

    with torch.no_grad():
    train_predictions = []
    train_labels = []
    for x_train, y_train in train_loader:
    x_train = x_train.float()
    y_train = y_train.float()
    train_pred = net(x_train)
    train_predictions.append(train_pred)
    train_labels.append(y_train)

    train_predictions = torch.cat(train_predictions)
    train_labels = torch.cat(train_labels)

    train_acc = 100 * torch.mean((np.abs(train_predictions - train_labels) < 1).float())
    trainacc.append(train_acc.item())


    X,y = next(iter(test_data))
    X = X.float() # Convert X to float for test data
    y = y.float() # Convert y to float for test data
    with torch.no_grad():
    yHat = net(X)

    testacc= 100*torch.mean((np.abs(yHat-y)< 1).float())

    return trainacc,testacc,losses,net


    trainAcc, testAcc, losses , net = trainModel()

    Continue reading...

Compartilhe esta Página