# RMSprop optimiser doesn't work with BCE loss

When I run this code with the RMSprop optimiser it doesn’t learn anything while if I use Adam it works. How can I make it work? Or what can be the cause?I would really appreciate some help

I think it has to do with the model because when I do transfer learning. Then the model works appropriate but when I apply my own model it doesn’t work. But I couldn’t figure out what exactly causes the problem

``````import torch
import torch.nn as nn
import torch.optim as optim
import torchvision.transforms as transforms
from torchvision.datasets import ImageFolder

def get_dataset(train_path, val_path, BATCHSIZE):
transform_train = transforms.Compose([
transforms.Resize((224, 224)),
transforms.RandomHorizontalFlip(),
transforms.RandomRotation(20),
transforms.RandomAffine(degrees=0, translate=(0.2, 0.2), scale=(0.8, 1.2), shear=0.2),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])

transform_valid = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])

train_dataset = ImageFolder(train_path, transform=transform_train)
val_dataset = ImageFolder(val_path, transform=transform_valid)

def build_model():
model = nn.Sequential(
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Flatten(),
nn.Linear(128 * 28 * 28, 1),
nn.Sigmoid()
)

return model

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = build_model().to(device)
criterion = nn.BCELoss()
optimizer = optim.RMSprop(model.parameters(), lr=learning_rate)

for epoch in range(num_epochs):
model.train()
train_loss = 0.0

images = images.to(device)
labels = labels.float().unsqueeze(1).to(device)

outputs = model(images)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()

train_loss += loss.item() * images.size(0)

print(f"Epoch [{epoch+1}/{num_epochs}], Train Loss: {train_loss:.4f}")

model.eval()
valid_loss = 0.0
correct = 0

images = images.to(device)
labels = labels.float().unsqueeze(1).to(device)

outputs = model(images)
loss = criterion(outputs, labels)
valid_loss += loss.item() * images.size(0)
predicted = (outputs >= 0.5).float()