Given groups=1, weight of size [6, 3, 3, 3], expected input[128, 1, 32, 32] to have 3 channels, but got 1 channels instead and i got 'Tensor' object has no attribute 'state_dict'

I got this error and i am new to pytorch .please help.i am running the below code
class ConvolutionalNetwork(nn.Module):
def init(self):
super().init()
self.conv1 = nn.Conv2d(3,6,kernel_size=(3,3), stride=1, padding=1, bias=False)
self.conv1.weight = nn.Parameter(kn.expand(6, 3, -1, -1))
self.conv2 = nn.Conv2d(6, 16, kernel_size=(3,3), stride=1, padding=1, bias=False)
self.conv2.weight = nn.Parameter(kn.expand(16, 6, -1, -1))
self.fc1 = nn.Linear(565616, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 18)

def forward(self, X):
        X = F.relu(self.conv1(X))
        X = F.max_pool2d(X, 2, 2)#max pooling de 2x2
        X = F.relu(self.conv2(X))
        X = F.max_pool2d(X, 2, 2)
        X = X.view(-1, 56*56*16)
        X = F.relu(self.fc1(X))
        X = F.relu(self.fc2(X))
        X = self.fc3(X)
        return F.log_softmax(X, dim=1)

torch.manual_seed(101)
CNNmodel = ConvolutionalNetwork()

x = torch.randn(1, 3, 224, 224,)
model = CNNmodel(x)
print(model)

Specify a path to save to

PATH = “model.pt”

Save

torch.save(model.state_dict(), PATH)

Load

device = torch.device(‘cpu’)
model = CNNmodel()
model.load_state_dict(torch.load(PATH, map_location=device))

Your model works fine after removing the undefined kn variable and fixing a shape mismatch in the linear layer:

class ConvolutionalNetwork(nn.Module):
    def __init__(self):
        super().__init__()
        self.conv1 = nn.Conv2d(3,6,kernel_size=(3,3), stride=1, padding=1, bias=False)
        self.conv2 = nn.Conv2d(6, 16, kernel_size=(3,3), stride=1, padding=1, bias=False)
        self.fc1 = nn.Linear(50176, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 18)
    
    def forward(self, X):
        X = F.relu(self.conv1(X))
        X = F.max_pool2d(X, 2, 2)#max pooling de 2x2
        X = F.relu(self.conv2(X))
        X = F.max_pool2d(X, 2, 2)
        X = X.view(x.size(0), -1)
        X = F.relu(self.fc1(X))
        X = F.relu(self.fc2(X))
        X = self.fc3(X)
        return F.log_softmax(X, dim=1)

torch.manual_seed(101)
CNNmodel = ConvolutionalNetwork()

x = torch.randn(1, 3, 224, 224)
model = CNNmodel(x)
print(model)

