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'

Jyothi: <p>I got this error and i am new to pytorch .please help.i am running the below code<br>
class ConvolutionalNetwork(nn.Module):<br>
def <strong>init</strong>(self):<br>
super().<strong>init</strong>()<br>
self.conv1 = nn.Conv2d(3,6,kernel_size=(3,3), stride=1, padding=1, bias=False)<br>
self.conv1.weight = nn.Parameter(kn.expand(6, 3, -1, -1))<br>
self.conv2 = nn.Conv2d(6, 16, kernel_size=(3,3), stride=1, padding=1, bias=False)<br>
self.conv2.weight = nn.Parameter(kn.expand(16, 6, -1, -1))<br>
self.fc1 = nn.Linear(56<em>56</em>16, 120)<br>
self.fc2 = nn.Linear(120, 84)<br>
self.fc3 = nn.Linear(84, 18)</p>
<pre><code>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)
</code></pre>
<p>torch.manual_seed(101)<br>
CNNmodel = ConvolutionalNetwork()</p>
<p>x = torch.randn(1, 3, 224, 224,)<br>
model = CNNmodel(x)<br>
print(model)</p>
<h1>
<a name="specify-a-path-to-save-to-1" class="anchor" href="#specify-a-path-to-save-to-1"></a>Specify a path to save to</h1>
<p>PATH = “model.pt”</p>
<h1>
<a name="save-2" class="anchor" href="#save-2"></a>Save</h1>
<p>torch.save(model.state_dict(), PATH)</p>
<h1>
<a name="load-3" class="anchor" href="#load-3"></a>Load</h1>
<p>device = torch.device(‘cpu’)<br>
model = CNNmodel()<br>
model.load_state_dict(torch.load(PATH, map_location=device))</p>

