Training Loss is constant in simple CNN


(Anoopsaini) #1

Hi,

I implemented a simple CNN which has 4 conv layers. When i train this network the training loss does not decreases. Neither accuracy increasing or test loss changes. Any help will be appreciated . Following is the code:

def __init__(self):
	super(network, self).__init__()	
	self.conv1 = ConvLayer(3, 16, 5)
	self.conv2 = ConvLayer(16, 32, 1)
	self.conv3 = ConvLayer(32, 64, 3, stride=2)
	self.conv4 = ConvLayer(64, 64, 1, stride=2)
	self.fc1 = nn.Linear(64*7*7, 1024)
	self.fc2 = nn.Linear(1024, 256)
	self.fc3 = nn.Linear(256, 128)
	self.fc4 = nn.Linear(128, 64)
	self.softmax = nn.Softmax(dim=1)

def forward(self, x1):
	x = self.conv1(x1)
	x = self.conv2(x)
	x = self.conv3(x)
	x = self.conv4(x)
	x = x.view(-1, 64 * 7 * 7)
	x = F.relu(self.fc1(x))
	x = F.relu(self.fc2(x))
	x = F.relu(self.fc3(x))
	x = F.relu(self.fc4(x))
	x = self.softmax(x)
	return x

model = network()
print(model)
if gpu_available:
model.cuda()
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.001, momentum=0.9)
writer1 = SummaryWriter(‘logs-1-0/exp-1’)
writer2 = SummaryWriter(‘logs-1-0/exp-2’)
writer3 = SummaryWriter(‘logs-1-0/exp-3’)
acc = 0
for epoch in range(1, n_epochs+1):
acc = 0
train_loss = 0.0
valid_loss = 0.0
test_loss = 0.0
model.train()
for data, target in train_loader:
if gpu_available:
data, target = data.cuda(), target.cuda()
optimizer.zero_grad()
output = model(data)
loss = criterion(output, target)
loss.backward()
optimizer.step()
train_loss += loss.item()
iteration = iteration + 1
correct = 0;
total = 0
model.eval()
for data, target in test_loader:
if gpu_available:
data, target = data.cuda(), target.cuda()
output = model(data)
loss = criterion(output, target)
test_loss += loss.item()data.size(0)
_, pred = torch.max(output, 1)
total += target.size(0)
correct += (pred == target).sum().item()
acc += 100
(correct/total);

print('Epoch: {}  Train loss: {:.2f}  Test loss: {:.2f}   Accuracy {:.2f}'.format(epoch, train_loss/len(train_loader), test_loss/len(test_loader), acc/len(test_loader)))

Here is ConvLayer class:
class ConvLayer(nn.Module):

def __init__(self, in_channel, out_channel, kernel_size, stride=1, padding=0):
	super(ConvLayer, self).__init__()
	self.conv0 = nn.Conv2d(in_channels=in_channel,out_channels=out_channel,kernel_size=kernel_size,stride=stride, padding = padding)
	self.relu = nn.ReLU(inplace=True)

def forward(self, x):
	out_conv0 = self.conv0(x)
	out_relu0 = self.relu(out_conv0)
	return out_relu0

(Anoopsaini) #2

This issue has been solved. Softmax was causing the problem.