I’m learning PyTorch and tried my concepts on my own custom data. I created two lists of random numbers and combined the corresponding items in a new list as X and another list containing the sum of corresponding items as labels y. In simple I want to train a model in such a way that to make it work as an addition calculator. But, I’m getting a very huge loss, I tried tweaking different parameters but getting almost the same results.
Here’s my code:
import torch as T
import torch.nn as nn
import torch.nn.functional as F
import random
randomList1 = random.sample(range(0, 10000), 10000)
randomList2 = random.sample(range(0, 10000), 10000)
xList = []
yList = []
for (item1, item2) in zip(randomList1, randomList2):
xList.append([item1, item2])
for x in xList:
yList.append(x[0] + x[1])
dataset = T.torch.utils.data.TensorDataset(
T.tensor(xList, dtype=T.float), T.tensor(yList, dtype=T.float))
train_batch = T.torch.utils.data.DataLoader(
dataset, batch_size=10, shuffle=True)
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.fc1 = nn.Linear(2, 128)
self.fc2 = nn.Linear(128, 128)
self.fc3 = nn.Linear(128, 128)
self.fc4 = nn.Linear(128, 1)
def forward(self, x):
x = T.relu(self.fc1(x))
x = T.tanh(self.fc2(x))
x = T.tanh(self.fc3(x))
x = T.tanh(self.fc4(x))
return x
net = Net()
loss_fn = nn.MSELoss()
optimizer = T.optim.Adam(net.parameters(), lr=0.0001)
for epoch in range(3):
for data in train_batch:
X, y = data
net.zero_grad()
output = net(X.view(-1, 2))
loss = T.sqrt(loss_fn(output, y))
loss.backward()
optimizer.step()
print(loss)
Output:
tensor(11254.4688, grad_fn=<SqrtBackward0>)
tensor(9933.7793, grad_fn=<SqrtBackward0>)
tensor(11308.9463, grad_fn=<SqrtBackward0>)