# Single layer precepton vs linear regression

I am doing a stock prediction task.

In theory, when N > p, single-layer perception, and linear regression would give the same results of the model.

However, in my experiments, the results don’t line up. what could be the causes? Is my code buggy?

``````#fit linear regression model
``````
``````# Single layer precepton
import torch
from torch import nn
from sklearn.preprocessing import StandardScaler

class Dataset(torch.utils.data.Dataset):
def __init__(self, X, y, scale_data=True):
if not torch.is_tensor(X) and not torch.is_tensor(y):
# Apply scaling if necessary
if scale_data:
X = StandardScaler(with_mean=True, with_std=True).fit_transform(X)
self.X = torch.from_numpy(X)
self.y = torch.from_numpy(y)

def __len__(self):
return len(self.X)

def __getitem__(self, i):
return self.X[i], self.y[i]

class MLP(nn.Module):
'''
Multilayer Perceptron for regression.
'''
def __init__(self):
super().__init__()
self.layers =  nn.Linear(12, 1)

def forward(self, x):
'''
Forward pass
'''
return self.layers(x)

# Set fixed random number seed
torch.manual_seed(42)

X, y = train_X, train_y.values

# Prepare dataset
dataset = Dataset(X, y)

# Initialize the MLP
mlp = MLP()

# Define the loss function and optimizer
loss_function = nn.MSELoss()

# Run the training loop
for epoch in range(0, 1000):

# Print epoch
# print(f'Starting epoch {epoch+1}')

# Set current loss value
current_loss = 0.0

# Iterate over the DataLoader for training data
for i, data in enumerate(trainloader, 0):
# Get and prepare inputs
inputs, targets = data
inputs, targets = inputs.float(), targets.float()
targets = targets.reshape((targets.shape[0], 1))

outputs = mlp(inputs)
loss = loss_function(outputs, targets)
loss.backward()
optimizer.step()

# Print statistics
current_loss += loss.item()

# print('Train Loss:', (current_loss))

# Train data
for i, data in enumerate(trainloader, 0):
inputs, targets = data
inputs, targets = inputs.float(), targets.float()
targets = targets.reshape((targets.shape[0], 1))
y_fit = mlp(inputs)

# Test data
dataset = Dataset(X, y)

# Iterate over the DataLoader for test data
for i, data in enumerate(testloader, 0):

inputs, targets = data
inputs, targets = inputs.float(), targets.float()
targets = targets.reshape((targets.shape[0], 1))
y_pred = mlp(inputs)

y_pred = y_pred.detach().numpy().reshape(-1)
y_fit = y_fit.detach().numpy().reshape(-1)

# Process is complete.
print('Training process has finished.')`````````