ptrblck: <p>Your model works fine after removing the undefined <code>kn</code> variable and fixing a shape mismatch in the linear layer:</p>
<pre><code class="lang-python">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)
</code></pre>
<p>PS: you can post code snippets by wrapping them into three backticks ```, which makes debugging easier <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=10" title=":wink:" class="emoji" alt=":wink:"></p>

Jyothi: <p>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</p>

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

Jyothi: <p>and with same model mentioned above i got an AttributeError:  ‘Tensor’ object has no attribute ‘state_dict’</p>

ptrblck: <p><code>state_dict()</code> is an <code>nn.Module</code> attribute, so the error is expected.</p>

Jyothi: <p>how can i copy my kernel into the <code>weight</code> parameters to work .</p>

ptrblck: <p>This would work:</p>
<pre><code class="lang-python">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)
</code></pre>

Jyothi: <p>If u have any code related to such kind of topic like kernel_intializer , can u please post</p>

Jyothi: <p>can you suggest any course related to pytorch for deep learning</p>

Jyothi: <p>following errors occured becoz parameters are not updated in state_dict<br>
File “C:\Users\LENOVO\Desktop-master - GCN\train.py”, line 100, in <br>
optimizer = torch.optim.SGD(model.parameters(), lr=args.lr, weight_decay=args.weight_decay)</p>
<p>File “C:\Users\LENOVO\anaconda3\envs\spyder\lib\site-packages\torch\optim\sgd.py”, line 69, in <strong>init</strong><br>
super(SGD, self).<strong>init</strong>(params, defaults)</p>
<p>File “C:\Users\LENOVO\anaconda3\envs\spyder\lib\site-packages\torch\optim\optimizer.py”, line 49, in <strong>init</strong><br>
raise ValueError(“optimizer got an empty parameter list”)</p>
<p>ValueError: optimizer got an empty parameter list</p>

ptrblck: <aside class="quote no-group" data-username="Jyothi" data-post="10" data-topic="135792" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jyothi/48/45303_2.png" class="avatar"> Jyothi:</div>
<blockquote>
<p>can you suggest any course related to pytorch for deep learning</p>
</blockquote>
</aside>
<p>I would probably start with the <a href="https://pytorch.org/tutorials/">tutorials</a>, then check out <a href="https://www.manning.com/books/deep-learning-with-pytorch">Deep Learning With PyTorch</a> by <a class="mention" href="/u/elistevens">@elistevens</a> <a class="mention" href="/u/lantiga">@lantiga</a> and <a class="mention" href="/u/tom">@tom</a>, and then maybe take a look at <a href="https://www.fast.ai/">FastAI</a>.</p>
<aside class="quote no-group" data-username="Jyothi" data-post="11" data-topic="135792">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jyothi/48/45303_2.png" class="avatar"> Jyothi:</div>
<blockquote>
<p>ValueError: optimizer got an empty parameter list</p>
</blockquote>
</aside>
<p>Could you post an executable code snippet reproducing the issue, please?</p>

Jyothi: <pre><code class="lang-python">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 &gt; best_SROCC and epoch &gt; 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()
</code></pre>

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

Jyothi: <p>any sugession to move further on it</p>

Jyothi: <p>can u give the full code for a CNN model with kernel_intializer as gaussian filter  for any application .</p>

Jyothi: <p>kn = torch.Tensor([[1 ,0, -1],[2, 0 ,-2], [1, 0 ,-1]]).unsqueeze(0).unsqueeze(0) ,<br>
what does this statement mean in</p>
<p>kn = torch.Tensor([[1 ,0, -1],[2, 0 ,-2], [1, 0 ,-1]]).unsqueeze(0).unsqueeze(0)</p>
<p>self.conv1.weight = nn.Parameter(kn.expand(6, 3, -1, -1))  and what exactly this line is doing?</p>
<p>class ConvolutionalNetwork(nn.Module):<br>
def <strong>init</strong>(self):<br>
super().<strong>init</strong>()<br>
self.conv1 = nn.Conv2d(3,6,kernel_size=(3,3), stride=1, padding=1, bias=False)<br>
self.conv1.weight = nn.Parameter(kn.expand(6, 3, -1, -1))<br>
self.conv2 = nn.Conv2d(6, 16, kernel_size=(3,3), stride=1, padding=1, bias=False)<br>
self.conv2.weight = nn.Parameter(kn.expand(16, 6, -1, -1))<br>
self.fc1 = nn.Linear(56<em>56</em>16, 120)<br>
self.fc2 = nn.Linear(120, 84)<br>
self.fc3 = nn.Linear(84, 18)</p>
<pre><code>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)
</code></pre>
<p>torch.manual_seed(101)<br>
CNNmodel = ConvolutionalNetwork()</p>
<p>x = torch.randn(1, 3, 224, 224,)<br>
out = CNNmodel(x)</p>

ptrblck: <aside class="quote no-group" data-username="Jyothi" data-post="17" data-topic="135792">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jyothi/48/45303_2.png" class="avatar"> Jyothi:</div>
<blockquote>
<p>kn = torch.Tensor([[1 ,0, -1],[2, 0 ,-2], [1, 0 ,-1]]).unsqueeze(0).unsqueeze(0)</p>
</blockquote>
</aside>
<p><code>kn</code> is initialized as a 2D tensor containing the specified values in the shape <code>[3, 3]</code> and gets two additional dimensions via the <code>unsqueeze</code> operations to have a shape of <code>[1, 1, 3, 3]</code>.</p>
<aside class="quote no-group" data-username="Jyothi" data-post="17" data-topic="135792">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jyothi/48/45303_2.png" class="avatar"> Jyothi:</div>
<blockquote>
<p>self.conv1.weight = nn.Parameter(kn.expand(6, 3, -1, -1)) and what exactly this line is doing?</p>
</blockquote>
</aside>
<p><code>kn.expand</code> expands the tensor in to have the shape <code>[6, 3, 3, 3]</code>. The <a href="https://pytorch.org/docs/stable/generated/torch.Tensor.expand.html?highlight=expand#torch.Tensor.expand">docs</a> explain its usage and which values can be used (e.g. in your use case the <code>-1</code> would just keep the shape of this dimension).</p>

Jyothi: <p>class GaborNN(nn.Module):<br>
def <strong>init</strong>(self):<br>
super(GaborNN, self).<strong>init</strong>()<br>
<span class="hashtag">#Gabor</span> Convolution 1<br>
self.g0 = GaborConv2d(in_channels=1, out_channels=128, kernel_size=11,stride=1,padding=2)<br>
self.relu1=nn.ReLU()<br>
print(self.g0.weight.shape)</p>
<pre><code>    #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
</code></pre>
<p>during training i got stucked with this error<br>
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 .</p>

ptrblck: <p><code>self.c1</code> is initialized as <code>nn.Conv2d(in_channels=128, out_channels=384, kernel_size=11,stride=1,padding=2)</code> and expects the input to have <code>128</code> channels. You are passing <code>x</code> to it, which seems to have <code>1</code> input channel:</p>
<pre><code class="lang-python">out=self.c1(x)
</code></pre>
<p>so you might either want to change the <code>in_channels</code> to <code>1</code> for this layer or pass <code>out</code> to it if this fits your use case.</p>