PS: you can post code snippets by wrapping them into three backticks ```, which makes debugging easier :wink:

i was trying to work on new kernel_initializer as in keras . how can i work kernel_intializer of my own like some gaussian derivative

You could check the torch.nn.init functions and see if one of them would fit your use case.
Alternatively, copying your kernel into the weight parameters would also work, but I needed to remove it for debugging as the code was not executable with these undefined variables.

and with same model mentioned above i got an AttributeError: ‘Tensor’ object has no attribute ‘state_dict’

state_dict() is an nn.Module attribute, so the error is expected.

how can i copy my kernel into the weight parameters to work .

This would work:

conv = nn.Conv2d(3, 6, 3, 1, 1)
my_weight = torch.ones(6, 3, 3, 3)
with torch.no_grad():
    conv.weight.copy_(my_weight)
print(conv.weight)

x = torch.randn(2, 3, 24, 24)
out = conv(x)

If u have any code related to such kind of topic like kernel_intializer , can u please post

can you suggest any course related to pytorch for deep learning

following errors occured becoz parameters are not updated in state_dict
File “C:\Users\LENOVO\Desktop-master - GCN\train.py”, line 100, in
optimizer = torch.optim.SGD(model.parameters(), lr=args.lr, weight_decay=args.weight_decay)

File “C:\Users\LENOVO\anaconda3\envs\spyder\lib\site-packages\torch\optim\sgd.py”, line 69, in init
super(SGD, self).init(params, defaults)

File “C:\Users\LENOVO\anaconda3\envs\spyder\lib\site-packages\torch\optim\optimizer.py”, line 49, in init
raise ValueError(“optimizer got an empty parameter list”)

ValueError: optimizer got an empty parameter list

I would probably start with the tutorials, then check out Deep Learning With PyTorch by @elistevens @lantiga and @tom, and then maybe take a look at FastAI.

Could you post an executable code snippet reproducing the issue, please?

class ConvolutionalNetwork(nn.Module):
    def __init__(self):
        super().__init__()
    conv = nn.Conv2d(3,6,kernel_size=(3,3), stride=1, padding=1, bias=False)
    my_weight = torch.ones(6, 3, 3, 3)
    with torch.no_grad():
        conv.weight.copy_(my_weight)
    print(conv.weight)

    x = torch.randn(2, 3, 24, 24)
    out = conv(x)
        #self.conv1.weight = nn.Parameter(kn.expand(6, 3, -1, -1))
    conv2 = nn.Conv2d(6, 16, kernel_size=(3,3), stride=1, padding=1, bias=False)
    my_weight = torch.ones(16, 6, 3, 3)
    with torch.no_grad():
        conv2.weight.copy_(my_weight)
    print(conv2.weight)

    x = torch.randn(3, 6, 24, 24)
    out = conv2(x)   
    #self.conv2.weight = nn.Parameter(kn.expand(16, 6, -1, -1))
    fc1 = nn.Linear(56*56*16, 120)
    fc2 = nn.Linear(120, 84)
    fc3 = nn.Linear(84, 18)
    
    def forward(self, X):
            X = F.relu(self.conv(X))
            X = F.max_pool2d(X, 2, 2)#max pooling de 2x2
            X = F.relu(self.conv2(X))
            X = F.max_pool2d(X, 2, 2)
            X = X.view(-1, 56*56*16)
            X = F.relu(self.fc1(X))
            X = F.relu(self.fc2(X))
            X = self.fc3(X)
            return F.log_softmax(X, dim=1)
torch.manual_seed(101)
CNNmodel = ConvolutionalNetwork().to(device)

x = torch.randn(3, 3, 224, 224,)
model = CNNmodel(x)    
#model = GaborCNN()
#model.summary()
print(model)

using this model here

device = torch.device('cpu')

def get_indexNum(config, index, status):
    test_ratio = config['test_ratio']
    train_ratio = config['train_ratio']
    trainindex = index[:int(train_ratio * len(index))]
    testindex = index[int((1 - test_ratio) * len(index)):]
    train_index, val_index, test_index = [], [], []

    ref_ids = []
    for line0 in open("./data/ref_ids.txt", "r"):
        line0 = float(line0[:-1])
        ref_ids.append(line0)
    ref_ids = np.array(ref_ids)
    # ref_ids = ref_ids[0:10]

    for i in range(len(ref_ids)):
        train_index.append(i) if (ref_ids[i] in trainindex) else \
            test_index.append(i) if (ref_ids[i] in testindex) else \
                val_index.append(i)
    if status == 'train':
        index = train_index
    if status == 'test':
        index = test_index
    if status == 'val':
        index = val_index

    return len(index)


if __name__ == '__main__':
    parser = ArgumentParser()
    parser.add_argument("--batch_size", type=int, default=128)
    parser.add_argument("--epochs", type=int, default=10)
    parser.add_argument("--lr", type=float, default=0.001)
    parser.add_argument("--dataset", type=str, default="LIVE")
    parser.add_argument("--weight_decay", type=float, default=0.0)

    args = parser.parse_args()

    save_model = "./savemodel/Gabormodel.pt"

    seed = random.randint(10000000, 99999999)
    torch.manual_seed(seed)
    np.random.seed(seed)
    print("seed:", seed)

    with open("config.yaml") as f:
        config = yaml.load(f, Loader=yaml.FullLoader)

    torch.backends.cudnn.deterministic = True
    torch.backends.cudnn.benchmark = True

    #device = torch.device("cuda" if torch.cuda.is_available() else "CPU")

    index = []
    if args.dataset == "LIVE":
        print("dataset: LIVE")
        index = list(range(1, 30))
        random.shuffle(index)
    elif args.dataset == "TID2013":
        print("dataset: TID2013")
        index = list(range(1, 26))

    print('rando index', index)

    dataset = args.dataset
    valnum = get_indexNum(config, index, "val")
    testnum = get_indexNum(config, index, "test")

    train_dataset = IQADataset(dataset, config, index, "train")
    train_loader = torch.utils.data.DataLoader(train_dataset,
                                               batch_size=args.batch_size,
                                               shuffle=True,
                                               pin_memory=True,
                                               num_workers=0)
    val_dataset = IQADataset(dataset, config, index, "val")
    val_loader = torch.utils.data.DataLoader(val_dataset)

    test_dataset = IQADataset(dataset, config, index, "test")
    test_loader = torch.utils.data.DataLoader(test_dataset)

    model = ConvolutionalNetwork().to(device)

    criterion = nn.L1Loss()
    optimizer = torch.optim.SGD(model.parameters(), lr=args.lr, weight_decay=args.weight_decay)

    best_SROCC = -1

    for epoch in range(args.epochs):
        #train
        model.train()
        LOSS = 0
        for i, (patches, label) in enumerate(train_loader):
            patches = patches.to(device)
            #patches = patches
            label = label.to(device)
            #label = label

            optimizer.zero_grad()
            outputs = model(patches)

            loss = criterion(outputs, label)

            loss.backward()
            optimizer.step()
            LOSS = LOSS + loss.item()
        train_loss = LOSS / (i + 1)

        #val
        y_pred = np.zeros(valnum)
        y_val = np.zeros(valnum)
        model.eval()
        L = 0
        with torch.no_grad():
            for i, (patches, label) in enumerate(val_loader):
                y_val[i] = label.item()
                patches = patches.to(device)
                #patches = patches
                label = label.to(device)
                #label = label
                outputs = model(patches)
                score = outputs.mean()
                y_pred[i] = score
                loss = criterion(score, label[0])
                L = L + loss.item()
        val_loss = L / (i+1)
        val_SROCC = stats.spearmanr(y_pred, y_val)[0]
        val_PLCC = stats.pearsonr(y_pred, y_val)[0]
        val_KROCC = stats.stats.kendalltau(y_pred, y_val)[0]
        val_RMSE = np.sqrt(((y_pred-y_val)**2).mean())

        #test
        y_pred = np.zeros(testnum)
        y_test = np.zeros(testnum)
        L = 0
        with torch.no_grad():
            for i, (patches, label) in enumerate(test_loader):
                y_test[i] = label.item()
                patches = patches.to(device)
                #patches = patches
                label = label.to(device)
                #patches = patches
                outputs = model(patches)
                score = outputs.mean()
                y_pred[i] = score
                loss = criterion(score, label[0])
                L = L + loss.item()
        test_loss = L / (i+1)
        SROCC = stats.spearmanr(y_pred, y_test)[0]
        PLCC = stats.pearsonr(y_pred, y_test)[0]
        KROCC = stats.stats.kendalltau(y_pred, y_test)[0]
        RMSE = np.sqrt(((y_pred - y_test) ** 2).mean())

        print("Epoch {} Valid Results: loss={:.3f} SROCC={:.3f} PLCC={:.3f} KROCC={:.3f} RMSE={:.3f}".format(epoch,
                                                                                         val_loss,
                                                                                val_SROCC,
                                                                                val_PLCC,
                                                                                val_KROCC,
                                                                                val_RMSE))
        print("Epoch {} Test Results: loss={:.3f} SROCC={:.3f} PLCC={:.3f} KROCC={:.3f} RMSE={:.3f}".format(epoch,
                                                                                        test_loss,
                                                                               SROCC,
                                                                               PLCC,
                                                                               KROCC,
                                                                               RMSE))

        if val_SROCC > best_SROCC and epoch > 100:
            print("Update Epoch {} best valid SROCC".format(epoch))
            print("Valid Results: loss={:.3f} SROCC={:.3f} PLCC={:.3f} KROCC={:.3f} RMSE={:.3f}".format(val_loss,
                                                                                    val_SROCC,
                                                                                    val_PLCC,
                                                                                    val_KROCC,
                                                                                    val_RMSE))
            print("Test Results: loss={:.3f} SROCC={:.3f} PLCC={:.3f} KROCC={:.3f} RMSE={:.3f}".format(test_loss,
                                                                                    SROCC,
                                                                                    PLCC,
                                                                                    KROCC,
                                                                                    RMSE))
            torch.save(model.state_dict(), save_model)
            best_SROCC = val_SROCC

    #final test
    model.load_state_dict(torch.load(save_model))
    model.eval()
with torch.no_grad():
        y_pred = np.zeros(testnum)
        y_test = np.zeros(testnum)
        L = 0
        for i, (patches, label) in enumerate(test_loader):
            y_test[i] = label.item()
            patches = patches.to(device)
            #patches = patches 
            label = label.to(device)
            label = label
            outputs = model(patches)
            score = outputs.mean()
            y_pred[i] = score
            loss = criterion(score, label[0])
            L = L + loss.item()

Your code is unfortunately not executable, as e.g. the config file is undefined.
I’ve also formatted it since you forgot to wrap it into the already mentioned backticks.

any sugession to move further on it

can u give the full code for a CNN model with kernel_intializer as gaussian filter for any application .

kn = torch.Tensor([[1 ,0, -1],[2, 0 ,-2], [1, 0 ,-1]]).unsqueeze(0).unsqueeze(0) ,
what does this statement mean in

kn = torch.Tensor([[1 ,0, -1],[2, 0 ,-2], [1, 0 ,-1]]).unsqueeze(0).unsqueeze(0)

self.conv1.weight = nn.Parameter(kn.expand(6, 3, -1, -1)) and what exactly this line is doing?

class ConvolutionalNetwork(nn.Module):
def init(self):
super().init()
self.conv1 = nn.Conv2d(3,6,kernel_size=(3,3), stride=1, padding=1, bias=False)
self.conv1.weight = nn.Parameter(kn.expand(6, 3, -1, -1))
self.conv2 = nn.Conv2d(6, 16, kernel_size=(3,3), stride=1, padding=1, bias=False)
self.conv2.weight = nn.Parameter(kn.expand(16, 6, -1, -1))
self.fc1 = nn.Linear(565616, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 18)

def forward(self, X):
        X = F.relu(self.conv1(X))
        X = F.max_pool2d(X, 2, 2)#max pooling de 2x2
        X = F.relu(self.conv2(X))
        X = F.max_pool2d(X, 2, 2)
        X = X.view(-1, 56*56*16)
        X = F.relu(self.fc1(X))
        X = F.relu(self.fc2(X))
        X = self.fc3(X)
        return F.log_softmax(X, dim=1)

torch.manual_seed(101)
CNNmodel = ConvolutionalNetwork()

x = torch.randn(1, 3, 224, 224,)
out = CNNmodel(x)

kn is initialized as a 2D tensor containing the specified values in the shape [3, 3] and gets two additional dimensions via the unsqueeze operations to have a shape of [1, 1, 3, 3].

kn.expand expands the tensor in to have the shape [6, 3, 3, 3]. The docs explain its usage and which values can be used (e.g. in your use case the -1 would just keep the shape of this dimension).

class GaborNN(nn.Module):
def init(self):
super(GaborNN, self).init()
#Gabor Convolution 1
self.g0 = GaborConv2d(in_channels=1, out_channels=128, kernel_size=11,stride=1,padding=2)
self.relu1=nn.ReLU()
print(self.g0.weight.shape)

    #Max Pool 1
    self.maxpool1=nn.MaxPool2d(2)
  
    #Convolution 2
    self.c1 = nn.Conv2d(in_channels=128, out_channels=384, kernel_size=11,stride=1,padding=2)
    self.relu2=nn.ReLU()
    print(self.c1.weight.shape)
    #Max Pool 2
    self.maxpool1=nn.MaxPool2d(2)
    #print(self.maxpool1.shape)
    self.fc1 = nn.Linear(384*8*8, 100)
    print(self.fc1.weight.shape)
    self.fc2 = nn.Linear(100, 1)
    #print(self.fc1.weight.shape)
    #self.fc2 = nn.Linear(64, 2)
    #print(self.fc2.weight.shape)

def forward(self, x):
    ##Convolution 1
    out=self.g0(x)
    out=self.relu1(out)
    #Max Pool 1
    out=self.maxpool1(out)
    #Convolution 2
    out=self.c1(x)
    out=self.relu2(out)
    #Max Pool 2
    out=self.maxpool2(out)
    #print(out.size)
    out=out.view(out.size(0),-1)
    out=self.fc1(out)
    out=self.fc2(out)
    print(out)
    return out

during training i got stucked with this error
RuntimeError: Given groups=1, weight of size [128, 128, 11, 11], expected input[128, 1, 32, 32] to have 128 channels, but got 1 channels instead… If such an error occurs where should i modify my code .

self.c1 is initialized as nn.Conv2d(in_channels=128, out_channels=384, kernel_size=11,stride=1,padding=2) and expects the input to have 128 channels. You are passing x to it, which seems to have 1 input channel:

out=self.c1(x)

so you might either want to change the in_channels to 1 for this layer or pass out to it if this fits your use case.