Problem when implementing MultiLayer Perceptron

Ashkan_Karimi: <p>import torch<br>
import torch.nn as nn<br>
import torch.optim as optim<br>
from torch.utils.data import DataLoader, TensorDataset<br>
from sklearn.model_selection import train_test_split<br>
import numpy as np</p>
<h1><a name="generate-synthetic-data-1" class="anchor" href="#generate-synthetic-data-1"></a>Generate synthetic data</h1>
<p>np.random.seed(42)</p>
<h1><a name="split-the-data-into-training-and-testing-sets-2" class="anchor" href="#split-the-data-into-training-and-testing-sets-2"></a>Split the data into training and testing sets</h1>
<h1><a name="convert-data-to-pytorch-tensors-3" class="anchor" href="#convert-data-to-pytorch-tensors-3"></a>Convert data to PyTorch tensors</h1>
<p>X_train_tensor = torch.tensor(result_df_train2.iloc[:, 1].values, dtype=torch.float32)<br>
y_train_tensor = torch.tensor(result_df_train2.iloc[:, 2].values, dtype=torch.long)<br>
X_test_tensor = torch.tensor(result_df_test2.iloc[:, 1].values, dtype=torch.float32)<br>
y_test_tensor = torch.tensor(result_df_test2.iloc[:, 2], dtype=torch.long)</p>
<h1><a name="create-dataloader-4" class="anchor" href="#create-dataloader-4"></a>Create DataLoader</h1>
<p>train_dataset = TensorDataset(X_train_tensor, y_train_tensor)<br>
train_loader = DataLoader(train_dataset, batch_size=64, shuffle=True)</p>
<h1><a name="define-the-neural-network-model-using-nnsequential-5" class="anchor" href="#define-the-neural-network-model-using-nnsequential-5"></a>Define the neural network model using nn.Sequential</h1>
<p>model = nn.Sequential(<br>
nn.Linear(1, 64),  # One input feature, 64 hidden units<br>
nn.ReLU(),<br>
nn.Linear(64, 4),  # 4 output units for 4 labels<br>
nn.LogSoftmax(dim=1)  # LogSoftmax for multiclass classification<br>
)</p>
<h1><a name="define-loss-function-and-optimizer-6" class="anchor" href="#define-loss-function-and-optimizer-6"></a>Define loss function and optimizer</h1>
<p>criterion = nn.NLLLoss()<br>
optimizer = optim.Adam(model.parameters(), lr=0.001)</p>
<h1><a name="training-loop-7" class="anchor" href="#training-loop-7"></a>Training loop</h1>
<p>num_epochs = 10</p>
<p>for epoch in range(num_epochs):<br>
# Training<br>
model.train()<br>
total_loss = 0.0<br>
correct_train = 0</p>
<pre><code>for inputs, labels in train_loader:
    optimizer.zero_grad()
    outputs = model(inputs)
    loss = criterion(outputs, labels)
    loss.backward()
    optimizer.step()

    total_loss += loss.item()
    _, predicted = torch.max(outputs, 1)
    correct_train += (predicted == labels).sum().item()

avg_loss = total_loss / len(train_loader.dataset)
accuracy_train = correct_train / len(train_loader.dataset)

# Testing
model.eval()
total_correct = 0

with torch.no_grad():
    outputs_test = model(X_test_tensor)
    _, predicted_test = torch.max(outputs_test, 1)
    total_correct = (predicted_test == y_test_tensor).sum().item()

accuracy_test = total_correct / len(y_test_tensor)
</code></pre>
<p>Now, I am getting this error:<br>
 in &lt;cell line: 39&gt;()<br>
45     for inputs, labels in train_loader:<br>
46         optimizer.zero_grad()<br>
—&gt; 47         outputs = model(inputs)<br>
48         loss = criterion(outputs, labels)<br>
49         loss.backward()</p>
<p>5 frames<br>
/usr/local/lib/python3.10/dist-packages/torch/nn/modules/linear.py in forward(self, input)<br>
112<br>
113     def forward(self, input: Tensor) → Tensor:<br>
 → 114         return F.linear(input, self.weight, self.bias)<br>
115<br>
116     def extra_repr(self) → str:</p>
<p>RuntimeError: mat1 and mat2 shapes cannot be multiplied (1x64 and 1x64)</p>
<p>Can someone please help me??</p>

ptrblck: <p>It seems your input has 64 features, so you might need to set <code>in_features=64</code> in your first linear layer.</p>