CUDA error: out of memory

RuntimeError: CUDA error: out of memory

Running the code cells for the week 3 assignment throws this.
I haven’t changed anything in the code. The cells are pre-written (not in the graded part).
I haven’t even reached the graded part yet in fact.

gen = Generator(z_dim).to(device)
gen_opt = torch.optim.Adam(gen.parameters(), lr=lr, betas=(beta_1, beta_2))
crit = Critic().to(device) 
crit_opt = torch.optim.Adam(crit.parameters(), lr=lr, betas=(beta_1, beta_2))
def weights_init(m):
    if isinstance(m, nn.Conv2d) or isinstance(m, nn.ConvTranspose2d):
        torch.nn.init.normal_(m.weight, 0.0, 0.02)
    if isinstance(m, nn.BatchNorm2d):
        torch.nn.init.normal_(m.weight, 0.0, 0.02)
        torch.nn.init.constant_(m.bias, 0)
gen = gen.apply(weights_init)
crit = crit.apply(weights_init)

The first line of the above code throws the error.

UPDATE:
I got to the graded part, completed the function and executed the testing cell.
The same error was thrown again.

@Ayazzia01, try choosing the “Restart and Clear Output” option from the Kernel menu to get your environment to a nice, clean state and see if that fixes the problem.

Also, to help avoid this kind of issue in the future, see this post: Staff Announcement: RuntimeError: CUDA error: out of memory FIX

1 Like