shape '[-1, 288]' is invalid for input of size 32768

Sekiro1: <p>from heading import *<br>
import torch<br>
class CONV_net(torch.nn.Module):<br>
def <strong>init</strong>(self, input_channels = 3, output_size = 10):<br>
super().<strong>init</strong>()<br>
self.conv1 = torch.nn.Sequential(<br>
torch.nn.Conv2d(input_channels,16,3,2,1),<br>
torch.nn.MaxPool2d(2),<br>
torch.nn.ReLU(),<br>
torch.nn.BatchNorm2d(16)<br>
)<br>
self.conv2 = torch.nn.Sequential(<br>
torch.nn.Conv2d(16,32,3,1,1),<br>
torch.nn.MaxPool2d(2),<br>
torch.nn.ReLU(),<br>
torch.nn.BatchNorm2d(32)<br>
)<br>
self.fc1 = torch.nn.Sequential(<br>
torch.nn.Linear(288,60),<br>
torch.nn.Dropout(0.5),<br>
torch.nn.ReLU()<br>
)<br>
self.fc2 = torch.nn.Sequential(<br>
torch.nn.Linear(60,20),<br>
torch.nn.Dropout(0.5),<br>
torch.nn.ReLU()<br>
)<br>
self.fc3 = torch.nn.Linear(20, output_size)<br>
def forward(self, x):<br>
x = self.conv1(x)<br>
x = self.conv2(x)<br>
x = x.view(-1,288)<br>
x = self.fc1(x)<br>
x = self.fc2(x)<br>
x = self.fc3(x)<br>
return x</p>

ptrblck: <p><code>x</code> seems to contain <code>32768</code> elements, so a view of <code>(-1, 288)</code> is invalid, since <code>32768</code> cannot be divided by <code>288</code> without a remainder.</p>

Sekiro1: <p>convlution = CONV_net(input_channels = 3, output_size = 10)<br>
net_work = create_network(convlution)<br>
image_size = 32<br>
transform = resize_image(image_size)<br>
BATCH_SIZE = 64<br>
def load_data(transform, BATCH_SIZE, train, PATH = ‘Data’):<br>
load_dataset = datasets.CIFAR100(PATH,<br>
train=train,<br>
transform=transform,<br>
download=False)</p>
<pre><code class="lang-auto">data_loader = torch.utils.data.DataLoader(dataset = load_dataset, 
                                        batch_size = BATCH_SIZE, shuffle = True)

return data_loader
</code></pre>
<p>train_loader=load_data(transform, BATCH_SIZE, train= True, PATH = ‘Data’)<br>
test_loader=load_data(transform, BATCH_SIZE, train = False, PATH = ‘Data’)<br>
EPOCH = 1<br>
LR = 0.001<br>
trained_model = train_model(net_work, train_loader, LR, EPOCH)</p>
<p>this is my code below it, so what should I do to fix the erroe?<br>
[/quote]</p>

ptrblck: <p>Without knowing anything about the use case: one approach would be to use:</p>
<pre><code class="lang-python">x = x.view(x.size(0), -1)
print(x.shape)
</code></pre>
<p>to keep the batch size equal and to check the activation shape afterwards. Once this is done, change the <code>in_features</code> of <code>self.fc1</code> to the printed <code>dim1</code> shape.</p>

Sekiro1: <p>I used the cifar-100 dataset from kaggle and try to use CNN</p>

Sekiro1: <p>Could you please tell me how to choose those parameters pls? I just want to build a CNN and load the cifar-100 dataset and train the model based on the dataset, how do I calculate the right parameters?</p>

ptrblck: <p>Use the provided code snippet to check the activation shape and change the <code>in_features</code> of <code>fc1</code>.<br>
For a CIFAR input of <code>[batch_size, 3, 32, 32]</code>, <code>fc1</code> should have <code>512</code> <code>in_features</code>:</p>
<pre><code class="lang-python">class CONV_net(torch.nn.Module):
    def __init__(self, input_channels = 3, output_size = 10):
        super().__init__()
        self.conv1 = torch.nn.Sequential(
        torch.nn.Conv2d(input_channels,16,3,2,1),
        torch.nn.MaxPool2d(2),
        torch.nn.ReLU(),
        torch.nn.BatchNorm2d(16)
        )
        self.conv2 = torch.nn.Sequential(
        torch.nn.Conv2d(16,32,3,1,1),
        torch.nn.MaxPool2d(2),
        torch.nn.ReLU(),
        torch.nn.BatchNorm2d(32)
        )
        self.fc1 = torch.nn.Sequential(
        torch.nn.Linear(512,60),
        torch.nn.Dropout(0.5),
        torch.nn.ReLU()
        )
        self.fc2 = torch.nn.Sequential(
        torch.nn.Linear(60,20),
        torch.nn.Dropout(0.5),
        torch.nn.ReLU()
        )
        self.fc3 = torch.nn.Linear(20, output_size)
    def forward(self, x):
        x = self.conv1(x)
        x = self.conv2(x)
        x = x.view(x.size(0), -1)
        x = self.fc1(x)
        x = self.fc2(x)
        x = self.fc3(x)
        return x
    
model = CONV_net()
x = torch.randn(2, 3, 32, 32)
out = model(x)
</code></pre>

Sekiro1: <p>thank you very much it works</p>