Problem in Training of a Simple MLP (PyTorch)

mhr_azizi: <p>Hello,<br>
I want to train a simple MLP of three layers using a table of data. The training data is such that the target is simply a multiplication of my three inputs.</p>
<p>target = x1 * x2 * x3</p>
<p>Here is the MLP network that I have defined using torch.nn:</p>
<blockquote>
<p>class MLP(nn.Module):<br>
def <strong>init</strong>(self, input_number, output_number, HLNN1, HLNN2):<br>
super().<strong>init</strong>()<br>
self.fc1 = nn.Linear(input_number, HLNN1)<br>
self.fc2 = nn.Linear(HLNN1, HLNN2)<br>
self.fc_out = nn.Linear(HLNN2, output_number)</p>
<pre><code>def forward(self, x):
    x = self.fc1(x)
    x = F.relu(x)
    x = self.fc2(x)
    x = F.tanh(x)
    x = self.fc_out(x)
    return x
</code></pre>
<p>model = MLP(1, 1, 30, 10)</p>
<p>BATCH_SIZE = 3<br>
EVALUATION_PRECENT = 0.2<br>
TRAINING_PRECENT = 1- EVALUATION_PRECENT<br>
LR = .5<br>
EPOCH_NUMBER = 50<br>
criterion = nn.MSELoss()<br>
optimizer = optim.AdamW(model.parameters(), LR)</p>
<p>total_data = pd.read_excel(“d:\Data.xlsx”)</p>
<p>total_data_num = len(total_data)</p>
<p>training_data_num = int(TRAINING_PRECENT * total_data_num)<br>
evaluation_data_num = total_data_num - training_data_num</p>
<p>total_data = np.array(total_data)</p>
<p>total_data = torch.tensor(total_data, dtype=torch.float32)</p>
<p>train_data, eval_data = D.random_split(total_data, [training_data_num, evaluation_data_num])</p>
<p>train_data = train_data[:]<br>
eval_data = eval_data[:]</p>
<p>train_dataloader = D.DataLoader(train_data, batch_size=BATCH_SIZE, shuffle=True)</p>
<p>eval_dataloader = D.DataLoader(eval_data, batch_size=BATCH_SIZE, shuffle=True)</p>
<p>def train_model(model, data_loader, loss_func, optimizer_func, epoch_num):<br>
epoch_loss = 0<br>
n = len(data_loader)<br>
model.train()<br>
for eopch in range(epoch_num+1):<br>
epoch_loss = 0<br>
for data in data_loader:<br>
input_data = data[:, 0]<br>
input_data = torch.reshape(input_data, (len(input_data),1))<br>
target_data = data[:, 3]<br>
prediction = model(input_data)<br>
loss = loss_func(prediction, target_data)<br>
optimizer_func.zero_grad()<br>
loss.backward()<br>
optimizer_func.step()<br>
epoch_loss += loss<br>
mean_loss = epoch_loss / n<br>
print(f"EPOCH [{eopch:&gt;02}]: Avrage Loss: {mean_loss:&gt;.02}")</p>
<p>train_model(model, train_dataloader, criterion, optimizer, EPOCH_NUMBER)</p>
</blockquote>
<p>where F is torch.nn.functional module.</p>
<p>I have also written a function to train the model as follows:</p>
<p>I tried to train the above model using 1100 training data and the above training function. However, the average loss in each epoch is approximately 1e+13 and doesn’t decrease during the training process.</p>
<p>Could anyone help me please about this issue and also about what that may be wrong in my code?<br>
Thank you,<br>
Regards</p>

yiftach: <p>Please format the code properly so it can be copied, and also supply the code for generating a dataset, so your code sample becomes useable.</p>

ptrblck: <p>Is the loss starting with auch a large value already or is your model diverging? In the former case you might want to normalize your data and target (and unnormalize it for inference and accuracy calculations) and check if it would help the model converge.</p>

mhr_azizi: <p>Thank you for your response. The loss starts from a large number and doesn’t reduce in training process. It remains approximately constant or oscillate between two large n umbers around 1e+13.<br>
I changed the number of neurons, learning rate, epoch number, and also activation function, but these have not any effects on the results.<br>
I also normalized the input data, but the result was not improved.</p>

Somayeh: <p>This is my problem, too. Could anyone please help on this issue??<br>
Thank you in advance <img src="https://discuss.pytorch.org/images/emoji/apple/smiling_face.png?v=12" title=":smiling_face:" class="emoji" alt=":smiling_face:" loading="lazy" width="20" height="20"></p>

ptrblck: <p>Did you try to normalize the data and targets as previously suggested? It seems <a class="mention" href="/u/mhr_azizi">@mhr_azizi</a> only normalized the data so you might give it a try.</p>

Somayeh: <p>Thanks. I’ll try it. <img src="https://discuss.pytorch.org/images/emoji/apple/+1/2.png?v=12" title=":+1:t2:" class="emoji" alt=":+1:t2:" loading="lazy" width="20" height="20"></p>