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

from heading import *
import torch
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(288,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(-1,288)
x = self.fc1(x)
x = self.fc2(x)
x = self.fc3(x)
return x

x seems to contain 32768 elements, so a view of (-1, 288) is invalid, since 32768 cannot be divided by 288 without a remainder.

convlution = CONV_net(input_channels = 3, output_size = 10)
net_work = create_network(convlution)
image_size = 32
transform = resize_image(image_size)
BATCH_SIZE = 64
def load_data(transform, BATCH_SIZE, train, PATH = ‘Data’):
load_dataset = datasets.CIFAR100(PATH,
train=train,
transform=transform,
download=False)

data_loader = torch.utils.data.DataLoader(dataset = load_dataset, 
                                        batch_size = BATCH_SIZE, shuffle = True)

return data_loader

train_loader=load_data(transform, BATCH_SIZE, train= True, PATH = ‘Data’)
test_loader=load_data(transform, BATCH_SIZE, train = False, PATH = ‘Data’)
EPOCH = 1
LR = 0.001
trained_model = train_model(net_work, train_loader, LR, EPOCH)

this is my code below it, so what should I do to fix the erroe?
[/quote]

Without knowing anything about the use case: one approach would be to use:

x = x.view(x.size(0), -1)
print(x.shape)

to keep the batch size equal and to check the activation shape afterwards. Once this is done, change the in_features of self.fc1 to the printed dim1 shape.

I used the cifar-100 dataset from kaggle and try to use CNN

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?

Use the provided code snippet to check the activation shape and change the in_features of fc1.
For a CIFAR input of [batch_size, 3, 32, 32], fc1 should have 512 in_features:

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)

thank you very much it works