And using those changes:
rn18 = models.resnet18()
rn18.fc = nn.Linear(512, 2048)
rn18.eval()
images = torch.randn(1000, 3, 32, 32)
with torch.no_grad():
    data = rn18(images)

labels = torch.empty(1000, dtype=torch.long).random_(3)
loss_function = nn.CrossEntropyLoss()
model = nn.Linear(2048, 3)
optimizer = torch.optim.Adam(model.parameters())

for i in range(10):
    l = train_model(model, data, labels)
    print(l)

Everything is working fine.
Also, you can do data.detach_() on your matrix.