RuntimeError: [enforce fail at CPUAllocator.cpp:65] . DefaultCPUAllocator: can't allocate memory: you tried to allocate 13153337344 bytes. Error code 12 (Cannot allocate memory)

dl_m: <p>am new in pytorch.<br>
I got this error when i was try to train my model.Any one can help me please</p>
<p>Here is my code</p>
<p>import numpy as np<br>
import pandas as pd<br>
import os<br>
import torchvision<br>
from torchvision import transforms, datasets, models<br>
import torch<br>
import torch.nn as nn<br>
import torch.nn.functional as F<br>
from torch import optim, cuda<br>
from torch.utils.data import sampler, DataLoader<br>
import matplotlib.pyplot as plt<br>
import seaborn as sn<br>
import torch.optim as optim<br>
import tqdm as tqdm<br>
import cv2<br>
from timeit import default_timer as timer<br>
from PIL import Image</p>
<p>image_transforms = {</p>
<pre><code>'train':
transforms.Compose([
    transforms.RandomResizedCrop(size=256, scale=(0.8, 1.0)),
    transforms.RandomRotation(degrees=15),
    transforms.ColorJitter(),
    transforms.RandomHorizontalFlip(),
    transforms.CenterCrop(size=224),  # Image net standards
    transforms.ToTensor(),
    transforms.Normalize([0.485, 0.456, 0.406],
                         [0.229, 0.224, 0.225])  # Imagenet standards
]),

'val':
transforms.Compose([
    transforms.Resize(size=256),
    transforms.CenterCrop(size=224),
    transforms.ToTensor(),
    transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]),

'test':
transforms.Compose([
    transforms.Resize(size=256),
    transforms.CenterCrop(size=224),
    transforms.ToTensor(),
    transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]),
</code></pre>
<p>}</p>
<p>data = {<br>
‘train’:<br>
datasets.ImageFolder(root=traindir, transform=image_transforms[‘train’]),<br>
‘val’:<br>
datasets.ImageFolder(root=validdir, transform=image_transforms[‘val’]),<br>
‘test’:<br>
datasets.ImageFolder(root=testdir, transform=image_transforms[‘test’])<br>
}</p>
<p>dataloaders = {<br>
‘train’: DataLoader(data[‘train’], batch_size=128, shuffle=True),<br>
‘val’: DataLoader(data[‘val’], batch_size=128, shuffle=True),<br>
‘test’: DataLoader(data[‘test’], batch_size=128, shuffle=True)<br>
}</p>
<p>class Net(nn.Module):</p>
<pre><code>def __init__(self):
    super(Net, self).__init__()

    
    self.conv1 = nn.Conv2d(3, 32, 5)
    self.conv2 = nn.Conv2d(32, 64, 5)
    self.conv3 = nn.Conv2d(64, 128, 5)
    
    self.fc1 = nn.Linear(128 * 224 * 224, 512)  
    self.fc2 = nn.Linear(512, 2)
    

def forward(self, x):
    # Max pooling over a (2, 2) window
    x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
    x = F.max_pool2d(F.relu(self.conv2(x)), (2, 2))
    x = F.max_pool2d(F.relu(self.conv3(x)), (2, 2))
  
    x = x.view(-1, self.num_flat_features(x))
    x = F.relu(self.fc1(x))
    x = self.fc2(x)
    return F.softmax(x, dim=1)

def num_flat_features(self, x):
    size = x.size()[1:]  # all dimensions except the batch dimension
    num_features = 1
    for s in size:
        num_features *= s
    return num_features
</code></pre>
<p>net = Net()<br>
print(net)</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/1/2/127f60a762b215d6e79f5c2de41e6ded7d5d5649.png" data-download-href="https://discuss.pytorch.org/uploads/default/127f60a762b215d6e79f5c2de41e6ded7d5d5649" title="error"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/1/2/127f60a762b215d6e79f5c2de41e6ded7d5d5649_2_690x293.png" alt="error" data-base62-sha1="2DDuYxdXGqE0pj0rdnATWENIANH" width="690" height="293" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/1/2/127f60a762b215d6e79f5c2de41e6ded7d5d5649_2_690x293.png, https://discuss.pytorch.org/uploads/default/original/3X/1/2/127f60a762b215d6e79f5c2de41e6ded7d5d5649.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/1/2/127f60a762b215d6e79f5c2de41e6ded7d5d5649.png 2x" data-dominant-color="F6F8F7"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">error</span><span class="informations">1019×434 59.2 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

ptrblck: <p>Your code tries to allocate approx. 12GB of memory, which is apparently not possible on your system.<br>
Lower the <code>batch_size</code> so that less memory will be allocated. <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>

dl_m: <p>Thanks in advance sir</p>