Hi all,
I am a newbie to pytorch and am trying to build a simple claasifier by my own. I am trying to train a tensor classifier with 4 classes, the inputs are one dimensional tensors with a length of 1000.
This is the architecture of my neural network, I have used BatchNorm layer:
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv1d(1, 6, 5)
self.bn1 = nn.BatchNorm1d(6)
self.conv2 = nn.Conv1d(6, 16, 1)
self.bn2 = nn.BatchNorm1d(16)
self.fc1 = nn.Linear(16 * 996, 120)
self.bn3 = nn.BatchNorm1d(120)
self.fc2 = nn.Linear(120, 84)
self.bn4 = nn.BatchNorm1d(84)
self.fc3 = nn.Linear(84, 4)
def forward(self, x):
x = F.relu(self.bn1(self.conv1(x)))
x = F.relu(self.bn2(self.conv2(x)))
x = torch.flatten(x, 1)
x = F.relu(self.bn3(self.fc1(x)))
x = F.relu(self.bn4(self.fc2(x)))
x = F.relu(self.fc3(x))
return x
A reference of my dataset format:
for i, data in enumerate(data_loader, 0):
input, label = data
input, label = input.unsqueeze(1).float().to(device), label.to(device)
print(input.shape)
print(label)
break
torch.Size([64, 1, 1000])
tensor([3, 0, 2, 1, 0, 1, 0, 1, 2, 0, 2, 2, 1, 1, 0, 3, 0, 0, 3, 1, 0, 2, 1, 1,
1, 2, 2, 2, 0, 1, 0, 2, 2, 1, 1, 1, 1, 1, 2, 1, 1, 2, 2, 3, 2, 0, 0, 3,
0, 0, 3, 2, 1, 1, 0, 2, 0, 1, 1, 1, 3, 1, 0, 2], device='cuda:0')
I have just used cross entropy as my loss, and I have tried different optimizors with different learnig rate, but they yielded the same issue:
net = Net()
net.to(device)
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=1e-10, momentum=0.9)
# optimizer = optim.Adagrad(net.parameters(), lr=1e-5)
# optimizer = optim.Adam(net.parameters(), lr=1e-5)
epochs = 100
for epoch in range(epochs):
running_loss = 0.0
for i, data in enumerate(data_loader, 0):
softmax_pdf, label, index = data
softmax_pdf, label = softmax_pdf.unsqueeze(1).float().to(device), label.to(device)
optimizer.zero_grad()
outputs = net(softmax_pdf)
loss = criterion(outputs, label)
print(loss.item())
loss.backward()
print(loss.item())
optimizer.step()
running_loss += loss.item()
if i % 100 == 0:
print('[%d, %5d] loss: %.3f' %
(epoch + 1, i + 1, running_loss / 2000))
running_loss = 0.0
print('Finished Training')
The outputs look similar to this for all my attempts:
1.4231895208358765
1.4231895208358765
[1, 1] loss: 0.001
1.39090096950531
1.39090096950531
1.3952362537384033
1.3952362537384033
1.3980228900909424
1.3980228900909424
1.3929697275161743
1.3929697275161743
1.3981080055236816
1.3981080055236816
1.448412299156189
1.448412299156189
1.4033156633377075
1.4033156633377075
1.403794288635254
1.403794288635254
1.422071099281311
1.422071099281311
1.4011094570159912
1.4011094570159912
1.411553978919983
1.411553978919983
1.3885104656219482
1.3885104656219482
1.3684502840042114
1.3684502840042114
1.4065546989440918
1.4065546989440918
1.3899188041687012
1.3899188041687012
1.3902665376663208
1.3902665376663208
1.3944473266601562
1.3944473266601562
1.3757106065750122
1.3757106065750122
1.439969539642334
1.439969539642334
1.4425773620605469
1.4425773620605469
1.4459556341171265
1.4459556341171265
1.3976593017578125
1.3976593017578125
1.4449955224990845
1.4449955224990845
1.4072251319885254
1.4072251319885254
1.3999367952346802
1.3999367952346802
1.4288455247879028
1.4288455247879028
1.3832045793533325
1.3832045793533325
1.4006547927856445
1.4006547927856445
1.439096212387085
1.439096212387085
1.4116154909133911
1.4116154909133911
1.4287461042404175
1.4287461042404175
1.4069699048995972
1.4069699048995972
1.40020751953125
1.40020751953125
1.3585326671600342
1.3585326671600342
1.4218270778656006
1.4218270778656006
1.3971164226531982
1.3971164226531982
1.394694209098816
1.394694209098816
1.4159125089645386
1.4159125089645386
1.3854421377182007
1.3854421377182007
1.3808670043945312
1.3808670043945312
1.3779351711273193
1.3779351711273193
1.4056364297866821
1.4056364297866821
1.4281848669052124
1.4281848669052124
1.4305639266967773
1.4305639266967773
1.3785184621810913
1.3785184621810913
1.3812319040298462
1.3812319040298462
1.39437997341156
1.39437997341156
1.4177370071411133
1.4177370071411133
1.4220192432403564
1.4220192432403564
1.4147902727127075
1.4147902727127075
1.4216375350952148
1.4216375350952148
1.4156986474990845
1.4156986474990845
1.416447401046753
1.416447401046753
1.405503511428833
1.405503511428833
1.4071837663650513
1.4071837663650513
1.4007548093795776
1.4007548093795776
1.3982759714126587
1.3982759714126587
1.415954351425171
1.415954351425171
1.3980753421783447
1.3980753421783447
1.429835557937622
1.429835557937622
1.4012715816497803
1.4012715816497803
1.3998645544052124
1.3998645544052124
1.405841588973999
1.405841588973999
nan
nan
nan
nan
nan
nan
nan
nan
nan
nan
nan
nan
nan
nan
nan
nan
nan
nan
nan
nan
nan
nan
nan
nan
nan
nan
nan
nan
nan
nan
nan
nan
nan
nan
nan
nan
nan
nan
nan
nan
nan
nan
nan
nan
nan
nan
nan
nan
[2, 1] loss: nan
nan
nan
nan
I have also tried to add a small epsilon to the loss (not sure if this is a rational way to add epsilon), but the issue still exists.
Any help would be genuinely appreciated here!